Skip to content

Commit

Permalink
Interceptors: Allow interception on ALL controllers.
Browse files Browse the repository at this point in the history
  • Loading branch information
robfig committed Apr 25, 2012
1 parent b6823e9 commit c1a1fe9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
28 changes: 18 additions & 10 deletions intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
9 changes: 6 additions & 3 deletions intercept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c1a1fe9

Please sign in to comment.