-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
75 lines (66 loc) · 1.56 KB
/
helper.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
package main
import (
"fmt"
"reflect"
"strings"
)
func UniqStringSlice(elements []string) []string {
encountered := make(map[string]bool)
var result []string
for v := range elements {
if encountered[elements[v]] == true {
continue
}
encountered[elements[v]] = true
result = append(result, elements[v])
}
return result
}
func ContainsStringSlice(slice []string, str string) bool {
for _, item := range slice {
if item == str {
return true
}
}
return false
}
func GetValueByJSONTag(obj reflect.Value, tagName string) reflect.Value {
if obj.IsZero() {
return reflect.Value{}
}
if obj.Kind() == reflect.Ptr {
obj = obj.Elem()
}
tp := obj.Type()
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
tag := field.Tag.Get("json")
tag = strings.Split(tag, ",")[0]
if tag == tagName {
return obj.Field(i)
}
}
return reflect.Value{}
}
func SetValueByJSONTag(obj reflect.Value, tagName string, value interface{}) error {
structValue := obj.Elem()
structType := structValue.Type()
for i := 0; i < structValue.NumField(); i++ {
field := structType.Field(i)
tag := field.Tag.Get("json")
tag = strings.Split(tag, ",")[0]
if tag == tagName {
fieldValue := structValue.Field(i)
if !fieldValue.CanSet() {
return fmt.Errorf("cannot set %s field value", field.Name)
}
val := reflect.ValueOf(value)
if fieldValue.Type() != val.Type() {
return fmt.Errorf("provided value type didn't match obj field type")
}
fieldValue.Set(val)
return nil
}
}
return fmt.Errorf("no such json tag in obj: %s", tagName)
}