forked from neelance/go-angularjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
128 lines (97 loc) · 2.7 KB
/
params.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package angularjs
import "fmt"
import "reflect"
import "github.com/gopherjs/gopherjs/js"
func typeOf(cb interface{}) (reflect.Type, bool) {
typ := reflect.TypeOf(cb)
if typ.Kind() == reflect.Ptr {
if meth, ok := typ.MethodByName("New"); ok {
return meth.Type, true
}
}
return typ, false
}
func makeConstructor(cb interface{}) func(this *js.Object, args []*js.Object) interface{} {
typ := reflect.TypeOf(cb)
newMethod, ok := typ.MethodByName("New")
if !ok && typ.Kind() != reflect.Func {
panic(fmt.Sprintf("Type %q doesn't have New method.", typ))
}
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
return func(jsThis *js.Object, jsArgs []*js.Object) interface{} {
this := reflect.New(typ).Elem()
objField := this.FieldByName("Object")
objField.Set(reflect.ValueOf(jsThis))
var args []reflect.Value
args = append(args, this.Addr())
for i, jsArg := range jsArgs {
argTyp := newMethod.Type.In(i + 1)
if argTyp.Kind() == reflect.Ptr {
argTyp = argTyp.Elem()
}
arg := reflect.New(argTyp)
arg.Elem().FieldByName("Object").Set(reflect.ValueOf(jsArg))
if meth, ok := arg.Type().MethodByName("Init"); ok {
//println("Calling Init for arg ", arg.Elem().Type().String())
meth.Func.Call([]reflect.Value{arg})
}
if newMethod.Type.In(i+1).Kind() == reflect.Ptr {
args = append(args, arg)
} else {
args = append(args, arg.Elem())
}
}
newMethod.Func.Call(args)
return nil
}
}
func funcReturn(cb interface{}, ret []interface{}) error {
typ, _ := typeOf(cb)
if typ.NumOut() != len(ret) {
return fmt.Errorf("Wrong function signature -- Expected %q, got %q", ret, typ)
}
for i := 0; i < typ.NumOut(); i++ {
if typ.Out(i).String() != reflect.TypeOf(ret[i]).String() {
return fmt.Errorf("Wrong function signature -- Expected %q, got %q", ret, typ)
}
}
return nil
}
func buildParams(cb interface{}, name string) (js.S, error) {
arg := js.S{}
typ, cls := typeOf(cb)
if typ.Kind() != reflect.Func {
return nil, fmt.Errorf("Expected a function, got %q", typ)
}
i := 0
if cls {
i = 1
}
for ; i < typ.NumIn(); i++ {
argTyp := typ.In(i)
if argTyp.Kind() != reflect.Struct {
argTyp = argTyp.Elem()
}
param, ok := argTyp.FieldByName("Object")
if !ok {
return nil, fmt.Errorf("Invalid paramater type %q (Missing Object)", argTyp)
}
if param.Tag.Get(name) == "" {
if param.Tag.Get("js") == "" {
return nil, fmt.Errorf("Invalid paramater type %q (Missing tag %s)", argTyp, name)
} else {
arg = append(arg, param.Tag.Get("js"))
}
} else {
arg = append(arg, param.Tag.Get(name))
}
}
if cls {
arg = append(arg, js.MakeFunc(makeConstructor(cb)))
} else {
arg = append(arg, cb)
}
return arg, nil
}