-
Notifications
You must be signed in to change notification settings - Fork 10
/
standard.go
422 lines (370 loc) · 14.2 KB
/
standard.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package go_openrpc_reflect
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"net"
"reflect"
"regexp"
"runtime"
"github.com/alecthomas/jsonschema"
"github.com/go-openapi/spec"
meta_schema "github.com/open-rpc/meta-schema"
)
// ReceiverReflectorT holds a field for each ReceiverRegisterer interface method.
// StandardReflectorT embeds this struct, together with self.FnMethod != nil checks, as a way to easily override
// defaults from the consuming application side. See example4_test.go for an an example.
type ReceiverReflectorT struct {
FnReceiverMethods func (name string, receiver interface{}) ([]meta_schema.MethodObject, error)
FnIsMethodEligible func(method reflect.Method) bool
FnGetMethodName func(moduleName string, r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
FnGetMethodTags func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectTags, error)
FnGetMethodDescription func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
FnGetMethodSummary func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
FnGetMethodDeprecated func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (bool, error)
FnGetMethodParamStructure func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
FnGetMethodErrors func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectErrors, error)
FnGetMethodExternalDocs func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.ExternalDocumentationObject, error)
FnGetMethodServers func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.Servers, error)
FnGetMethodLinks func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectLinks, error)
FnGetMethodExamples func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectExamples, error)
FnGetMethodParams func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) ([]meta_schema.ContentDescriptorObject, error)
FnGetMethodResult func(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (meta_schema.ContentDescriptorObject, error)
FnGetContentDescriptorName func(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
FnGetContentDescriptorSummary func(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
FnGetContentDescriptorDescription func(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
FnGetContentDescriptorRequired func(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error)
FnGetContentDescriptorDeprecated func(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error)
FnGetSchema func(r reflect.Value, m reflect.Method, field *ast.Field, ty reflect.Type) (schema meta_schema.JSONSchema, err error)
FnSchemaIgnoredTypes func () []interface{}
FnSchemaTypeMap func () func(ty reflect.Type) *jsonschema.Type
FnSchemaMutations func (ty reflect.Type) []func (*spec.Schema) func(*spec.Schema) error
FnSchemaExamples func (ty reflect.Type) (examples *meta_schema.Examples, err error)
}
type StandardReflectorT struct{
ReceiverReflectorT
}
var StandardReflector = &StandardReflectorT{}
func (c *StandardReflectorT) GetServers() func (listeners []net.Listener) (*meta_schema.Servers, error) {
return func (listeners []net.Listener) (*meta_schema.Servers, error) {
if listeners == nil {
return nil, nil
}
if len(listeners) == 0 {
return nil, nil
}
servers := []meta_schema.ServerObject{}
for _, listener := range listeners {
if listener == nil {
continue
}
addr := listener.Addr().String()
network := listener.Addr().Network()
servers = append(servers, meta_schema.ServerObject{
Url: (*meta_schema.ServerObjectUrl)(&addr),
Name: (*meta_schema.ServerObjectName)(&network),
})
}
return (*meta_schema.Servers)(&servers), nil
}
}
func (c *StandardReflectorT) ReceiverMethods(name string, receiver interface{}) ([]meta_schema.MethodObject, error) {
if c.FnReceiverMethods != nil {
return c.FnReceiverMethods(name, receiver)
}
return receiverMethods(c, name, receiver)
}
// ------------------------------------------------------------------------------
func (c *StandardReflectorT) IsMethodEligible(method reflect.Method) bool {
if c.FnIsMethodEligible != nil {
return c.FnIsMethodEligible(method)
}
// Method must be exported.
if !isExportedMethod(method) {
return false
}
mtype := method.Type
// Method needs three ins: receiver, *args, *reply.
if mtype.NumIn() != 3 {
return false
}
// First arg need not be a pointer.
argType := mtype.In(1)
if !isExportedOrBuiltinType(argType) {
return false
}
// Second arg must be a pointer.
replyType := mtype.In(2)
if replyType.Kind() != reflect.Ptr {
return false
}
// Reply type must be exported.
if !isExportedOrBuiltinType(replyType) {
return false
}
// Method needs one out.
if mtype.NumOut() != 1 {
return false
}
// The return type of the method must be error.
if returnType := mtype.Out(0); returnType != errType {
return false
}
return true
}
func (c *StandardReflectorT) GetMethodName(moduleName string, r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error) {
if c.FnGetMethodName != nil {
return c.FnGetMethodName(moduleName, r, m, funcDecl)
}
if moduleName == "" {
ty := r.Type()
if ty.Kind() == reflect.Ptr {
ty = ty.Elem()
}
moduleName = ty.Name()
}
return moduleName + "." + m.Name, nil
}
func (c *StandardReflectorT) GetMethodParams(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) ([]meta_schema.ContentDescriptorObject, error) {
if c.FnGetMethodParams != nil {
return c.FnGetMethodParams(r, m, funcDecl)
}
// A case where expanded fields arg expression would fail (if anyof `funcDecl.Type.Params` == nil)
// should be caught by the IsMethodEligible condition.
if funcDecl.Type.Params == nil {
panic("unreachable")
}
expandedFields := expandedFieldNamesFromList(funcDecl.Type.Params.List)
// We always want only the first param.
nf := expandedFields[0]
ty := m.Type.In(1)
cd, err := buildContentDescriptorObject(c, r, m, nf, ty)
if err != nil {
return nil, err
}
// Spec says params are always a list.
return []meta_schema.ContentDescriptorObject{cd}, nil
}
func (c *StandardReflectorT) GetMethodResult(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (cd meta_schema.ContentDescriptorObject, err error) {
if c.FnGetMethodResult != nil {
return c.FnGetMethodResult(r, m, funcDecl)
}
if funcDecl.Type.Params == nil {
panic("unreachable")
}
expandedFields := expandedFieldNamesFromList(funcDecl.Type.Params.List)
// We always want only the second param.
nf := expandedFields[1]
ty := m.Type.In(2)
return buildContentDescriptorObject(c, r, m, nf, ty)
}
func (c *StandardReflectorT) GetMethodDescription(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error) {
if c.FnGetMethodDescription != nil {
return c.FnGetMethodDescription(r, m, funcDecl)
}
tokenFileSet := token.NewFileSet()
printed := []byte{}
buf := bytes.NewBuffer(printed)
err := printer.Fprint(buf, tokenFileSet, funcDecl)
if err != nil {
return "", err
}
printed = buf.Bytes()
return fmt.Sprintf("```go\n%s\n```", string(printed)), nil
}
func (c *StandardReflectorT) GetMethodSummary(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error) {
if c.FnGetMethodSummary != nil {
return c.FnGetMethodSummary(r, m, funcDecl)
}
if funcDecl.Doc != nil {
return funcDecl.Doc.Text(), nil
}
return "", nil
}
func (c *StandardReflectorT) GetMethodDeprecated(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (bool, error) {
if c.FnGetMethodDeprecated != nil {
return c.FnGetMethodDeprecated(r, m, funcDecl)
}
var comment string
if funcDecl.Doc != nil {
comment = funcDecl.Doc.Text()
}
if comment == "" {
return false, nil
}
matched, _ := regexp.MatchString(`(?im)deprecated`, funcDecl.Doc.Text())
return matched, nil
}
func (c *StandardReflectorT) GetMethodExternalDocs(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.ExternalDocumentationObject, error) {
if c.FnGetMethodExternalDocs != nil {
return c.FnGetMethodExternalDocs(r, m, funcDecl)
}
// NOTE: This will NOT work for forks. Hm.
// If we can assemble a github.com/ url for the method, then
// return that prefixed before the printed code.
runtimeFunc := runtime.FuncForPC(m.Func.Pointer())
githubURL, err := githubLinkFromValue(r, runtimeFunc)
if err == nil {
description := "Github remote link"
u := githubURL.String()
return &meta_schema.ExternalDocumentationObject{
Description: (*meta_schema.ExternalDocumentationObjectDescription)(&description),
Url: (*meta_schema.ExternalDocumentationObjectUrl)(&u),
}, nil
}
return nil, nil
}
/*
TODO: These.
*/
func (c *StandardReflectorT) GetMethodTags(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectTags, error) {
if c.FnGetMethodTags != nil {
return c.FnGetMethodTags(r, m, funcDecl)
}
return nil, nil
}
func (c *StandardReflectorT) GetMethodParamStructure(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error) {
if c.FnGetMethodParamStructure != nil {
return c.FnGetMethodParamStructure(r, m, funcDecl)
}
return "by-position", nil
}
func (c *StandardReflectorT) GetMethodErrors(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectErrors, error) {
if c.FnGetMethodErrors != nil {
return c.FnGetMethodErrors(r, m, funcDecl)
}
return nil, nil
}
func (c *StandardReflectorT) GetMethodServers(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.Servers, error) {
if c.FnGetMethodServers != nil {
return c.FnGetMethodServers(r, m, funcDecl)
}
return nil, nil
}
func (c *StandardReflectorT) GetMethodLinks(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectLinks, error) {
if c.FnGetMethodLinks != nil {
return c.FnGetMethodLinks(r, m, funcDecl)
}
return nil, nil
}
func (c *StandardReflectorT) GetMethodExamples(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectExamples, error) {
if c.FnGetMethodExamples != nil {
return c.FnGetMethodExamples(r, m, funcDecl)
}
return nil, nil
}
// ------------------------------------------------------------------------------
func (c *StandardReflectorT) GetContentDescriptorName(r reflect.Value, m reflect.Method, field *ast.Field) (string, error) {
if c.FnGetContentDescriptorName != nil {
return c.FnGetContentDescriptorName(r, m, field)
}
fs := expandedFieldNamesFromList([]*ast.Field{field})
name := fs[0].Names[0].Name
//// Remove '*' signifying pointers in Go.
//if strings.HasPrefix(name, "*") {
// name = strings.TrimPrefix(name, "*")
//}
return name, nil
}
func (c *StandardReflectorT) GetContentDescriptorDescription(r reflect.Value, m reflect.Method, field *ast.Field) (string, error) {
if c.FnGetContentDescriptorDescription != nil {
return c.FnGetContentDescriptorDescription(r, m, field)
}
return printIdentField(field), nil
}
func (c *StandardReflectorT) GetContentDescriptorSummary(r reflect.Value, m reflect.Method, field *ast.Field) (string, error) {
if c.FnGetContentDescriptorSummary != nil {
return c.FnGetContentDescriptorSummary(r, m, field)
}
summary := field.Comment.Text()
if summary == "" {
summary = field.Doc.Text()
}
return summary, nil
}
func (c *StandardReflectorT) GetContentDescriptorRequired(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error) {
if c.FnGetContentDescriptorRequired != nil {
return c.FnGetContentDescriptorRequired(r, m, field)
}
// The standard method signature pattern does not allow for variadic arguments.
return true, nil
}
func (c *StandardReflectorT) GetContentDescriptorDeprecated(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error) {
if c.FnGetContentDescriptorDeprecated != nil {
return c.FnGetContentDescriptorDeprecated(r, m, field)
}
var comment string
if field.Doc != nil {
comment = field.Doc.Text()
}
if comment == "" && field.Comment != nil {
comment = field.Comment.Text()
}
if comment == "" {
return false, nil
}
matched, _ := regexp.MatchString(`(?im)deprecated`, comment)
return matched, nil
}
func (c *StandardReflectorT) GetSchema(r reflect.Value, m reflect.Method, field *ast.Field, ty reflect.Type) (meta_schema.JSONSchema, error) {
if c.FnGetSchema != nil {
return c.FnGetSchema(r, m, field, ty)
}
return buildJSONSchemaObject(c, r, m, field, ty)
}
// ------------------------------------------------------------------------------
func (c *StandardReflectorT) SchemaIgnoredTypes() []interface{} {
if c.FnSchemaIgnoredTypes != nil {
return c.FnSchemaIgnoredTypes()
}
return nil
}
func (c *StandardReflectorT) SchemaTypeMap() func(ty reflect.Type) *jsonschema.Type {
if c.FnSchemaTypeMap != nil {
return c.FnSchemaTypeMap()
}
return nil
}
func (c *StandardReflectorT) SchemaMutations(ty reflect.Type) []func(*spec.Schema) func(*spec.Schema) error {
if c.FnSchemaMutations != nil {
return c.FnSchemaMutations(ty)
}
return []func(*spec.Schema) func(*spec.Schema) error{
SchemaMutationRequireDefaultOn,
SchemaMutationExpand,
SchemaMutationRemoveDefinitionsField,
}
}
func (c *StandardReflectorT) SchemaExamples(ty reflect.Type) (examples *meta_schema.Examples, err error) {
if c.FnSchemaExamples != nil {
return c.FnSchemaExamples(ty)
}
return nil, nil
}
// ------------------------------------------------------------------------------
func SchemaMutationRemoveDefinitionsField(root *spec.Schema) func (s *spec.Schema) error {
return func(s *spec.Schema) error {
s.Definitions = nil
s.Ref = spec.Ref{}
return nil
}
}
func SchemaMutationExpand(root *spec.Schema) func (s *spec.Schema) error {
return func(s *spec.Schema) error {
return spec.ExpandSchema(s, root, nil)
}
}
func SchemaMutationRequireDefaultOn(root *spec.Schema) func (s *spec.Schema) error {
return func(s *spec.Schema) error {
// If we didn't explicitly set any fields as required with jsonschema tags,
// then we can assume the default, that ALL properties are required.
if len(s.Required) == 0 {
for k := range s.Properties {
s.Required = append(s.Required, k)
}
}
return nil
}
}