forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
184 lines (152 loc) · 4.66 KB
/
builder.go
File metadata and controls
184 lines (152 loc) · 4.66 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
package sqlx
import (
"fmt"
"github.com/kataras/go-errors"
"github.com/tietang/sqlx/reflectx"
"reflect"
"regexp"
"sort"
"upper.io/db.v3"
)
// MapOptions represents options for the mapper.
type MapOptions struct {
IncludeZeroed bool
IncludeNil bool
}
var defaultMapOptions = MapOptions{
IncludeZeroed: false,
IncludeNil: false,
}
type hasIsZero interface {
IsZero() bool
}
type hasArguments interface {
Arguments() []interface{}
}
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
reColumnCompareExclude = regexp.MustCompile(`[^a-zA-Z0-9]`)
)
func snakeCasedName(name string) string {
newstr := make([]rune, 0)
for idx, chr := range name {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
if idx > 0 {
newstr = append(newstr, '_')
}
chr -= ('A' - 'a')
}
newstr = append(newstr, chr)
}
return string(newstr)
}
// Map receives a pointer to map or struct and maps it to columns and values.
func Map(item interface{}, options *MapOptions) (string, []string, []interface{}, error) {
var fv fieldValue
if options == nil {
options = &defaultMapOptions
}
itemV := reflect.ValueOf(item)
if !itemV.IsValid() {
return "", nil, nil, errors.New("is valid: " + itemV.String())
}
itemT := itemV.Type()
if itemT.Kind() == reflect.Ptr {
// Single dereference. Just in case the user passes a pointer to struct
// instead of a struct.
item = itemV.Elem().Interface()
itemV = reflect.ValueOf(item)
itemT = itemV.Type()
}
name := snakeCasedName(itemV.Type().Name())
switch itemT.Kind() {
case reflect.Struct:
fieldMap := mapper().TypeMap(itemT).Names
nfields := len(fieldMap)
fv.values = make([]interface{}, 0, nfields)
fv.fields = make([]string, 0, nfields)
for _, fi := range fieldMap {
// Field options
_, tagOmitEmpty := fi.Options["omitempty"]
fld := reflectx.FieldByIndexesReadOnly(itemV, fi.Index)
if fld.Kind() == reflect.Ptr && fld.IsNil() {
if tagOmitEmpty && !options.IncludeNil {
continue
}
fv.fields = append(fv.fields, fi.Name)
if tagOmitEmpty {
fv.values = append(fv.values, "")
} else {
fv.values = append(fv.values, nil)
}
continue
}
value := fld.Interface()
isZero := false
if t, ok := fld.Interface().(hasIsZero); ok {
if t.IsZero() {
isZero = true
}
} else if fld.Kind() == reflect.Array || fld.Kind() == reflect.Slice {
if fld.Len() == 0 {
isZero = true
}
} else if reflect.DeepEqual(fi.Zero.Interface(), value) {
isZero = true
}
if isZero && tagOmitEmpty && !options.IncludeZeroed {
continue
}
fv.fields = append(fv.fields, fi.Name)
v, err := marshal(value)
if err != nil {
return "", nil, nil, err
}
if isZero && tagOmitEmpty {
v = ""
}
fv.values = append(fv.values, v)
}
case reflect.Map:
nfields := itemV.Len()
fv.values = make([]interface{}, nfields)
fv.fields = make([]string, nfields)
mkeys := itemV.MapKeys()
for i, keyV := range mkeys {
valv := itemV.MapIndex(keyV)
fv.fields[i] = fmt.Sprintf("%v", keyV.Interface())
v, err := marshal(valv.Interface())
if err != nil {
return "", nil, nil, err
}
fv.values[i] = v
}
default:
return "", nil, nil, ErrExpectingPointerToEitherMapOrStruct
}
sort.Sort(&fv)
return name, fv.fields, fv.values, nil
}
func marshal(v interface{}) (interface{}, error) {
if m, isMarshaler := v.(db.Marshaler); isMarshaler {
var err error
if v, err = m.MarshalDB(); err != nil {
return nil, err
}
}
return v, nil
}
type fieldValue struct {
fields []string
values []interface{}
}
func (fv *fieldValue) Len() int {
return len(fv.fields)
}
func (fv *fieldValue) Swap(i, j int) {
fv.fields[i], fv.fields[j] = fv.fields[j], fv.fields[i]
fv.values[i], fv.values[j] = fv.values[j], fv.values[i]
}
func (fv *fieldValue) Less(i, j int) bool {
return fv.fields[i] < fv.fields[j]
}