-
Notifications
You must be signed in to change notification settings - Fork 0
/
stom.go
310 lines (248 loc) · 7.46 KB
/
stom.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
// Package stom is about converting structs to map[string]interface{} with
// minimum processing and overhead
package stom
import (
"database/sql/driver"
"fmt"
"reflect"
)
// Policy is a type to define policy of dealing with 'nil' values
type Policy uint8
const (
// PolicyUseDefault enforces SToM to use defined default value instead
// of 'nil' value in resulting map[string]interface{}.
// Default value can be nil also.
PolicyUseDefault Policy = iota
// PolicyExclude tells SToM to ignore 'nil' values and to not include them in
// resulting map
PolicyExclude
)
// Package settings
// They are used as defaults for initialization if new SToMs
var (
tagSetting = "db"
policySetting = PolicyUseDefault
defaultValueSetting interface{}
)
// Zeroable is an interface that allows to filter values that can explicitly
// state that they are 'zeroes'. For example, this interface allows to filter
// zero time.Time,
type Zeroable interface {
IsZero() bool
}
// ToMappable defines an entity that knows how to convert itself to map[string]interface{}.
// If an entity implements this interface, SToM won't do any magic,
// it will just call ToMap() method to makes thigs simpler.
// Such approach allows custom conversion
type ToMappable interface {
ToMap() (map[string]interface{}, error)
}
// ToMapper defines a service that is able to convert something to map[string]interface{}
type ToMapper interface {
ToMap(s interface{}) (map[string]interface{}, error)
}
// ToMapperFunc defines a function that implements ToMapper
type ToMapperFunc func(s interface{}) (map[string]interface{}, error)
// ToMap implements ToMapper
func (f ToMapperFunc) ToMap(s interface{}) (map[string]interface{}, error) {
return f(s)
}
type tags struct {
Simple map[int]string
Nested map[int]tags
}
func (t tags) TagsList() []string {
tagsList := []string{}
tagsMap := map[string]interface{}{}
for _, tagName := range t.Simple {
tagsMap[tagName] = nil
}
for _, nestedTags := range t.Nested {
nestedTagsList := nestedTags.TagsList()
for _, tagName := range nestedTagsList {
tagsMap[tagName] = nil
}
}
for tagName := range tagsMap {
tagsList = append(tagsList, tagName)
}
return tagsList
}
func newTags() tags {
return tags{
Simple: make(map[int]string),
Nested: make(map[int]tags),
}
}
// stom is a small handy tool that is instantiated for certain type and caches
// all knowledge about this type to increase conversion speed
type stom struct {
defaultValue interface{}
policy Policy
tag string
typ reflect.Type
cache tags
tagValues []string
}
// MustNewStom creates new instance of a SToM converter for type of given structure.
// Panics if no structure provided
func MustNewStom(s interface{}) *stom {
typ, err := getStructType(s)
if err != nil {
panic(err.Error())
}
stom := &stom{
typ: typ,
defaultValue: defaultValueSetting,
policy: policySetting,
}
stom.SetTag(tagSetting)
return stom
}
// SetTag sets SToM to scan for given tag in structure
func (s *stom) SetTag(tag string) *stom {
s.tag = tag
s.cache = extractTagValues(s.typ, s.tag)
s.tagValues = s.cache.TagsList()
return s
}
// SetDefault makes SToM to put given default value in 'nil' values of structure's fields
func (s *stom) SetDefault(defaultValue interface{}) *stom {
s.defaultValue = defaultValue
return s
}
// SetPolicy sets policy for 'nil' values
func (s *stom) SetPolicy(policy Policy) *stom {
s.policy = policy
return s
}
// TagValues returns list of cached tag values that were processed by SToM
// Note that these tag values do not include tag values of nested structures
func (s *stom) TagValues() []string {
return s.tagValues
}
// ToMap converts a structure to map[string]interface{}.
// SToM converts only structures it was initialized for
func (s *stom) ToMap(obj interface{}) (map[string]interface{}, error) {
typ, err := getStructType(obj)
if err != nil {
return nil, err
}
if typ != s.typ {
return nil, fmt.Errorf("stom is set up to work with type %s, but %s given", s.typ, typ)
}
return toMap(obj, s.cache, s.defaultValue, s.policy)
}
// SetTag sets package setting for tag to look for in incoming structures
func SetTag(t string) { tagSetting = t }
// SetDefault sets package default value to set instead of 'nil' in resulting maps
func SetDefault(dv interface{}) { defaultValueSetting = dv }
// SetPolicy sets package setting for policy. Policy defines what to do with
// 'nil' values in resulting maps.
// There are 2 policies:
// - PolicyUseDefault - with this policy default value will be used instead of 'nil'
// - PolicyExclude - 'nil' values will be discarded
func SetPolicy(p Policy) { policySetting = p }
// ConvertToMap converts given structure into map[string]interface{}
func ConvertToMap(s interface{}) (map[string]interface{}, error) {
if tomappable, ok := s.(ToMappable); ok {
return tomappable.ToMap()
}
typ, err := getStructType(s)
if err != nil {
return nil, err
}
tagmap := extractTagValues(typ, tagSetting)
return toMap(s, tagmap, defaultValueSetting, policySetting)
}
func getStructType(s interface{}) (t reflect.Type, err error) {
t = reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Invalid {
err = fmt.Errorf("value is invalid:\n %v", s)
return
}
if t.Kind() != reflect.Struct {
err = fmt.Errorf("provided value is not a struct but %v", t.Kind())
}
return
}
// extractTagValues scans given type and tries to find all fields with given tag
// Indices of all found fields are stored as values in resulting map
// Keys of resulting map are actual values of tags
func extractTagValues(typ reflect.Type, tag string) tags {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
tagValues := newTags()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tagValue := field.Tag.Get(tag)
if field.Anonymous && tagValue != "-" {
tagValues.Nested[i] = extractTagValues(field.Type, tag)
continue
}
if tagValue != "" && tagValue != "-" && field.PkgPath == "" { // exported
tagValues.Simple[i] = tagValue
}
}
return tagValues
}
func toMap(obj interface{}, tagmap tags, defaultValue interface{}, policy Policy) (map[string]interface{}, error) {
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
result := map[string]interface{}{}
for index, tag := range tagmap.Simple {
vField := val.Field(index)
v, err := filterValue(vField)
if err != nil {
return result, err
}
if v != nil {
result[tag] = v
} else if policy == PolicyUseDefault {
result[tag] = defaultValue
}
}
for index, tags := range tagmap.Nested {
vField := val.Field(index)
valueMap, err := toMap(vField.Interface(), tags, defaultValue, policy)
if err != nil {
return result, err
}
for k, v := range valueMap {
result[k] = v
}
}
return result, nil
}
// filterValue filters given value of some structure's field.
// Simple values are left as is. There is some special logic about particular types
// like ToMappable
func filterValue(vField reflect.Value) (v interface{}, err error) {
kind := vField.Kind()
if kind == reflect.Ptr {
if vField.Elem().IsValid() {
v = vField.Elem().Interface()
}
} else {
v = vField.Interface()
}
switch t := v.(type) {
case driver.Valuer: // support for NullTypes like sql.NullString and so on
if converted, convErr := t.Value(); convErr != nil || converted == nil {
v = nil
}
case Zeroable:
if t.IsZero() {
v = nil
}
case ToMappable:
v, err = t.ToMap()
}
return v, nil
}