/
ast.go
2978 lines (2507 loc) · 77 KB
/
ast.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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ast
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"go/ast"
"go/token"
"io"
"math/rand"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/icrowley/fake"
"github.com/influx6/gobuild/build"
"github.com/influx6/moz/gen"
)
const (
annotationFileFormat = "%s_annotation_%s.%s"
altAnnotationFileFormat = "%s_annotation_%s"
)
// Contains giving sets of variables exposing sytem GOPATH and GOPATHSRC.
var (
goPath = os.Getenv("GOPATH")
goSrcPath = filepath.Join(goPath, "src")
timeLayout = "2006-01-02T15:04:05Z07:00"
spaces = regexp.MustCompile(`/s+`)
itag = regexp.MustCompile(`((\w+):"(\w+|[\w,?\s+\w]+)")`)
annotation = regexp.MustCompile("@(\\w+(:\\w+)?)(\\([.\\s\\S]+\\))?")
ASTTemplatFuncs = map[string]interface{}{
"getTag": GetTag,
"fieldFor": FieldFor,
"getFields": GetFields,
"fieldNameFor": FieldNameFor,
"mapFields": MapOutFields,
"mapValues": MapOutValues,
"fieldByName": FieldByFieldName,
"mapJSON": MapOutFieldsToJSON,
"mapRandomJSON": MapOutFieldsWithRandomValuesToJSON,
"mapFieldTypeJSON": MapOutFieldsWithTypeToJSON,
"mapTypeJSON": MapOutTypeToJSON,
"mapRandomTypeJSON": MapOutTypeToJSONWriterWithRandomValues,
"stringValueFor": ToValueString,
"randomValue": RandomFieldAssign,
"defaultGoType": DefaultGoTypeString,
"defaultValue": AssignDefaultValue,
"randomFieldValue": RandomFieldValue,
"defaultType": DefaultTypeValueString,
"defaultFieldValue": DefaultFieldValue,
}
naturalIdents = map[string]bool{
"string": true,
"bool": true,
"rune": true,
"byte": true,
"int": true,
"int8": true,
"int16": true,
"int32": true,
"int64": true,
"uint": true,
"uint8": true,
"uint32": true,
"uint64": true,
"uintptr": true,
"float32": true,
"float64": true,
"complex128": true,
"complex64": true,
"error": true,
"struct": true,
"interface": true,
"interface{}": true,
"struct{}": true,
}
)
// Packages defines a type to represent a slice of Packages.
type Packages []Package
// TestPackageForFile returns package associated with path.
func (pkgs Packages) TestPackageForFile(path string, targetFile string) (PackageDeclaration, Package, bool) {
pl, ok := pkgs.TestPackageFor(path)
if !ok {
return PackageDeclaration{}, Package{}, ok
}
plDeclr, ok := pl.DeclarationFor(targetFile)
return plDeclr, pl, ok
}
// PackageForFile returns package associated with path.
func (pkgs Packages) PackageForFile(path string, targetFile string) (PackageDeclaration, Package, bool) {
pl, ok := pkgs.PackageFor(path)
if !ok {
return PackageDeclaration{}, Package{}, ok
}
plDeclr, ok := pl.DeclarationFor(targetFile)
return plDeclr, pl, ok
}
// TestPackageFor returns package associated with path for its tests.
func (pkgs Packages) TestPackageFor(path string) (Package, bool) {
for _, pkg := range pkgs {
if pkg.Path == path && strings.HasSuffix(pkg.Tag, "_test") {
return pkg, true
}
}
return Package{}, false
}
// PackageFor returns package associated with path.
func (pkgs Packages) PackageFor(path string) (Package, bool) {
for _, pkg := range pkgs {
if pkg.Path == path && !strings.HasSuffix(pkg.Tag, "_test") {
return pkg, true
}
}
return Package{}, false
}
// ImportDeclaration defines a type to contain import declaration within a package.
type ImportDeclaration struct {
Name string
Path string
Source string
Comments string
InternalPkg bool
}
// Package defines the central repository of all PackageDeclaration.
type Package struct {
Name string
Tag string
Path string
Dir string
FilePath string
Files []string
BuildPkg *build.Package
Packages []PackageDeclaration
TestPackages []PackageDeclaration
}
// HasFunctionFor returns true/false if the giving Struct Declaration has the giving function name.
func (pkg Package) HasFunctionFor(str StructDeclaration, funcName string) bool {
for _, elem := range pkg.Packages {
if elem.HasFunctionFor(str, funcName) {
return true
}
}
return false
}
// PackagesWithAnnotation returns a slice of all PackageDeclaration which have the annotation at package level.
func (pkg Package) PackagesWithAnnotation(name string) []PackageDeclaration {
var pkgs []PackageDeclaration
for _, elem := range pkg.Packages {
if !elem.HasAnnotation(name) {
continue
}
pkgs = append(pkgs, elem)
}
return pkgs
}
// AnnotationFirstFor returns all annotations with the giving name.
func (pkg Package) AnnotationFirstFor(typeName string) (AnnotationDeclaration, PackageDeclaration, bool) {
for _, elem := range pkg.Packages {
if annon, ok := elem.GetAnnotation(typeName); ok {
return annon, elem, true
}
}
return AnnotationDeclaration{}, PackageDeclaration{}, false
}
// HasAnnotation returns true/false if the giving package has any files having
// a giving annotation on the package level.
func (pkg Package) HasAnnotation(name string) bool {
for _, elem := range pkg.Packages {
if elem.HasAnnotation(name) {
return true
}
}
return false
}
// AnnotationsFor returns all annotations with the giving name.
func (pkg Package) AnnotationsFor(typeName string) []AnnotationDeclaration {
var found []AnnotationDeclaration
for _, elem := range pkg.Packages {
found = append(found, elem.AnnotationsFor(typeName)...)
}
return found
}
// FunctionsForName returns a slice of FuncDeclaration for the giving name.
func (pkg Package) FunctionsForName(objName string) []FuncDeclaration {
var funcs []FuncDeclaration
for _, elem := range pkg.Packages {
funcs = append(funcs, elem.FunctionsForName(objName)...)
}
return funcs
}
// ImportFor returns the ImportDeclaration associated with the giving handle.
// Returns error if the import is not found.
func (pkg Package) ImportFor(imp string) (ImportDeclaration, error) {
for _, elem := range pkg.Packages {
if impDeclr, err := elem.ImportFor(imp); err == nil {
return impDeclr, nil
}
}
return ImportDeclaration{}, errors.New("Not found")
}
// FunctionsFor returns a slice of FuncDeclaration for the giving object.
func (pkg Package) FunctionsFor(obj *ast.Object) []FuncDeclaration {
var funcs []FuncDeclaration
for _, elem := range pkg.Packages {
funcs = append(funcs, elem.FunctionsFor(obj)...)
}
return funcs
}
// DeclarationFor returns the associated declaration for the giving file path.
func (pkg Package) DeclarationFor(targetFile string) (PackageDeclaration, bool) {
for _, declr := range pkg.Packages {
if declr.FilePath == targetFile || declr.File == targetFile {
return declr, true
}
}
return PackageDeclaration{}, false
}
// TypeFor returns associated TypeDeclaration for importPath in file with the typeName.
func (pkg Package) TypeFor(typeName string) (TypeDeclaration, bool) {
for _, declr := range pkg.Packages {
for _, elem := range declr.Types {
if elem.Object.Name.Name == typeName {
return elem, true
}
}
}
return TypeDeclaration{}, false
}
// FunctionFor returns associated FuncDeclaration for importPath in file with the typeName.
func (pkg Package) FunctionFor(typeName string) (FuncDeclaration, bool) {
for _, declr := range pkg.Packages {
for _, elem := range declr.Functions {
if elem.FuncDeclr.Name.Name == typeName {
return elem, true
}
}
}
return FuncDeclaration{}, false
}
// StructFor returns associated StructDeclaration for importPath in file with the typeName.
func (pkg Package) StructFor(typeName string) (StructDeclaration, bool) {
for _, declr := range pkg.Packages {
for _, elem := range declr.Structs {
if elem.Object.Name.Name == typeName {
return elem, true
}
}
}
return StructDeclaration{}, false
}
// InterfaceFor returns associated InterfaceDeclaration for importPath in file with the typeName.
func (pkg Package) InterfaceFor(typeName string) (InterfaceDeclaration, bool) {
for _, declr := range pkg.Packages {
for _, elem := range declr.Interfaces {
if elem.Object.Name.Name == typeName {
return elem, true
}
}
}
return InterfaceDeclaration{}, false
}
//===========================================================================================================
// PackageDeclaration defines a type which holds details relating to annotations declared on a
// giving package.
type PackageDeclaration struct {
Package string
Path string
Dir string
FilePath string
File string
Source string
Comments []string
Imports map[string]ImportDeclaration
ImportedPackages map[string]Package
Annotations []AnnotationDeclaration
Types []TypeDeclaration
Structs []StructDeclaration
Interfaces []InterfaceDeclaration
Functions []FuncDeclaration
Variables []VariableDeclaration
ObjectFunc map[*ast.Object][]FuncDeclaration
importedloaded bool
}
// HasFunctionFor returns true/false if the giving Struct Declaration has the giving function name.
func (pkg PackageDeclaration) HasFunctionFor(str StructDeclaration, funcName string) bool {
functions := Functions(pkg.FunctionsFor(str.Object.Name.Obj))
if _, err := functions.Find(funcName); err != nil {
return false
}
return true
}
// ImportedPackageFor returns the Package for a giving imported package based on the package name
// or aliased used in the package for this declaration.
func (pkg PackageDeclaration) ImportedPackageFor(packageName string) (Package, bool) {
pkgPath, ok := pkg.Imports[packageName]
if !ok {
var found bool
for _, imp := range pkg.Imports {
if imp.Name == packageName {
found = true
pkgPath = imp
break
}
}
if !found {
return Package{}, false
}
}
pkgItem, ok := pkg.ImportedPackages[pkgPath.Path]
return pkgItem, ok
}
// HasAnnotation returns true/false if giving PackageDeclaration has annotation at package level.
func (pkg PackageDeclaration) HasAnnotation(typeName string) bool {
typeName = strings.TrimPrefix(typeName, "@")
for _, item := range pkg.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
return true
}
return false
}
// GetAnnotation returns the first annotation with the giving name.
func (pkg PackageDeclaration) GetAnnotation(typeName string) (AnnotationDeclaration, bool) {
typeName = strings.TrimPrefix(typeName, "@")
for _, item := range pkg.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
return item, true
}
return AnnotationDeclaration{}, false
}
// AnnotationsFor returns all annotations with the giving name.
func (pkg PackageDeclaration) AnnotationsFor(typeName string) []AnnotationDeclaration {
typeName = strings.TrimPrefix(typeName, "@")
var found []AnnotationDeclaration
for _, item := range pkg.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
found = append(found, item)
}
return found
}
// FunctionsForName returns a slice of FuncDeclaration for the giving name.
func (pkg PackageDeclaration) FunctionsForName(objName string) []FuncDeclaration {
var funcs []FuncDeclaration
for obj, list := range pkg.ObjectFunc {
if obj.Name != objName {
continue
}
funcs = append(funcs, list...)
}
return funcs
}
// ImportFor returns the ImportDeclaration associated with the giving handle.
// Returns error if the import is not found.
func (pkg PackageDeclaration) ImportFor(imp string) (ImportDeclaration, error) {
impDeclr, ok := pkg.Imports[imp]
if !ok {
return ImportDeclaration{}, errors.New("Not found")
}
return impDeclr, nil
}
// FunctionFor returns associated FuncDeclaration with giving name.
func (pkg PackageDeclaration) FunctionFor(typeName string) (FuncDeclaration, bool) {
for _, typed := range pkg.Functions {
if typed.FuncDeclr.Name.Name == typeName {
return typed, true
}
}
return FuncDeclaration{}, false
}
// TypeFor returns associated TypeDeclaration associated with name.
func (pkg PackageDeclaration) TypeFor(typeName string) (TypeDeclaration, bool) {
for _, typed := range pkg.Types {
if typed.Object.Name.Name == typeName {
return typed, true
}
}
return TypeDeclaration{}, false
}
// InterfaceFor returns associated InterfaceDeclaration associated with name.
func (pkg PackageDeclaration) InterfaceFor(intrName string) (InterfaceDeclaration, bool) {
for _, inter := range pkg.Interfaces {
if inter.Object.Name.Name == intrName {
return inter, true
}
}
return InterfaceDeclaration{}, false
}
// StructFor returns associated StructDeclaration associated with name.
func (pkg PackageDeclaration) StructFor(structName string) (StructDeclaration, bool) {
for _, structd := range pkg.Structs {
if structd.Object.Name.Name == structName {
return structd, true
}
}
return StructDeclaration{}, false
}
// FunctionsFor returns a slice of FuncDeclaration for the giving object.
func (pkg PackageDeclaration) FunctionsFor(obj *ast.Object) []FuncDeclaration {
if funcs, ok := pkg.ObjectFunc[obj]; ok {
return funcs
}
return pkg.FunctionsForName(obj.Name)
}
// MethodFor returns associated FuncDeclaration with has struct declaration has receiver.
func (pkg PackageDeclaration) MethodFor(structName string) ([]FuncDeclaration, bool) {
for obj, set := range pkg.ObjectFunc {
if obj.Name != structName {
continue
}
return set, true
}
return nil, false
}
//===========================================================================================================
// VariableDeclaration defines a type which holds annotation data for a giving variable declaration.
type VariableDeclaration struct {
From int
Length int
Package string
Path string
Name string
NameWithPackage string
NameIdent *ast.Ident
FilePath string
Source string
Comments string
File string
Position token.Pos
Object *ast.ValueSpec
GenObj *ast.GenDecl
Declr *PackageDeclaration
Annotations []AnnotationDeclaration
Associations map[string]AnnotationAssociationDeclaration
}
// StructDeclaration defines a type which holds annotation data for a giving struct type declaration.
type StructDeclaration struct {
From int
Length int
Package string
Name string
NameWithPackage string
Path string
FilePath string
Source string
Comments string
File string
Struct *ast.StructType
Object *ast.TypeSpec
GenObj *ast.GenDecl
Position token.Pos
Declr *PackageDeclaration
Annotations []AnnotationDeclaration
Associations map[string]AnnotationAssociationDeclaration
}
// Fields returns a slice containing all fields of giving struct.
// If struct has no associated PackageDeclaration, error is returned.
func (str StructDeclaration) Fields() ([]FieldDeclaration, error) {
if str.Declr == nil {
return nil, errors.New("not possible")
}
return GetFields(str, str.Declr), nil
}
// AnnotationsFor returns all annotations with the giving name.
func (str StructDeclaration) AnnotationsFor(typeName string) []AnnotationDeclaration {
typeName = strings.TrimPrefix(typeName, "@")
var found []AnnotationDeclaration
for _, item := range str.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
found = append(found, item)
}
return found
}
// TypeDeclaration defines a type which holds annotation data for a giving type declaration.
type TypeDeclaration struct {
From int
Length int
Package string
Name string
NameWithPackage string
Path string
FilePath string
Source string
Comments string
File string
Aliased bool
Type *ast.Ident
TypeInfo *ArgType
AliasedType *ast.Object
AliasedTypeSpec *ast.TypeSpec
Object *ast.TypeSpec
GenObj *ast.GenDecl
Position token.Pos
Declr *PackageDeclaration
Annotations []AnnotationDeclaration
Associations map[string]AnnotationAssociationDeclaration
}
// AnnotationsFor returns all annotations with the giving name.
func (ty TypeDeclaration) AnnotationsFor(typeName string) []AnnotationDeclaration {
typeName = strings.TrimPrefix(typeName, "@")
var found []AnnotationDeclaration
for _, item := range ty.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
found = append(found, item)
}
return found
}
//===========================================================================================================
// FuncDeclaration defines a type used to annotate a giving type declaration
// associated with a ast for a function.
type FuncDeclaration struct {
From int
Length int
Package string
Path string
FilePath string
Exported bool
File string
FuncName string
FuncNameWithPackage string
RecieverName string
Source string
Comments string
Position token.Pos
TypeDeclr ast.Decl
FuncDeclr *ast.FuncDecl
Type *ast.FuncType
Reciever *ast.Object
RecieverIdent *ast.Ident
RecieverPointer *ast.StarExpr
FuncType *ast.FieldList
Returns *ast.FieldList
Arguments *ast.FieldList
Declr *PackageDeclaration
Annotations []AnnotationDeclaration
Associations map[string]AnnotationAssociationDeclaration
}
// AnnotationsFor returns all annotations with the giving name.
func (fun FuncDeclaration) AnnotationsFor(typeName string) []AnnotationDeclaration {
typeName = strings.TrimPrefix(typeName, "@")
var found []AnnotationDeclaration
for _, item := range fun.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
found = append(found, item)
}
return found
}
// GetAnnotation returns AnnotationDeclaration if giving FuncDeclaration has annotation at package level.
func (fun FuncDeclaration) GetAnnotation(typeName string) (AnnotationDeclaration, bool) {
typeName = strings.TrimPrefix(typeName, "@")
for _, item := range fun.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
return item, true
}
return AnnotationDeclaration{}, false
}
// HasAnnotation returns true/false if giving FuncDeclaration has annotation at package level.
func (fun FuncDeclaration) HasAnnotation(typeName string) bool {
typeName = strings.TrimPrefix(typeName, "@")
for _, item := range fun.Annotations {
if strings.TrimPrefix(item.Name, "@") != typeName {
continue
}
return true
}
return false
}
// Definition returns a FunctionDefinition for this function.
func (fun FuncDeclaration) Definition(pkg *PackageDeclaration) (FunctionDefinition, error) {
return GetFunctionDefinitionFromDeclaration(fun, pkg)
}
// Functions defines a slice of FuncDeclaration.
type Functions []FuncDeclaration
// Find returns the giving Function of the giving name.
func (fnList Functions) Find(name string) (FuncDeclaration, error) {
for _, fn := range fnList {
if fn.FuncName == name {
return fn, nil
}
}
return FuncDeclaration{}, fmt.Errorf("Function with %q not found", name)
}
//===========================================================================================================
// AnnotationAssociationDeclaration defines a type which defines an association between
// a giving annotation and a series of values.
type AnnotationAssociationDeclaration struct {
Annotation string
Action string
Template string
TypeName string
Record AnnotationDeclaration
}
// InterfaceDeclaration defines a type which holds annotation data for a giving interface type declaration.
type InterfaceDeclaration struct {
From int
Length int
Package string
Path string
Name string
NameWithPackage string
Source string
Comments string
FilePath string
File string
Interface *ast.InterfaceType
Object *ast.TypeSpec
GenObj *ast.GenDecl
Position token.Pos
Declr *PackageDeclaration
methods []FunctionDefinition
Annotations []AnnotationDeclaration
Associations map[string]AnnotationAssociationDeclaration
}
// GetImports returns a map containing all import paths related to
// types used for the methods of giving interface.
func (i *InterfaceDeclaration) GetImports(pkg *PackageDeclaration, internal bool) map[string]string {
imports := make(map[string]string, 0)
methods := i.Methods(pkg)
for _, method := range methods {
// Retrieve all import paths for arguments.
func(args []ArgType) {
for _, argument := range args {
if argument.Import2.InternalPkg && !internal {
continue
}
if argument.Import2.Path != "" {
if argument.Import2.InternalPkg && !internal {
continue
}
imports[argument.Import2.Path] = argument.Import2.Name
}
if argument.Import.Path != "" {
if argument.Import.InternalPkg && !internal {
continue
}
imports[argument.Import.Path] = argument.Import.Name
}
}
}(method.Args)
// Retrieve all import paths for returns.
func(args []ArgType) {
for _, argument := range args {
if argument.Import2.Path != "" {
if argument.Import2.InternalPkg && !internal {
continue
}
imports[argument.Import2.Path] = argument.Import2.Name
}
if argument.Import.Path != "" {
if argument.Import.InternalPkg && !internal {
continue
}
imports[argument.Import.Path] = argument.Import.Name
}
}
}(method.Returns)
}
return imports
}
// GetMethods returns the associated methods for the giving interface.
func (i *InterfaceDeclaration) Methods(pkg *PackageDeclaration) []FunctionDefinition {
if len(i.methods) == 0 {
i.methods = GetInterfaceFunctions(i.Interface, pkg)
}
return i.methods
}
// ArgType defines a type to represent the information for a giving functions argument or
// return type declaration.
type ArgType struct {
Owner string
Name string
Type string
ExType string
IsReturn bool
FromMethod bool
Package string
IsStruct bool
BaseType bool
Import ImportDeclaration
Import2 ImportDeclaration
NameObject *ast.Object
TypeObject *ast.Object
StructObject *ast.StructType
Spec *ast.TypeSpec
InterfaceObject *ast.InterfaceType
ImportedObject *ast.SelectorExpr
SelectPackage *ast.Ident
SelectObject *ast.Ident
ArrayType *ast.ArrayType
MapType *ast.MapType
ChanType *ast.ChanType
PointerType *ast.StarExpr
IdentType *ast.Ident
Tags []TagDeclaration
Pkg *PackageDeclaration
}
//type ArgsBasic struct {
// Owner string
// Name string
// Type string
// ExType string
// IsReturn bool
// FromMethod bool
// IsStruct bool
// IsSlice bool
// IsMap bool
//}
//
//func (a ArgType) GetBasics() ArgBasics {
//
//}
func (a ArgType) GetStructJSON() string {
if a.StructObject == nil {
return ""
}
str := StructDeclaration{
Object: a.Spec,
Struct: a.StructObject,
Name: a.Type,
Declr: a.Pkg,
NameWithPackage: fmt.Sprintf("%s.%s", a.Package, a.Type),
}
res, err := MapOutFieldsToJSON(str, "json", "")
if err != nil {
return ""
}
return res
}
// FunctionDefinition defines a type to represent the function/method declarations of an
// interface type.
type FunctionDefinition struct {
Name string
Args []ArgType
Returns []ArgType
Func *ast.FuncType
Interface *ast.InterfaceType
Struct *ast.StructType
}
// TotalReturns returns length of function return set.
func (fd FunctionDefinition) TotalReturns() int {
return len(fd.Returns)
}
// TotalArgs returns length of function arg set.
func (fd FunctionDefinition) TotalArgs() int {
return len(fd.Args)
}
// HasReturns returns true/false if giving function has return types.
func (fd FunctionDefinition) HasReturns() bool {
return len(fd.Returns) != 0
}
// HasArgs returns true/false if giving function has aarguments.
func (fd FunctionDefinition) HasArgs() bool {
return len(fd.Args) != 0
}
// GetReturnsAt gets returns ArgType at index point.
func (fd FunctionDefinition) GetReturnsAt(i int) ArgType {
if i >= len(fd.Returns) {
return ArgType{}
}
return fd.Returns[i]
}
// GetArgsAt gets argument ArgType at index point.
func (fd FunctionDefinition) GetArgsAt(i int) ArgType {
if i >= len(fd.Args) {
return ArgType{}
}
return fd.Args[i]
}
// ReturnTypePos returns position of giving type if part of
// the function's return types else returning -1.
func (fd FunctionDefinition) ReturnTypePos(wanted string) int {
for index, arg := range fd.Returns {
if arg.ExType == wanted {
return index
}
}
return -1
}
// ArgTypePos returns position of giving type if part of
// the function's argument types else returning -1.
func (fd FunctionDefinition) ArgTypePos(wanted string) int {
for index, arg := range fd.Args {
if arg.ExType == wanted {
return index
}
}
return -1
}
// CountOfReturnType counts total number of giving type in the returns lists
// of function.
func (fd FunctionDefinition) CountOfReturnType(wanted string) int {
var count int
for _, arg := range fd.Returns {
if arg.ExType == wanted {
count++
}
}
return count
}
// CountOfArgType counts total number of giving type in the arguments lists
// of function.
func (fd FunctionDefinition) CountOfArgType(wanted string) int {
var count int
for _, arg := range fd.Args {
if arg.ExType == wanted {
count++
}
}
return count
}
// HasNoReturnType returns true/false if giving type is not part of
// the function's return types.
func (fd FunctionDefinition) HasNoReturnType(wanted string) bool {
for _, arg := range fd.Returns {
if arg.ExType == wanted {
return false
}
}
return true
}
// HasNoArgType returns true/false if giving type is part of
// the function's arguments types.
func (fd FunctionDefinition) HasNoArgType(wanted string) bool {
for _, arg := range fd.Args {
if arg.ExType == wanted {
return false
}
}
return true
}
// HasReturnType returns true/false if giving type is part of
// the function's return types.
func (fd FunctionDefinition) HasReturnType(wanted string) bool {
for _, arg := range fd.Returns {
if arg.ExType == wanted {
return true
}
}
return false
}
// HasArgType returns true/false if giving type is part of
// the function's arguments types.
func (fd FunctionDefinition) HasArgType(wanted string) bool {