/
option.go
126 lines (114 loc) · 3.07 KB
/
option.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
package scaffolder
import (
"errors"
"reflect"
)
var (
// ErrInvalidTarget is returned if the target given in the Init function is not a pointer.
ErrInvalidTarget = errors.New("the target must be a pointer")
// ErrInvalidOption is returned if the given Option does not respect the prototype:
// func(pointer) error.
ErrInvalidOption = errors.New("the option does not respect the mandatory prototype")
errUnmatchingTargetType = errors.New("unmatching target type and option argument")
)
// Defaulter optional interface which can be used to attach default values
// to a component.
type Defaulter interface {
Default()
}
// Option are generic functor used to configure a component,
// it is intended to be used to set one field at a time.
//
// func(f *Form) error {
// f.Age = age
// return nil
// }
//
// the function should always respect the prototype: func(pointer) error
type Option interface{}
func validate(o Option, target reflect.Type) error {
oType := reflect.TypeOf(o)
switch {
case oType == nil:
fallthrough
case oType.Kind() != reflect.Func:
fallthrough
case oType.NumIn() != 1:
fallthrough
case oType.NumOut() != 1:
fallthrough
case oType.In(0).Kind() != reflect.Ptr:
fallthrough
case !oType.Out(0).Implements(errorInterface):
return ErrInvalidOption
case oType.In(0) != target:
return errUnmatchingTargetType
}
return nil
}
// Init will take care of initializing the given Component by first calling
// the default method, if the component implements the Defaulter interface.
//
// Afterward, it will iterate through the list of options
// and apply them one after another.
//
// type Form struct {
// Age int
// FirstName string
// }
//
// func (f *Form) Default() {
// f.FirstName = "FirstName"
// }
//
// func WithAge(age int) scaffolder.Option {
// return func(f *Form) error {
// f.Age = age
// return nil
// }
// }
func Init(target Component, opts ...Option) error {
targetType := reflect.TypeOf(target)
if targetType.Kind() != reflect.Ptr {
return ErrInvalidOption
}
if defaulter, ok := target.(Defaulter); ok {
defaulter.Default()
}
targetValue := reflect.ValueOf(target)
args := []reflect.Value{targetValue}
for _, opt := range opts {
err := validate(opt, targetType)
switch {
case err == errUnmatchingTargetType:
continue
case err != nil:
return err
}
returnedValues := reflect.ValueOf(opt).Call(args)
if err, _ := returnedValues[0].Interface().(error); err != nil {
return err
}
}
return nil
}
// Configuration define a generic interface to turn configuration structure into
// usable component in the Scaffolder framework.
type Configuration interface {
Options() []Option
}
// Configure apply the Options returned by the Configuration.
//
// type Config struct {
// FirstName string `json:"first_name"`
// Age int `json:"age"`
// }
//
// func (c *Config) Options() []scaffolder.Option {
// return []scaffolder.Option{
// WithAge(c.Age),
// }
// }
func Configure(target Component, cfg Configuration) error {
return Init(target, cfg.Options()...)
}