-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargsdef.go
311 lines (274 loc) · 8.74 KB
/
argsdef.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
package command
import (
"bytes"
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
reflection "github.com/ungerik/go-reflection"
)
var WithoutArgs ArgsDef
// ArgsDef implements Args
type ArgsDef struct {
outerStructType reflect.Type
argStructFields []reflection.NamedStructField
argInfos []Arg
initialized bool
}
func (def *ArgsDef) NumArgs() int {
return len(def.argInfos)
}
func (def *ArgsDef) Args() []Arg {
return def.argInfos
}
func (def *ArgsDef) ArgTag(index int, tag string) string {
return def.argStructFields[index].Field.Tag.Get(tag)
}
// String implements the fmt.Stringer interface.
func (def *ArgsDef) String() string {
if !def.initialized {
return "ArgsDef not initialized"
}
var b strings.Builder
for _, arg := range def.argInfos {
if b.Len() > 0 {
b.WriteByte(' ')
}
fmt.Fprintf(&b, "<%s:%s>", arg.Name, reflection.DerefType(arg.Type))
}
return b.String()
}
// Init initializes ArgsDef with the reflection data from
// outerStructPtr wich has to be the address of the struct
// variable that embedds ArgsDef.
func (def *ArgsDef) Init(outerStructPtr interface{}) error {
if def.initialized {
return nil
}
if _, ok := outerStructPtr.(Args); !ok {
return fmt.Errorf("outerStructPtr of type %T does not implement interface Args", outerStructPtr)
}
def.outerStructType = reflection.DerefType(reflect.TypeOf(outerStructPtr))
if def.outerStructType.Kind() != reflect.Struct {
return fmt.Errorf("ArgsDef must be contained in a struct, but outer type is %s", def.outerStructType)
}
def.argStructFields = reflection.FlatExportedNamedStructFields(def.outerStructType, ArgNameTag)
def.argInfos = make([]Arg, len(def.argStructFields))
for i := range def.argInfos {
def.argInfos[i].Name = def.argStructFields[i].Name
def.argInfos[i].Description = def.ArgTag(i, ArgDescriptionTag)
def.argInfos[i].Type = def.argStructFields[i].Field.Type
}
def.initialized = true
return nil
}
func (def *ArgsDef) argValsFromStringArgs(callerArgs []string) ([]reflect.Value, error) {
// Allocate a new args struct because we need addressable
// variables of struct field types to hold arg values.
// Instead of new individual variable use fields of args struct.
argsStruct := reflect.New(def.outerStructType).Elem()
argVals := make([]reflect.Value, def.NumArgs())
numStringArgs := len(callerArgs)
for i := range argVals {
argVals[i] = argsStruct.FieldByIndex(def.argStructFields[i].Field.Index)
if i >= numStringArgs {
continue
}
err := assignString(argVals[i], callerArgs[i])
if err != nil {
return nil, err
}
}
return argVals, nil
}
func (def *ArgsDef) argValsFromStringMapArgs(callerArgs map[string]string) ([]reflect.Value, error) {
// Allocate a new args struct because we need addressable
// variables of struct field types to hold arg values.
// Instead of new individual variable use fields of args struct.
argsStruct := reflect.New(def.outerStructType).Elem()
argVals := make([]reflect.Value, def.NumArgs())
for i := range argVals {
argVals[i] = argsStruct.FieldByIndex(def.argStructFields[i].Field.Index)
argName := def.argStructFields[i].Name
stringArg, hasArg := callerArgs[argName]
if !hasArg {
continue
}
err := assignString(argVals[i], stringArg)
if err != nil {
return nil, err
}
}
return argVals, nil
}
func (def *ArgsDef) argValsFromMapArgs(callerArgs map[string]interface{}) ([]reflect.Value, error) {
// Allocate a new args struct because we need addressable
// variables of struct field types to hold arg values.
// Instead of new individual variable use fields of args struct.
argsStruct := reflect.New(def.outerStructType).Elem()
argVals := make([]reflect.Value, def.NumArgs())
for i := range argVals {
argVals[i] = argsStruct.FieldByIndex(def.argStructFields[i].Field.Index)
argName := def.argStructFields[i].Name
varArg, hasArg := callerArgs[argName]
if !hasArg {
continue
}
err := assignAny(argVals[i], varArg) // TODO implement assignAny
if err != nil {
return nil, err
}
}
return argVals, nil
}
func (def *ArgsDef) argValsFromJSON(argsJSON []byte) ([]reflect.Value, error) {
argsJSON = bytes.TrimSpace(argsJSON)
if len(argsJSON) < 2 {
return nil, fmt.Errorf("invalid JSON: '%s'", string(argsJSON))
}
// Handle JSON array
if argsJSON[0] == '[' {
var callerArray []interface{}
err := json.Unmarshal(argsJSON, &callerArray)
if err != nil {
return nil, err
}
argsStruct := reflect.New(def.outerStructType).Elem()
argVals := make([]reflect.Value, def.NumArgs())
for i := range argVals {
argVals[i] = argsStruct.FieldByIndex(def.argStructFields[i].Field.Index)
if i < len(callerArray) {
err := assignAny(argVals[i], callerArray[i]) // TODO implement assignAny
if err != nil {
return nil, err
}
}
}
return argVals, nil
}
// Unmarshal argsJSON to new args struct
argsStructPtr := reflect.New(def.outerStructType)
err := json.Unmarshal(argsJSON, argsStructPtr.Interface())
if err != nil {
return nil, err
}
argsStruct := argsStructPtr.Elem()
argVals := make([]reflect.Value, def.NumArgs())
for i := range argVals {
argVals[i] = argsStruct.FieldByIndex(def.argStructFields[i].Field.Index)
}
return argVals, nil
}
func (def *ArgsDef) StringArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (StringArgsFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, callerArgs ...string) error {
argVals, err := def.argValsFromStringArgs(callerArgs)
if err != nil {
return err
}
return dispatcher.callWithResultsHandlers(ctx, argVals, resultsHandlers)
}
return f, nil
}
func (def *ArgsDef) StringMapArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (StringMapArgsFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, callerArgs map[string]string) (err error) {
argVals, err := def.argValsFromStringMapArgs(callerArgs)
if err != nil {
return err
}
return dispatcher.callWithResultsHandlers(ctx, argVals, resultsHandlers)
}
return f, nil
}
func (def *ArgsDef) MapArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (MapArgsFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, callerArgs map[string]interface{}) (err error) {
argVals, err := def.argValsFromMapArgs(callerArgs)
if err != nil {
return err
}
return dispatcher.callWithResultsHandlers(ctx, argVals, resultsHandlers)
}
return f, nil
}
func (def *ArgsDef) JSONArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (JSONArgsFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, callerArgs []byte) (err error) {
argVals, err := def.argValsFromJSON(callerArgs)
if err != nil {
return err
}
return dispatcher.callWithResultsHandlers(ctx, argVals, resultsHandlers)
}
return f, nil
}
func (def *ArgsDef) StringArgsResultValuesFunc(commandFunc interface{}) (StringArgsResultValuesFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, args []string) ([]reflect.Value, error) {
argVals, err := def.argValsFromStringArgs(args)
if err != nil {
return nil, err
}
return dispatcher.callAndReturnResults(ctx, argVals)
}
return f, nil
}
func (def *ArgsDef) StringMapArgsResultValuesFunc(commandFunc interface{}) (StringMapArgsResultValuesFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, args map[string]string) ([]reflect.Value, error) {
argVals, err := def.argValsFromStringMapArgs(args)
if err != nil {
return nil, err
}
return dispatcher.callAndReturnResults(ctx, argVals)
}
return f, nil
}
func (def *ArgsDef) MapArgsResultValuesFunc(commandFunc interface{}) (MapArgsResultValuesFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, args map[string]interface{}) ([]reflect.Value, error) {
argVals, err := def.argValsFromMapArgs(args)
if err != nil {
return nil, err
}
return dispatcher.callAndReturnResults(ctx, argVals)
}
return f, nil
}
func (def *ArgsDef) JSONArgsResultValuesFunc(commandFunc interface{}) (JSONArgsResultValuesFunc, error) {
dispatcher, err := newFuncDispatcher(def, commandFunc)
if err != nil {
return nil, err
}
f := func(ctx context.Context, argsJSON []byte) ([]reflect.Value, error) {
argVals, err := def.argValsFromJSON(argsJSON)
if err != nil {
return nil, err
}
return dispatcher.callAndReturnResults(ctx, argVals)
}
return f, nil
}