-
Notifications
You must be signed in to change notification settings - Fork 52
/
util.go
59 lines (54 loc) · 1.27 KB
/
util.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
package hood
import (
"bytes"
"reflect"
"strings"
)
func toSnake(s string) string {
buf := bytes.NewBufferString("")
for i, v := range s {
if i > 0 && v >= 'A' && v <= 'Z' {
buf.WriteRune('_')
}
buf.WriteRune(v)
}
return strings.ToLower(buf.String())
}
func interfaceToSnake(f interface{}) string {
t := reflect.TypeOf(f)
for {
c := false
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
t = t.Elem()
c = true
}
if !c {
break
}
}
return toSnake(t.Name())
}
func snakeToUpperCamel(s string) string {
buf := bytes.NewBufferString("")
for _, v := range strings.Split(s, "_") {
if len(v) > 0 {
buf.WriteString(strings.ToUpper(v[:1]))
buf.WriteString(v[1:])
}
}
return buf.String()
}
func columnsMarkersAndValuesForModel(dialect Dialect, model *Model, markerPos *int) ([]string, []string, []interface{}) {
columns := make([]string, 0, len(model.Fields))
markers := make([]string, 0, len(columns))
values := make([]interface{}, 0, len(columns))
for _, column := range model.Fields {
if !column.PrimaryKey() {
columns = append(columns, column.Name)
markers = append(markers, dialect.NextMarker(markerPos))
values = append(values, column.Value)
}
}
return columns, markers, values
}