-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
217 lines (188 loc) · 4.18 KB
/
field.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
package flagx
import (
"flag"
"fmt"
"reflect"
"strconv"
"strings"
)
const (
KEY_USAGE = "usage"
KEY_DEF = "def"
KEY_MAX = "max"
KEY_MIN = "min"
)
var (
ErrOptMissHandler = Error("opt(%v) in field(%v) missing handler")
ErrOptInvalid = Error("optString(%v) in field(%v) is invalid")
ErrTypeSupport = Error("type(%v) is not supported")
)
type Fielder interface {
Init() error
Default() interface{}
BindFlag(*flag.FlagSet)
}
type ArgsSetter interface {
SetArgs(v *reflect.Value, args []string) error
}
type ArgSetter interface {
SetArg(v *reflect.Value, arg string) error
}
type OptBinder interface {
BindOpt(key, val string) error
}
type Arg interface {
Arg(n int) string
}
type AfterParser interface {
AfterParse() error
}
type Field struct {
Name string // field name
Type reflect.Type // field type
Usage string
flagName string
DefVal string
Val *reflect.Value
fielder Fielder
}
func NewField(t reflect.StructField, val reflect.Value) (f *Field, err error) {
f = &Field{
Name: t.Name,
Val: &val,
Type: t.Type,
}
if !IsPubField(t.Name) {
return nil, nil
}
fieldFunc, errSelectField := f.selectFielder(t.Type)
if errSelectField == nil {
f.fielder = fieldFunc(f)
}
process, err := f.decodeTag(t.Tag)
if errSelectField != nil && process {
panic(errSelectField)
} else if !process || err != nil {
return nil, err
}
if err = f.fielder.Init(); err != nil {
return nil, err
}
return f, nil
}
func (f *Field) selectFielder(t reflect.Type) (func(f *Field) Fielder, error) {
switch t.Kind() {
case reflect.Int, reflect.Int64, reflect.Uint:
fallthrough
case reflect.Int8, reflect.Int16, reflect.Int32:
if n := IntFieldHook.Select(t); n != nil {
return n, nil
}
return NewIntField, nil
case reflect.Bool:
if n := BoolFieldHook.Select(t); n != nil {
return n, nil
}
return NewBoolField, nil
case reflect.String:
if n := StringFieldHook.Select(t); n != nil {
return n, nil
}
return NewStringField, nil
case reflect.Slice:
if n := SliceFieldHook.Select(t); n != nil {
return n, nil
}
return NewSliceField, nil
default:
return nil, fmt.Errorf("not support for type: %v", t)
}
}
func (f *Field) ArgIdx() (int, bool) {
list := RegexpArgNumName.FindAllStringSubmatch(f.flagName, -1)
if len(list) == 0 {
return 0, false
}
idx, err := strconv.Atoi(list[0][1])
if err != nil {
idx = -1
}
return idx, true
}
func (f *Field) decodeTag(t reflect.StructTag) (bool, error) {
flagString := t.Get("flag")
if flagString == "-" {
return false, nil
}
tags := strings.Split(flagString, ";")
start := 1
if strings.Contains(tags[0], "=") {
start = 0
} else {
f.flagName = tags[0]
}
for i := start; i < len(tags); i++ {
sp := strings.Split(tags[i], "=")
if len(sp) != 2 {
return true, ErrOptInvalid.Format(flagString, f.Name)
}
switch sp[0] {
case KEY_USAGE:
f.Usage = sp[1]
case KEY_DEF:
f.DefVal = sp[1]
default:
ob, ok := f.fielder.(OptBinder)
if !ok {
return true, ErrOptMissHandler.Format(sp[0], f.Name)
}
if err := ob.BindOpt(sp[0], sp[1]); err != nil {
return true, err
}
}
}
return true, nil
}
func (f *Field) FlagName() string {
if f.flagName == "" {
return strings.ToLower(f.Name[:1]) + f.Name[1:]
}
return f.flagName
}
func (f *Field) String() string {
return fmt.Sprintf("&%+v", *f)
}
func (f *Field) Default() interface{} {
return f.fielder.Default()
}
func (f *Field) Instance() interface{} {
return f.Val.Addr().Interface()
}
func (f *Field) BindFlag(fs *flag.FlagSet) {
f.fielder.BindFlag(fs)
}
func (f *Field) SetArgs(v *reflect.Value, fs *flag.FlagSet) error {
as, ok := f.fielder.(ArgsSetter)
if !ok {
return fmt.Errorf("the type(%v) of field(%v) can't be args", f.Type, f.Name)
}
return as.SetArgs(v, fs.Args())
}
func (f *Field) SetArg(v *reflect.Value, fs *flag.FlagSet) error {
as, ok := f.fielder.(ArgSetter)
if !ok {
return fmt.Errorf("field %v is not settable arg", f)
}
idx, ok := f.ArgIdx()
if !ok {
return fmt.Errorf("field %v is not define to arg", f)
}
return as.SetArg(v, fs.Arg(idx))
}
func (f *Field) AfterParse() error {
ap, ok := f.fielder.(AfterParser)
if !ok {
return nil
}
return ap.AfterParse()
}