-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmethod.go
More file actions
181 lines (158 loc) · 4.73 KB
/
Copy pathmethod.go
File metadata and controls
181 lines (158 loc) · 4.73 KB
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
package jsonrpc2
import (
"context"
"encoding/json"
"fmt"
"reflect"
)
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
var typeOfContext = reflect.TypeOf((*context.Context)(nil)).Elem()
// methodArgTypes returns the arg types and whether all the types are valid
// (exported or builtin).
func methodArgTypes(methodType reflect.Type) (argTypes []reflect.Type, hasCtx bool, ok bool) {
argNum := methodType.NumIn()
argTypes = make([]reflect.Type, 0, argNum-1)
argPos := 1 // Skip receiver
for ; argPos < argNum; argPos++ {
argType := methodType.In(argPos)
if !isExportedOrBuiltin(argType) {
return nil, hasCtx, false
}
if argType == typeOfContext {
hasCtx = true
continue
}
argTypes = append(argTypes, argType)
}
return argTypes, hasCtx, true
}
// methodErrPos returns the return value index position of an error type for
// supported return layouts: (), (interface{}), (error), (interface{}, error)
func methodErrPos(methodType reflect.Type) (int, bool) {
switch methodType.NumOut() {
case 0:
case 1:
if methodType.Out(0) == typeOfError {
// Single error return value
return 0, true
}
// Single non-error return value
return -1, true
case 2:
if methodType.Out(1) == typeOfError {
// Two return values, one error type
return 1, true
}
// Two return values, no error type, unsupported.
return -1, false
}
return -1, false
}
// MethodByName returns a single Method from receiver that matches the name.
func MethodByName(receiver interface{}, name string) (Method, error) {
kind := reflect.TypeOf(receiver)
val := reflect.ValueOf(receiver)
if name := reflect.Indirect(val).Type().Name(); !isExported(name) {
return Method{}, fmt.Errorf("receiver must be exported: %s", name)
}
method, ok := kind.MethodByName(name)
if !ok || method.PkgPath != "" {
return Method{}, fmt.Errorf("method must be exported: %s", name)
}
// Load arg types (skip first arg, the receiver)
argTypes, hasCtx, ok := methodArgTypes(method.Type)
if !ok {
// Skip methods with unexported arg types
return Method{}, fmt.Errorf("method args must be exported in method: %s", method.Name)
}
// Find ErrPos, if any.
errPos, ok := methodErrPos(method.Type)
if !ok {
return Method{}, fmt.Errorf("unsupported return values in method: %s", method.Name)
}
return Method{
Receiver: val,
Method: method,
ArgTypes: argTypes,
ErrPos: errPos,
HasCtx: hasCtx,
}, nil
}
// Methods returns a mapping of valid method names to Method definitions for a
// instance's receiver.
func Methods(receiver interface{}) (map[string]Method, error) {
kind := reflect.TypeOf(receiver)
val := reflect.ValueOf(receiver)
if name := reflect.Indirect(val).Type().Name(); !isExported(name) {
return nil, fmt.Errorf("receiver must be exported: %s", name)
}
methods := map[string]Method{}
for i := 0; i < kind.NumMethod(); i++ {
method := kind.Method(i)
if method.PkgPath != "" {
// Skip unexported methods
continue
}
// Load arg types (skip first arg, the receiver)
argTypes, hasCtx, ok := methodArgTypes(method.Type)
if !ok {
// Skip methods with unexported arg types
continue
}
// Substitute injected types
// Find ErrPos, if any.
errPos, ok := methodErrPos(method.Type)
if !ok {
return nil, fmt.Errorf("unsupported return values in method: %s", method.Name)
}
methods[method.Name] = Method{
Receiver: val,
Method: method,
ArgTypes: argTypes,
ErrPos: errPos,
HasCtx: hasCtx,
}
}
return methods, nil
}
// Method is the definition of a callable method.
type Method struct {
Receiver reflect.Value
Method reflect.Method
ArgTypes []reflect.Type
ErrPos int
HasCtx bool
}
// CallJSON wraps Call but supports JSON-encoded args
func (m *Method) CallJSON(ctx context.Context, rawArgs json.RawMessage) (interface{}, error) {
args, err := parsePositionalArguments(rawArgs, m.ArgTypes)
if err != nil {
return nil, err
}
return m.Call(ctx, args)
}
// Call executes the method with the given arguments.
func (m *Method) Call(ctx context.Context, args []reflect.Value) (interface{}, error) {
if len(args) != len(m.ArgTypes) {
return nil, fmt.Errorf("invalid number of args: expected %d, got %d", len(m.ArgTypes), len(args))
}
arguments := []reflect.Value{m.Receiver}
if m.HasCtx {
arguments = append(arguments, reflect.ValueOf(ctx))
}
if len(args) > 0 {
arguments = append(arguments, args...)
}
reply := m.Method.Func.Call(arguments)
// Are there any return values?
if len(reply) == 0 {
return nil, nil
}
// Is there an error return value?
if m.ErrPos >= 0 && !reply[m.ErrPos].IsNil() {
return nil, reply[m.ErrPos].Interface().(error)
}
// All is good, assume the first result is what we want to return
// This supports (), (err), (res, err)
return reply[0].Interface(), nil
}