-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_tags.go
165 lines (139 loc) · 3.94 KB
/
struct_tags.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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"configcenter/src/framework/core/log"
"configcenter/src/framework/core/types"
"fmt"
"reflect"
)
// GetTags parse a object and get the all tags
func GetTags(target interface{}) []string {
targetType := reflect.TypeOf(target)
switch targetType.Kind() {
default:
break
case reflect.Ptr:
fmt.Printf("hello")
targetType = targetType.Elem()
}
numField := targetType.NumField()
tags := make([]string, 0)
for i := 0; i < numField; i++ {
structField := targetType.Field(i)
if tag, ok := structField.Tag.Lookup("field"); ok {
tags = append(tags, tag)
}
}
return tags
}
// SetValueToMapStrByTags convert a struct to MapStr by tags
func SetValueToMapStrByTags(source interface{}) types.MapStr {
values := types.MapStr{}
targetType := reflect.TypeOf(source)
targetValue := reflect.ValueOf(source)
switch targetType.Kind() {
case reflect.Ptr:
targetType = targetType.Elem()
targetValue = targetValue.Elem()
}
numField := targetType.NumField()
for i := 0; i < numField; i++ {
structField := targetType.Field(i)
tag, ok := structField.Tag.Lookup("field")
if !ok {
continue
}
fieldValue := targetValue.FieldByName(structField.Name)
values.Set(tag, fieldValue.Interface())
}
return values
}
// SetValueToStructByTags set the struct object field value by tags
func SetValueToStructByTags(target interface{}, values types.MapStr) error {
targetType := reflect.TypeOf(target)
targetValue := reflect.ValueOf(target)
switch targetType.Kind() {
case reflect.Ptr:
targetType = targetType.Elem()
targetValue = targetValue.Elem()
}
numField := targetType.NumField()
for i := 0; i < numField; i++ {
structField := targetType.Field(i)
tag, ok := structField.Tag.Lookup("field")
if !ok {
continue
}
tagVal, ok := values[tag]
if !ok {
continue
}
if nil == tagVal {
continue
}
fieldValue := targetValue.FieldByName(structField.Name)
if !fieldValue.CanSet() {
continue
}
switch structField.Type.Kind() {
default:
log.Errorf("unsuport the type %s %v", structField.Name, structField.Type.Kind())
case reflect.Interface:
tmpVal := reflect.ValueOf(tagVal)
switch tmpVal.Kind() {
case reflect.Ptr:
fieldValue.Set(tmpVal.Elem())
default:
fieldValue.Set(tmpVal)
}
case reflect.Bool:
fieldValue.SetBool(tagVal.(bool))
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8, reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint8:
switch t := tagVal.(type) {
case int:
fieldValue.SetInt(int64(t))
case int16:
fieldValue.SetInt(int64(t))
case int32:
fieldValue.SetInt(int64(t))
case int64:
fieldValue.SetInt(int64(t))
case int8:
fieldValue.SetInt(int64(t))
case uint:
fieldValue.SetInt(int64(t))
case uint16:
fieldValue.SetInt(int64(t))
case uint32:
fieldValue.SetInt(int64(t))
case uint64:
fieldValue.SetInt(int64(t))
case uint8:
fieldValue.SetInt(int64(t))
}
case reflect.Float32, reflect.Float64:
switch t := tagVal.(type) {
case float32:
fieldValue.SetFloat(float64(t))
case float64:
fieldValue.SetFloat(float64(t))
}
case reflect.String:
switch t := tagVal.(type) {
case string:
fieldValue.SetString(t)
}
}
}
return nil
}