/
register.go
213 lines (184 loc) · 6.19 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2018 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 (
"context"
"fmt"
"reflect"
"go.uber.org/yarpc/api/transport"
)
var (
_ctxType = reflect.TypeOf((*context.Context)(nil)).Elem()
_errorType = reflect.TypeOf((*error)(nil)).Elem()
_interfaceEmptyType = reflect.TypeOf((*interface{})(nil)).Elem()
)
// Register calls the RouteTable's Register method.
//
// This function exists for backwards compatibility only. It will be removed
// in a future version.
//
// Deprecated: Use the RouteTable's Register method directly.
func Register(r transport.RouteTable, rs []transport.Procedure) {
r.Register(rs)
}
// Procedure builds a Procedure from the given JSON handler. handler must be
// a function with a signature similar to,
//
// f(ctx context.Context, body $reqBody) ($resBody, error)
//
// Where $reqBody and $resBody are a map[string]interface{} or pointers to
// structs.
func Procedure(name string, handler interface{}) []transport.Procedure {
return []transport.Procedure{
{
Name: name,
HandlerSpec: transport.NewUnaryHandlerSpec(
wrapUnaryHandler(name, handler),
),
Encoding: Encoding,
},
}
}
// OnewayProcedure builds a Procedure from the given JSON handler. handler must be
// a function with a signature similar to,
//
// f(ctx context.Context, body $reqBody) error
//
// Where $reqBody is a map[string]interface{} or pointer to a struct.
func OnewayProcedure(name string, handler interface{}) []transport.Procedure {
return []transport.Procedure{
{
Name: name,
HandlerSpec: transport.NewOnewayHandlerSpec(
wrapOnewayHandler(name, handler)),
Encoding: Encoding,
},
}
}
// wrapUnaryHandler takes a valid JSON handler function and converts it into a
// transport.UnaryHandler.
func wrapUnaryHandler(name string, handler interface{}) transport.UnaryHandler {
reqBodyType := verifyUnarySignature(name, reflect.TypeOf(handler))
return newJSONHandler(reqBodyType, handler)
}
// wrapOnewayHandler takes a valid JSON handler function and converts it into a
// transport.OnewayHandler.
func wrapOnewayHandler(name string, handler interface{}) transport.OnewayHandler {
reqBodyType := verifyOnewaySignature(name, reflect.TypeOf(handler))
return newJSONHandler(reqBodyType, handler)
}
func newJSONHandler(reqBodyType reflect.Type, handler interface{}) jsonHandler {
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),
}
}
// verifyUnarySignature verifies that the given type matches what we expect from
// JSON unary handlers and returns the request type.
func verifyUnarySignature(n string, t reflect.Type) reflect.Type {
reqBodyType := verifyInputSignature(n, t)
if t.NumOut() != 2 {
panic(fmt.Sprintf(
"expected handler for %q to have 2 results but it had %v",
n, t.NumOut(),
))
}
if t.Out(1) != _errorType {
panic(fmt.Sprintf(
"handler for %q must return error as its second reuslt, not %v",
n, t.Out(1),
))
}
resBodyType := t.Out(0)
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
}
// verifyOnewaySignature verifies that the given type matches what we expect
// from oneway JSON handlers.
//
// Returns the request type.
func verifyOnewaySignature(n string, t reflect.Type) reflect.Type {
reqBodyType := verifyInputSignature(n, t)
if t.NumOut() != 1 {
panic(fmt.Sprintf(
"expected handler for %q to have 1 result but it had %v",
n, t.NumOut(),
))
}
if t.Out(0) != _errorType {
panic(fmt.Sprintf(
"the result of the handler for %q must be of type error, and not: %v",
n, t.Out(0),
))
}
return reqBodyType
}
// verifyInputSignature verifies that the given input argument types match
// what we expect from JSON handlers and returns the request body type.
func verifyInputSignature(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() != 2 {
panic(fmt.Sprintf(
"expected handler for %q to have 2 arguments but it had %v",
n, t.NumIn(),
))
}
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),
))
}
reqBodyType := t.In(1)
if !isValidReqResType(reqBodyType) {
panic(fmt.Sprintf(
"the second argument of the handler for %q must be "+
"a struct pointer, a map[string]interface{}, or interface{}, and not: %v",
n, reqBodyType,
))
}
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)
}