-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathset_default.go
254 lines (232 loc) · 6.49 KB
/
set_default.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
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"errors"
"reflect"
"strconv"
"strings"
"time"
)
var (
errInvalidTagValue = errors.New("invalid tag value")
errNotPointerToStruct = errors.New("the argument must be a pointer to struct")
)
// Defaulter is used to set the default value if the data or the field of data
// is ZERO.
type Defaulter interface {
SetDefault(data interface{}) error
}
// DefaulterFunc is the function type implementing the interface Defaulter.
type DefaulterFunc func(interface{}) error
// SetDefault implements the interface Defaulter.
func (d DefaulterFunc) SetDefault(data interface{}) error { return d(data) }
// NothingDefaulter returns a Defaulter that does nothing.
func NothingDefaulter() Defaulter { return DefaulterFunc(nothingDefaulter) }
func nothingDefaulter(interface{}) error { return nil }
// SetStructFieldToDefault sets the default value of the fields of the pointer
// to struct v to the value of the tag "default" of the fields when the field
// value is ZERO.
//
// For the type of the field, it only supports some base types as follow:
// string
// float32
// float64
// int
// int8
// int16
// int32
// int64
// uint
// uint8
// uint16
// uint32
// uint64
// struct
// struct slice
// interface{ SetDefault(_default interface{}) error }
// time.Time // Format: A. Integer(UTC); B. String(RFC3339)
// time.Duration // Format: A. Integer(ms); B. String(time.ParseDuration)
// pointer to the types above
//
// Notice: If the tag value starts with ".", it represents a field name and
// the default value of current field is set to the value of that field.
// But their types must be consistent, or panic.
func SetStructFieldToDefault(v interface{}) (err error) {
vf := reflect.ValueOf(v)
switch kind := vf.Kind(); kind {
case reflect.Ptr:
vf = vf.Elem()
if vf.Kind() != reflect.Struct {
return errNotPointerToStruct
}
err = setDefault(vf)
case reflect.Struct:
return errNotPointerToStruct
}
return
}
type setDefaulter interface {
SetDefault(_default interface{}) error
}
func setDefault(vf reflect.Value) (err error) {
vt := vf.Type()
for i, _len := 0, vt.NumField(); i < _len; i++ {
fieldv := vf.Field(i)
tag := strings.TrimSpace(vt.Field(i).Tag.Get("default"))
if fieldv.Kind() == reflect.Ptr {
if !fieldv.IsNil() {
fieldv = fieldv.Elem()
} else if tag != "" {
fieldv.Set(reflect.New(fieldv.Type().Elem()))
fieldv = fieldv.Elem()
}
}
if !fieldv.CanSet() {
continue
}
switch v := fieldv.Interface().(type) {
case string:
if v == "" && tag != "" {
fieldv.SetString(tag)
}
case int:
err = setFieldInt(vf, fieldv, int64(v), tag)
case int8:
err = setFieldInt(vf, fieldv, int64(v), tag)
case int16:
err = setFieldInt(vf, fieldv, int64(v), tag)
case int32:
err = setFieldInt(vf, fieldv, int64(v), tag)
case int64:
err = setFieldInt(vf, fieldv, v, tag)
case uint:
err = setFieldUint(vf, fieldv, uint64(v), tag)
case uint8:
err = setFieldUint(vf, fieldv, uint64(v), tag)
case uint16:
err = setFieldUint(vf, fieldv, uint64(v), tag)
case uint32:
err = setFieldUint(vf, fieldv, uint64(v), tag)
case uint64:
err = setFieldUint(vf, fieldv, v, tag)
case uintptr:
err = setFieldUint(vf, fieldv, uint64(v), tag)
case float32:
err = setFieldFloat(vf, fieldv, float64(v), tag)
case float64:
err = setFieldFloat(vf, fieldv, v, tag)
case time.Duration:
err = setFieldDuration(vf, fieldv, v, tag)
case time.Time:
err = setFieldTime(vf, fieldv, v, tag)
case setDefaulter:
if tag != "" {
err = v.SetDefault(tag)
}
default:
switch fieldv.Kind() {
case reflect.Struct:
err = setDefault(fieldv)
case reflect.Slice:
for i, _len := 0, fieldv.Len(); i < _len; i++ {
if f := fieldv.Index(i); f.Kind() == reflect.Struct {
if err = setDefault(f); err != nil {
return
}
}
}
}
}
if err != nil {
return
}
}
return
}
func setFieldInt(structv, fieldv reflect.Value, v int64, tag string) (err error) {
if v == 0 && tag != "" {
if tag[0] == '.' {
if tag = tag[1:]; tag == "" {
return errInvalidTagValue
}
fieldv.Set(structv.FieldByName(tag))
} else if v, err = strconv.ParseInt(tag, 10, 64); err == nil {
fieldv.SetInt(v)
}
}
return
}
func setFieldUint(structv, fieldv reflect.Value, v uint64, tag string) (err error) {
if v == 0 && tag != "" {
if tag[0] == '.' {
if tag = tag[1:]; tag == "" {
return errInvalidTagValue
}
fieldv.Set(structv.FieldByName(tag))
} else if v, err = strconv.ParseUint(tag, 10, 64); err == nil {
fieldv.SetUint(v)
}
}
return
}
func setFieldFloat(structv, fieldv reflect.Value, v float64, tag string) (err error) {
if v == 0 && tag != "" {
if tag[0] == '.' {
if tag = tag[1:]; tag == "" {
return errInvalidTagValue
}
fieldv.Set(structv.FieldByName(tag))
} else if v, err = strconv.ParseFloat(tag, 64); err == nil {
fieldv.SetFloat(v)
}
}
return
}
func setFieldTime(structv, fieldv reflect.Value, v time.Time, tag string) (err error) {
if v.IsZero() && tag != "" {
if tag[0] == '.' {
if tag = tag[1:]; tag == "" {
return errInvalidTagValue
}
fieldv.Set(structv.FieldByName(tag))
} else if IsInteger(tag) {
var i int64
if i, err = strconv.ParseInt(tag, 10, 64); err == nil {
fieldv.Set(reflect.ValueOf(time.Unix(i, 0)))
}
} else if v, err = time.Parse(time.RFC3339, tag); err == nil {
fieldv.Set(reflect.ValueOf(v))
}
}
return
}
func setFieldDuration(structv, fieldv reflect.Value, v time.Duration, tag string) (err error) {
if v == 0 && tag != "" {
if tag[0] == '.' {
if tag = tag[1:]; tag == "" {
return errInvalidTagValue
}
fieldv.Set(structv.FieldByName(tag))
} else if IsInteger(tag) {
var i int64
if i, err = strconv.ParseInt(tag, 10, 64); err == nil {
fieldv.SetInt(i * int64(time.Millisecond))
}
} else if v, err = time.ParseDuration(tag); err == nil {
fieldv.SetInt(int64(v))
}
}
return
}