From c1a1fe9d09a8b12823f7b7f1a560c9b7c24a2471 Mon Sep 17 00:00:00 2001 From: Rob Figueiredo Date: Tue, 24 Apr 2012 21:08:32 -0400 Subject: [PATCH] Interceptors: Allow interception on ALL controllers. --- intercept.go | 28 ++++++++++++++++++---------- intercept_test.go | 9 ++++++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/intercept.go b/intercept.go index d4481b8ce..288350ed8 100644 --- a/intercept.go +++ b/intercept.go @@ -46,14 +46,21 @@ const ( AFTER ) +type InterceptTarget int + +const ( + ALL_CONTROLLERS InterceptTarget = iota +) + type Interception struct { When InterceptTime function InterceptorFunc method InterceptorMethod - callable reflect.Value - target reflect.Type + callable reflect.Value + target reflect.Type + interceptAll bool } // Perform the given interception. @@ -84,19 +91,20 @@ var interceptors []*Interception // Install a general interceptor. // This can be applied to any Controller. // It must have the signature of: -// func example(c *play.Controller) play.Result +// func example(c *play.Controller) play.Result func InterceptFunc(intc InterceptorFunc, when InterceptTime, target interface{}) { interceptors = append(interceptors, &Interception{ - When: when, - function: intc, - callable: reflect.ValueOf(intc), - target: reflect.TypeOf(target), + When: when, + function: intc, + callable: reflect.ValueOf(intc), + target: reflect.TypeOf(target), + interceptAll: target == ALL_CONTROLLERS, }) } // Install an interceptor method that applies to its own Controller. -// func (c AppController) example() play.Result -// func (c *AppController) example() play.Result +// func (c AppController) example() play.Result +// func (c *AppController) example() play.Result func InterceptMethod(intc InterceptorMethod, when InterceptTime) { methodType := reflect.TypeOf(intc) if methodType.Kind() != reflect.Func || methodType.NumOut() != 1 || methodType.NumIn() != 1 { @@ -118,7 +126,7 @@ func getInterceptors(when InterceptTime, val reflect.Value) []*Interception { continue } - if findTarget(val, intc.target).IsValid() { + if intc.interceptAll || findTarget(val, intc.target).IsValid() { result = append(result, intc) } } diff --git a/intercept_test.go b/intercept_test.go index ee68fd462..6ce5f1d24 100644 --- a/intercept_test.go +++ b/intercept_test.go @@ -6,6 +6,7 @@ import ( ) var funcP = func(c *Controller) Result { return nil } +var funcP2 = func(c *Controller) Result { return nil } type InterceptController struct{ *Controller } type InterceptControllerN struct{ InterceptController } @@ -47,18 +48,20 @@ func TestInvokeArgType(t *testing.T) { func testInterceptorController(t *testing.T, appControllerPtr reflect.Value, methods []interface{}) { interceptors = []*Interception{} InterceptFunc(funcP, BEFORE, appControllerPtr.Elem().Interface()) + InterceptFunc(funcP2, BEFORE, ALL_CONTROLLERS) for _, m := range methods { InterceptMethod(m, BEFORE) } ints := getInterceptors(BEFORE, appControllerPtr) - if len(ints) != 5 { - t.Fatalf("N: Expected 5 interceptors, got %d.", len(ints)) + if len(ints) != 6 { + t.Fatalf("N: Expected 6 interceptors, got %d.", len(ints)) } testInterception(t, ints[0], reflect.ValueOf(&Controller{})) + testInterception(t, ints[1], reflect.ValueOf(&Controller{})) for i := range methods { - testInterception(t, ints[i+1], appControllerPtr) + testInterception(t, ints[i+2], appControllerPtr) } }