/
handler.go
336 lines (302 loc) · 10.3 KB
/
handler.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Package handler provides implementations of the jrpc2.Assigner interface,
// and support for adapting functions to the jrpc2.Handler interface.
package handler
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"sort"
"strings"
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/code"
)
// A Func adapts a function having the correct signature to a jrpc2.Handler.
type Func func(context.Context, *jrpc2.Request) (interface{}, error)
// Handle implements the jrpc2.Handler interface by calling m.
func (m Func) Handle(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
return m(ctx, req)
}
// A Map is a trivial implementation of the jrpc2.Assigner interface that looks
// up method names in a map of static jrpc2.Handler values.
type Map map[string]jrpc2.Handler
// Assign implements part of the jrpc2.Assigner interface.
func (m Map) Assign(_ context.Context, method string) jrpc2.Handler { return m[method] }
// Names implements part of the jrpc2.Assigner interface.
func (m Map) Names() []string {
var names []string
for name := range m {
names = append(names, name)
}
sort.Strings(names)
return names
}
// A ServiceMap combines multiple assigners into one, permitting a server to
// export multiple services under different names.
//
// Example:
// m := handler.ServiceMap{
// "Foo": handler.NewService(fooService), // methods Foo.A, Foo.B, etc.
// "Bar": handler.NewService(barService), // methods Bar.A, Bar.B, etc.
// }
//
type ServiceMap map[string]jrpc2.Assigner
// Assign splits the inbound method name as Service.Method, and passes the
// Method portion to the corresponding Service assigner. If method does not
// have the form Service.Method, or if Service is not set in m, the lookup
// fails and returns nil.
func (m ServiceMap) Assign(ctx context.Context, method string) jrpc2.Handler {
parts := strings.SplitN(method, ".", 2)
if len(parts) == 1 {
return nil
} else if ass, ok := m[parts[0]]; ok {
return ass.Assign(ctx, parts[1])
}
return nil
}
// Names reports the composed names of all the methods in the service, each
// having the form Service.Method.
func (m ServiceMap) Names() []string {
var all []string
for svc, assigner := range m {
for _, name := range assigner.Names() {
all = append(all, svc+"."+name)
}
}
sort.Strings(all)
return all
}
// New adapts a function to a jrpc2.Handler. The concrete value of fn must be a
// function with one of the following type signatures:
//
// func(context.Context) error
// func(context.Context) Y
// func(context.Context) (Y, error)
// func(context.Context, X) error
// func(context.Context, X) Y
// func(context.Context, X) (Y, error)
// func(context.Context, ...X) (Y, error)
// func(context.Context, *jrpc2.Request) (Y, error)
// func(context.Context, *jrpc2.Request) (interface{}, error)
//
// for JSON-marshalable types X and Y. New will panic if the type of fn does
// not have one of these forms. The resulting method will handle encoding and
// decoding of JSON and report appropriate errors.
//
// Functions adapted by in this way can obtain the *jrpc2.Request value using
// the jrpc2.InboundRequest helper on the context value supplied by the server.
func New(fn interface{}) Func {
m, err := newHandler(fn)
if err != nil {
panic(err)
}
return m
}
// NewService adapts the methods of a value to a map from method names to
// Handler implementations as constructed by New. It will panic if obj has no
// exported methods with a suitable signature.
func NewService(obj interface{}) Map {
out := make(Map)
val := reflect.ValueOf(obj)
typ := val.Type()
// This considers only exported methods, as desired.
for i, n := 0, val.NumMethod(); i < n; i++ {
mi := val.Method(i)
if v, err := newHandler(mi.Interface()); err == nil {
out[typ.Method(i).Name] = v
}
}
if len(out) == 0 {
panic("no matching exported methods")
}
return out
}
var (
ctxType = reflect.TypeOf((*context.Context)(nil)).Elem() // type context.Context
errType = reflect.TypeOf((*error)(nil)).Elem() // type error
reqType = reflect.TypeOf((*jrpc2.Request)(nil)) // type *jrpc2.Request
)
func newHandler(fn interface{}) (Func, error) {
if fn == nil {
return nil, errors.New("nil method")
}
// Special case: If fn has the exact signature of the Handle method, don't do
// any (additional) reflection at all.
if f, ok := fn.(func(context.Context, *jrpc2.Request) (interface{}, error)); ok {
return Func(f), nil
}
// Check that fn is a function of one of the correct forms.
typ, err := checkFunctionType(fn)
if err != nil {
return nil, err
}
// Construct a function to unpack the parameters from the request message,
// based on the signature of the user's callback.
var newinput func(req *jrpc2.Request) ([]reflect.Value, error)
if typ.NumIn() == 1 {
// Case 1: The function does not want any request parameters.
// Nothing needs to be decoded, but verify no parameters were passed.
newinput = func(req *jrpc2.Request) ([]reflect.Value, error) {
if req.HasParams() {
return nil, jrpc2.Errorf(code.InvalidParams, "no parameters accepted")
}
return nil, nil
}
} else if a := typ.In(1); a == reqType {
// Case 2: The function wants the underlying *jrpc2.Request value.
newinput = func(req *jrpc2.Request) ([]reflect.Value, error) {
return []reflect.Value{reflect.ValueOf(req)}, nil
}
} else {
// Check whether the function wants a pointer to its argument. We need
// to create one either way to support unmarshaling, but we need to
// indirect it back off if the callee didn't want it.
// Case 3a: The function wants a bare value, not a pointer.
argType := typ.In(1)
undo := reflect.Value.Elem
if argType.Kind() == reflect.Ptr {
// Case 3b: The function wants a pointer.
undo = func(v reflect.Value) reflect.Value { return v }
argType = argType.Elem()
}
newinput = func(req *jrpc2.Request) ([]reflect.Value, error) {
in := reflect.New(argType).Interface()
if err := req.UnmarshalParams(in); err != nil {
return nil, jrpc2.Errorf(code.InvalidParams, "invalid parameters: %v", err)
}
arg := reflect.ValueOf(in)
return []reflect.Value{undo(arg)}, nil
}
}
// Construct a function to decode the result values.
var decodeOut func([]reflect.Value) (interface{}, error)
switch typ.NumOut() {
case 1:
if typ.Out(0) == errType {
// A function that returns only error: Result is always nil.
decodeOut = func(vals []reflect.Value) (interface{}, error) {
oerr := vals[0].Interface()
if oerr != nil {
return nil, oerr.(error)
}
return nil, nil
}
} else {
// A function that returns a single non-error: err is always nil.
decodeOut = func(vals []reflect.Value) (interface{}, error) {
return vals[0].Interface(), nil
}
}
default:
// A function that returns a value and an error.
decodeOut = func(vals []reflect.Value) (interface{}, error) {
out, oerr := vals[0].Interface(), vals[1].Interface()
if oerr != nil {
return nil, oerr.(error)
}
return out, nil
}
}
f := reflect.ValueOf(fn)
call := f.Call
if typ.IsVariadic() {
call = f.CallSlice
}
return Func(func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
rest, ierr := newinput(req)
if ierr != nil {
return nil, ierr
}
args := append([]reflect.Value{reflect.ValueOf(ctx)}, rest...)
return decodeOut(call(args))
}), nil
}
func checkFunctionType(fn interface{}) (reflect.Type, error) {
typ := reflect.TypeOf(fn)
if typ.Kind() != reflect.Func {
return nil, errors.New("not a function")
} else if np := typ.NumIn(); np == 0 || np > 2 {
return nil, errors.New("wrong number of parameters")
} else if no := typ.NumOut(); no < 1 || no > 2 {
return nil, errors.New("wrong number of results")
} else if typ.In(0) != ctxType {
return nil, errors.New("first parameter is not context.Context")
} else if no == 2 && typ.Out(1) != errType {
return nil, errors.New("result is not of type error")
}
return typ, nil
}
// Args is a wrapper that decodes an array of positional parameters into
// concrete locations.
//
// Unmarshaling a JSON value into an Args value v succeeds if the JSON encodes
// an array with length len(v), and unmarshaling each subvalue i into the
// corresponding v[i] succeeds. As a special case, if v[i] == nil the
// corresponding value is discarded.
//
// Marshaling an Args value v into JSON succeeds if each element of the slice
// is JSON marshalable, and yields a JSON array of length len(v) containing the
// JSON values corresponding to the elements of v.
//
// Usage example:
//
// func Handler(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
// var x, y int
// var s string
//
// if err := req.UnmarshalParams(&handler.Args{&x, &y, &s}); err != nil {
// return nil, err
// }
// // do useful work with x, y, and s
// }
//
type Args []interface{}
// UnmarshalJSON supports JSON unmarshaling for a.
func (a Args) UnmarshalJSON(data []byte) error {
var elts []json.RawMessage
if err := json.Unmarshal(data, &elts); err != nil {
return fmt.Errorf("decoding args: %w", err)
} else if len(elts) != len(a) {
return fmt.Errorf("wrong number of args (got %d, want %d)", len(elts), len(a))
}
for i, elt := range elts {
if a[i] == nil {
continue
} else if err := json.Unmarshal(elt, a[i]); err != nil {
return fmt.Errorf("decoding argument %d: %w", i+1, err)
}
}
return nil
}
// MarshalJSON supports JSON marshaling for a.
func (a Args) MarshalJSON() ([]byte, error) {
if len(a) == 0 {
return []byte(`[]`), nil
}
return json.Marshal([]interface{}(a))
}
// Obj is a wrapper that maps object fields into concrete locations.
//
// Unmarshaling a JSON text into an Obj value v succeeds if the JSON encodes an
// object, and unmarshaling the value for each key k of the object into v[k]
// succeeds. If k does not exist in v, it is ignored.
//
// Marshaling an Obj into JSON works as for an ordinary map.
type Obj map[string]interface{}
// UnmarshalJSON supports JSON unmarshaling into o.
func (o Obj) UnmarshalJSON(data []byte) error {
var base map[string]json.RawMessage
if err := json.Unmarshal(data, &base); err != nil {
return fmt.Errorf("decoding object: %v", err)
}
for key, val := range base {
arg, ok := o[key]
if !ok {
continue
} else if err := json.Unmarshal(val, arg); err != nil {
return fmt.Errorf("decoding %q: %v", key, err)
}
}
return nil
}