-
Notifications
You must be signed in to change notification settings - Fork 0
/
casting.go
143 lines (117 loc) · 3.31 KB
/
casting.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
// Package utils
package utils
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// ToString converts a value to string.
func ToString(value interface{}) string {
switch value := value.(type) {
case string:
return value
case int:
return strconv.FormatInt(int64(value), 10)
case int8:
return strconv.FormatInt(int64(value), 10)
case int16:
return strconv.FormatInt(int64(value), 10)
case int32:
return strconv.FormatInt(int64(value), 10)
case int64:
return strconv.FormatInt(int64(value), 10)
case uint:
return strconv.FormatUint(uint64(value), 10)
case uint8:
return strconv.FormatUint(uint64(value), 10)
case uint16:
return strconv.FormatUint(uint64(value), 10)
case uint32:
return strconv.FormatUint(uint64(value), 10)
case uint64:
return strconv.FormatUint(uint64(value), 10)
case float32:
return strconv.FormatFloat(float64(value), 'g', -1, 64)
case float64:
return strconv.FormatFloat(float64(value), 'g', -1, 64)
case bool:
return strconv.FormatBool(value)
}
return fmt.Sprintf("%+v", value)
}
// ToMap converts a struct to a map using the struct's tags.
//
// ToMap uses tags on struct fields to decide which fields to add to the
// returned map.
func StructToMap(src interface{}, tag string) (map[string]interface{}, error) {
out := map[string]interface{}{}
v := reflect.ValueOf(src)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// we only accept structs
if v.Kind() != reflect.Struct {
return out, fmt.Errorf("only accepted %s, got %s", reflect.Struct.String(), v.Kind().String())
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
// gets us a StructField
fi := typ.Field(i)
tagsv := strings.Split(fi.Tag.Get(tag), ",")
if tagsv[0] != "" && fi.PkgPath == "" {
// skip if omitempty
if (len(tagsv) > 1 && tagsv[1] == "omitempty") && IsEmptyValue(v.Field(i).Interface()) {
continue
}
if isTime(v.Field(i)) {
if v.Field(i).Interface().(time.Time).IsZero() && tagsv[1] == "omitempty" {
continue
}
}
// set key value of map interface output
out[tagsv[0]] = v.Field(i).Interface()
}
}
return out, nil
}
// StructExtractFieldValue iterate struct to separate key field and value
func StructExtractFieldValue(src interface{}, tag string) ([]string, []interface{}, error) {
var columns []string
var values []interface{}
v := reflect.ValueOf(src)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// we only accept structs
if v.Kind() != reflect.Struct {
return nil, nil, fmt.Errorf("only accepted %s, got %s", reflect.Struct.String(), v.Kind().String())
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
// gets us a StructField
fi := typ.Field(i)
tagsv := strings.Split(fi.Tag.Get(tag), ",")
if tagsv[0] != "" && fi.PkgPath == "" {
// skip if omitempty
if (len(tagsv) > 1 && tagsv[1] == "omitempty") && IsEmptyValue(v.Field(i).Interface()) {
continue
}
if isTime(v.Field(i)) {
if v.Field(i).Interface().(time.Time).IsZero() && tagsv[1] == "omitempty" {
continue
}
}
// set value of string slice to value in struct field
columns = append(columns, tagsv[0])
// set value interface of value struct field
values = append(values, v.Field(i).Interface())
}
}
return columns, values, nil
}
func isTime(obj reflect.Value) bool {
_, ok := obj.Interface().(time.Time)
return ok
}