-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.go
775 lines (725 loc) · 20.3 KB
/
gen.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
// The following directive is necessary to make the package coherent:
// +bкuild ignore
// This program generates mappers.go. It can be invoked by running
// go generate
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
pathUtil "path"
"path/filepath"
"strconv"
"strings"
"text/template"
"time"
"gopkg.in/yaml.v2"
)
const (
defaultOutFile = "mappers.go"
)
const (
mapperTmpl = `// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
// using data from
// {{ .ConfPath }}
package {{ .PackageName }}
import (
{{- range .ImportPackages }}
{{if .Alias}}{{ .Alias }} {{end}}"{{ .Path }}"
{{- end }}
)
{{- range .TypeToPtrList }}
func {{ . }}Ptr(src {{ . }}) *{{ . }} {
return &src
}
{{- end }}
{{- range .TypesCastList }}
{{ $name := .Name }}
{{- range .CastTypes }}
func {{ $name }}ArrTo{{ . | ToTitle }}PtrArr(src []{{ $name }}) (dst []*{{ . }}) {
dst = make([]*{{ . }}, len(src))
for i := range src {
dst[i] = {{ . }}Ptr({{ . }}(src[i]))
}
return dst
}
func {{ $name }}PtrArrTo{{ . | ToTitle }}Arr(src []*{{ $name }}) (dst []{{ . }}) {
dst = make([]{{ . }}, len(src))
for i := range src {
dst[i] = {{ . }}(*src[i])
}
return dst
}
{{- end }}
{{- end }}
{{- range .Mappers }}
{{- $mapper := . }}
func {{ .MapperFuncName }}({{- range $index, $element := .SrcList }}{{if $index}}, {{end}}{{ $element.Alias }} *{{ $element.ShortPath }}{{- end }}) ({{ .Dst.Alias }} *{{ .Dst.ShortPath }}) {
{{- $dst := .Dst }}
{{- range .SrcList }}
if {{ .Alias }} != nil {
if {{ $dst.Alias }} == nil {
{{ $dst.Alias }} = &{{ $dst.ShortPath }}{}
}
{{- range $mapper.FieldMappingRules }}
{{- if .Casted }}
{{- if .SrcFieldPtr }}
if {{ .SrcAlias }}.{{ .SrcFieldName }} != nil {
{{ $dst.Alias }}.{{ .DstFieldName }} = {{ .CastStr }}
}
{{- else }}
{{ $dst.Alias }}.{{ .DstFieldName }} = {{ .CastStr }}
{{- end }}
{{- else }}
//{{ $dst.Alias }}.{{ .DstFieldName }} = {{ .CastStr }}
{{- end }}
{{- end }}
}
{{- end }}
return {{ .Dst.Alias }}
}
func {{ .ListMapperFuncName }}({{- range $index, $element := .SrcList }}{{if $index}}, {{end}}{{ $element.Alias }} []*{{ $element.ShortPath }}{{- end }}) ({{ .Dst.Alias }} []*{{ .Dst.ShortPath }}) {
var count int
{{- range .SrcList }}
if count == 0 || count > len({{ .Alias }}) {
count = len({{ .Alias }})
}
{{- end }}
{{ .Dst.Alias }} = make([]*{{ .Dst.ShortPath }}, 0, count)
for i := 0; i < count; i++ {
{{ .Dst.Alias }} = append(dst, {{ .MapperFuncName }}({{- range $index, $element := .SrcList }}{{if $index}}, {{end}}{{ $element.Alias }}[i]{{- end }}))
}
return {{ .Dst.Alias }}
}
{{- end }}`
)
var (
importPackageAliasMap = make(map[string]string, 10)
typeToPtrList = []string{"bool", "string", "byte", "int", "int64", "float32", "float64"}
typesCastList = []struct {
Name string
CastTypes []string
}{
{
Name: "bool",
CastTypes: []string{"bool"},
},
{
Name: "string",
CastTypes: []string{"string"},
},
{
Name: "int",
CastTypes: []string{"int", "int64"},
},
{
Name: "int64",
CastTypes: []string{"int", "int64"},
},
{
Name: "float32",
CastTypes: []string{"float32", "float64"},
},
{
Name: "float64",
CastTypes: []string{"float32", "float64"},
},
}
templateFuncMap = template.FuncMap{
"ToTitle": strings.Title,
}
)
type sourceConfig struct {
Alias string `yaml:"alias"`
Path string `yaml:"path"`
}
func (sc sourceConfig) StructureName() string {
_, structureName, _ := parsePackageAndStructure(sc.Path)
return structureName
}
type mapperConfig struct {
Alias string `yaml:"alias,omitempty"`
Destination sourceConfig `yaml:"destination"`
Sources []sourceConfig `yaml:"source"`
Mapping []string `yaml:"map"`
Relations []string `yaml:"relations"`
}
func (mc mapperConfig) MapperName() string {
prefix := mc.Alias
if len(prefix) == 0 {
prefix = mc.Destination.StructureName()
}
return fmt.Sprintf("%sMapper", prefix)
}
func (mc mapperConfig) ListMapperName() string {
prefix := mc.Alias
if len(prefix) == 0 {
prefix = mc.Destination.StructureName()
}
return fmt.Sprintf("%sListMapper", prefix)
}
type config struct {
path string
out string
Imports []importPackage `yaml:"imports"`
Mappers []mapperConfig `yaml:"mappers"`
}
func main() {
configPath := flag.String("c", "mappers.yml", "Config file path")
outPath := flag.String("o", "mappers_gen.go", "Out file path")
flag.Parse()
mappersConfig := loadConfig(*configPath)
mappersConfig.out = *outPath
err := os.MkdirAll(filepath.Dir(*outPath), os.ModePerm)
f, err := os.Create(*outPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
params, err := params(mappersConfig)
if err != nil {
log.Fatal(err)
}
packageTemplate, err := template.New("").Funcs(template.FuncMap{
"ToTitle": strings.Title,
}).Parse(mapperTmpl)
err = packageTemplate.Execute(f, params)
if err != nil {
log.Fatal(err)
}
}
func loadConfig(path string) *config {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(fmt.Sprintf("Mapping configuration file %s not found", path))
}
mappersConfig := config{path: path}
err = yaml.NewDecoder(bytes.NewReader(data)).Decode(&mappersConfig)
if err != nil {
log.Fatal("Mapping configuration file format error:", err.Error())
}
if err != nil {
log.Fatal("Generated mappers file error")
}
return &mappersConfig
}
type src struct {
Alias string
ShortPath string
Fields []field
}
type field struct {
Name string
Ptr bool
TypeStr string
}
type mappingParams struct {
MapperFuncName string
ListMapperFuncName string
Dst src
SrcList []src
FieldMappingRules []struct {
DstFieldName string
SrcAlias string
SrcShortPath string
SrcFieldName string
SrcFieldPtr bool
CastStr string
Casted bool
}
}
type importPackage struct {
Alias string
Path string
}
func params(mappersConfig *config) (interface{}, error) {
packageName := mapperFilePackage(mappersConfig.out)
mappers := make([]mappingParams, 0, len(mappersConfig.Mappers)*2)
for _, mapperConfig := range mappersConfig.Mappers {
var dst src
dst.Alias = mapperConfig.Destination.Alias
dstMeta, err := parseStructure(pathUtil.Dir(mappersConfig.path), mapperConfig.Destination.Path)
if err != nil {
return nil, err
}
dst.ShortPath = shortPath(dstMeta)
if err != nil {
return nil, err
}
for _, f := range dstMeta.fields {
dst.Fields = append(dst.Fields, struct {
Name string
Ptr bool
TypeStr string
}{
Name: f.name,
Ptr: isPtr(f.typeAST),
TypeStr: typeStrValue(f.typeAST),
})
}
var srcList []src
for _, mapperSrc := range mapperConfig.Sources {
srcMeta, err := parseStructure(pathUtil.Dir(mappersConfig.path), mapperSrc.Path)
if err != nil {
return nil, err
}
var fields []field
for _, f := range srcMeta.fields {
fields = append(fields, struct {
Name string
Ptr bool
TypeStr string
}{
Name: f.name,
Ptr: isPtr(f.typeAST),
TypeStr: typeStrValue(f.typeAST),
})
}
srcList = append(srcList, src{
Alias: mapperSrc.Alias,
ShortPath: shortPath(srcMeta),
Fields: fields,
})
}
fieldMappingRuleMap := map[string]struct {
DstFieldName string
SrcAlias string
SrcShortPath string
SrcFieldName string
SrcFieldPtr bool
CastStr string
Casted bool
}{}
for _, dstField := range dst.Fields {
for _, srcStruct := range srcList {
for _, srcField := range srcStruct.Fields {
if dstField.Name == srcField.Name {
var fieldMappingRule struct {
DstFieldName string
SrcAlias string
SrcShortPath string
SrcFieldName string
SrcFieldPtr bool
CastStr string
Casted bool
}
fieldMappingRule.DstFieldName = dstField.Name
fieldMappingRule.SrcAlias = srcStruct.Alias
fieldMappingRule.SrcShortPath = srcStruct.ShortPath
fieldMappingRule.SrcFieldName = srcField.Name
fieldMappingRule.SrcFieldPtr = srcField.Ptr
fieldMappingRule.CastStr, fieldMappingRule.Casted = castDstField(srcStruct.Alias, srcField, dstField)
fieldMappingRuleMap[dstField.Name] = fieldMappingRule
}
}
}
}
for _, relation := range mapperConfig.Relations {
var fieldMappingRule struct {
DstFieldName string
SrcAlias string
SrcShortPath string
SrcFieldName string
SrcFieldPtr bool
CastStr string
Casted bool
}
_, dstFieldName, srcCastRow := parseRelation(relation)
fieldMappingRule.DstFieldName = dstFieldName
fieldMappingRule.CastStr, fieldMappingRule.Casted = srcCastRow, true
usedSrc := searchUsedSrc(srcList, srcCastRow)
if usedSrc != nil {
usedField := searchUsedField(usedSrc, srcCastRow)
if usedField != nil {
fieldMappingRule.SrcAlias = usedSrc.Alias
fieldMappingRule.SrcShortPath = usedSrc.ShortPath
fieldMappingRule.SrcFieldName = usedField.Name
fieldMappingRule.SrcFieldPtr = usedField.Ptr
}
}
fieldMappingRuleMap[dstFieldName] = fieldMappingRule
}
var fieldMappingRules []struct {
DstFieldName string
SrcAlias string
SrcShortPath string
SrcFieldName string
SrcFieldPtr bool
CastStr string
Casted bool
}
for _, fieldMappingRule := range fieldMappingRuleMap {
fieldMappingRules = append(fieldMappingRules, fieldMappingRule)
}
mappers = append(mappers, mappingParams{
MapperFuncName: mapperConfig.MapperName(),
ListMapperFuncName: mapperConfig.ListMapperName(),
Dst: dst,
SrcList: srcList,
FieldMappingRules: fieldMappingRules,
})
}
importPackages := make([]importPackage, 0, len(importPackageAliasMap))
for alias, packagePath := range importPackageAliasMap {
importPackages = append(importPackages, importPackage{
Alias: alias,
Path: packagePath,
})
}
for i := range mappersConfig.Imports {
importPackages = append(importPackages, importPackage{
Alias: mappersConfig.Imports[i].Alias,
Path: mappersConfig.Imports[i].Path,
})
}
return struct {
Timestamp time.Time
ConfPath string
PackageName string
ImportPackages []importPackage
Mappers []mappingParams
TypeToPtrList []string
TypesCastList []struct {
Name string
CastTypes []string
}
}{
Timestamp: time.Now(),
ConfPath: mappersConfig.path,
PackageName: packageName,
ImportPackages: importPackages,
TypeToPtrList: typeToPtrList,
TypesCastList: typesCastList,
Mappers: mappers,
}, nil
}
func searchUsedField(srcStruct *src, castRow string) *field {
for _, srcField := range srcStruct.Fields {
if castRow == fmt.Sprintf("%s.%s", srcStruct.Alias, srcField.Name) {
return &srcField
}
}
var res *field
for _, srcField := range srcStruct.Fields {
if strings.Contains(castRow, fmt.Sprintf("%s.%s", srcStruct.Alias, srcField.Name)) {
if res == nil || len(res.Name) < len(srcField.Name) {
res = &field{}
*res = srcField
}
}
}
return res
}
func searchUsedSrc(srcList []src, srcCastRow string) *src {
for _, srcStruct := range srcList {
if strings.Contains(srcCastRow, srcStruct.Alias) {
return &srcStruct
}
}
return nil
}
func shortPath(meta *structMeta) string {
packageAlias := getPackageAlias(meta.packagePath)
if len(packageAlias) != 0 {
return fmt.Sprintf("%s.%s", packageAlias, meta.name)
}
return meta.name
}
func castDstField(srcAlias string, srcField struct {
Name string
Ptr bool
TypeStr string
}, dstField struct {
Name string
Ptr bool
TypeStr string
}) (string, bool) {
dstType := dstField.TypeStr
srcType := srcField.TypeStr
srcRow := fmt.Sprintf("%s.%s", srcAlias, srcField.Name)
if dstType == srcType {
return srcRow, true
}
if dstType != srcType {
if "*"+dstType == srcType {
srcRow = "*" + srcRow
return srcRow, true
} else {
switch dstType {
case "bool":
switch srcType {
case "*bool":
return fmt.Sprintf("*%s", srcRow), true
}
case "string":
switch srcType {
case "*string":
return fmt.Sprintf("*%s", srcRow), true
}
case "*bool":
switch srcType {
case "bool":
return fmt.Sprintf("%sPtr(%s)", strings.Trim(dstType, "*"), srcRow), true
}
case "*string":
switch srcType {
case "string":
return fmt.Sprintf("%sPtr(%s)", strings.Trim(dstType, "*"), srcRow), true
}
case "byte",
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64",
"float32", "float64":
switch srcType {
case "byte",
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64",
"float32", "float64":
return fmt.Sprintf("%s(%s(%s))", dstType, dstType, srcRow), true
case "*byte",
"*uint", "*uint8", "*uint16", "*uint32", "*uint64",
"*int", "*int8", "*int16", "*int32", "*int64",
"*float32", "*float64":
return fmt.Sprintf("%s(*%s)", dstType, srcRow), true
}
case "*byte",
"*uint", "*uint8", "*uint16", "*uint32", "*uint64",
"*int", "*int8", "*int16", "*int32", "*int64",
"*float32", "*float64":
switch srcType {
case "byte",
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64",
"float32", "float64":
return fmt.Sprintf("%sPtr(%s(%s))", strings.Trim(dstType, "*"), strings.Trim(dstType, "*"), srcRow), true
case "*byte",
"*uint", "*uint8", "*uint16", "*uint32", "*uint64",
"*int", "*int8", "*int16", "*int32", "*int64",
"*float32", "*float64":
return fmt.Sprintf("%s(%s(%s))", strings.Trim(dstType, "*"), strings.Trim(dstType, "*"), srcRow), true
}
case "[]bool", "[]string", "[]byte",
"[]uint", "[]uint8", "[]uint16", "[]uint32", "[]uint64",
"[]int", "[]int8", "[]int16", "[]int32", "[]int64",
"[]float32", "[]float64":
switch srcType {
case "[]bool", "[]string", "[]byte",
"[]uint", "[]uint8", "[]uint16", "[]uint32", "[]uint64",
"[]int", "[]int8", "[]int16", "[]int32", "[]int64",
"[]float32", "[]float64":
return fmt.Sprintf("%sArrTo%sArr(%s)", strings.Trim(srcType, "[]"), strings.Title(strings.Trim(dstType, "[]")), srcRow), true
case "[]*bool", "[]*string", "[]*byte",
"[]*uint", "[]*uint8", "[]*uint16", "[]*uint32", "[]*uint64",
"[]*int", "[]*int8", "[]*int16", "[]*int32", "[]*int64",
"[]*float32", "[]*float64":
return fmt.Sprintf("%sPtrArrTo%sArr(%s)", strings.Trim(srcType, "[]*"), strings.Title(strings.Trim(dstType, "[]")), srcRow), true
}
case "[]*bool", "[]*string", "[]*byte",
"[]*uint", "[]*uint8", "[]*uint16", "[]*uint32", "[]*uint64",
"[]*int", "[]*int8", "[]*int16", "[]*int32", "[]*int64",
"[]*float32", "[]*float64":
switch srcType {
case "[]bool", "[]string", "[]byte",
"[]uint", "[]uint8", "[]uint16", "[]uint32", "[]uint64",
"[]int", "[]int8", "[]int16", "[]int32", "[]int64",
"[]float32", "[]float64":
return fmt.Sprintf("%sArrTo%sPtrArr(%s)", strings.Trim(srcType, "[]"), strings.Title(strings.Trim(dstType, "[]*")), srcRow), true
case "[]*bool", "[]*string", "[]*byte",
"[]*uint", "[]*uint8", "[]*uint16", "[]*uint32", "[]*uint64",
"[]*int", "[]*int8", "[]*int16", "[]*int32", "[]*int64",
"[]*float32", "[]*float64":
return fmt.Sprintf("%sPtrArrTo%sPtrArr(%s)", strings.Trim(srcType, "[]*"), strings.Title(strings.Trim(dstType, "[]*")), srcRow), true
}
}
}
}
return srcRow, false
}
func isPtr(expr ast.Expr) bool {
switch t := expr.(type) {
case *ast.ArrayType:
return true
case *ast.MapType:
return true
case *ast.SelectorExpr:
return isPtr(t.X)
case *ast.StarExpr:
return true
case *ast.Ident:
return false
default:
//log.Printf("Unknown %T type", t)
}
return false
}
func typeStrValue(node ast.Expr) string {
switch t := node.(type) {
case *ast.ArrayType:
return fmt.Sprintf("[]%s", typeStrValue(t.Elt))
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", typeStrValue(t.Key), typeStrValue(t.Value))
case *ast.SelectorExpr:
return typeStrValue(t.X)
case *ast.StarExpr:
return fmt.Sprintf("*%s", typeStrValue(t.X))
case *ast.Ident:
return t.Name
default:
//log.Printf("Unknown %T type", t)
}
return ""
}
func parseRelation(relation string) (dstAlias, dstField, srcCastRow string) {
res := strings.Split(relation, ":")
if len(res) != 2 {
panic(fmt.Errorf("relation %s incorrect format", relation))
}
var dstRow string
dstRow, srcCastRow = strings.TrimSpace(res[0]), strings.TrimSpace(res[1])
res = strings.Split(dstRow, ".")
if len(res) == 2 {
dstAlias, dstField = strings.TrimSpace(res[0]), strings.TrimSpace(res[1])
} else {
dstField = dstRow
}
return
}
func mapperFilePackage(mapperFilePath string) string {
absPath, err := filepath.Abs(mapperFilePath)
if err != nil {
panic(err)
}
return filepath.Base(filepath.Dir(absPath))
}
func parseStructure(dir, path string) (*structMeta, error) {
switch path {
case "byte", "bool", "string",
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64",
"float32", "float64":
return &structMeta{name: path}, nil
}
i := strings.LastIndex(path, ".")
if i <= 0 {
return nil, fmt.Errorf("source path \"%s\" incorrect", path)
}
filePath := path[:i] + ".go"
structName := path[i+1:]
fileSet := token.NewFileSet()
if !strings.HasSuffix(filePath, ".go") {
filePath = filePath + ".go"
}
data, err := ioutil.ReadFile(pathUtil.Join(dir, filePath))
if err != nil {
data, err = ioutil.ReadFile(filepath.Join(os.Getenv("GOPATH"), "src", filePath))
if err != nil {
return nil, fmt.Errorf("incorrect file path %s", filePath)
}
} else {
if packagePath, err := filepath.Abs(pathUtil.Join(dir, filePath)); err == nil {
filePath = strings.TrimPrefix(packagePath, filepath.Join(os.Getenv("GOPATH"), "src", string(os.PathSeparator)))
}
}
fileAST, err := parser.ParseFile(fileSet, "", data, 0)
if err != nil {
return nil, err
}
//importsMap := make(map[string]string, 10)
//for i := range fileAST.Imports {
// var packageAlias string
// if fileAST.Imports[i].Name != nil {
// packageAlias = fileAST.Imports[i].Name.Name
// } else {
// packageAlias = getPackageAlias(fileAST.Imports[i].Path.Value)
// }
// importsMap[packageAlias] = fileAST.Imports[i].Path.Value
//}
//log.Printf("%v", importsMap)
var res *structMeta
if o, exist := fileAST.Scope.Objects[structName]; exist {
if ts, ok := o.Decl.(*ast.TypeSpec); ok {
res = &structMeta{
name: ts.Name.Name,
packagePath: parseImportPackagePath(filePath),
}
if s, ok := ts.Type.(*ast.StructType); ok {
fields := make([]fieldMeta, 0, len(s.Fields.List))
for i := range s.Fields.List {
f := s.Fields.List[i]
if len(f.Names) == 0 {
continue
}
field := struct {
name string
tag string
typeAST ast.Expr
}{
name: f.Names[0].Name,
}
if tag := f.Tag; tag != nil {
field.tag = tag.Value
}
field.typeAST = f.Type
fields = append(fields, field)
}
res.fields = fields
}
}
}
if res == nil {
return nil, fmt.Errorf("structure %s not found in file %s", structName, filePath)
}
return res, nil
}
type fieldMeta struct {
name string
tag string
typeAST ast.Expr
}
type structMeta struct {
name string
packagePath string
fields []fieldMeta
}
func parseImportPackagePath(sourcePath string) string {
return filepath.Dir(strings.TrimLeft(sourcePath, filepath.Join(os.Getenv("GOPATH"), "src")))
}
func parsePackageAndStructure(srcPath string) (string, string, error) {
i := strings.LastIndex(srcPath, ".")
if i <= 0 || i+1 >= len(srcPath) {
return "", "", fmt.Errorf("src path \"%s\" incorrect format", srcPath)
}
return srcPath[:i], srcPath[i+1:], nil
}
func getPackageAlias(importPackagePath string) string {
if strings.HasSuffix(importPackagePath, ".go") {
importPackagePath = strings.TrimRight(importPackagePath, ".go")
}
i := strings.LastIndex(importPackagePath, "/")
if i <= 0 || i+1 >= len(importPackagePath) {
return importPackagePath
}
defaultAlias := importPackagePath[i+1:]
alias := defaultAlias
i = 1
v, exist := importPackageAliasMap[alias]
for exist {
if v == importPackagePath {
exist = false
} else {
alias = defaultAlias + strconv.Itoa(i)
i++
v, exist = importPackageAliasMap[alias]
}
}
importPackageAliasMap[alias] = importPackagePath
return alias
}