-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
95 lines (84 loc) · 2.17 KB
/
struct.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
package querybuilder
import (
"errors"
"reflect"
"sort"
"strings"
)
type KeyValue struct {
Key string
Value interface{}
}
type IndexedColumnValues map[int]KeyValue
func mapToIndexColumnValue(columnValues map[string]interface{}) IndexedColumnValues {
indexedColumnValues := make(IndexedColumnValues, len(columnValues))
columns := make([]string, len(columnValues))
i := 0
for column := range columnValues {
columns[i] = column
i++
}
sort.Strings(columns)
for i = 0; i < len(columns); i++ {
indexedColumnValues[i] = KeyValue{
Key: columns[i],
Value: columnValues[columns[i]],
}
}
return indexedColumnValues
}
func structToMap(s interface{}) (IndexedColumnValues, error) {
columnValues := make(IndexedColumnValues)
v := reflect.ValueOf(s)
// if its a pointer, resolve its value
if v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
if v.Kind() != reflect.Struct {
return nil, errors.New("unexpected type")
}
e := v.Type()
columnIndex := 0
for i := 0; i < e.NumField(); i++ {
name := e.Field(i).Name
tag := strings.Split(e.Field(i).Tag.Get("db"), ",")[0] // use split to ignore tag "options"
// ignore columns with -
if tag == "-" {
continue
}
value := v.FieldByIndex(e.Field(i).Index)
column := tag
if tag == "" {
column = name
}
if e.Field(i).Type.Kind() == reflect.Struct {
st := reflect.TypeOf(value.Interface())
if _, ok := st.MethodByName("String"); !ok {
nestedColumnValues, err := structToMap(value.Interface())
if err != nil {
return nil, err
}
for index := 0; index < len(nestedColumnValues); index++ {
columnValues[columnIndex] = KeyValue{Key: nestedColumnValues[index].Key, Value: nestedColumnValues[index].Value}
columnIndex++
}
continue
}
}
// ignore nil pointer values
if value.IsZero() && value.Kind() == reflect.Ptr {
continue
}
// if the value is a pointer, resolve its value
if value.Kind() == reflect.Ptr {
value = reflect.Indirect(value)
}
// if the value is nil, skip adding it
if value.Interface() == nil {
continue
}
columnValues[columnIndex] = KeyValue{Key: column, Value: value.Interface()}
columnIndex++
}
return columnValues, nil
}