-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfield.go
143 lines (124 loc) · 2.75 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
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
package golang
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/plugin-sdk-go/plugin"
)
type Field struct {
Name string // CamelCased name for Go
DBName string // Name as used in the DB
Type string
Tags map[string]string
Comment string
Column *plugin.Column
// EmbedFields contains the embedded fields that require scanning.
EmbedFields []Field
}
func (gf Field) Tag() string {
return TagsToString(gf.Tags)
}
func (gf Field) HasSqlcSlice() bool {
return gf.Column.IsSqlcSlice
}
func TagsToString(tags map[string]string) string {
if len(tags) == 0 {
return ""
}
tagParts := make([]string, 0, len(tags))
for key, val := range tags {
tagParts = append(tagParts, fmt.Sprintf("%s:%q", key, val))
}
sort.Strings(tagParts)
return strings.Join(tagParts, " ")
}
func JSONTagName(name string, options *opts.Options) string {
style := options.JsonTagsCaseStyle
idUppercase := options.JsonTagsIdUppercase
if style == "" || style == "none" {
return name
} else {
return SetJSONCaseStyle(name, style, idUppercase)
}
}
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 SetJSONCaseStyle(name string, style string, idUppercase bool) string {
switch style {
case "camel":
return toJsonCamelCase(name, idUppercase)
case "pascal":
return toPascalCase(name)
case "snake":
return toSnakeCase(name)
default:
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style))
}
}
var camelPattern = regexp.MustCompile("[^A-Z][A-Z]+")
func toSnakeCase(s string) string {
if !strings.ContainsRune(s, '_') {
s = camelPattern.ReplaceAllStringFunc(s, func(x string) string {
return x[:1] + "_" + x[1:]
})
}
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
}
func toJsonCamelCase(name string, idUppercase bool) string {
out := ""
idStr := "Id"
if idUppercase {
idStr = "ID"
}
for i, p := range strings.Split(name, "_") {
if i == 0 {
out += p
continue
}
if p == "id" {
out += idStr
} else {
out += strings.Title(p)
}
}
return out
}
func toLowerCase(str string) string {
if str == "" {
return ""
}
return strings.ToLower(str[:1]) + str[1:]
}