-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
215 lines (172 loc) · 4.65 KB
/
runner.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
package tagrunner
import (
"context"
"reflect"
"github.com/pkg/errors"
)
func NewRunner(opts ...Opt) *Runner {
r := &Runner{}
for _, opt := range opts {
opt(r)
}
return r
}
// RunFn the handler for the registered key and value. If returns skip == true, then skip the following values. Only goes into effect when err is nil.
type RunFn func(ctx context.Context, info *RunnerInfo) (skip bool, err error)
type LabelFn func(*TagField) string
type Runner struct {
dive bool // if true, runner would traverse all elements in an array
fns map[string]map[string]RunFn
labelFn LabelFn
}
func (runner *Runner) MustRegister(key string, value string, fn RunFn) {
if err := runner.Register(key, value, fn); err != nil {
panic(err)
}
}
// Register the handler in key and value. Not concurrent safe.
func (runner *Runner) Register(key string, value string, fn RunFn) error {
if runner == nil {
return errors.Wrapf(ErrRunnerNil, "register fail, key is %s, value is %s", key, value)
}
if runner.fns == nil {
runner.fns = make(map[string]map[string]RunFn)
}
if runner.fns[key] == nil {
runner.fns[key] = make(map[string]RunFn)
}
runner.fns[key][value] = fn
return nil
}
// SetLabelFn get label name. Mainly used in error message, with priority: label fn > json name > field name.
func (runner *Runner) SetLabelFn(fn LabelFn) {
runner.labelFn = fn
}
func (runner *Runner) Run(ctx context.Context, source interface{}) error {
return runner.runAny(ctx, nil, reflect.StructField{}, reflect.ValueOf(source))
}
func (runner *Runner) RunStruct(ctx context.Context, source interface{}) error {
return runner.runStruct(
ctx,
newTagField(
runner,
getVal(reflect.ValueOf(source)),
reflect.StructField{},
nil,
),
reflect.ValueOf(source),
)
}
func (runner *Runner) runAny(ctx context.Context, parent *TagField, field reflect.StructField, fieldValue reflect.Value) error {
// if fieldValue is nil, then return.
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
return nil
}
switch vi := getVal(fieldValue); vi.Kind() {
case reflect.Struct:
if err := runner.runStruct(
ctx,
newTagField(runner, vi, field, parent),
vi); err != nil {
return err
}
case reflect.Slice, reflect.Array:
if err := runner.runSlice(ctx, vi); err != nil {
return err
}
}
return nil
}
func (runner *Runner) runStruct(ctx context.Context, parent *TagField, fieldValue reflect.Value) error {
if runner == nil {
return errors.Wrap(ErrRunnerNil, "run struct failed")
}
val := getVal(fieldValue)
if val.Kind() != reflect.Struct {
return errors.Wrapf(ErrInvalidInput, "expected type is struct, which is %v", val.Kind())
}
for i := 0; i < val.NumField(); i++ {
v := val.Field(i)
f := val.Type().Field(i)
if err := runner.runField(ctx, parent, f, v); err != nil {
return err
}
if err := runner.runAny(ctx, parent, f, v); err != nil {
return err
}
}
return nil
}
func (runner *Runner) runSlice(ctx context.Context, fieldValue reflect.Value) error {
if !runner.dive {
return nil
}
val := getVal(fieldValue)
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return errors.Wrapf(ErrInvalidInput, "expected type is array or slice, which is %v", val.Kind())
}
for i := 0; i < val.Len(); i++ {
if err := runner.runAny(
ctx,
nil,
reflect.StructField{},
val.Index(i),
); err != nil {
return err
}
}
return nil
}
func (runner *Runner) runField(ctx context.Context, parent *TagField, structField reflect.StructField, fieldValue reflect.Value) error {
if runner == nil {
return errors.Wrap(ErrRunnerNil, "run field failed")
}
for key := range runner.fns {
if err := runner.runTag(ctx, parent, structField, fieldValue, key); err != nil {
return err
}
}
return nil
}
func (runner *Runner) runTag(
ctx context.Context,
parent *TagField,
structField reflect.StructField,
fieldValue reflect.Value,
tagKey string,
) error {
tag, ok := structField.Tag.Lookup(tagKey)
if !ok {
return nil
}
vas, err := deconstructTag(tag)
if err != nil {
return err
}
runnerInfo := &RunnerInfo{
Key: tagKey,
Field: newTagField(runner, fieldValue, structField, parent),
}
for _, p := range vas {
runnerInfo.Value = p.val
runnerInfo.Args = p.args
if fns := runner.fns; fns == nil || fns[tagKey] == nil || fns[tagKey][runnerInfo.Value] == nil {
continue
}
skip, err := runner.fns[tagKey][runnerInfo.Value](ctx, runnerInfo)
if err != nil {
return runnerInfo.wrapError(err)
}
// if skip is true, then skip the followings.
if skip {
return nil
}
}
return nil
}
func getVal(val reflect.Value) reflect.Value {
if val.Kind() == reflect.Ptr {
return getVal(val.Elem())
}
return val
}