-
Notifications
You must be signed in to change notification settings - Fork 0
/
genRoutes.go
896 lines (662 loc) · 20.2 KB
/
genRoutes.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
package gen
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/macinnir/dvc/core/lib"
)
func extractControllerNameFromFileName(path string) string {
fileName := filepath.Base(path)
// Must be aleast 14 chars (e.g. AController.go)
// fmt.Println("Extracting controller name", fileName)
if
// 14 chars
len(fileName) < 14 ||
// .go extension
fileName[len(fileName)-3:] != ".go" ||
// Uppercase first letter
!unicode.IsUpper([]rune(fileName)[0]) ||
// Not a test file
fileName[len(fileName)-8:] == "_test.go" {
return ""
}
return fileName[:len(fileName)-13]
}
func fetchAllPermissions(controllersDir string) (map[string]string, error) {
cf := NewControllerFetcher()
permissionMap := loadPermissionsFromJSON()
controllers, e := cf.Fetch(controllersDir)
if e != nil {
return nil, e
}
for k := range controllers {
controller := controllers[k]
// Extract the permissions from the controller
permissionMap[controller.Name+"_View"] = "View " + controller.Name
for k := range controller.Routes {
permissionMap[controller.Routes[k].Permission] = controller.Routes[k].Description
}
}
return permissionMap, nil
}
type ControllerFetcher struct {
routeMap map[string]bool
}
func NewControllerFetcher() *ControllerFetcher {
return &ControllerFetcher{
routeMap: map[string]bool{},
}
}
func (cf *ControllerFetcher) Fetch(dir string) (controllers []*lib.Controller, e error) {
controllers = []*lib.Controller{}
var files []os.FileInfo
files, e = ioutil.ReadDir(dir)
if e != nil {
log.Println("ERROR: Fetch Controllers - ", dir, e.Error())
return
}
for k := range files {
filePath := path.Join(dir, files[k].Name())
if files[k].IsDir() {
var subControllers []*lib.Controller
if subControllers, e = cf.Fetch(filePath); e != nil {
return
}
controllers = append(controllers, subControllers...)
continue
}
// Build a controller object from the controller file
var controller *lib.Controller
if controller, e = cf.BuildControllerObjFromControllerFile(filePath); e != nil {
return
}
if controller != nil {
controllers = append(controllers, controller)
}
}
return
}
// BuildControllerObjFromControllerFile parses a file and extracts all of its @route comments
func (cf *ControllerFetcher) BuildControllerObjFromControllerFile(filePath string) (controller *lib.Controller, e error) {
pkgName := filepath.Base(filepath.Dir(filePath))
controllerName := extractControllerNameFromFileName(filePath)
if controllerName == "" {
return nil, nil
}
var src []byte
src, e = ioutil.ReadFile(filePath)
if e != nil {
log.Println("Error with ", filePath)
return
}
controller = &lib.Controller{
Name: controllerName,
Path: filePath,
Routes: []*lib.ControllerRoute{},
Package: pkgName,
}
controllerFullName := controller.Name + "Controller"
// Get the controller name
var methods []lib.Method
methods, _, controller.Description = lib.ParseStruct(src, controllerFullName, true, true, "controllers")
// Remove the name of the controller from the description
controller.Description = strings.TrimPrefix(controller.Description, controller.Name)
for _, method := range methods {
route := &lib.ControllerRoute{
Queries: []lib.ControllerRouteQuery{},
Params: []lib.ControllerRouteParam{},
}
for line, doc := range method.Documents {
// This is the title of the method
if line == 0 {
lineParts := strings.Split(doc, " ")
route.Name = lineParts[1]
route.Description = strings.Join(lineParts[2:], " ")
continue
}
route.IsAuth = true
// @anonymous
if len(doc) > 12 && doc[0:13] == "// @anonymous" {
route.IsAuth = false
continue
}
// @body
if len(doc) > 9 && doc[0:9] == "// @body " {
bodyComment := strings.Split(strings.Trim(doc[9:], " "), " ")
route.BodyFormat = bodyComment[0]
if len(bodyComment) > 1 {
route.BodyType = bodyComment[1]
}
controller.HasDTOsImport = true
controller.HasResponseImport = true
route.HasBody = true
continue
}
// @response (last line)
if len(doc) > 13 && doc[0:13] == "// @response " {
responseComment := strings.Split(strings.Trim(doc[13:], " "), " ")
if route.ResponseCode, e = strconv.Atoi(responseComment[0]); e != nil {
log.Fatalf("Invalid @response comment: %s at %s.%s", doc, controller.Name, route.Name)
}
if len(responseComment) > 1 {
route.ResponseFormat = responseComment[1]
}
if len(responseComment) > 2 {
route.ResponseType = responseComment[2]
}
continue
}
// @perm
// if len(doc) > 9 && doc[0:9] == "// @perm " {
// route.Permission = strings.TrimSpace(doc[9:])
// usesPerms = true
// continue
// }
// @route
if len(doc) > 9 && doc[0:9] == "// @route" {
lineParts := strings.Split(doc, " ")
if len(lineParts) < 4 {
log.Fatalf("Invalid route comment `%s` for method `%s.%s`", doc, controller.Name, route.Name)
}
route.Method = lineParts[2]
route.Raw = lineParts[3]
// Queries
if strings.Contains(route.Raw, "?") {
subParts := strings.Split(route.Raw, "?")
route.Path = subParts[0]
queries := strings.Split(subParts[1], "&")
for _, query := range queries {
if !strings.Contains(query, "=") {
continue
}
queryParts := strings.Split(query, "=")
o := lib.ControllerRouteQuery{
Name: queryParts[0],
ValueRaw: queryParts[1],
}
if strings.Contains(o.ValueRaw, ":") {
queryValueParts := strings.Split(o.ValueRaw, ":")
// Remove the starting "{"
o.VariableName = queryValueParts[0][1:]
// Remove the ending "}"
o.Pattern = strings.Join(queryValueParts[1:], ":")
o.Pattern = o.Pattern[0 : len(o.Pattern)-1]
// Check if the value isn't a constant value
if o.Pattern == "[0-9]" || o.Pattern == "[0-9]+" {
o.Type = "int64"
} else {
o.Type = "string"
}
} else {
// Try to parse the value as an int64
// e.g. param=123
o.VariableName = o.Name
if _, e := strconv.ParseInt(o.ValueRaw, 10, 64); e != nil {
o.Type = "string"
} else {
o.Type = "int64"
}
}
route.Queries = append(route.Queries, o)
}
} else {
route.Path = route.Raw
}
params, _ := extractParamsFromRoutePath(route.Path)
route.Params = append(route.Params, params...)
} else {
route.Description += " " + doc[3:]
}
}
if route.IsAuth {
controller.PermCount++
route.Permission = controller.Name + "_" + route.Name
}
routeSignature := route.Method + " " + route.Path
if _, ok := cf.routeMap[routeSignature]; ok {
e = fmt.Errorf("Duplicate route signature `%s` for method `%s`.`%s`", routeSignature, controller.Name, route.Name)
return
}
controller.Routes = append(controller.Routes, route)
}
return
}
// GenRoutes generates a list of routes from a directory of controller files
func GenRoutesAndPermissions(config *lib.Config) error {
// permissionMap := loadPermissionsFromJSON()
imports := []string{
path.Join(config.BasePackage, config.Dirs.Controllers),
path.Join(config.BasePackage, config.Dirs.IntegrationInterfaces),
path.Join(config.BasePackage, config.Dirs.Aggregates),
"net/http",
"github.com/gorilla/mux",
}
// fmt.Println(imports)
code := ""
rest := ""
// controllerCalls := []string{}
hasBodyImports := false
packageUsesPermission := false
cf := NewControllerFetcher()
controllers, e := cf.Fetch(config.Dirs.Controllers)
if e != nil {
return e
}
for k := range controllers {
controller := controllers[k]
// fmt.Println("ControllerName:", controllerName)
if controller.PermCount > 0 {
packageUsesPermission = true
}
// Documentation routes
controllers = append(controllers, controller)
// Include imports for dtos and response if necessary for JSON http body
if controller.HasDTOsImport {
hasBodyImports = true
}
var routesString string
routesString, e = BuildRoutesCodeFromController(controller)
if e != nil {
return e
}
rest += "\n" + routesString + "\n"
// controllerCalls = append(
// controllerCalls,
// "map"+strings.Title(controller.Package)+controller.Name+"Routes(res, r, auth, c, log)",
// )
}
// code += strings.Join(controllerCalls, "\n\t")
code += rest
code += "\n\n}\n"
if hasBodyImports {
// imports = append(imports, g.Config.BasePackage+"/core/utils/response")
imports = append(imports, config.BasePackage+"/core/definitions/dtos")
}
imports = append(imports, "github.com/macinnir/dvc/core/lib/utils/request")
if packageUsesPermission {
imports = append(imports, "github.com/macinnir/dvc/core/lib/utils")
imports = append(imports, path.Join(config.BasePackage, config.Dirs.Permissions))
}
final := `// Generated Code; DO NOT EDIT.
package api
import (
`
for _, i := range imports {
final += fmt.Sprintf("\t\"%s\"\n", i)
}
final += `)
// MapRoutesToControllers maps the routes to the controllers
func MapRoutesToControllers(r *mux.Router, auth integrations.IAuth, c *controllers.Controllers, res request.IResponseLogger, log integrations.ILog) {
`
final += code
ioutil.WriteFile("core/api/routes.go", []byte(final), 0777)
routesContainer := &lib.RoutesJSONContainer{
Routes: map[string]*lib.ControllerRoute{},
DTOs: genDTOSMap(),
Models: genModelsMap(),
Aggregates: genAggregatesMap(),
Constants: genConstantsMap(),
}
for k := range controllers {
for i := range controllers[k].Routes {
key := controllers[k].Routes[i].Name
routesContainer.Routes[key] = controllers[k].Routes[i]
}
}
routesJSON, _ := json.MarshalIndent(routesContainer, " ", " ")
// fmt.Println("Writing Routes JSON to path", lib.RoutesFilePath)
ioutil.WriteFile(lib.RoutesFilePath, routesJSON, 0777)
return nil
}
func extractParamsFromRoutePath(routePath string) (params []lib.ControllerRouteParam, e error) {
params = []lib.ControllerRouteParam{}
// Params
if strings.Contains(routePath, "{") {
routeParts := strings.Split(routePath, "{")
for _, p := range routeParts[1:] {
if !strings.Contains(p, "}") || !strings.Contains(p, ":") {
continue
}
param := extractParamFromString(p)
params = append(params, param)
}
}
return
}
func extractParamFromString(paramString string) (param lib.ControllerRouteParam) {
// Incase there are parts after the param, split on the closing bracket
pParts := strings.Split(paramString, "}")
paramString = pParts[0]
paramParts := strings.Split(paramString, ":")
param = lib.ControllerRouteParam{
Name: paramParts[0],
Pattern: paramParts[1],
}
param.Type = matchPatternToDataType(param.Pattern)
return
}
func matchPatternToDataType(pattern string) string {
if pattern == "[0-9]" || pattern == "[0-9]+" {
return "int64"
}
return "string"
}
// BuildRoutesCodeFromController builds controller code based on a route
func BuildRoutesCodeFromController(controller *lib.Controller) (out string, e error) {
s := []string{
"",
"\t////",
"\t// " + strings.Title(controller.Package) + "." + controller.Name,
"\t////",
"",
}
// fmt.Sprintf("// map%sRoutes maps all of the routes for %s", controller.Name, controller.Name),
// fmt.Sprintf("func map%s%sRoutes(res request.IResponseLogger, r *mux.Router, auth integrations.IAuth, c *controllers.Controllers.%s, log integrations.ILog) {\n", strings.Title(controller.Package), controller.Name, strings.Title(controller.Package)),
// }
for _, route := range controller.Routes {
// fmt.Println("Route: " + route.Name)
// Method comments
s = append(s, fmt.Sprintf("\t// %s.%s.%s", strings.Title(controller.Package), controller.Name, route.Name))
s = append(s, fmt.Sprintf("\t// %s %s", route.Method, route.Raw))
if !route.IsAuth {
s = append(s, "\t// @anonymous")
}
// Method args
args := []string{
"w", // http.ResponseWriter
"req", // *http.Request
}
if route.IsAuth {
s = append(s, fmt.Sprintf("\tr.Handle(\"%s\", auth.AuthMiddleware(func(w http.ResponseWriter, currentUser *aggregates.UserAggregate, req *request.Request) {\n", route.Path))
// s = append(s, fmt.Sprintf("\t\tcurrentUser := auth.GetCurrentUser(r)\n"))
// args = append(args, "currentUser")
} else {
s = append(s, fmt.Sprintf("\tr.Handle(\"%s\", auth.AnonMiddleware(func(w http.ResponseWriter, req *request.Request) {\n", route.Path))
}
s = append(s, fmt.Sprintf("\n\t\tlog.Debug(\"ROUTE: %s %s => %s\")\n\n", route.Method, route.Path, route.Name))
// Permission
if route.IsAuth {
// ucFirst
// permission := string(unicode.ToUpper(rune(route.Permission[0]))) + route.Permission[1:]
s = append(s, `
if !utils.HasPerm(req, currentUser, permissions.`+route.Permission+`) {
res.Forbidden(req, w)
return
}`)
}
if len(route.BodyType) > 0 {
if route.BodyType[0:1] == "*" {
route.BodyType = route.BodyType[1:]
}
s = append(s, fmt.Sprintf("\t\tbody := &%s{}", route.BodyType))
s = append(s, "\t\treq.BodyJSON(body)\n")
}
if len(route.Params) > 0 {
for _, param := range route.Params {
s = append(s, fmt.Sprintf("\t\t// URL Param %s", param.Name))
if param.Type == "int64" {
s = append(s, fmt.Sprintf("\t\t%s := req.ArgInt64(\"%s\", 0)\n", param.Name, param.Name))
} else {
s = append(s, fmt.Sprintf("\t\t%s := req.Arg(\"%s\", \"\")\n", param.Name, param.Name))
}
args = append(args, param.Name)
}
}
if len(route.Queries) > 0 {
for _, query := range route.Queries {
s = append(s, fmt.Sprintf("\t\t// Query Arg %s", query.VariableName))
if query.Type == "int64" {
s = append(s, fmt.Sprintf("\t\t%s := req.ArgInt64(\"%s\", 0)\n", query.VariableName, query.VariableName))
} else {
s = append(s, fmt.Sprintf("\t\t%s := req.Arg(\"%s\", \"\")\n", query.VariableName, query.VariableName))
}
args = append(args, query.VariableName)
}
}
// Add the body as the last argument
if len(route.BodyType) > 0 {
args = append(args, "body")
}
s = append(s, fmt.Sprintf("\t\tc.%s.%s.%s(", strings.Title(controller.Package), controller.Name, route.Name)+strings.Join(args, ", ")+")\n")
s = append(s, "\t})).")
s = append(s, fmt.Sprintf("\t\tMethods(\"%s\").", route.Method))
if len(route.Queries) > 0 {
s = append(s, "\t\tQueries(")
for _, query := range route.Queries {
s = append(s, fmt.Sprintf("\t\t\t\"%s\", \"%s\",", query.Name, query.ValueRaw))
}
s = append(s, "\t\t).")
}
s = append(s, fmt.Sprintf("\t\tName(\"%s\")\n", route.Name))
}
out = strings.Join(s, "\n") // + "\n}"
return
}
func genDTOSMap() map[string]map[string]string {
dtosDir := "core/definitions/dtos"
dirHandle, err := os.Open(dtosDir)
if err != nil {
panic(err)
}
defer dirHandle.Close()
var dirFileNames []string
dirFileNames, err = dirHandle.Readdirnames(-1)
if err != nil {
panic(err)
}
// reader := bufio.NewReader(os.Stdin)
result := map[string]map[string]string{}
for _, name := range dirFileNames {
if name == ".DS_Store" {
continue
}
fullPath := path.Join(dtosDir, name)
model, e := InspectFile(fullPath)
if e != nil {
panic(e)
}
k := 0
result[model.Name] = map[string]string{}
for k < model.Fields.Len() {
result[model.Name][model.Fields.Get(k).Name] = model.Fields.Get(k).DataType
k++
}
}
return result
}
func genModelsMap() map[string]map[string]string {
modelsDir := "core/definitions/models"
dirHandle, err := os.Open(modelsDir)
if err != nil {
panic(err)
}
defer dirHandle.Close()
var dirFileNames []string
dirFileNames, err = dirHandle.Readdirnames(-1)
if err != nil {
panic(err)
}
// reader := bufio.NewReader(os.Stdin)
result := map[string]map[string]string{}
for _, name := range dirFileNames {
if name == ".DS_Store" {
continue
}
// fileNameNoExt := name[0 : len(name)-3]
fullPath := path.Join(modelsDir, name)
// fmt.Println(fullPath)
model, e := InspectFile(fullPath)
if e != nil {
panic(e)
}
k := 0
result[model.Name] = map[string]string{}
for k < model.Fields.Len() {
result[model.Name][model.Fields.Get(k).Name] = model.Fields.Get(k).DataType
k++
}
}
return result
}
func genAggregatesMap() map[string]map[string]string {
modelsDir := "core/definitions/aggregates"
dirHandle, err := os.Open(modelsDir)
if err != nil {
panic(err)
}
defer dirHandle.Close()
var dirFileNames []string
dirFileNames, err = dirHandle.Readdirnames(-1)
if err != nil {
panic(err)
}
// reader := bufio.NewReader(os.Stdin)
result := map[string]map[string]string{}
for _, name := range dirFileNames {
if name == ".DS_Store" {
continue
}
// fileNameNoExt := name[0 : len(name)-3]
fullPath := path.Join(modelsDir, name)
// fmt.Println(fullPath)
fileBytes, e := ioutil.ReadFile(fullPath)
if e != nil {
panic(e)
}
contents := string(fileBytes)
re := regexp.MustCompile("^type [a-zA-Z0-9]+ struct {$")
contentLines := strings.Split(contents, "\n")
currentStruct := ""
for k := range contentLines {
if re.Match([]byte(contentLines[k])) {
structName := contentLines[k][5 : len(contentLines[k])-9]
// fmt.Println(k, structName)
result[structName] = map[string]string{}
currentStruct = structName
continue
}
if len(currentStruct) > 0 {
contentLines[k] = strings.TrimSpace(contentLines[k])
if contentLines[k] == "}" {
currentStruct = ""
continue
}
parts := []string{}
preParts := strings.Split(contentLines[k], " ")
for l := range preParts {
if len(preParts[l]) == 0 {
continue
}
parts = append(parts, preParts[l])
}
fieldName := ""
fieldType := ""
if len(parts) > 1 {
fieldName = parts[0]
fieldType = parts[1]
} else {
// This is an embedded type
if strings.Contains(parts[0], ".") {
sParts := strings.Split(parts[0], ".")
fieldName = sParts[1]
fieldType = parts[0]
}
// fmt.Println(">>>> " + strings.TrimSpace(parts[0]))
}
if len(fieldName) > 0 && len(fieldType) > 0 {
result[currentStruct][fieldName] = fieldType
}
}
// if len(contentLines[k]) > 5 && contentLines[k][0:5] == "type " {
// }
}
// model, e := InspectFile(fullPath)
// if e != nil {
// panic(e)
// }
// k := 0
// for k < model.Fields.Len() {
// result[model.Name][model.Fields.Get(k).Name] = model.Fields.Get(k).DataType
// k++
// }
}
return result
}
func genConstantsMap() map[string][]string {
modelsDir := "core/definitions/constants"
files, err := ioutil.ReadDir(modelsDir)
if err != nil {
panic(err)
}
// defer dirHandle.Close()
// var dirFileNames []string
// dirFileNames, err = dirHandle.Readdirnames(-1)
// reader := bufio.NewReader(os.Stdin)
result := map[string][]string{}
for _, file := range files {
if file.Name() == ".DS_Store" {
continue
}
if file.IsDir() {
continue
}
// fileNameNoExt := name[0 : len(name)-3]
fullPath := path.Join(modelsDir, file.Name())
// fmt.Println(fullPath)
fileBytes, e := ioutil.ReadFile(fullPath)
if e != nil {
panic(e)
}
contents := string(fileBytes)
re := regexp.MustCompile("^type [a-zA-Z0-9]+ [a-zA-Z0-9]+$")
contentLines := strings.Split(contents, "\n")
currentStruct := ""
isConsts := false
for k := range contentLines {
// fmt.Println(k, contentLines[k])
if re.Match([]byte(contentLines[k])) {
structName := contentLines[k][5:]
structName = strings.Split(structName, " ")[0]
// fmt.Println(k, structName)
result[structName] = []string{}
currentStruct = structName
continue
}
if contentLines[k] == "const (" {
isConsts = true
continue
}
if isConsts == true {
contentLines[k] = strings.TrimSpace(contentLines[k])
if contentLines[k] == ")" {
break
}
if len(contentLines[k]) > 2 && contentLines[k][0:2] == "//" {
continue
}
parts := strings.Split(contentLines[k], " ")
result[currentStruct] = append(result[currentStruct], parts[0])
}
// if len(contentLines[k]) > 5 && contentLines[k][0:5] == "type " {
// }
}
// model, e := InspectFile(fullPath)
// if e != nil {
// panic(e)
// }
// k := 0
// for k < model.Fields.Len() {
// result[model.Name][model.Fields.Get(k).Name] = model.Fields.Get(k).DataType
// k++
// }
}
return result
}