-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.go
89 lines (76 loc) · 1.77 KB
/
interface.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
package nex
import "reflect"
func Handler(f interface{}) *Nex {
t := reflect.TypeOf(f)
if t.Kind() != reflect.Func {
panic("invalid parameter")
}
numOut := t.NumOut()
if numOut != 2 && numOut != 3 {
panic("unsupported function type, function return values should contain response data & error")
}
if numOut == 3 {
if t.Out(0) != contextType {
panic("unsupported function type")
}
}
var (
adapter HandlerAdapter
numIn = t.NumIn()
outContext = numOut == 3
inContext = false
)
if numIn > 0 {
for i := 0; i < numIn; i++ {
if t.In(i) == contextType {
inContext = true
}
}
}
if numIn == 0 {
adapter = &simplePlainAdapter{
inContext: false,
outContext: outContext,
method: reflect.ValueOf(f),
cacheArgs: []reflect.Value{},
}
} else if numIn == 1 && inContext {
adapter = &simplePlainAdapter{
inContext: true,
outContext: outContext,
method: reflect.ValueOf(f),
cacheArgs: make([]reflect.Value, 1),
}
} else if numIn == 1 && !isSupportType(t.In(0)) && t.In(0).Kind() == reflect.Ptr {
adapter = &simpleUnaryAdapter{
outContext: outContext,
argType: t.In(0),
method: reflect.ValueOf(f),
cacheArgs: make([]reflect.Value, 1),
}
} else {
adapter = makeGenericAdapter(reflect.ValueOf(f), inContext, outContext)
}
return &Nex{adapter: adapter}
}
func SetErrorEncoder(c ErrorEncoder) {
if c == nil {
panic("nil pointer to error encoder")
}
errorEncoder = c
}
func SetResponseEncoder(c ResponseEncoder) {
if c == nil {
panic("nil pointer to error encoder")
}
responseEncoder = c
}
func SetStatusCodeEncoder(c StatusCodeEncoder) {
if c == nil {
panic("nil pointer to error encoder")
}
statusCodeEncoder = c
}
func SetMultipartFormMaxMemory(m int64) {
maxMemory = m
}