8
8
"log"
9
9
"os"
10
10
"path/filepath"
11
+ "regexp"
11
12
"strings"
12
13
13
14
"github.com/fatih/structtag"
@@ -109,10 +110,6 @@ const (
109
110
var valueCheck = regexp .MustCompile ("^[0-9a-zA-Z_]+$" )
110
111
111
112
func generate (gen * generator , fs * token.FileSet , structType * ast.StructType ) error {
112
- var (
113
- tags * structtag.Tags
114
- err error
115
- )
116
113
dupMap := make (map [string ]int )
117
114
filedLabel := gen .StructName + queryLabel
118
115
for _ , field := range structType .Fields .List {
@@ -128,118 +125,129 @@ func generate(gen *generator, fs *token.FileSet, structType *ast.StructType) err
128
125
129
126
pos := fs .Position (field .Pos ()).String ()
130
127
131
- tags , err = structtag .Parse (strings .Trim (field .Tag .Value , "`" ))
132
-
133
- if err != nil {
128
+ if tags , err := structtag .Parse (strings .Trim (field .Tag .Value , "`" )); err != nil {
134
129
log .Printf (
135
130
"%s: tag for %s in struct %s in %s" ,
136
131
pos , name , gen .StructName , gen .GeneratedFileName + ".go" ,
137
132
)
138
133
continue
139
- }
140
-
141
- if name == "Indexes" {
142
- gen .EnableIndexes = true
143
- continue
144
- }
145
-
146
- if _ , err := tags .Get ("datastore_key" ); err != nil {
147
- f := func () string {
148
- u := uppercaseExtraction (name )
149
- if _ , ok := dupMap [u ]; ! ok {
150
- dupMap [u ] = 1
151
- } else {
152
- dupMap [u ]++
153
- u = fmt .Sprintf ("%s%d" , u , dupMap [u ])
134
+ } else {
135
+ if name == "Indexes" {
136
+ gen .EnableIndexes = true
137
+ fieldInfo := & FieldInfo {
138
+ DsTag : name ,
139
+ Field : name ,
140
+ FieldType : getTypeName (field .Type ),
154
141
}
155
- return u
156
- }
157
- fieldInfo := & FieldInfo {
158
- Field : name ,
159
- FieldType : getTypeName (field .Type ),
160
- Indexes : make ([]* IndexesInfo , 0 ),
161
- }
162
-
163
- if dsTag , err := tags .Get ("datastore" ); err != nil {
164
- fieldInfo .DsTag = fieldInfo .Field
165
- } else {
166
- fieldInfo .DsTag = strings .Split (dsTag .Value (), "," )[0 ]
142
+ if tag , err := tagCheck (pos , tags ); err != nil {
143
+ return xerrors .Errorf ("error in tagCheck method: %w" , err )
144
+ } else if tag != "" {
145
+ fieldInfo .DsTag = tag
146
+ }
147
+ gen .FieldInfoForIndexes = fieldInfo
148
+ continue
167
149
}
168
150
169
- if idr , err := tags .Get ("indexer" ); err != nil || fieldInfo .FieldType != typeString {
170
- _ = err
171
- idx := & IndexesInfo {
172
- ConstName : filedLabel + name ,
173
- Label : f (),
174
- Method : "Add" ,
151
+ 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
175
161
}
176
- idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , name )
177
- if fieldInfo .FieldType != typeString {
178
- idx .Method += "Something"
162
+ fieldInfo := & FieldInfo {
163
+ DsTag : name ,
164
+ Field : name ,
165
+ FieldType : getTypeName (field .Type ),
166
+ Indexes : make ([]* IndexesInfo , 0 ),
179
167
}
180
- fieldInfo .Indexes = append (fieldInfo .Indexes , idx )
181
- } else {
182
- filters := strings .Split (idr .Value (), "," )
183
- dupIdr := make (map [string ]struct {})
184
- for _ , fil := range filters {
168
+
169
+ if tag , err := tagCheck (pos , tags ); err != nil {
170
+ return xerrors .Errorf ("error in tagCheck method: %w" , err )
171
+ } else if tag != "" {
172
+ fieldInfo .DsTag = tag
173
+ }
174
+
175
+ if idr , err := tags .Get ("indexer" ); err != nil || fieldInfo .FieldType != typeString {
176
+ _ = err
185
177
idx := & IndexesInfo {
186
178
ConstName : filedLabel + name ,
187
179
Label : f (),
188
180
Method : "Add" ,
189
181
}
190
- var dupFlag string
191
- switch fil {
192
- case "p" , "prefix" : // 前方一致 (AddPrefix)
193
- idx .Method += prefix
194
- idx .ConstName += prefix
195
- idx .Comment = fmt .Sprintf ("%s %s前方一致" , idx .ConstName , name )
196
- dupFlag = "p"
197
- case "s" , "suffix" : /* TODO 後方一致
198
- idx.Method += Suffix
199
- idx.ConstName += Suffix
200
- idx.Comment = fmt.Sprintf("%s %s後方一致", idx.ConstName, name)
201
- dup = "s"*/
202
- case "e" , "equal" : // 完全一致 (Add) Default
203
- idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , name )
204
- dupIdr ["equal" ] = struct {}{}
205
- dupFlag = "e"
206
- case "l" , "like" : // 部分一致
207
- idx .Method += biunigrams
208
- idx .ConstName += "Like"
209
- idx .Comment = fmt .Sprintf ("%s %s部分一致" , idx .ConstName , name )
210
- dupFlag = "l"
211
- default :
212
- continue
182
+ idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , name )
183
+ if fieldInfo .FieldType != typeString {
184
+ idx .Method += "Something"
213
185
}
214
- if _ , ok := dupIdr [dupFlag ]; ok {
215
- continue
216
- }
217
- dupIdr [dupFlag ] = struct {}{}
218
186
fieldInfo .Indexes = append (fieldInfo .Indexes , idx )
187
+ } else {
188
+ filters := strings .Split (idr .Value (), "," )
189
+ dupIdr := make (map [string ]struct {})
190
+ for _ , fil := range filters {
191
+ idx := & IndexesInfo {
192
+ ConstName : filedLabel + name ,
193
+ Label : f (),
194
+ Method : "Add" ,
195
+ }
196
+ var dupFlag string
197
+ switch fil {
198
+ case "p" , "prefix" : // 前方一致 (AddPrefix)
199
+ idx .Method += prefix
200
+ idx .ConstName += prefix
201
+ idx .Comment = fmt .Sprintf ("%s %s前方一致" , idx .ConstName , name )
202
+ dupFlag = "p"
203
+ case "s" , "suffix" : /* TODO 後方一致
204
+ idx.Method += Suffix
205
+ idx.ConstName += Suffix
206
+ idx.Comment = fmt.Sprintf("%s %s後方一致", idx.ConstName, name)
207
+ dup = "s"*/
208
+ case "e" , "equal" : // 完全一致 (Add) Default
209
+ idx .Comment = fmt .Sprintf ("%s %s" , idx .ConstName , name )
210
+ dupIdr ["equal" ] = struct {}{}
211
+ dupFlag = "e"
212
+ case "l" , "like" : // 部分一致
213
+ idx .Method += biunigrams
214
+ idx .ConstName += "Like"
215
+ idx .Comment = fmt .Sprintf ("%s %s部分一致" , idx .ConstName , name )
216
+ dupFlag = "l"
217
+ default :
218
+ continue
219
+ }
220
+ if _ , ok := dupIdr [dupFlag ]; ok {
221
+ continue
222
+ }
223
+ dupIdr [dupFlag ] = struct {}{}
224
+ fieldInfo .Indexes = append (fieldInfo .Indexes , idx )
225
+ }
219
226
}
227
+
228
+ gen .FieldInfos = append (gen .FieldInfos , fieldInfo )
229
+ continue
220
230
}
221
231
222
- gen .FieldInfos = append (gen .FieldInfos , fieldInfo )
223
- continue
224
- }
232
+ dsTag , err := tags .Get ("datastore" )
225
233
226
- dsTag , err := tags .Get ("datastore" )
234
+ // datastore タグが存在しないか-になっていない
235
+ if err != nil || strings .Split (dsTag .Value (), "," )[0 ] != "-" {
236
+ return xerrors .Errorf ("%s: key field for datastore should have datastore:\" -\" tag" , pos )
237
+ }
227
238
228
- // datastore タグが存在しないか-になっていない
229
- if err != nil || strings .Split (dsTag .Value (), "," )[0 ] != "-" {
230
- return xerrors .Errorf ("%s: key field for datastore should have datastore:\" -\" tag" , pos )
231
- }
239
+ gen .KeyFieldName = name
240
+ gen .KeyFieldType = getTypeName (field .Type )
232
241
233
- gen .KeyFieldName = name
234
- gen .KeyFieldType = getTypeName (field .Type )
242
+ if gen .KeyFieldType != typeInt64 &&
243
+ gen .KeyFieldType != typeString &&
244
+ ! strings .HasSuffix (gen .KeyFieldType , ".Key" ) {
245
+ return xerrors .Errorf ("%s: supported key types are int64, string, *datastore.Key" , pos )
246
+ }
235
247
236
- if gen .KeyFieldType != typeInt64 &&
237
- gen .KeyFieldType != typeString &&
238
- ! strings .HasSuffix (gen .KeyFieldType , ".Key" ) {
239
- return xerrors .Errorf ("%s: supported key types are int64, string, *datastore.Key" , pos )
240
- }
248
+ gen .KeyValueName = strcase .ToLowerCamel (name )
241
249
242
- gen . KeyValueName = strcase . ToLowerCamel ( name )
250
+ }
243
251
}
244
252
245
253
{
0 commit comments