-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
80 lines (68 loc) · 1.65 KB
/
helpers.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
package erudito
import (
"reflect"
"strings"
)
func checkIfTagExists(check string, tags string) bool {
stringSplit := strings.Split(tags, ";")
for _, tag := range stringSplit {
if tag == check {
return true
}
}
return false
}
func getJSONFieldNameByTag(tag string) string {
stringSplit := strings.Split(tag, ",")
return stringSplit[0]
}
func fieldIsAEruditoModel(field reflect.StructField) bool {
switch field.Type.Kind() {
case reflect.Slice:
return field.Type.Elem().Implements(reflect.TypeOf((*Model)(nil)).Elem()) ||
field.Type.Elem() == reflect.TypeOf(FullModel{}) ||
field.Type.Elem() == reflect.TypeOf(HardDeleteModel{}) ||
field.Type.Elem() == reflect.TypeOf(SimpleModel{})
case reflect.Struct:
return field.Type.Implements(reflect.TypeOf((*Model)(nil)).Elem()) ||
field.Type == reflect.TypeOf(FullModel{}) ||
field.Type == reflect.TypeOf(HardDeleteModel{}) ||
field.Type == reflect.TypeOf(SimpleModel{})
default:
return false
}
}
func upperCamelCase(s string) string {
return camelCase(s, true)
}
func lowerCamelCase(s string) string {
return camelCase(s, false)
}
func camelCase(s string, upper bool) string {
s = strings.TrimSpace(s)
buffer := make([]rune, 0, len(s))
var prev rune
for _, curr := range s {
if curr != '_' {
if prev == '_' || (upper && prev == 0) || prev == '.' {
buffer = append(buffer, toUpper(curr))
} else {
buffer = append(buffer, toLower(curr))
}
}
prev = curr
}
return string(buffer)
}
func toUpper(ch rune) rune {
if ch >= 'a' && ch <= 'z' {
return ch - 32
}
return ch
}
func toLower(ch rune) rune {
if ch >= 'A' && ch <= 'Z' {
return ch + 32
}
return ch
}