forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 20
/
openrpc.go
180 lines (130 loc) · 3.82 KB
/
openrpc.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
package rpc
import (
"encoding"
"encoding/json"
"errors"
"reflect"
openrpc "github.com/octanolabs/g0penrpc"
"github.com/ubiq/go-ubiq/v6/common/hexutil"
"github.com/ubiq/go-ubiq/v6/log"
)
func makeOpenRpcSpecV1(server *Server) (*openrpc.DocumentSpec1, error) {
methods := make([]*openrpc.Method, 0)
p, _ := openrpc.NewPointer("#/components/schemas")
reg, err := openrpc.NewSchemaRegistry(p)
if err != nil {
return nil, errors.New("could not create schema registry")
}
bnh := reflect.TypeOf((*BlockNumberOrHash)(nil)).Elem()
reg.AddTypeException(bnh)
hxb := reflect.TypeOf((*hexutil.Big)(nil)).Elem()
reg.AddTypeException(hxb)
// subscribeSyncStatus accepts a chan and is not available publicly so how did it show up here
log.Warn("openrpc: discovering services")
for serviceName, service := range server.services.services {
if serviceName != "rpc" {
for methodName, callback := range service.callbacks {
method, err := methodObjectFromCallback(reg, service.name, methodName, callback)
if err != nil {
log.Warn("openrpc: error making content", "service", serviceName, "method", methodName, "err", err)
continue
}
methods = append(methods, method)
}
}
}
info := &openrpc.Info{
Title: "go-ubiq JSON-RPC",
Version: "1.0",
}
doc := openrpc.NewDocument(methods, info)
doc.Components.Schemas = reg
return doc, nil
}
func methodObjectFromCallback(reg *openrpc.SchemaRegistry, service, methodName string, cb *callback) (*openrpc.Method, error) {
argTypes := cb.argTypes
method := &openrpc.Method{
Name: service + serviceMethodSeparator + methodName,
Params: nil,
Result: nil,
}
params := make([]*openrpc.ContentDescriptor, len(cb.argTypes))
//For argtypes we need to check if custom types implement un-marshaling (text or json)
for idx, t := range argTypes {
var (
paramPtr openrpc.Pointer
paramName string
err error
)
marshalable := checkMarshalable(t)
paramPtr, paramName, err = reg.RegisterType(t, marshalable)
if err != nil {
return nil, errors.New("error handling type: " + t.String() + " : " + err.Error())
}
// If param is pointer it can be omitted
isReq := t.Kind() != reflect.Ptr
params[idx] = &openrpc.ContentDescriptor{
Name: paramName,
Schema: paramPtr,
Required: isReq,
}
}
method.Params = params
var err error
if cb.fn.Type().NumOut() > 0 {
returnType := cb.fn.Type().Out(0)
unmarshalable := checkUnmarshalable(returnType)
returnPtr, returnName, err := reg.RegisterType(returnType, unmarshalable)
if err != nil {
return nil, errors.New("error handling type: " + returnType.String() + " : " + err.Error())
}
method.Result = &openrpc.ContentDescriptor{
Name: returnName,
Required: returnType.Kind() != reflect.Ptr,
Schema: returnPtr,
}
}
return method, err
}
func checkUnmarshalable(t reflect.Type) (ok bool) {
if ok = t.Kind() == reflect.String; ok {
return
}
if ok = t.Kind() == reflect.Map; ok {
return checkUnmarshalable(t.Key()) && checkUnmarshalable(t.Elem())
}
txt := reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
if ok = t.Implements(txt); ok {
return
}
jsn := reflect.TypeOf(new(json.Unmarshaler)).Elem()
if ok = t.Implements(jsn); ok {
return
}
if t.Kind() != reflect.Ptr {
p := reflect.PtrTo(t)
return checkUnmarshalable(p)
}
return
}
func checkMarshalable(t reflect.Type) (ok bool) {
if ok = t.Kind() == reflect.String; ok {
return
}
if ok = t.Kind() == reflect.Map; ok {
return checkMarshalable(t.Key()) && checkMarshalable(t.Elem())
}
txt := reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
if ok = t.Implements(txt); ok {
return
}
jsn := reflect.TypeOf(new(json.Marshaler)).Elem()
if ok = t.Implements(jsn); ok {
return
}
if t.Kind() != reflect.Ptr {
p := reflect.PtrTo(t)
return checkMarshalable(p)
}
return
}