-
Notifications
You must be signed in to change notification settings - Fork 3
/
omitempty.go
200 lines (184 loc) · 5.35 KB
/
omitempty.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
package pie
import (
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"unsafe"
)
type result struct {
t reflect.Type
changed bool
hasIface bool
}
type TagMaker interface {
// MakeTag makes tag for the field the fieldIndex in the structureType.
// Result should depends on constant parameters of creation of the TagMaker and parameters
// passed to the MakeTag. The MakeTag should not produce side effects (like a pure function).
MakeTag(structureType reflect.Type, fieldIndex int) reflect.StructTag
}
type cacheKey struct {
reflect.Type
TagMaker
}
var cache = struct {
sync.RWMutex
m map[cacheKey]result
}{
m: make(map[cacheKey]result),
}
func getType(structType reflect.Type, maker TagMaker, any bool) result {
// TODO(yar): Improve synchronization for cases when one analogue
// is produced concurently by different goroutines in the same time
key := cacheKey{structType, maker}
cache.RLock()
res, ok := cache.m[key]
cache.RUnlock()
if !ok || (res.hasIface && !any) {
res = makeType(structType, maker, any)
cache.Lock()
cache.m[key] = res
cache.Unlock()
}
return res
}
func makeStructType(structType reflect.Type, maker TagMaker, any bool) result {
if structType.NumField() == 0 {
return result{t: structType, changed: false}
}
changed := false
hasPrivate := false
hasIface := false
fields := make([]reflect.StructField, 0, structType.NumField())
for i := 0; i < structType.NumField(); i++ {
strField := structType.Field(i)
oldType := strField.Type
new := getType(oldType, maker, any)
strField.Type = new.t
if oldType != new.t {
changed = true
}
if new.hasIface {
hasIface = true
}
oldTag := strField.Tag
newTag := maker.MakeTag(structType, i)
strField.Tag = newTag
if oldTag != newTag {
changed = true
}
fields = append(fields, strField)
}
if !changed {
return result{t: structType, changed: false, hasIface: hasIface}
} else if hasPrivate {
panic(fmt.Sprintf("unable to change tags for type %s, because it contains unexported fields", structType))
}
newType := reflect.StructOf(fields)
compareStructTypes(structType, newType)
return result{t: newType, changed: true, hasIface: hasIface}
}
func makeType(t reflect.Type, maker TagMaker, any bool) result {
switch t.Kind() {
case reflect.Struct:
return makeStructType(t, maker, any)
case reflect.Ptr:
res := getType(t.Elem(), maker, any)
if !res.changed {
return result{t: t, changed: false}
}
return result{t: reflect.PtrTo(res.t), changed: true}
case reflect.Array:
res := getType(t.Elem(), maker, any)
if !res.changed {
return result{t: t, changed: false}
}
return result{t: reflect.ArrayOf(t.Len(), res.t), changed: true}
case reflect.Slice:
res := getType(t.Elem(), maker, any)
if !res.changed {
return result{t: t, changed: false}
}
return result{t: reflect.SliceOf(res.t), changed: true}
case reflect.Map:
resKey := getType(t.Key(), maker, any)
resElem := getType(t.Elem(), maker, any)
if !resKey.changed && !resElem.changed {
return result{t: t, changed: false}
}
return result{t: reflect.MapOf(resKey.t, resElem.t), changed: true}
case reflect.Interface:
if any {
return result{t: t, changed: false, hasIface: true}
}
fallthrough
case
reflect.Chan,
reflect.Func,
reflect.UnsafePointer:
panic("tags.Map: Unsupported type: " + t.Kind().String())
default:
// don't modify type in another case
return result{t: t, changed: false}
}
}
func compareStructTypes(source, result reflect.Type) {
if source.Size() != result.Size() {
// TODO: debug
// fmt.Println(newType.Size(), newType)
// for i := 0; i < newType.NumField(); i++ {
// fmt.Println(newType.Field(i))
// }
// fmt.Println(structType.Size(), structType)
// for i := 0; i < structType.NumField(); i++ {
// fmt.Println(structType.Field(i))
// }
panic("tags.Map: Unexpected case - type has a size different from size of original type")
}
}
//func insertOmitemptyTag(u driver{}) driver{} {
// strPtrVal := reflect.ValueOf(u)
// t := strPtrVal.Type().Elem()
// num := t.NumField()
// fields := make([]reflect.StructField, 0, t.NumField())
// for index := 0; index < num; index++ {
// field := t.Field(index)
// tag := t.Field(index).Tag.Get("bson")
// p := (*reflect.StructField)(unsafe.Pointer(&field))
// tag = strings.Replace(tag, " ", "", -1)
// p.Tag = reflect.StructTag(fmt.Sprintf(`bson:"%s,omitempty"`, tag))
// fields = append(fields, field)
// }
//
// newType := reflect.StructOf(fields)
// newPtrVal := reflect.NewAt(newType, unsafe.Pointer(strPtrVal.Pointer()))
// return newPtrVal.Interface()
//}
func insertOmitemptyTag(u interface{}) interface{} {
strPtrVal := reflect.ValueOf(u)
res := getType(strPtrVal.Type().Elem(), maker{}, true)
newPtrVal := reflect.NewAt(res.t, unsafe.Pointer(strPtrVal.Pointer()))
return newPtrVal.Interface()
}
var structTypeConstructorBugWasFixed bool
func init() {
switch {
case strings.HasPrefix(runtime.Version(), "go1.7"):
// there is bug in reflect.StructOf
default:
structTypeConstructorBugWasFixed = true
}
}
type maker struct{}
func (m maker) MakeTag(t reflect.Type, fieldIndex int) reflect.StructTag {
tag := t.Field(fieldIndex).Tag.Get("bson")
if tag == "" {
return ""
}
tag = strings.Replace(tag, " ", "", -1)
if strings.Index(tag, "omitempty") > 0 {
return reflect.StructTag(fmt.Sprintf(`bson:"%s"`, tag))
}
return reflect.StructTag(fmt.Sprintf(`bson:"%s,omitempty"`, tag))
}