-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_tool.go
More file actions
214 lines (180 loc) · 5.14 KB
/
Copy pathstruct_tool.go
File metadata and controls
214 lines (180 loc) · 5.14 KB
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
package gtools
import (
"reflect"
"strings"
)
func Struct2MapAny(in any, tagName string) map[string]any {
out := make(map[string]interface{})
if in == nil {
return out
}
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct { // 非结构体返回错误提示
return out
}
for i := 0; i < v.NumField(); i++ {
fi := v.Type().Field(i)
if tagValue := fi.Tag.Get(tagName); tagValue != "" {
var keyName string
if tagName == "gorm" {
begin := strings.Index(tagValue, "column:") + 7
end := strings.Index(tagValue[begin:], ";")
if end < 0 {
end = len(tagValue)
} else {
end = begin + end
}
keyName = tagValue[begin:end]
} else {
keyName = tagValue
}
if !isBlank(v.Field(i)) {
out[keyName] = v.Field(i).Interface()
}
}
}
return out
}
func Struct2MapString(in any, tagName string) map[string]string {
out := make(map[string]string)
if in == nil {
return out
}
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct { // 非结构体返回错误提示
return out
}
for i := 0; i < v.NumField(); i++ {
fi := v.Type().Field(i)
if tagValue := fi.Tag.Get(tagName); tagValue != "" {
var keyName string
if tagName == "gorm" {
begin := strings.Index(tagValue, "column:") + 7
end := strings.Index(tagValue[begin:], ";")
if end < 0 {
end = len(tagValue)
} else {
end = begin + end
}
keyName = tagValue[begin:end]
} else {
keyName = tagValue
}
if !isBlank(v.Field(i)) {
out[keyName] = Any(v.Field(i).Interface()).ToString()
}
}
}
return out
}
// CopyStruct 结构体复制, 忽略空值,暂不支持结构体内部map复制(有需要可扩展)
func CopyStruct[DST any](src any) DST {
if reflect.ValueOf(src).Kind() != reflect.Pointer {
panic("src must be pointer to struct object")
}
var dst DST
dstValue := reflect.ValueOf(&dst).Elem()
srcValue := reflect.ValueOf(src).Elem()
if srcValue.Kind() != reflect.Struct {
panic("src must point to struct object")
}
if dstValue.Kind() != reflect.Struct {
panic("DST must be struct type")
}
if src == nil {
return dst
}
// Recursively copy the original.
copyStructRecursive(srcValue.Addr().Interface(), dstValue.Addr().Interface(), true)
return dst
}
// CopyStructTo 复制到目标结构体对象,忽略空值,暂不支持结构体内部map复制(有需要可扩展)
func CopyStructTo(dst any, src any) {
if reflect.ValueOf(src).Kind() != reflect.Pointer {
panic("src must be pointer to struct object")
}
if reflect.ValueOf(dst).Kind() != reflect.Pointer {
panic("dst must be pointer to struct object")
}
// Make the interface a reflect.Value
srcValue := reflect.ValueOf(src).Elem()
dstValue := reflect.ValueOf(dst).Elem()
if srcValue.Kind() != reflect.Struct {
panic("src must point to struct object")
}
if dstValue.Kind() != reflect.Struct {
panic("dst must point to struct object")
}
if src == nil {
return
}
// Recursively copy the original.
copyStructRecursive(srcValue.Addr().Interface(), dstValue.Addr().Interface(), false)
return
}
func copyStructRecursive(src, dst interface{}, replaceAll bool) {
dstValue := reflect.ValueOf(dst).Elem()
srcValue := reflect.ValueOf(src).Elem()
if (replaceAll || srcValue.Kind() != reflect.Struct) && srcValue.Type() == dstValue.Type() && !isBlank(srcValue) && dstValue.CanSet() {
dstValue.Set(srcValue)
return
}
for i := 0; i < srcValue.NumField(); i++ {
srcField := srcValue.Field(i)
srcName := srcValue.Type().Field(i).Name
dstFieldByName := dstValue.FieldByName(srcName)
if !dstFieldByName.IsValid() {
continue
}
switch srcField.Kind() {
case reflect.Map:
continue
case reflect.Slice:
// Make a new slice and copy each element.
if srcField.IsNil() {
continue
}
if dstFieldByName.Kind() != reflect.Slice {
continue
}
dstFieldByName.Set(reflect.MakeSlice(dstFieldByName.Type(), srcField.Len(), srcField.Cap()))
for j := 0; j < srcField.Len(); j++ {
srcInterface := srcField.Index(j).Addr().Interface()
dstInterface := dstFieldByName.Index(j).Addr().Interface()
copyStructRecursive(srcInterface, dstInterface, replaceAll)
}
case reflect.Struct:
if dstFieldByName.Kind() != reflect.Struct {
continue
}
copyStructRecursive(srcField.Addr().Interface(), dstFieldByName.Addr().Interface(), replaceAll)
default:
if dstFieldByName.CanSet() && !isBlank(srcField) && dstFieldByName.Type() == srcField.Type() {
dstFieldByName.Set(srcField)
}
}
}
}
func isBlank(value reflect.Value) bool {
switch value.Kind() {
case reflect.String:
return value.Len() == 0
case reflect.Bool:
return !value.Bool()
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.Interface, reflect.Ptr:
return value.IsNil()
}
return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
}