-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.go
More file actions
188 lines (151 loc) · 4.33 KB
/
casting.go
File metadata and controls
188 lines (151 loc) · 4.33 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
// 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
}
// StructToBulkInsert extract struct slice tu bulk insert sql
func StructToBulkInsert(src interface{}, tag string) ([]string, []interface{}, []string, error) {
var columns []string
var questions []string
var values []interface{}
v := reflect.Indirect(reflect.ValueOf(src))
t := reflect.TypeOf(src)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Slice {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return columns, values, questions, fmt.Errorf("only accepted %s, got %s", reflect.Struct.String(), t.Kind().String())
}
for i := 0; i < v.Len(); i++ {
item := v.Index(i)
if !item.IsValid() {
continue
}
cols, val, err := StructExtractFieldValue(item.Interface(), tag)
if err != nil {
return columns, values, questions, err
}
if len(columns) == 0 {
columns = cols
}
question := fmt.Sprintf(`(%s)`, strings.TrimRight(strings.Repeat("?,", len(columns)), `,`))
questions = append(questions, question)
values = append(values, val...)
}
return columns, values, questions, nil
}
func isTime(obj reflect.Value) bool {
_, ok := obj.Interface().(time.Time)
return ok
}