81
81
setStr = flag .String ("set" , "" , "generate 'set' functions(union, intersection, difference, subset, superset) for user defined type. format- <StructName>:<FieldName> eg. Employee:Name, Employee:Salary" )
82
82
83
83
onlyList []string
84
+
85
+ switchTemplateDistinct = false
84
86
)
85
87
86
88
func main () {
@@ -125,7 +127,10 @@ func main() {
125
127
onlyList = strings .Split (* only , "," )
126
128
onlyList = fp .MapStr (strings .TrimSpace , onlyList )
127
129
}
128
- generatedCode , err := generateFPCode (* pkgName , * types , * imports )
130
+
131
+ structToFieldsMap , structToFieldsMapUnExpected := findStructNamesAndFieldsGivenInGoGenerate ()
132
+
133
+ generatedCode , err := generateFPCode (* pkgName , * types , * imports , structToFieldsMapUnExpected )
129
134
if err != nil {
130
135
usage ()
131
136
log .Fatalf ("Failed code generation: %v" , err )
@@ -173,7 +178,6 @@ import "sync"`, -1)
173
178
174
179
// Generate set and sort functions for all the types of struct
175
180
if * sortStr == "" || * setStr == "" {
176
- structToFieldsMap := findStructNamesAndFieldsGivenInGoGenerate ()
177
181
178
182
// 2nd condition is ignoring auto generation of sort functions for `all fp in 1 place strategy"
179
183
// Can be done in future : change the logic to find struct name(employee.Teacher) current logic is not checking .
@@ -201,6 +205,9 @@ import "time"`, -1)
201
205
}
202
206
}
203
207
208
+ if switchTemplateDistinct {
209
+ generatedCode = strings .Replace (generatedCode , `import _ "reflect"` , `import "reflect"` , - 1 )
210
+ }
204
211
f .Write ([]byte (generatedCode + "\n " + generatedCodeIO + "\n " + generatedCodeII + sortingCode + setCode ))
205
212
defer f .Close ()
206
213
@@ -220,14 +227,15 @@ func removeFirstPartOfDot(str string) string {
220
227
return str
221
228
}
222
229
223
- func generateFPCode (pkg , dataTypes , imports string ) (string , error ) {
230
+ func generateFPCode (pkg , dataTypes , imports string , structToFieldsMapUnexpected map [ string ][] string ) (string , error ) {
224
231
basicTypes := "int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8, float64, float32, string, bool"
225
232
conditionalType := ""
226
233
types := strings .Split (dataTypes , "," )
227
234
types = fp .DistinctStrIgnoreCase (types )
228
235
229
236
template := "// Code generated by 'gofp'. DO NOT EDIT.\n "
230
237
template += "package <PACKAGE>\n "
238
+ template += "import _ \" reflect\" \n "
231
239
template += "import \" sync\" \n "
232
240
233
241
if imports != "" {
@@ -255,6 +263,9 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
255
263
256
264
template = r .Replace (template )
257
265
266
+ // it collects info of members of struct other than basic types
267
+ unexpectedFieldsForDistinct := structToFieldsMapUnexpected [t ]
268
+
258
269
if len (onlyList ) > 0 {
259
270
// Always include these functions
260
271
@@ -474,7 +485,30 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
474
485
template = r2 .Replace (template )
475
486
}
476
487
477
- /*
488
+ if len (unexpectedFieldsForDistinct ) > 1 {
489
+ switchTemplateDistinct = true
490
+
491
+ if fp .ExistsStrIgnoreCase ("DistinctP" , onlyList ) {
492
+ template += basic .DistinctP2 ()
493
+ template = r2 .Replace (template )
494
+ }
495
+
496
+ if fp .ExistsStrIgnoreCase ("DistinctPPtr" , onlyList ) {
497
+ template += basic .DistinctPPtr2 ()
498
+ template = r2 .Replace (template )
499
+ }
500
+
501
+ if fp .ExistsStrIgnoreCase ("Distinct" , onlyList ) {
502
+ template += basic .Distinct2 ()
503
+ template = r2 .Replace (template )
504
+ }
505
+
506
+ if fp .ExistsStrIgnoreCase ("DistinctPtr" , onlyList ) {
507
+ template += basic .DistinctPtr2 ()
508
+ template = r2 .Replace (template )
509
+ }
510
+
511
+ } else {
478
512
if fp .ExistsStrIgnoreCase ("DistinctP" , onlyList ) {
479
513
template += basic .DistinctP ()
480
514
template = r2 .Replace (template )
@@ -494,8 +528,7 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
494
528
template += basic .DistinctPtr ()
495
529
template = r2 .Replace (template )
496
530
}
497
-
498
- */
531
+ }
499
532
500
533
} else {
501
534
template += template2 .Map ()
@@ -641,7 +674,23 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
641
674
642
675
template += basic .TakePtr ()
643
676
template = r2 .Replace (template )
644
- /*
677
+
678
+ if len (unexpectedFieldsForDistinct ) > 1 {
679
+ switchTemplateDistinct = true
680
+
681
+ template += basic .DistinctP2 ()
682
+ template = r2 .Replace (template )
683
+
684
+ template += basic .DistinctPPtr2 ()
685
+ template = r2 .Replace (template )
686
+
687
+ template += basic .Distinct2 ()
688
+ template = r2 .Replace (template )
689
+
690
+ template += basic .DistinctPtr2 ()
691
+ template = r2 .Replace (template )
692
+
693
+ } else {
645
694
template += basic .DistinctP ()
646
695
template = r2 .Replace (template )
647
696
@@ -653,10 +702,8 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
653
702
654
703
template += basic .DistinctPtr ()
655
704
template = r2 .Replace (template )
656
-
657
- */
705
+ }
658
706
}
659
-
660
707
}
661
708
return template , nil
662
709
}
@@ -1128,7 +1175,7 @@ func listDir(dirName string) ([]string, error) {
1128
1175
return files , nil
1129
1176
}
1130
1177
1131
- func findStructNamesAndFieldsGivenInGoGenerate () map [string ][]string {
1178
+ func findStructNamesAndFieldsGivenInGoGenerate () ( map [string ][]string , map [ string ][] string ) {
1132
1179
allTypesInGoGenerate := strings .Split (* types , "," )
1133
1180
1134
1181
isUserDefinedType := func (dataType string ) bool {
@@ -1141,6 +1188,7 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1141
1188
userDefinedTypesInGoGenerate := fp .MapStr (strings .TrimSpace , fp .FilterStr (isUserDefinedType , allTypesInGoGenerate ))
1142
1189
1143
1190
structToFieldsMap := make (map [string ][]string , len (userDefinedTypesInGoGenerate ))
1191
+ structToFieldsMapUnExpected := make (map [string ][]string , len (userDefinedTypesInGoGenerate ))
1144
1192
structToFieldsMapIndex := 0
1145
1193
1146
1194
path , err := os .Getwd ()
@@ -1150,7 +1198,7 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1150
1198
files , err := listDir (path )
1151
1199
if err != nil {
1152
1200
fmt .Printf ("\n error scanning current folder=%v, error=%v. sort and set methods will be skipped" , path , err )
1153
- return nil
1201
+ return nil , nil
1154
1202
}
1155
1203
1156
1204
onlyGoFiles := fp .FilterStr (func (str string ) bool {
@@ -1167,14 +1215,15 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1167
1215
file , err := os .Open (fileStr )
1168
1216
if err != nil {
1169
1217
fmt .Printf ("\n error reading file=%s to generate sort and set methods. skipping set and sort functions" , fileStr )
1170
- return nil
1218
+ return nil , nil
1171
1219
}
1172
1220
defer file .Close ()
1173
1221
1174
1222
scanner := bufio .NewScanner (file )
1175
1223
packageFound := false
1176
1224
startCollectingStructInfo := false
1177
1225
var structFields []string
1226
+ var structFieldsUnExpected []string
1178
1227
1179
1228
for scanner .Scan () {
1180
1229
txtLine := scanner .Text ()
@@ -1199,9 +1248,15 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1199
1248
for i , v := range structFields {
1200
1249
newStructFieldsArr [i ] = v
1201
1250
}
1251
+
1252
+ newStructFieldsArrUnExpected := make ([]string , len (structFieldsUnExpected ))
1253
+ copy (newStructFieldsArrUnExpected , structFieldsUnExpected )
1254
+
1202
1255
structToFieldsMap [userDefinedTypesInGoGenerate [structToFieldsMapIndex ]] = newStructFieldsArr
1256
+ structToFieldsMapUnExpected [userDefinedTypesInGoGenerate [structToFieldsMapIndex ]] = newStructFieldsArrUnExpected
1203
1257
structToFieldsMapIndex ++
1204
1258
structFields = make ([]string , 0 )
1259
+ structFieldsUnExpected = make ([]string , 0 )
1205
1260
}
1206
1261
1207
1262
if startCollectingStructInfo {
@@ -1212,6 +1267,8 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1212
1267
switch dataType {
1213
1268
case "int" , "int64" , "int32" , "int16" , "int8" , "uint" , "uint64" , "uint32" , "uint16" , "uint8" , "float64" , "float32" , "string" , "time.Time" , "*time.Time" , "*int" , "*int64" , "*int32" , "*int16" , "*int8" , "*uint" , "*uint64" , "*uint32" , "*uint16" , "*uint8" , "*float64" , "*float32" , "*string" :
1214
1269
structFields = append (structFields , field + " " + dataType )
1270
+ default :
1271
+ structFieldsUnExpected = append (structFieldsUnExpected , field + " " + dataType )
1215
1272
}
1216
1273
}
1217
1274
}
@@ -1220,108 +1277,9 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1220
1277
1221
1278
if err := scanner .Err (); err != nil {
1222
1279
fmt .Printf ("\n error scanning data from file %s. skipping generation of sort and set functions" , fileStr )
1223
- return nil
1224
- }
1225
- }
1226
-
1227
- return structToFieldsMap
1228
- }
1229
-
1230
- func allFeildsInStruct () map [string ][]string {
1231
- allTypesInGoGenerate := strings .Split (* types , "," )
1232
-
1233
- isUserDefinedType := func (dataType string ) bool {
1234
- switch strings .ToLower (strings .TrimSpace (dataType )) {
1235
- case "int" , "int64" , "int32" , "int16" , "int8" , "uint" , "uint64" , "uint32" , "uint16" , "uint8" , "float64" , "float32" , "string" , "bool" :
1236
- return false
1237
- }
1238
- return true
1239
- }
1240
- userDefinedTypesInGoGenerate := fp .MapStr (strings .TrimSpace , fp .FilterStr (isUserDefinedType , allTypesInGoGenerate ))
1241
-
1242
- structToFieldsMap := make (map [string ][]string , len (userDefinedTypesInGoGenerate ))
1243
- structToFieldsMapIndex := 0
1244
-
1245
- path , err := os .Getwd ()
1246
- if err != nil {
1247
- fmt .Println (err )
1248
- }
1249
- files , err := listDir (path )
1250
- if err != nil {
1251
- fmt .Printf ("\n error scanning current folder=%v, error=%v. sort and set methods will be skipped" , path , err )
1252
- return nil
1253
- }
1254
-
1255
- onlyGoFiles := fp .FilterStr (func (str string ) bool {
1256
- return strings .Contains (str , ".go" )
1257
- }, files )
1258
-
1259
- totalStructCount := 0
1260
- for _ , fileStr := range onlyGoFiles {
1261
-
1262
- if totalStructCount == len (userDefinedTypesInGoGenerate ) {
1263
- break
1264
- }
1265
-
1266
- file , err := os .Open (fileStr )
1267
- if err != nil {
1268
- fmt .Printf ("\n error reading file=%s to generate sort and set methods. skipping set and sort functions" , fileStr )
1269
- return nil
1270
- }
1271
- defer file .Close ()
1272
-
1273
- scanner := bufio .NewScanner (file )
1274
- packageFound := false
1275
- startCollectingStructInfo := false
1276
- var structFields []string
1277
-
1278
- for scanner .Scan () {
1279
- txtLine := scanner .Text ()
1280
- if len (txtLine ) > 0 && strings .Contains (txtLine , * pkgName ) {
1281
- packageFound = true
1282
- }
1283
- // reading lines of file of package mentioned in go:generate
1284
- if packageFound {
1285
-
1286
- words := strings .Fields (txtLine )
1287
-
1288
- // Found struct
1289
- if len (words ) == 4 && strings .Contains (words [0 ], "type" ) && fp .ExistsStr (words [1 ], userDefinedTypesInGoGenerate ) && strings .Contains (words [2 ], "struct" ) && strings .Contains (words [3 ], "{" ) {
1290
- startCollectingStructInfo = true
1291
- totalStructCount ++
1292
- }
1293
-
1294
- if startCollectingStructInfo && strings .TrimSpace (txtLine ) == "}" {
1295
- startCollectingStructInfo = false
1296
-
1297
- newStructFieldsArr := make ([]string , len (structFields ))
1298
- for i , v := range structFields {
1299
- newStructFieldsArr [i ] = v
1300
- }
1301
- structToFieldsMap [userDefinedTypesInGoGenerate [structToFieldsMapIndex ]] = newStructFieldsArr
1302
- structToFieldsMapIndex ++
1303
- structFields = make ([]string , 0 )
1304
- }
1305
-
1306
- if startCollectingStructInfo {
1307
- if len (words ) >= 2 {
1308
- field := strings .TrimSpace (words [0 ])
1309
- dataType := strings .TrimSpace (words [1 ])
1310
-
1311
- //switch dataType {
1312
- //case "int", "int64", "int32", "int16", "int8", "uint", "uint64", "uint32", "uint16", "uint8", "float64", "float32", "string", "time.Time", "*time.Time", "*int", "*int64", "*int32", "*int16", "*int8", "*uint", "*uint64", "*uint32", "*uint16", "*uint8", "*float64", "*float32", "*string":
1313
- structFields = append (structFields , field + " " + dataType )
1314
- //}
1315
- }
1316
- }
1317
- }
1318
- }
1319
-
1320
- if err := scanner .Err (); err != nil {
1321
- fmt .Printf ("\n error scanning data from file %s. skipping generation of sort and set functions" , fileStr )
1322
- return nil
1280
+ return nil , nil
1323
1281
}
1324
1282
}
1325
1283
1326
- return structToFieldsMap
1284
+ return structToFieldsMap , structToFieldsMapUnExpected
1327
1285
}
0 commit comments