forked from picatic/norm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
41 lines (36 loc) · 962 Bytes
/
utils.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
package norm
import (
"fmt"
"github.com/AlekSi/reflector"
"github.com/picatic/norm/field"
)
// escape fields for queries
func escapeFields(fields field.Names) []string {
var newFields = make([]string, len(fields))
for i := 0; i < len(fields); i++ {
newFields[i] = fmt.Sprintf("`%s`", fields[i].SnakeCase())
}
return newFields
}
// Get the fieldNames as a []string escaped field names
func defaultFieldsEscaped(model Model, fields field.Names) []string {
if fields == nil {
fields = ModelFields(model)
}
return escapeFields(fields)
}
// Create a map of strings and values from the model to work with dbr's interfaces
func defaultUpdate(m Model, fields field.Names) map[string]interface{} {
kv := make(map[string]interface{})
reflector.StructToMap(m, kv, "db")
if fields == nil {
return kv
}
fv := make(map[string]interface{})
for _, k := range fields {
if val, ok := kv[string(k)]; ok {
fv[k.SnakeCase()] = val
}
}
return fv
}