-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstruct.go
49 lines (43 loc) · 936 Bytes
/
struct.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
package golang
import (
"strings"
"unicode"
"unicode/utf8"
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/plugin-sdk-go/plugin"
)
type Struct struct {
Table *plugin.Identifier
Name string
Fields []Field
Comment string
}
func StructName(name string, options *opts.Options) string {
if rename := options.Rename[name]; rename != "" {
return rename
}
out := ""
name = strings.Map(func(r rune) rune {
if unicode.IsLetter(r) {
return r
}
if unicode.IsDigit(r) {
return r
}
return rune('_')
}, name)
for _, p := range strings.Split(name, "_") {
if _, found := options.InitialismsMap[p]; found {
out += strings.ToUpper(p)
} else {
out += strings.Title(p)
}
}
// If a name has a digit as its first char, prepand an underscore to make it a valid Go name.
r, _ := utf8.DecodeRuneInString(out)
if unicode.IsDigit(r) {
return "_" + out
} else {
return out
}
}