@@ -111,7 +111,29 @@ var valueCheck = regexp.MustCompile("^[0-9a-zA-Z_]+$")
111
111
112
112
func generate (gen * generator , fs * token.FileSet , structType * ast.StructType ) error {
113
113
dupMap := make (map [string ]int )
114
- filedLabel := gen .StructName + queryLabel
114
+ f := func (name string ) string {
115
+ u := uppercaseExtraction (name )
116
+ if _ , ok := dupMap [u ]; ! ok {
117
+ dupMap [u ] = 1
118
+ } else {
119
+ dupMap [u ]++
120
+ u = fmt .Sprintf ("%s%d" , u , dupMap [u ])
121
+ }
122
+ return u
123
+ }
124
+ fieldLabel := gen .StructName + queryLabel
125
+ appendIndexesInfo := func (fieldInfo * FieldInfo ) {
126
+ idx := & IndexesInfo {
127
+ ConstName : fieldLabel + fieldInfo .Field ,
128
+ Label : f (fieldInfo .Field ),
129
+ Method : "Add" ,
130
+ }
131
+ idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , fieldInfo .Field )
132
+ if fieldInfo .FieldType != typeString {
133
+ idx .Method += "Something"
134
+ }
135
+ fieldInfo .Indexes = append (fieldInfo .Indexes , idx )
136
+ }
115
137
for _ , field := range structType .Fields .List {
116
138
// structの各fieldを調査
117
139
if len (field .Names ) != 1 {
@@ -120,6 +142,14 @@ func generate(gen *generator, fs *token.FileSet, structType *ast.StructType) err
120
142
name := field .Names [0 ].Name
121
143
122
144
if field .Tag == nil {
145
+ fieldInfo := & FieldInfo {
146
+ DsTag : name ,
147
+ Field : name ,
148
+ FieldType : getTypeName (field .Type ),
149
+ Indexes : make ([]* IndexesInfo , 0 ),
150
+ }
151
+ appendIndexesInfo (fieldInfo )
152
+ gen .FieldInfos = append (gen .FieldInfos , fieldInfo )
123
153
continue
124
154
}
125
155
@@ -147,50 +177,28 @@ func generate(gen *generator, fs *token.FileSet, structType *ast.StructType) err
147
177
gen .FieldInfoForIndexes = fieldInfo
148
178
continue
149
179
}
150
-
151
180
if _ , err := tags .Get ("datastore_key" ); err != nil {
152
- f := func () string {
153
- u := uppercaseExtraction (name )
154
- if _ , ok := dupMap [u ]; ! ok {
155
- dupMap [u ] = 1
156
- } else {
157
- dupMap [u ]++
158
- u = fmt .Sprintf ("%s%d" , u , dupMap [u ])
159
- }
160
- return u
161
- }
162
181
fieldInfo := & FieldInfo {
163
182
DsTag : name ,
164
183
Field : name ,
165
184
FieldType : getTypeName (field .Type ),
166
185
Indexes : make ([]* IndexesInfo , 0 ),
167
186
}
168
-
169
187
if tag , err := tagCheck (pos , tags ); err != nil {
170
188
return xerrors .Errorf ("error in tagCheck method: %w" , err )
171
189
} else if tag != "" {
172
190
fieldInfo .DsTag = tag
173
191
}
174
192
175
193
if idr , err := tags .Get ("indexer" ); err != nil || fieldInfo .FieldType != typeString {
176
- _ = err
177
- idx := & IndexesInfo {
178
- ConstName : filedLabel + name ,
179
- Label : f (),
180
- Method : "Add" ,
181
- }
182
- idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , name )
183
- if fieldInfo .FieldType != typeString {
184
- idx .Method += "Something"
185
- }
186
- fieldInfo .Indexes = append (fieldInfo .Indexes , idx )
194
+ appendIndexesInfo (fieldInfo )
187
195
} else {
188
196
filters := strings .Split (idr .Value (), "," )
189
197
dupIdr := make (map [string ]struct {})
190
198
for _ , fil := range filters {
191
199
idx := & IndexesInfo {
192
- ConstName : filedLabel + name ,
193
- Label : f (),
200
+ ConstName : fieldLabel + name ,
201
+ Label : f (name ),
194
202
Method : "Add" ,
195
203
}
196
204
var dupFlag string
@@ -259,7 +267,7 @@ func generate(gen *generator, fs *token.FileSet, structType *ast.StructType) err
259
267
gen .generate (fp )
260
268
}
261
269
262
- {
270
+ if gen . EnableIndexes {
263
271
path := gen .FileName + "_label.go"
264
272
fp , err := os .Create (path )
265
273
if err != nil {
@@ -284,7 +292,7 @@ func generate(gen *generator, fs *token.FileSet, structType *ast.StructType) err
284
292
func tagCheck (pos string , tags * structtag.Tags ) (string , error ) {
285
293
if dsTag , err := tags .Get ("datastore" ); err == nil {
286
294
tag := strings .Split (dsTag .Value (), "," )[0 ]
287
- if ! valueCheck .MatchString (tag ) { // 空白と記号チェック
295
+ if ! valueCheck .MatchString (tag ) {
288
296
return "" , xerrors .Errorf ("%s: key field for datastore should have other than blanks and symbols tag" , pos )
289
297
}
290
298
if strings .Contains ("0123456789" , string (tag [0 ])) {
0 commit comments