-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobld.go
230 lines (202 loc) · 6.01 KB
/
gobld.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"go/ast"
"go/token"
"regexp"
"strings"
"unicode"
)
type Visibility int
const (
ExportedVisibility Visibility = iota
PackageLevelVisibility
NoVisibility
)
type StructParser struct {
fileSet *token.FileSet
fileContent []byte
whitespaceRegexp *regexp.Regexp
constructorExportedRegexp *regexp.Regexp
constructorPackageRegexp *regexp.Regexp
constructorNoRegexp *regexp.Regexp
flagOptionalRegexp *regexp.Regexp
flagGetterRegexp *regexp.Regexp
flagAcronymRegex *regexp.Regexp
}
type StructField struct {
StructFlags *StructFlags
StructName string
FieldName string
FieldTypeText string
Acronym bool
}
type StructFlags struct {
ProcessStruct bool
PtrReceiver bool
Visibility Visibility
}
func GeneratePackage(astFile *ast.File) string {
bld := &strings.Builder{}
bld.WriteString("// Code generated by gobetter; DO NOT EDIT.\n\n")
bld.WriteString(fmt.Sprintf("package %s\n\n", astFile.Name.Name))
return bld.String()
}
func GenerateImports(astFile *ast.File) string {
bld := &strings.Builder{}
bld.WriteString("import (\n")
for _, i := range astFile.Imports {
bld.WriteString(fmt.Sprintf("\t%s\n", i.Path.Value))
}
bld.WriteString(")\n\n")
return bld.String()
}
func (sf *StructField) GenerateGetter() string {
var addedFieldName string
if sf.Acronym {
addedFieldName = strings.ToUpper(sf.FieldName)
} else {
addedFieldName = strings.Title(sf.FieldName)
}
return fmt.Sprintf(`
func (v *%s) %s() %s {
return v.%s
}
`, sf.StructName, addedFieldName, sf.FieldTypeText,
sf.FieldName)
}
func (sf *StructField) GenerateSourceCodeForStructField(prev *StructField, last bool) string {
bld := &strings.Builder{}
if prev == nil {
sf.generateConstructor(bld)
}
sf.generateBuilderStruct(bld)
if prev != nil {
sf.generateBuilderSetter(bld, prev)
}
if last {
finalSf := StructField{
StructFlags: sf.StructFlags,
StructName: sf.StructName,
FieldName: "GobFinalizer",
FieldTypeText: "AAAAAA",
Acronym: false,
}
finalSf.generateBuilderStruct(bld)
finalSf.generateBuilderSetter(bld, sf)
finalSf.generateBuildFunction(bld)
}
return bld.String()
}
func (sf *StructField) generateBuildFunction(bld *strings.Builder) {
builderStructName := sf.builderFieldStructName()
bld.WriteString(fmt.Sprintf(`
func (b %s) Build() *%s {
return b.root
}
`, builderStructName, sf.StructName,
))
}
func (sf *StructField) generateBuilderStruct(bld *strings.Builder) {
builderStructName := sf.builderFieldStructName()
bld.WriteString(fmt.Sprintf(`
type %s struct {
root *%s
}
`, builderStructName, sf.StructName))
}
func (sf *StructField) generateBuilderSetter(bld *strings.Builder, prev *StructField) {
var setterName string
if prev.Acronym {
setterName = strings.ToUpper(prev.FieldName)
} else {
setterName = strings.Title(prev.FieldName)
}
prevBuilderStructName := prev.builderFieldStructName()
builderStructName := sf.builderFieldStructName()
bld.WriteString(fmt.Sprintf(`
func (b %s) %s(arg %s) %s {
b.root.%s = arg
return %s{root: b.root}
}
`, prevBuilderStructName, setterName, prev.FieldTypeText, builderStructName,
prev.FieldName,
builderStructName,
))
}
func (sf *StructField) generateConstructor(bld *strings.Builder) {
builderStructName := sf.builderFieldStructName()
var funcName string
firstChar := rune(sf.StructName[0])
if unicode.IsLower(firstChar) || sf.StructFlags.Visibility == PackageLevelVisibility {
funcName = "new" + strings.Title(sf.StructName)
} else {
funcName = "New" + strings.Title(sf.StructName)
}
bld.WriteString(fmt.Sprintf(`
func %sBuilder() %s {
return %s{root: &%s{}}
}
`,
funcName, builderStructName,
builderStructName, sf.StructName,
))
}
func (sf *StructField) builderFieldStructName() string {
var title string
if sf.Acronym {
title = strings.ToUpper(sf.FieldName)
} else {
title = strings.Title(sf.FieldName)
}
return sf.StructName + "_Builder_" + title
}
func NewStructParser(fileSet *token.FileSet, fileContent []byte) StructParser {
return StructParser{
fileSet: fileSet,
fileContent: fileContent,
whitespaceRegexp: regexp.MustCompile(`\s+`),
constructorExportedRegexp: regexp.MustCompile(`\b+gob:Constructor\b`),
constructorPackageRegexp: regexp.MustCompile(`\b+gob:constructor\b`),
constructorNoRegexp: regexp.MustCompile(`\b+gob:_\b`),
flagOptionalRegexp: regexp.MustCompile(`\b+gob:_\b`),
flagGetterRegexp: regexp.MustCompile(`\b+gob:getter\b`),
flagAcronymRegex: regexp.MustCompile(`\b+gob:acronym\b`),
}
}
func (sp *StructParser) fieldTypeText(field *ast.Field) string {
begin := sp.fileSet.Position(field.Type.Pos()).Offset
end := sp.fileSet.Position(field.Type.End()).Offset
return sp.whitespaceRegexp.ReplaceAllString(string(sp.fileContent[begin:end]), " ")
}
func (sp *StructParser) fieldOptional(field *ast.Field) bool {
return sp.flagOptionalRegexp.MatchString(field.Comment.Text())
}
func (sp *StructParser) fieldGetter(field *ast.Field) bool {
return sp.flagGetterRegexp.MatchString(field.Comment.Text())
}
func (sp *StructParser) fieldAcronym(field *ast.Field) bool {
return sp.flagAcronymRegex.MatchString(field.Comment.Text())
}
func (sp *StructParser) constructorFlags(st *ast.StructType) StructFlags {
begin := st.Struct
endLine := sp.fileSet.File(begin).Line(begin) + 1
end := sp.fileSet.File(begin).LineStart(endLine)
result := string(sp.fileContent[sp.fileSet.Position(begin).Offset:sp.fileSet.Position(end).Offset])
flags := StructFlags{
ProcessStruct: false,
PtrReceiver: false,
Visibility: ExportedVisibility,
}
if sp.constructorPackageRegexp.MatchString(result) {
flags.ProcessStruct = true
flags.Visibility = PackageLevelVisibility
} else if sp.constructorExportedRegexp.MatchString(result) {
flags.ProcessStruct = true
flags.Visibility = ExportedVisibility
} else if sp.constructorNoRegexp.MatchString(result) {
flags.ProcessStruct = true
flags.Visibility = NoVisibility
}
return flags
}