forked from astaxie/beedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
177 lines (147 loc) · 3.84 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
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
package beedb
import (
"errors"
"github.com/grsmv/inflect"
"reflect"
"strconv"
"strings"
"time"
)
func getTypeName(obj interface{}) (typestr string) {
typ := reflect.TypeOf(obj)
typestr = typ.String()
lastDotIndex := strings.LastIndex(typestr, ".")
if lastDotIndex != -1 {
typestr = typestr[lastDotIndex+1:]
}
return
}
func snakeCasedName(name string) string {
newstr := make([]rune, 0)
firstTime := true
for _, chr := range name {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
if firstTime == true {
firstTime = false
} else {
newstr = append(newstr, '_')
}
chr -= ('A' - 'a')
}
newstr = append(newstr, chr)
}
return string(newstr)
}
func titleCasedName(name string) string {
newstr := make([]rune, 0)
upNextChar := true
for _, chr := range name {
switch {
case upNextChar:
upNextChar = false
chr -= ('a' - 'A')
case chr == '_':
upNextChar = true
continue
}
newstr = append(newstr, chr)
}
return string(newstr)
}
func pluralizeString(str string) string {
if strings.HasSuffix(str, "y") {
str = str[:len(str)-1] + "ie"
}
return str + "s"
}
func scanMapIntoStruct(obj interface{}, objMap map[string][]byte) error {
dataStruct := reflect.Indirect(reflect.ValueOf(obj))
if dataStruct.Kind() != reflect.Struct {
return errors.New("expected a pointer to a struct")
}
for key, data := range objMap {
structField := dataStruct.FieldByName(titleCasedName(key))
if !structField.CanSet() {
continue
}
var v interface{}
switch structField.Type().Kind() {
case reflect.Slice:
v = data
case reflect.String:
v = string(data)
case reflect.Bool:
v = string(data) == "1"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
x, err := strconv.Atoi(string(data))
if err != nil {
return errors.New("arg " + key + " as int: " + err.Error())
}
v = x
case reflect.Int64:
x, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return errors.New("arg " + key + " as int: " + err.Error())
}
v = x
case reflect.Float32, reflect.Float64:
x, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return errors.New("arg " + key + " as float64: " + err.Error())
}
v = x
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return errors.New("arg " + key + " as int: " + err.Error())
}
v = x
//Now only support Time type
case reflect.Struct:
if structField.Type().String() != "time.Time" {
return errors.New("unsupported struct type in Scan: " + structField.Type().String())
}
x, err := time.Parse("2006-01-02 15:04:05", string(data))
if err != nil {
x, err = time.Parse("2006-01-02 15:04:05.000 -0700", string(data))
if err != nil {
return errors.New("unsupported time format: " + string(data))
}
}
v = x
default:
return errors.New("unsupported type in Scan: " + reflect.TypeOf(v).String())
}
structField.Set(reflect.ValueOf(v))
}
return nil
}
func scanStructIntoMap(obj interface{}) (map[string]interface{}, error) {
dataStruct := reflect.Indirect(reflect.ValueOf(obj))
if dataStruct.Kind() != reflect.Struct {
return nil, errors.New("expected a pointer to a struct")
}
dataStructType := dataStruct.Type()
mapped := make(map[string]interface{})
for i := 0; i < dataStructType.NumField(); i++ {
field := dataStructType.Field(i)
fieldName := field.Name
mapKey := snakeCasedName(fieldName)
value := dataStruct.FieldByName(fieldName).Interface()
mapped[mapKey] = value
}
return mapped, nil
}
func StructName(s interface{}) string {
v := reflect.TypeOf(s)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v.Name()
}
func getTableName(name string) string {
if PluralizeTableNames {
return inflect.Pluralize(snakeCasedName(name))
}
return snakeCasedName(name)
}