forked from mgutz/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_mapping.go
88 lines (75 loc) · 2.25 KB
/
struct_mapping.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
package dat
import (
"fmt"
"reflect"
"unicode"
"github.com/mgutz/str"
"gopkg.in/mgutz/dat.v1/reflectx"
)
// ToSnake convert the given string to snake case following the Golang format:
// acronyms are converted to lower-case and preceded by an underscore.
func snakeCase(in string) string {
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}
var fieldMapper = reflectx.NewMapperTagFunc("db", nil, nil)
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return v.IsNil()
}
return false
}
// reflectFields gets a cached field information about record
func reflectFields(rec interface{}) *reflectx.StructMap {
val := reflect.Indirect(reflect.ValueOf(rec))
vtype := val.Type()
return fieldMapper.TypeMap(vtype)
}
// ValuesFor ...
func valuesFor(recordType reflect.Type, record reflect.Value, columns []string) ([]interface{}, error) {
vals := fieldMapper.FieldsByName(record, columns)
values := make([]interface{}, len(columns))
for i, val := range vals {
if !val.IsValid() {
return nil, fmt.Errorf("Could not find struct tag in type %s: `db:\"%s\"`", recordType.Name(), columns[i])
}
values[i] = val.Interface()
}
return values, nil
}
func reflectColumns(v interface{}) []string {
cols := []string{}
for _, name := range reflectFields(v).DeclaredNames {
cols = append(cols, name)
}
return cols
}
func reflectExcludeColumns(v interface{}, blacklist []string) []string {
cols := []string{}
for _, name := range reflectFields(v).DeclaredNames {
if str.SliceContains(blacklist, name) {
continue
}
cols = append(cols, name)
}
return cols
}