/
struct.go
153 lines (125 loc) · 3.48 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package util
import (
"reflect"
"strings"
)
// STMergeSame merge none zero field of s2 into s1,
// s1 and s2 must have the same struct type.
// unexported field will be skipped and embedded struct
// field will be treated as a single field.
func STMergeSame(s1, s2 interface{}) {
v1 := reflect.ValueOf(s1)
if v1.Kind() == reflect.Ptr {
v1 = v1.Elem()
} else {
panic("STMergeSame: param 1 must be pointer")
}
v2 := reflect.ValueOf(s2)
if v2.Kind() == reflect.Ptr {
v2 = v2.Elem()
}
if v1.Kind() != reflect.Struct || v2.Kind() != reflect.Struct {
panic("STMergeSame: both params must be struct")
}
if v1.Type() != v2.Type() {
panic("STMergeSame: params must have same type")
}
for i, n := 0, v2.NumField(); i < n; i++ {
field1, field2 := v1.Field(i), v2.Field(i)
zero := reflect.Zero(field2.Type())
// merge none zero field, unexported field will be skipped.
// use reflect.DeepEqual to check equality of underlying value,
// because slice type does not support equal operator.
if field2.CanInterface() && !reflect.DeepEqual(field2.Interface(), zero.Interface()) {
if field1.CanSet() {
field1.Set(field2)
}
}
}
}
// STMergeField merge the same or compatible field of s2 into s1,
// zero and unexported field will be skipped and embedded struct
// field will be treated as a single field.
func STMergeField(s1, s2 interface{}) {
v1 := reflect.ValueOf(s1)
if v1.Kind() == reflect.Ptr {
v1 = v1.Elem()
} else {
panic("STMergeField: param 1 must be pointer")
}
v2 := reflect.ValueOf(s2)
if v2.Kind() == reflect.Ptr {
v2 = v2.Elem()
}
if v1.Kind() != reflect.Struct || v2.Kind() != reflect.Struct {
panic("STMergeField: both params must be struct")
}
for i, n := 0, v2.NumField(); i < n; i++ {
field2 := v2.Field(i)
zero := reflect.Zero(field2.Type())
// skip zero or unexported field, see STMergeSame(s1, s2)
if !field2.CanInterface() || reflect.DeepEqual(field2.Interface(), zero.Interface()) {
continue
}
// get field1 by name of field2
field1 := v1.FieldByName(v2.Type().Field(i).Name)
if !field1.IsValid() || !field1.CanSet() {
continue
}
// check if type of two field is convertible
if !field2.Type().ConvertibleTo(field1.Type()) {
continue
}
// set value of field1 to convertible value of field2
field1.Set(field2.Convert(field1.Type()))
}
}
// ST2Map Struct to map
// stayZero 是否保留零值
// dftTag 获取标签为字段名,如果未传标签或者没有标签 获取字段名字,并首字母小写当作字段名
func ST2Map(s interface{}, stayZero bool, dftTag ...string) map[string]interface{} {
v1 := reflect.ValueOf(s)
if v1.Kind() == reflect.Ptr {
v1 = v1.Elem()
} else {
panic("ST2Map: param 1 must be pointer")
}
if v1.Kind() != reflect.Struct {
panic("ST2Map: param 1 must be struct")
}
tag := ""
if len(dftTag) > 0 {
tag = dftTag[0]
}
t1 := v1.Type()
ret := make(map[string]interface{})
for i, n := 0, v1.NumField(); i < n; i++ {
fieldT := t1.Field(i)
var field string
if tag != "" {
field = fieldT.Tag.Get(tag)
if field == "-" {
continue
}
}
if field == "" {
name := fieldT.Name
field = strings.ToLower(fieldT.Name[0:1])
if len(name) > 1 {
field = field + name[1:]
}
}
fieldV := v1.Field(i)
if fieldV.CanInterface() == false{
continue
}
if stayZero == false {
zero := reflect.Zero(fieldT.Type)
if reflect.DeepEqual(fieldV.Interface(), zero.Interface()) {
continue
}
}
ret[field] = fieldV.Interface()
}
return ret
}