-
Notifications
You must be signed in to change notification settings - Fork 100
/
register.go
183 lines (158 loc) · 5.54 KB
/
register.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package json
import (
"fmt"
"reflect"
"go.uber.org/yarpc"
"go.uber.org/yarpc/transport"
"golang.org/x/net/context"
)
var (
_ctxType = reflect.TypeOf((*context.Context)(nil)).Elem()
_reqMetaType = reflect.TypeOf((*yarpc.ReqMeta)(nil)).Elem()
_resMetaType = reflect.TypeOf((*yarpc.ResMeta)(nil)).Elem()
_errorType = reflect.TypeOf((*error)(nil)).Elem()
_interfaceEmptyType = reflect.TypeOf((*interface{})(nil)).Elem()
)
// Registrant is used for types that define or know about different JSON
// procedures.
type Registrant interface {
// Gets a mapping from procedure name to the handler for that procedure for
// all procedures provided by this registrant.
getHandlers() map[string]interface{}
}
// procedure is a simple Registrant that has a single procedure.
type procedure struct {
Name string
Handler interface{}
}
func (p procedure) getHandlers() map[string]interface{} {
return map[string]interface{}{p.Name: p.Handler}
}
// Procedure builds a Registrant with a single procedure in it. handler must
// be a function with a signature similar to,
//
// f(ctx context.Context, reqMeta yarpc.ReqMeta, body $reqBody) ($resBody, yarpc.ResMeta, error)
//
// Where $reqBody and $resBody are a map[string]interface{} or pointers to
// structs.
func Procedure(name string, handler interface{}) Registrant {
return procedure{Name: name, Handler: handler}
}
// Register registers the procedures defined by the given JSON registrant with
// the given registry.
//
// Handlers must have a signature similar to the following or the system will
// panic.
//
// f(ctx context.Context, reqMeta yarpc.ReqMeta, body $reqBody) ($resBody, yarpc.ResMeta, error)
//
// Where $reqBody and $resBody are a map[string]interface{} or pointers to
// structs.
func Register(reg transport.Registry, registrant Registrant) {
for name, handler := range registrant.getHandlers() {
reg.Register("", name, wrapHandler(name, handler))
}
}
// wrapHandler takes a valid JSON handler function and converts it into a
// transport.Handler.
func wrapHandler(name string, handler interface{}) transport.Handler {
reqBodyType := verifySignature(name, reflect.TypeOf(handler))
var r requestReader
if reqBodyType == _interfaceEmptyType {
r = ifaceEmptyReader{}
} else if reqBodyType.Kind() == reflect.Map {
r = mapReader{reqBodyType}
} else {
// struct ptr
r = structReader{reqBodyType.Elem()}
}
return jsonHandler{
reader: r,
handler: reflect.ValueOf(handler),
}
}
// verifySignature verifies that the given type matches what we expect from
// JSON handlers and returns the request and response types.
//
// Returns the request type.
func verifySignature(n string, t reflect.Type) reflect.Type {
if t.Kind() != reflect.Func {
panic(fmt.Sprintf(
"handler for %q is not a function but a %v", n, t.Kind(),
))
}
if t.NumIn() != 3 {
panic(fmt.Sprintf(
"expected handler for %q to have 3 arguments but it had %v",
n, t.NumIn(),
))
}
if t.NumOut() != 3 {
panic(fmt.Sprintf(
"expected handler for %q to have 3 results but it had %v",
n, t.NumOut(),
))
}
if t.In(0) != _ctxType {
panic(fmt.Sprintf(
"the first argument of the handler for %q must be of type "+
"context.Context, and not: %v", n, t.In(0),
))
}
if t.In(1) != _reqMetaType {
panic(fmt.Sprintf(
"the second argument of the handler for %q must be of type "+
"yarpc.ReqMeta, and not: %v", n, t.In(0),
))
}
if t.Out(1) != _resMetaType || t.Out(2) != _errorType {
panic(fmt.Sprintf(
"the last two results of the handler for %q must be of type "+
"yarpc.ResMeta and error, and not: %v, %v",
n, t.Out(1), t.Out(2),
))
}
reqBodyType := t.In(2)
resBodyType := t.Out(0)
if !isValidReqResType(reqBodyType) {
panic(fmt.Sprintf(
"the thrifd argument of the handler for %q must be "+
"a struct pointer, a map[string]interface{}, or interface{}, and not: %v",
n, reqBodyType,
))
}
if !isValidReqResType(resBodyType) {
panic(fmt.Sprintf(
"the first result of the handler for %q must be "+
"a struct pointer, a map[string]interface{}, or interface{], and not: %v",
n, resBodyType,
))
}
return reqBodyType
}
// isValidReqResType checks if the given type is a pointer to a struct, a
// map[string]interface{}, or a interface{}.
func isValidReqResType(t reflect.Type) bool {
return (t == _interfaceEmptyType) ||
(t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct) ||
(t.Kind() == reflect.Map && t.Key().Kind() == reflect.String)
}