-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
63 lines (54 loc) · 1.18 KB
/
field.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
package main
import (
"fmt"
"strings"
)
type Field struct {
Name string // CamelCased name for Go
DBName string // Name as used in the DB
Type string
TypecheckTemplate string
Comment string
}
func (f Field) HasTypecheck() bool {
return f.TypecheckTemplate == ""
}
func (f Field) Typecheck(i int) string {
return strings.ReplaceAll(f.TypecheckTemplate, "%", fmt.Sprintf("row[%d]", i))
}
func SetCaseStyle(name string, style string) string {
switch style {
case "camel":
return toCamelCase(name)
case "pascal":
return toPascalCase(name)
case "snake":
return toSnakeCase(name)
default:
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style))
}
}
func toSnakeCase(s string) string {
return strings.ToLower(s)
}
func toCamelCase(s string) string {
return toCamelInitCase(s, false)
}
func toPascalCase(s string) string {
return toCamelInitCase(s, true)
}
func toCamelInitCase(name string, initUpper bool) string {
out := ""
for i, p := range strings.Split(name, "_") {
if !initUpper && i == 0 {
out += p
continue
}
if p == "id" {
out += "ID"
} else {
out += strings.Title(p)
}
}
return out
}