-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfiller.go
147 lines (122 loc) · 3.34 KB
/
filler.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
package defaults
import (
"fmt"
"reflect"
)
type FieldData struct {
Field reflect.StructField
Value reflect.Value
TagValue string
Parent *FieldData
}
type FillerFunc func(field *FieldData)
// Filler contains all the functions to fill any struct field with any type
// allowing to define function by Kind, Type of field name
type Filler struct {
FuncByName map[string]FillerFunc
FuncByType map[TypeHash]FillerFunc
FuncByKind map[reflect.Kind]FillerFunc
Tag string
}
// Fill apply all the functions contained on Filler, setting all the possible
// values
func (f *Filler) Fill(variable interface{}) {
fields := f.getFields(variable)
f.SetDefaultValues(fields)
}
func (f *Filler) getFields(variable interface{}) []*FieldData {
valueObject := reflect.ValueOf(variable).Elem()
return f.GetFieldsFromValue(valueObject, nil)
}
func (f *Filler) GetFieldsFromValue(valueObject reflect.Value, parent *FieldData) []*FieldData {
typeObject := valueObject.Type()
count := valueObject.NumField()
var results []*FieldData
for i := 0; i < count; i++ {
value := valueObject.Field(i)
field := typeObject.Field(i)
if value.CanSet() {
results = append(results, &FieldData{
Value: value,
Field: field,
TagValue: field.Tag.Get(f.Tag),
Parent: parent,
})
}
}
return results
}
func (f *Filler) SetDefaultValues(fields []*FieldData) {
for _, field := range fields {
if f.isEmpty(field) {
f.SetDefaultValue(field)
}
}
}
func (f *Filler) isEmpty(field *FieldData) bool {
switch field.Value.Kind() {
case reflect.Bool:
return !field.Value.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Value.Int() == 0
case reflect.Float32, reflect.Float64:
return field.Value.Float() == .0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return field.Value.Uint() == 0
case reflect.Slice:
switch field.Value.Type().Elem().Kind() {
case reflect.Struct:
// always assume the structs in the slice is empty and can be filled
// the actually struct filling logic should take care of the rest
return true
default:
return field.Value.Len() == 0
}
case reflect.String:
return field.Value.String() == ""
}
return true
}
func (f *Filler) SetDefaultValue(field *FieldData) {
getters := []func(field *FieldData) FillerFunc{
f.getFunctionByName,
f.getFunctionByType,
f.getFunctionByKind,
}
for _, getter := range getters {
filler := getter(field)
if filler != nil {
filler(field)
return
}
}
return
}
func (f *Filler) getFunctionByName(field *FieldData) FillerFunc {
if f, ok := f.FuncByName[field.Field.Name]; ok == true {
return f
}
return nil
}
func (f *Filler) getFunctionByType(field *FieldData) FillerFunc {
if f, ok := f.FuncByType[GetTypeHash(field.Field.Type)]; ok == true {
return f
}
return nil
}
func (f *Filler) getFunctionByKind(field *FieldData) FillerFunc {
if f, ok := f.FuncByKind[field.Field.Type.Kind()]; ok == true {
return f
}
return nil
}
// TypeHash is a string representing a reflect.Type following the next pattern:
// <package.name>.<type.name>
type TypeHash string
// GetTypeHash returns the TypeHash for a given reflect.Type
func GetTypeHash(t reflect.Type) TypeHash {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return TypeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
}