forked from ungerik/go-start
/
frontcontroller.go
44 lines (41 loc) · 1.56 KB
/
frontcontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package view
import (
"fmt"
"reflect"
)
type FrontController func(content View) ViewWithURL
func (self FrontController) ContentFunc(contentFunc interface{}) ViewWithURL {
v := reflect.ValueOf(contentFunc)
t := v.Type()
if t.Kind() != reflect.Func {
panic(fmt.Errorf("FrontController.ForContent: contentFunc must be a function, got %s", t))
}
if t.NumIn() != 2 {
panic(fmt.Errorf("FrontController.ForContent: contentFunc must have two arguments, got %d", t.NumIn()))
}
if t.In(0) != reflect.TypeOf((*Context)(nil)) {
panic(fmt.Errorf("FrontController.ForContent: contentFunc's first argument must be of type *Context, got %s", t.In(0)))
}
if t.NumOut() != 2 {
panic(fmt.Errorf("FrontController.ForContent: contentFunc must have two results, got %d", t.NumOut()))
}
if t.Out(0) != reflect.TypeOf((*View)(nil)).Elem() {
panic(fmt.Errorf("FrontController.ForContent: contentFunc's first result must be of type View, got %s", t.Out(0)))
}
if t.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
panic(fmt.Errorf("FrontController.ForContent: contentFunc's second result must be of type error, got %s", t.Out(1)))
}
content := DynamicView(
func(ctx *Context) (View, error) {
if reflect.TypeOf(ctx.Data) != t.In(1) {
panic(fmt.Errorf("FrontController: Context.Data must be of type %s, got %T", t.In(1), ctx.Data))
}
args := []reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(ctx.Data)}
results := v.Call(args)
view, _ := results[0].Interface().(View)
err, _ := results[1].Interface().(error)
return view, err
},
)
return self(content)
}