@@ -474,25 +474,28 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
474
474
template = r2.Replace(template)
475
475
}
476
476
477
- if fp.ExistsStrIgnoreCase("DistinctP", onlyList) {
478
- template += basic.DistinctP()
479
- template = r2.Replace(template)
480
- }
477
+ /*
478
+ if fp.ExistsStrIgnoreCase("DistinctP", onlyList) {
479
+ template += basic.DistinctP()
480
+ template = r2.Replace(template)
481
+ }
481
482
482
- if fp.ExistsStrIgnoreCase("DistinctPPtr", onlyList) {
483
- template += basic.DistinctPPtr()
484
- template = r2.Replace(template)
485
- }
483
+ if fp.ExistsStrIgnoreCase("DistinctPPtr", onlyList) {
484
+ template += basic.DistinctPPtr()
485
+ template = r2.Replace(template)
486
+ }
486
487
487
- if fp.ExistsStrIgnoreCase("Distinct", onlyList) {
488
- template += basic.Distinct()
489
- template = r2.Replace(template)
490
- }
488
+ if fp.ExistsStrIgnoreCase("Distinct", onlyList) {
489
+ template += basic.Distinct()
490
+ template = r2.Replace(template)
491
+ }
491
492
492
- if fp.ExistsStrIgnoreCase("DistinctPtr", onlyList) {
493
- template += basic.DistinctPtr()
494
- template = r2.Replace(template)
495
- }
493
+ if fp.ExistsStrIgnoreCase("DistinctPtr", onlyList) {
494
+ template += basic.DistinctPtr()
495
+ template = r2.Replace(template)
496
+ }
497
+
498
+ */
496
499
497
500
} else {
498
501
template += template2.Map()
@@ -638,18 +641,20 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
638
641
639
642
template += basic.TakePtr()
640
643
template = r2.Replace(template)
644
+ /*
645
+ template += basic.DistinctP()
646
+ template = r2.Replace(template)
641
647
642
- template += basic.DistinctP ()
643
- template = r2.Replace(template)
648
+ template += basic.DistinctPPtr ()
649
+ template = r2.Replace(template)
644
650
645
- template += basic.DistinctPPtr ()
646
- template = r2.Replace(template)
651
+ template += basic.Distinct ()
652
+ template = r2.Replace(template)
647
653
648
- template += basic.Distinct ()
649
- template = r2.Replace(template)
654
+ template += basic.DistinctPtr ()
655
+ template = r2.Replace(template)
650
656
651
- template += basic.DistinctPtr()
652
- template = r2.Replace(template)
657
+ */
653
658
}
654
659
655
660
}
@@ -1221,3 +1226,102 @@ func findStructNamesAndFieldsGivenInGoGenerate() map[string][]string {
1221
1226
1222
1227
return structToFieldsMap
1223
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
1323
+ }
1324
+ }
1325
+
1326
+ return structToFieldsMap
1327
+ }
0 commit comments