-
Notifications
You must be signed in to change notification settings - Fork 10
/
document.go
204 lines (176 loc) · 7.45 KB
/
document.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
package go_openrpc_reflect
import (
"errors"
"fmt"
"go/ast"
"net"
"reflect"
"sort"
"github.com/alecthomas/jsonschema"
"github.com/go-openapi/spec"
meta_schema "github.com/open-rpc/meta-schema"
)
// MetaRegisterer implements methods that must come from the mind of the developer.
// They describe the document (well, provide document description values) that cannot be
// parsed from anything available.
//
type MetaRegisterer interface {
ServerRegisterer
GetInfo() func() (info *meta_schema.InfoObject)
GetExternalDocs() func() (exdocs *meta_schema.ExternalDocumentationObject)
}
// ServerRegisterer implements a method translating a slice of net Listeners into
// document `.servers`.
type ServerRegisterer interface {
GetServers() func(listeners []net.Listener) (*meta_schema.Servers, error)
}
type ReceiverRegisterer interface {
MethodRegisterer
ReceiverMethods(name string, receiver interface{}) ([]meta_schema.MethodObject, error)
}
type MethodRegisterer interface {
ContentDescriptorRegisterer
IsMethodEligible(method reflect.Method) bool
GetMethodName(moduleName string, r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
GetMethodTags(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectTags, error)
GetMethodDescription(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
GetMethodSummary(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
GetMethodDeprecated(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (bool, error)
GetMethodParamStructure(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (string, error)
GetMethodErrors(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectErrors, error)
GetMethodExternalDocs(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.ExternalDocumentationObject, error)
GetMethodServers(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.Servers, error)
GetMethodLinks(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectLinks, error)
GetMethodExamples(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (*meta_schema.MethodObjectExamples, error)
GetMethodParams(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) ([]meta_schema.ContentDescriptorObject, error)
GetMethodResult(r reflect.Value, m reflect.Method, funcDecl *ast.FuncDecl) (meta_schema.ContentDescriptorObject, error)
}
type ContentDescriptorRegisterer interface {
SchemaRegisterer
GetContentDescriptorName(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
GetContentDescriptorSummary(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
GetContentDescriptorDescription(r reflect.Value, m reflect.Method, field *ast.Field) (string, error)
GetContentDescriptorRequired(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error)
GetContentDescriptorDeprecated(r reflect.Value, m reflect.Method, field *ast.Field) (bool, error)
GetSchema(r reflect.Value, m reflect.Method, field *ast.Field, ty reflect.Type) (schema meta_schema.JSONSchema, err error)
}
type SchemaRegisterer interface {
// Since our implementation will be piggy-backed on jsonschema.Reflector,
// this is where our field-by-field Getter abstraction ends.
// If we didn't rely so heavily on this dependency, we would use
// a pattern like method and content descriptor, where fields are defined
// individually per reflector.
// JSON Schemas have a lot of fields.
//
// And including meta_schema.go, we're using 3 data types to develop this object.
// - alecthomas/jsonschema.Schema: use .Reflector to reflect the schema from its Go declaration.
// - openapi/spec.Schema: the "official" spec data type from swagger
// - <generated> meta_schema.go.JSONSchema as eventual local implementation type.
//
// Since the native language for this data type is JSON, I (the developer) assume
// that using the standard lib to Un/Marshal between these data types is as good a glue as any.
// Unmarshaling will be slow, but should only ever happen once up front, so I'm not concerned with performance.
//
// SchemaIgnoredTypes reply will be passed directly to the jsonschema.Reflector.IgnoredTypes field.
SchemaIgnoredTypes() []interface{}
// SchemaTypeMap will be passed directory to the jsonschema.Reflector.TypeMapper field.
SchemaTypeMap() func(ty reflect.Type) *jsonschema.Type
// SchemaMutations will be run in a depth-first walk on the reflected schema.
// They will be run in order.
// Function wrapping allows closure fn to have context of root schema.
SchemaMutations(ty reflect.Type) []func(*spec.Schema) func(*spec.Schema) error
SchemaExamples(ty reflect.Type) (examples *meta_schema.Examples, err error)
}
//type Service int
//const (
// Standard Service = iota
// Ethereum
//)
type Document struct {
meta MetaRegisterer
reflector ReceiverRegisterer
receiverNames []string
receivers []interface{}
listeners []net.Listener
}
//func (d *Document) RPCDiscover(kind Service) (receiver interface{}) {
// switch kind {
// case Standard:
// return &RPC{d}
// case Ethereum:
// return &RPCEthereum{d}
// }
// return nil
//}
func (d *Document) RegisterReceiver(receiver interface{}) {
d.RegisterReceiverName("", receiver)
}
func (d *Document) RegisterReceiverName(name string, receiver interface{}) {
if d.receivers == nil {
d.receivers = []interface{}{}
}
if d.receiverNames == nil {
d.receiverNames = []string{}
}
d.receiverNames = append(d.receiverNames, name)
d.receivers = append(d.receivers, receiver)
}
func (d *Document) RegisterListener(listener net.Listener) {
if d.listeners == nil {
d.listeners = []net.Listener{}
}
d.listeners = append(d.listeners, listener)
}
func (d *Document) WithMeta(meta MetaRegisterer) *Document {
d.meta = meta
return d
}
func (d *Document) WithReflector(reflector ReceiverRegisterer) *Document {
d.reflector = reflector
return d
}
var errMissingInterface = errors.New("missing interface")
func (d *Document) Discover() (*meta_schema.OpenrpcDocument, error) {
if d.meta == nil {
return nil, fmt.Errorf("meta: %v", errMissingInterface)
}
openRPCDocumentVersion := meta_schema.OpenrpcEnum0
out := &meta_schema.OpenrpcDocument{
Openrpc: &openRPCDocumentVersion,
Info: d.meta.GetInfo()(), // This will panic if the developer misuses it (leaves it nil).
ExternalDocs: d.meta.GetExternalDocs()(), // This too.
}
getServersFn := d.meta.GetServers()
servers, err := getServersFn(d.listeners)
if err != nil {
return nil, fmt.Errorf("listener error: %w", err)
}
out.Servers = servers
// Return no error if no receivers registered.
// > While it is required, the array may be empty (to handle security filtering, for example).
// > https://spec.open-rpc.org/#openrpc-object
if d.reflector == nil {
return out, nil
}
if d.receivers == nil || len(d.receivers) == 0 {
return out, nil
}
// Iterate all registered receivers (aka 'modules'),
// building and collecting eligible methods for each.
methods := []meta_schema.MethodObject{}
for i, rec := range d.receivers {
name := d.receiverNames[i]
ms, err := d.reflector.ReceiverMethods(name, rec)
if err != nil {
return nil, fmt.Errorf("receiver method error: %w", err)
}
methods = append(methods, ms...)
}
sort.Slice(methods, func(i, j int) bool {
return *methods[i].Name < *methods[j].Name
})
// Assign by slice address.
m := meta_schema.Methods(methods)
out.Methods = &m
return out, nil
}