-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.go
More file actions
169 lines (145 loc) · 3.72 KB
/
parser.go
File metadata and controls
169 lines (145 loc) · 3.72 KB
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
package main
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/tools/go/ast/inspector"
)
func camelCaseToSnakeCase(camel string) (snake string) {
re := regexp.MustCompile("([a-z])([A-Z]+)")
snake = re.ReplaceAllString(camel, "${1}_${2}")
snake = strings.ToLower(snake)
return
}
func snakeCaseToCamelCase(snake string, firstLetterUpperCase bool) (camel string) {
titler := cases.Title(language.AmericanEnglish)
words := strings.Split(snake, "_")
if firstLetterUpperCase {
camel = titler.String(words[0])
} else {
camel = words[0]
}
for _, word := range words[1:] {
camel += titler.String(word)
}
return
}
func mkPlural(singular string) (plural string) {
re := regexp.MustCompile("y$")
plural = re.ReplaceAllString(singular, "ie")
plural += "s"
return
}
func procFieldTag(f *ast.Field, ent *entity, fld *field) error {
if f.Tag == nil {
log.Println(ent.GoName, "field", fld.GoName, "no tag")
return nil
}
log.Println(ent.GoName, "field", fld.GoName, "tag", f.Tag.Value)
tp := tagParser{Text: f.Tag.Value}
if err := tp.parse(); err != nil {
return err
}
tag, tagExists := tp.Result["gentity"]
if !tagExists {
return nil
}
if index, ok := tag["index"]; ok {
if _, ok := ent.NonUniqIndexes[index]; !ok {
ent.NonUniqIndexes[index] = []*field{fld}
} else {
ent.NonUniqIndexes[index] = append(ent.NonUniqIndexes[index], fld)
}
}
if unique, ok := tag["unique"]; ok {
log.Println(ent.GoName, "uniq", unique, "field", fld.GoName)
if _, ok := ent.UniqIndexes[unique]; !ok {
ent.UniqIndexes[unique] = []*field{fld}
} else {
ent.UniqIndexes[unique] = append(ent.UniqIndexes[unique], fld)
}
}
if _, ok := tag["autoincrement"]; ok {
ent.AutoIncrementField = fld
}
return nil
}
func parse() (packageName string, entities []entity, err error) {
path := os.Getenv("GOFILE")
if path == "" {
return "", nil, errors.New("GOFILE must be set")
}
astPkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Dir(path), nil, parser.ParseComments)
if err != nil {
return "", nil, fmt.Errorf("parse dir: %v", err)
}
if len(astPkgs) != 1 {
return "", nil, errors.New("not one package found")
}
var files []*ast.File
for _, p := range astPkgs {
files = append(files, maps.Values(p.Files)...)
packageName = p.Name
}
imports := make(map[string]string)
structs := structsMap(make(map[string]structInfo))
inspector.New(files).Nodes([]ast.Node{&ast.GenDecl{}}, func(node ast.Node, _ bool) (proceed bool) { return procNode(node, structs, imports) })
for name, si := range structs {
if !si.isForGentity {
continue
}
var entity *entity
entity, err = structs.procStruct(name, imports)
if err != nil {
return
}
entities = append(entities, *entity)
}
return
}
func procNode(node ast.Node, sm structsMap, imports map[string]string) (proceed bool) {
genDecl, ok := node.(*ast.GenDecl)
if !ok {
panic("unexpected node type")
}
si := structInfo{}
for _, spec := range genDecl.Specs {
if is, ok := spec.(*ast.ImportSpec); ok {
var alias string
if is.Name == nil {
pathParts := strings.Split(is.Path.Value, "/")
alias = pathParts[len(pathParts)-1]
} else {
alias = is.Name.Name
}
imports[strings.Trim(alias, "\"")] = strings.Trim(is.Path.Value, "\"")
}
}
si.typeSpec, ok = genDecl.Specs[0].(*ast.TypeSpec)
if !ok {
return false
}
si.structType, ok = si.typeSpec.Type.(*ast.StructType)
if !ok {
return false
}
if genDecl.Doc != nil {
for _, comment := range genDecl.Doc.List {
if comment.Text == "// gentity" {
si.isForGentity = true
}
}
}
sm[si.typeSpec.Name.Name] = si
return false
}