-
Notifications
You must be signed in to change notification settings - Fork 101
/
spec.go
674 lines (574 loc) · 22.1 KB
/
spec.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package yarpcconfig
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/uber-go/mapdecode"
"go.uber.org/multierr"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/config"
)
// TransportSpec specifies the configuration parameters for a transport. These
// specifications are registered against a Configurator to teach it how to
// parse the configuration for that transport and build instances of it.
//
// Every TransportSpec MUST have a BuildTransport function. The spec may
// provide BuildInbound, BuildUnaryOutbound, and BuildOnewayOutbound functions
// if the Transport supports that functionality. For example, if a transport
// only supports incoming and outgoing Oneway requests, its spec will provide a
// BuildTransport, BuildInbound, and BuildOnewayOutbound function.
//
// The signature of BuildTransport must have the shape:
//
// func(C, *config.Kit) (transport.Transport, error)
//
// Where C is a struct defining the configuration parameters for the transport,
// the kit carries information and tools from the configurator to this and
// other builders.
//
// The remaining Build* functions must have a similar signature, but also
// receive the transport instance.
//
// func(C, transport.Transport, *config.Kit) (X, error)
//
// Where X is one of, transport.Inbound, transport.UnaryOutbound, or
// transport.OnewayOutbound.
//
// For example,
//
// func(*OutboundConfig, transport.Transport) (transport.UnaryOutbound, error)
//
// Is a function to build a unary outbound from its outbound configuration and
// the corresponding transport.
type TransportSpec struct {
// Name of the transport
Name string
// A function in the shape,
//
// func(C, *config.Kit) (transport.Transport, error)
//
// Where C is a struct or pointer to a struct defining the configuration
// parameters accepted by this transport.
//
// This function will be called with the parsed configuration to build
// Transport defined by this spec.
BuildTransport interface{}
// TODO(abg): Make error returns optional
// A function in the shape,
//
// func(C, transport.Transport, *config.Kit) (transport.Inbound, error)
//
// Where C is a struct or pointer to a struct defining the configuration
// parameters for the inbound.
//
// This may be nil if this transport does not support inbounds.
//
// This function will be called with the parsed configuration and the
// transport built by BuildTransport to build the inbound for this
// transport.
BuildInbound interface{}
// The following two are functions in the shapes,
//
// func(C, transport.Transport, *config.Kit) (transport.UnaryOutbound, error)
// func(C, transport.Transport, *config.Kit) (transport.OnewayOutbound, error)
//
// Where C is a struct or pointer to a struct defining the configuration
// parameters for outbounds of that RPC type.
//
// Either value may be nil to indicate that the transport does not support
// unary or oneway outbounds.
//
// These functions will be called with the parsed configurations and the
// transport built by BuildTransport to build the unary and oneway
// outbounds for this transport.
BuildUnaryOutbound interface{}
BuildOnewayOutbound interface{}
// Named presets.
//
// These may be used by specifying a `with` key in the outbound
// configuration.
PeerChooserPresets []PeerChooserPreset
// TODO(abg): Allow functions to return and accept specific
// implementations. Instead of returning a transport.Transport and
// accepting a transport.Transport, we could make it so that
//
// BuildTransport: func(...) (*http.Transport, error)
// BuildInbound: func(..., t *http.Transport) (*http.Inbound, error)
//
// This will get rid of the `t.(*http.Transport)` users will have to do
// the first thing inside their BuildInbound.
}
// PeerChooserPreset defines a named preset for a peer chooser. Peer chooser
// presets may be used by specifying a `with` key in the outbound
// configuration.
//
// http:
// with: mypreset
type PeerChooserPreset struct {
Name string
// A function in the shape,
//
// func(peer.Transport, *config.Kit) (peer.Chooser, error)
//
// Where the first argument is the transport object for which this preset
// is being built.
BuildPeerChooser interface{}
// NOTE(abg): BuildChooser /could/ be a well-defined func type rather
// than an interface{}. We've kept it as an interface{} so that we have
// the freedom to add more information to the functions in the future.
}
// PeerListSpec specifies the configuration parameters for an outbound peer
// list. Peer lists dictate the peer selection strategy and receive updates of
// new and removed peers from peer updaters. These specifications are
// registered against a Configurator to teach it how to parse the
// configuration for that peer list and build instances of it.
//
// For example, we could implement and register a peer list spec that selects
// peers at random and a peer list updater which pushes updates to it by
// polling a specific DNS A record.
//
// myoutbound:
// random:
// dns:
// name: myservice.example.com
type PeerListSpec struct {
Name string
// A function in the shape,
//
// func(C, *config.Kit) (peer.ChooserList, error)
//
// Where C is a struct or pointer to a struct defining the configuration
// parameters needed to build this peer list. Parameters on the struct
// should not conflict with peer list updater names as they share the
// namespace with these fields.
//
// BuildPeerList is required.
BuildPeerList interface{}
}
// PeerListUpdaterSpec specifies the configuration parameters for an outbound
// peer list updater. Peer list updaters inform peer lists about peers as they
// are added or removed. These specifications are registered against a
// Configurator to teach it how to parse the configuration for that peer list
// updater and build instances of it.
//
// For example, we could implement a peer list updater which monitors a
// specific file on the system for a list of peers and pushes updates to any
// peer list.
//
// myoutbound:
// round-robin:
// peers-file:
// format: json
// path: /etc/hosts.json
type PeerListUpdaterSpec struct {
// Name of the peer selection strategy.
Name string
// A function in the shape,
//
// func(C, *config.Kit) (peer.Binder, error)
//
// Where C is a struct or pointer to a struct defining the configuration
// parameters accepted by this peer chooser.
//
// The returned peer binder will receive the peer list specified alongside
// the peer updater; it should return a peer updater that feeds updates to
// that peer list once started.
//
// BuildPeerListUpdater is required.
BuildPeerListUpdater interface{}
}
var (
_typeOfError = reflect.TypeOf((*error)(nil)).Elem()
_typeOfTransport = reflect.TypeOf((*transport.Transport)(nil)).Elem()
_typeOfInbound = reflect.TypeOf((*transport.Inbound)(nil)).Elem()
_typeOfUnaryOutbound = reflect.TypeOf((*transport.UnaryOutbound)(nil)).Elem()
_typeOfOnewayOutbound = reflect.TypeOf((*transport.OnewayOutbound)(nil)).Elem()
_typeOfPeerTransport = reflect.TypeOf((*peer.Transport)(nil)).Elem()
_typeOfPeerChooserList = reflect.TypeOf((*peer.ChooserList)(nil)).Elem()
_typeOfPeerChooser = reflect.TypeOf((*peer.Chooser)(nil)).Elem()
_typeOfBinder = reflect.TypeOf((*peer.Binder)(nil)).Elem()
)
// Compiled internal representation of a user-specified TransportSpec.
type compiledTransportSpec struct {
Name string // name of the transport
// configSpec of the top-level transport object
Transport *configSpec
// The following are non-nil only if the transport supports that specific
// functionality.
Inbound *configSpec
UnaryOutbound *configSpec
OnewayOutbound *configSpec
PeerChooserPresets map[string]*compiledPeerChooserPreset
}
func (s *compiledTransportSpec) SupportsUnaryOutbound() bool {
return s.UnaryOutbound != nil
}
func (s *compiledTransportSpec) SupportsOnewayOutbound() bool {
return s.OnewayOutbound != nil
}
func compileTransportSpec(spec *TransportSpec) (*compiledTransportSpec, error) {
out := compiledTransportSpec{Name: spec.Name}
if spec.Name == "" {
return nil, errors.New("Name is required")
}
switch strings.ToLower(spec.Name) {
case "unary", "oneway":
return nil, fmt.Errorf("transport name cannot be %q: %q is a reserved name", spec.Name, spec.Name)
}
if spec.BuildTransport == nil {
return nil, errors.New("BuildTransport is required")
}
var err error
// Helper to chain together the compile calls
appendError := func(cs *configSpec, e error) *configSpec {
err = multierr.Append(err, e)
return cs
}
out.Transport = appendError(compileTransportConfig(spec.BuildTransport))
if spec.BuildInbound != nil {
out.Inbound = appendError(compileInboundConfig(spec.BuildInbound))
}
if spec.BuildUnaryOutbound != nil {
out.UnaryOutbound = appendError(compileUnaryOutboundConfig(spec.BuildUnaryOutbound))
}
if spec.BuildOnewayOutbound != nil {
out.OnewayOutbound = appendError(compileOnewayOutboundConfig(spec.BuildOnewayOutbound))
}
if len(spec.PeerChooserPresets) == 0 {
return &out, err
}
presets := make(map[string]*compiledPeerChooserPreset, len(spec.PeerChooserPresets))
out.PeerChooserPresets = presets
for _, p := range spec.PeerChooserPresets {
if _, ok := presets[p.Name]; ok {
err = multierr.Append(err, fmt.Errorf(
"found multiple peer lists with the name %q under transport %q",
p.Name, spec.Name))
continue
}
cp, e := compilePeerChooserPreset(p)
if e != nil {
err = multierr.Append(err, fmt.Errorf(
"failed to compile preset for transport %q: %v", spec.Name, e))
continue
}
presets[p.Name] = cp
}
return &out, err
}
func compileTransportConfig(build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
var err error
switch {
case t.Kind() != reflect.Func:
err = errors.New("must be a function")
case t.NumIn() != 2:
err = fmt.Errorf("must accept exactly two arguments, found %v", t.NumIn())
case !isDecodable(t.In(0)):
err = fmt.Errorf("must accept a struct or struct pointer as its first argument, found %v", t.In(0))
case t.In(1) != _typeOfKit:
err = fmt.Errorf("must accept a %v as its second argument, found %v", _typeOfKit, t.In(1))
case t.NumOut() != 2:
err = fmt.Errorf("must return exactly two results, found %v", t.NumOut())
case t.Out(0) != _typeOfTransport:
err = fmt.Errorf("must return a transport.Transport as its first result, found %v", t.Out(0))
case t.Out(1) != _typeOfError:
err = fmt.Errorf("must return an error as its second result, found %v", t.Out(1))
}
if err != nil {
return nil, fmt.Errorf("invalid BuildTransport %v: %v", t, err)
}
return &configSpec{inputType: t.In(0), factory: v}, nil
}
func compileInboundConfig(build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
if err := validateConfigFunc(t, _typeOfInbound); err != nil {
return nil, fmt.Errorf("invalid BuildInbound: %v", err)
}
inputType := t.In(0)
fields := fieldNames(inputType)
if _, hasType := fields["Type"]; hasType {
return nil, errors.New("inbound configurations must not have a Type field: Type is a reserved field name")
}
if _, hasDisabled := fields["Disabled"]; hasDisabled {
return nil, errors.New("inbound configurations must not have a Disabled field: Disabled is a reserved field name")
}
return &configSpec{inputType: inputType, factory: v}, nil
}
func compileUnaryOutboundConfig(build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
if err := validateConfigFunc(t, _typeOfUnaryOutbound); err != nil {
return nil, fmt.Errorf("invalid BuildUnaryOutbound: %v", err)
}
return &configSpec{inputType: t.In(0), factory: v}, nil
}
func compileOnewayOutboundConfig(build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
if err := validateConfigFunc(t, _typeOfOnewayOutbound); err != nil {
return nil, fmt.Errorf("invalid BuildOnewayOutbound: %v", err)
}
return &configSpec{inputType: t.In(0), factory: v}, nil
}
// Common validation for all build functions except Tranport.
func validateConfigFunc(t reflect.Type, outputType reflect.Type) error {
switch {
case t.Kind() != reflect.Func:
return errors.New("must be a function")
case t.NumIn() != 3:
return fmt.Errorf("must accept exactly three arguments, found %v", t.NumIn())
case !isDecodable(t.In(0)):
return fmt.Errorf("must accept a struct or struct pointer as its first argument, found %v", t.In(0))
case t.In(1) != _typeOfTransport:
// TODO: We can make this smarter by making transport.Transport
// optional and either the first or the second argument instead of
// requiring it as the second argument.
return fmt.Errorf("must accept a transport.Transport as its second argument, found %v", t.In(1))
case t.In(2) != _typeOfKit:
return fmt.Errorf("must accept a %v as its third argument, found %v", _typeOfKit, t.In(2))
case t.NumOut() != 2:
return fmt.Errorf("must return exactly two results, found %v", t.NumOut())
case t.Out(0) != outputType:
return fmt.Errorf("must return a %v as its first result, found %v", outputType, t.Out(0))
case t.Out(1) != _typeOfError:
return fmt.Errorf("must return an error as its second result, found %v", t.Out(1))
}
return nil
}
type compiledPeerChooserPreset struct {
name string
factory reflect.Value
}
// Build builds the peer.Chooser from the compiled peer chooser preset.
func (c *compiledPeerChooserPreset) Build(t peer.Transport, k *Kit) (peer.Chooser, error) {
results := c.factory.Call([]reflect.Value{reflect.ValueOf(t), reflect.ValueOf(k)})
chooser, _ := results[0].Interface().(peer.Chooser)
err, _ := results[1].Interface().(error)
return chooser, err
}
func compilePeerChooserPreset(preset PeerChooserPreset) (*compiledPeerChooserPreset, error) {
if preset.Name == "" {
return nil, errors.New("Name is required")
}
if preset.BuildPeerChooser == nil {
return nil, errors.New("BuildPeerChooser is required")
}
v := reflect.ValueOf(preset.BuildPeerChooser)
t := v.Type()
var err error
switch {
case t.Kind() != reflect.Func:
err = errors.New("must be a function")
case t.NumIn() != 2:
err = fmt.Errorf("must accept exactly two arguments, found %v", t.NumIn())
case t.In(0) != _typeOfPeerTransport:
err = fmt.Errorf("must accept a peer.Transport as its first argument, found %v", t.In(0))
case t.In(1) != _typeOfKit:
err = fmt.Errorf("must accept a %v as its second argument, found %v", _typeOfKit, t.In(1))
case t.NumOut() != 2:
err = fmt.Errorf("must return exactly two results, found %v", t.NumOut())
case t.Out(0) != _typeOfPeerChooser:
err = fmt.Errorf("must return a peer.Chooser as its first result, found %v", t.Out(0))
case t.Out(1) != _typeOfError:
err = fmt.Errorf("must return an error as its second result, found %v", t.Out(1))
}
if err != nil {
return nil, fmt.Errorf("invalid BuildPeerChooser %v: %v", t, err)
}
return &compiledPeerChooserPreset{name: preset.Name, factory: v}, nil
}
// Compiled internal representation of a user-specified PeerListSpec.
type compiledPeerListSpec struct {
Name string
PeerList *configSpec
}
func compilePeerListSpec(spec *PeerListSpec) (*compiledPeerListSpec, error) {
out := compiledPeerListSpec{Name: spec.Name}
if spec.Name == "" {
return nil, errors.New("Name is required")
}
if spec.BuildPeerList == nil {
return nil, errors.New("BuildPeerList is required")
}
buildPeerList, err := compilePeerListConfig(spec.BuildPeerList)
if err != nil {
return nil, err
}
out.PeerList = buildPeerList
return &out, nil
}
func compilePeerListConfig(build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
var err error
switch {
case t.Kind() != reflect.Func:
err = errors.New("must be a function")
case t.NumIn() != 3:
err = fmt.Errorf("must accept exactly three arguments, found %v", t.NumIn())
case !isDecodable(t.In(0)):
err = fmt.Errorf("must accept a struct or struct pointer as its first argument, found %v", t.In(0))
case t.In(1) != _typeOfPeerTransport:
err = fmt.Errorf("must accept a %v as its second argument, found %v", _typeOfPeerTransport, t.In(1))
case t.In(2) != _typeOfKit:
err = fmt.Errorf("must accept a %v as its third argument, found %v", _typeOfKit, t.In(2))
case t.NumOut() != 2:
err = fmt.Errorf("must return exactly two results, found %v", t.NumOut())
case t.Out(0) != _typeOfPeerChooserList:
err = fmt.Errorf("must return a peer.ChooserList as its first result, found %v", t.Out(0))
case t.Out(1) != _typeOfError:
err = fmt.Errorf("must return an error as its second result, found %v", t.Out(1))
}
if err != nil {
return nil, fmt.Errorf("invalid BuildPeerList %v: %v", t, err)
}
return &configSpec{inputType: t.In(0), factory: v}, nil
}
type compiledPeerListUpdaterSpec struct {
Name string
PeerListUpdater *configSpec
}
func compilePeerListUpdaterSpec(spec *PeerListUpdaterSpec) (*compiledPeerListUpdaterSpec, error) {
out := compiledPeerListUpdaterSpec{Name: spec.Name}
if spec.Name == "" {
return nil, errors.New("Name is required")
}
if spec.BuildPeerListUpdater == nil {
return nil, errors.New("BuildPeerListUpdater is required")
}
buildPeerListUpdater, err := compilePeerListUpdaterConfig(spec.Name, spec.BuildPeerListUpdater)
if err != nil {
return nil, err
}
out.PeerListUpdater = buildPeerListUpdater
return &out, nil
}
func compilePeerListUpdaterConfig(name string, build interface{}) (*configSpec, error) {
v := reflect.ValueOf(build)
t := v.Type()
var err error
switch {
case t.Kind() != reflect.Func:
err = errors.New("must be a function")
case t.NumIn() != 2:
err = fmt.Errorf("must accept exactly two arguments, found %v", t.NumIn())
case !isDecodable(t.In(0)):
err = fmt.Errorf("must accept a struct or struct pointer as its first argument, found %v", t.In(0))
case t.In(1) != _typeOfKit:
err = fmt.Errorf("must accept a %v as its second argument, found %v", _typeOfKit, t.In(1))
case t.NumOut() != 2:
err = fmt.Errorf("must return exactly two results, found %v", t.NumOut())
case t.Out(0) != _typeOfBinder:
err = fmt.Errorf("must return a peer.Binder as its first result, found %v", t.Out(0))
case t.Out(1) != _typeOfError:
err = fmt.Errorf("must return an error as its second result, found %v", t.Out(1))
}
if err != nil {
return nil, fmt.Errorf("invalid BuildPeerListUpdater %v: %v", t, err)
}
return &configSpec{inputType: t.In(0), factory: v}, nil
}
// Validated representation of a configuration function specified by the user.
type configSpec struct {
// Type of object expected by the factory function
inputType reflect.Type
// Factory function to call
factory reflect.Value
// Example:
//
// factory = func(http.InboundConfig, ..) (transport.Inbound, error) { .. }
// inputType = http.InboundConfig
}
// Decode the configuration for this type from the data map.
func (cs *configSpec) Decode(attrs config.AttributeMap, opts ...mapdecode.Option) (*buildable, error) {
inputConfig := reflect.New(cs.inputType)
if err := attrs.Decode(inputConfig.Interface(), opts...); err != nil {
return nil, fmt.Errorf("failed to decode %v: %v", cs.inputType, err)
}
return &buildable{factory: cs.factory, inputData: inputConfig.Elem()}, nil
}
// A fully configured object that can be built into an
// Inbound/Outbound/Transport.
type buildable struct {
// Decoded configuration data. This is a value of the same type as the
// factory function's input argument.
inputData reflect.Value
// A function that accepts Config as its first argument and returns a
// result and an error.
//
// Build(...) will call this function and interpret the result.
factory reflect.Value
// Example:
//
// factory = func(*http.InboundConfig, _) .. { .. }
// inputData = &http.InboundConfig{Address: ..}
}
// Build the object configured by this value. The arguments are passed to the
// build function with the underlying configuration as the first parameter.
//
// Arguments may be reflect.Value objects or any other type.
func (cv *buildable) Build(args ...interface{}) (interface{}, error) {
// This function roughly translates to,
//
// return factory(inputData, args...)
callArgs := make([]reflect.Value, len(args)+1)
callArgs[0] = cv.inputData
for i, v := range args {
if value, ok := v.(reflect.Value); ok {
callArgs[i+1] = value
} else {
callArgs[i+1] = reflect.ValueOf(v)
}
}
result := cv.factory.Call(callArgs)
err, _ := result[1].Interface().(error)
return result[0].Interface(), err
}
// Returns a list of struct fields for the given type. The type may be a
// struct or a pointer to a struct (arbitrarily deep).
func fieldNames(t reflect.Type) map[string]struct{} {
for ; t.Kind() == reflect.Ptr; t = t.Elem() {
}
if t.Kind() != reflect.Struct {
return nil
}
fields := make(map[string]struct{}, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.PkgPath != "" {
continue // unexported field
}
fields[field.Name] = struct{}{}
}
return fields
}
func isDecodable(t reflect.Type) bool {
for ; t.Kind() == reflect.Ptr; t = t.Elem() {
}
// TODO(abg): Do we want to support top-level map types for configuration
return t.Kind() == reflect.Struct
}