-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
135 lines (116 loc) · 2.64 KB
/
struct.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
package disorder
import (
"fmt"
"reflect"
"strings"
"sync"
)
type IsZeroer interface {
IsZero() bool
}
var (
structsMap = make(map[reflect.Type]*structInfo)
structsMapMutex sync.RWMutex
)
type structInfo struct {
fieldsMap map[string]*fieldInfo
fieldsList []*fieldInfo
}
type fieldInfo struct {
key string
index int
omitEmpty bool
}
func getStructInfo(typ reflect.Type) (*structInfo, error) {
structsMapMutex.RLock()
s, exists := structsMap[typ]
structsMapMutex.RUnlock()
if exists {
return s, nil
}
fmt.Println("create new struct info:", typ.String())
count := typ.NumField()
s = &structInfo{
fieldsMap: map[string]*fieldInfo{},
fieldsList: make([]*fieldInfo, 0, count),
}
for i := 0; i < count; i++ {
field := typ.Field(i)
if field.PkgPath != "" && !field.Anonymous {
continue // Private field
}
f := &fieldInfo{
index: i,
}
tag := field.Tag.Get("disorder")
if tag == "" && !strings.Contains(string(field.Tag), ":") {
tag = string(field.Tag)
}
if tag == "-" {
continue
}
fields := strings.Split(tag, ",")
if len(fields) > 1 {
for _, flag := range fields[1:] {
switch flag {
case "omitempty":
f.omitEmpty = true
default:
return nil, fmt.Errorf("unsupported flag %s in tag %s", flag, tag)
}
}
tag = fields[0]
}
if tag != "" {
f.key = tag
} else {
f.key = field.Name
}
if _, exists = s.fieldsMap[f.key]; exists {
return nil, fmt.Errorf("duplicated key '" + f.key + "' in struct " + typ.String())
}
s.fieldsList = append(s.fieldsList, f)
s.fieldsMap[f.key] = f
}
structsMap[typ] = s
return s, nil
}
func isZero(value reflect.Value) bool {
kind := value.Kind()
if z, ok := value.Interface().(IsZeroer); ok {
if (kind == reflect.Ptr || kind == reflect.Interface) && value.IsNil() {
return true
}
return z.IsZero()
}
switch kind {
case reflect.String:
return len(value.String()) == 0
case reflect.Interface, reflect.Ptr:
return value.IsNil()
case reflect.Slice:
return value.Len() == 0
case reflect.Map:
return value.Len() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return value.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return value.Uint() == 0
case reflect.Float32, reflect.Float64:
return value.Float() == 0
case reflect.Bool:
return !value.Bool()
case reflect.Struct:
typ := value.Type()
for i := value.NumField() - 1; i >= 0; i-- {
if typ.Field(i).PkgPath != "" {
continue // Private field
}
if !isZero(value.Field(i)) {
return false
}
}
return true
}
return false
}