-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_strings.go
85 lines (81 loc) · 2.27 KB
/
struct_strings.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
package gsf
import (
"reflect"
"strconv"
"strings"
"time"
)
func StructToStringSlice(src interface{}) (result []string, err error) {
v, err := valueOf(src)
if err != nil {
return
}
tvs := getNameAndValues(v)
result = make([]string, len(tvs))
for i, tv := range tvs {
switch tv.Value.Kind() {
case reflect.Bool:
result[i] = strconv.FormatBool(tv.Value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result[i] = strconv.FormatInt(tv.Value.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
result[i] = strconv.FormatUint(tv.Value.Uint(), 10)
case reflect.Float32, reflect.Float64:
result[i] = strconv.FormatFloat(tv.Value.Float(), 'f', 20, 64)
case reflect.String:
result[i] = tv.Value.String()
case reflect.Struct:
switch item := tv.Value.Interface().(type) {
case time.Time:
result[i] = item.Format(time.RFC3339)
}
}
}
return
}
type structTagValue struct {
Tag string
Value reflect.Value
}
func StructToStringMap(src interface{}, tags ...string) (result map[string]string, err error) {
var tag string
if len(tags) == 0 {
tag = "json"
} else {
tag = tags[0]
}
srcV, err := valueOf(src)
if err != nil {
return
}
srcTVs := getNameAndValues(srcV)
tvs := make([]structTagValue, len(srcTVs))
for _, sTV := range srcTVs {
t := sTV.Type.Tag.Get(tag)
if len(t) == 0 || strings.EqualFold(t, "-") {
continue
}
tvs = append(tvs, structTagValue{Tag: t, Value: sTV.Value})
}
result = make(map[string]string, len(tvs))
for _, tv := range tvs {
switch tv.Value.Kind() {
case reflect.Bool:
result[tv.Tag] = strconv.FormatBool(tv.Value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result[tv.Tag] = strconv.FormatInt(tv.Value.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
result[tv.Tag] = strconv.FormatUint(tv.Value.Uint(), 10)
case reflect.Float32, reflect.Float64:
result[tv.Tag] = strconv.FormatFloat(tv.Value.Float(), 'f', 20, 64)
case reflect.String:
result[tv.Tag] = tv.Value.String()
case reflect.Struct:
switch item := tv.Value.Interface().(type) {
case time.Time:
result[tv.Tag] = item.Format(time.RFC3339)
}
}
}
return
}