-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_mapping.go
66 lines (54 loc) · 1.56 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
package dat
import (
"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.NewMapperFunc("db", snakeCase)
// reflectFields gets a cached field information about record
func reflectFields(rec interface{}) *reflectx.FieldMeta {
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, v := range vals {
values[i] = v.Interface()
}
return values, nil
}
func reflectColumns(v interface{}) []string {
cols := []string{}
for _, tag := range reflectFields(v).Names {
cols = append(cols, tag)
}
return cols
}
func reflectBlacklistedColumns(v interface{}, blacklist []string) []string {
cols := []string{}
for _, tag := range reflectFields(v).Names {
if str.SliceContains(blacklist, tag) {
continue
}
cols = append(cols, tag)
}
return cols
}