@@ -28,25 +28,25 @@ func NewCodeGenerator() *CodeGenerator {
28
28
return & CodeGenerator {}
29
29
}
30
30
31
- func (g * CodeGenerator ) Generate (file * resolver.File , enums [] * resolver. Enum ) ([]byte , error ) {
31
+ func (g * CodeGenerator ) Generate (file * resolver.File ) ([]byte , error ) {
32
32
tmpl , err := loadTemplate ()
33
33
if err != nil {
34
34
return nil , err
35
35
}
36
- return generateGoContent (tmpl , NewFile (file , enums ))
36
+ return generateGoContent (tmpl , NewFile (file ))
37
37
}
38
38
39
39
type File struct {
40
40
* resolver.File
41
- enums [] * resolver.Enum
42
- pkgMap map [* resolver.GoPackage ]struct {}
41
+ pkgMap map [ * resolver.GoPackage ] struct {}
42
+ aliasMap map [* resolver.GoPackage ]string
43
43
}
44
44
45
- func NewFile (file * resolver.File , enums [] * resolver. Enum ) * File {
45
+ func NewFile (file * resolver.File ) * File {
46
46
return & File {
47
- File : file ,
48
- pkgMap : make (map [* resolver.GoPackage ]struct {}),
49
- enums : enums ,
47
+ File : file ,
48
+ pkgMap : make (map [* resolver.GoPackage ]struct {}),
49
+ aliasMap : make ( map [ * resolver. GoPackage ] string ) ,
50
50
}
51
51
}
52
52
@@ -608,21 +608,22 @@ func (f *File) Imports() []*Import {
608
608
if _ , exists := importPathMap [pkg .ImportPath ]; exists {
609
609
return
610
610
}
611
+ alias := pkg .Name
611
612
if _ , exists := importAliasMap [pkg .Name ]; exists {
612
613
// conflict alias name
613
614
suffixIndex := 1 // start from 1.
614
615
for {
615
- alias : = pkg .Name + fmt .Sprint (suffixIndex )
616
+ alias = pkg .Name + fmt .Sprint (suffixIndex )
616
617
if _ , exists := importAliasMap [alias ]; ! exists {
617
- pkg .Name = alias
618
618
break
619
619
}
620
620
suffixIndex ++
621
621
}
622
622
}
623
+ f .aliasMap [pkg ] = alias
623
624
imports = append (imports , & Import {
624
625
Path : pkg .ImportPath ,
625
- Alias : pkg . Name ,
626
+ Alias : alias ,
626
627
Used : true ,
627
628
})
628
629
importPathMap [pkg .ImportPath ] = struct {}{}
@@ -644,15 +645,23 @@ func (f *File) Imports() []*Import {
644
645
return imports
645
646
}
646
647
648
+ func (f * File ) getAlias (pkg * resolver.GoPackage ) string {
649
+ alias , exists := f .aliasMap [pkg ]
650
+ if exists {
651
+ return alias
652
+ }
653
+ return pkg .Name
654
+ }
655
+
647
656
type Enum struct {
648
657
ProtoName string
649
658
GoName string
650
659
EnumAttribute * EnumAttribute
651
660
}
652
661
653
662
func (f * File ) Enums () []* Enum {
654
- ret := make ( []* Enum , 0 , len ( f . enums ))
655
- for _ , enum := range f .enums {
663
+ var enums []* Enum
664
+ for _ , enum := range f .AllEnumsIncludeDeps () {
656
665
protoName := enum .FQDN ()
657
666
// ignore standard library's enum.
658
667
if strings .HasPrefix (protoName , "google." ) {
@@ -661,7 +670,6 @@ func (f *File) Enums() []*Enum {
661
670
if strings .HasPrefix (protoName , "grpc.federation." ) {
662
671
continue
663
672
}
664
- // f.enums contain all the enums defined in the package
665
673
// Currently Enums are used only from a File contains Services.
666
674
if len (f .File .Services ) != 0 {
667
675
f .pkgMap [enum .GoPackage ()] = struct {}{}
@@ -680,14 +688,13 @@ func (f *File) Enums() []*Enum {
680
688
})
681
689
}
682
690
}
683
-
684
- ret = append (ret , & Enum {
691
+ enums = append (enums , & Enum {
685
692
ProtoName : protoName ,
686
693
GoName : f .enumTypeToText (enum ),
687
694
EnumAttribute : enumAttr ,
688
695
})
689
696
}
690
- return ret
697
+ return enums
691
698
}
692
699
693
700
type EnumAttribute struct {
@@ -895,11 +902,12 @@ func (s *Service) ServiceName() string {
895
902
}
896
903
897
904
func (s * Service ) PackageName () string {
898
- return s .GoPackage (). Name
905
+ return s .file . getAlias ( s . GoPackage ())
899
906
}
900
907
901
908
type ServiceDependency struct {
902
909
* resolver.ServiceDependency
910
+ file * File
903
911
}
904
912
905
913
func (dep * ServiceDependency ) ServiceName () string {
@@ -923,15 +931,15 @@ func (dep *ServiceDependency) PrivateClientName() string {
923
931
func (dep * ServiceDependency ) ClientType () string {
924
932
return fmt .Sprintf (
925
933
"%s.%sClient" ,
926
- dep .Service .GoPackage (). Name ,
934
+ dep .file . getAlias ( dep . Service .GoPackage ()) ,
927
935
dep .Service .Name ,
928
936
)
929
937
}
930
938
931
939
func (dep * ServiceDependency ) ClientConstructor () string {
932
940
return fmt .Sprintf (
933
941
"%s.New%sClient" ,
934
- dep .Service .GoPackage (). Name ,
942
+ dep .file . getAlias ( dep . Service .GoPackage ()) ,
935
943
dep .Service .Name ,
936
944
)
937
945
}
@@ -940,7 +948,10 @@ func (s *Service) ServiceDependencies() []*ServiceDependency {
940
948
deps := s .Service .ServiceDependencies ()
941
949
ret := make ([]* ServiceDependency , 0 , len (deps ))
942
950
for _ , dep := range deps {
943
- ret = append (ret , & ServiceDependency {dep })
951
+ ret = append (ret , & ServiceDependency {
952
+ ServiceDependency : dep ,
953
+ file : s .file ,
954
+ })
944
955
}
945
956
return ret
946
957
}
@@ -1198,7 +1209,7 @@ func (f *File) messageTypeToText(msg *resolver.Message) string {
1198
1209
if f .GoPackage .ImportPath == msg .GoPackage ().ImportPath {
1199
1210
return fmt .Sprintf ("*%s" , name )
1200
1211
}
1201
- return fmt .Sprintf ("*%s.%s" , msg .GoPackage (). Name , name )
1212
+ return fmt .Sprintf ("*%s.%s" , f . getAlias ( msg .GoPackage ()) , name )
1202
1213
}
1203
1214
1204
1215
func (f * File ) enumTypeToText (enum * resolver.Enum ) string {
@@ -1215,7 +1226,7 @@ func (f *File) enumTypeToText(enum *resolver.Enum) string {
1215
1226
if f .GoPackage .ImportPath == enum .GoPackage ().ImportPath {
1216
1227
return name
1217
1228
}
1218
- return fmt .Sprintf ("%s.%s" , enum .GoPackage (). Name , name )
1229
+ return fmt .Sprintf ("%s.%s" , f . getAlias ( enum .GoPackage ()) , name )
1219
1230
}
1220
1231
1221
1232
type LogValue struct {
@@ -2258,7 +2269,7 @@ func (f *CastField) ToStruct() *CastStruct {
2258
2269
}
2259
2270
name := strings .Join (names , "_" )
2260
2271
if f .service .GoPackage ().ImportPath != toMsg .GoPackage ().ImportPath {
2261
- name = fmt .Sprintf ("%s.%s" , toMsg .GoPackage (). Name , name )
2272
+ name = fmt .Sprintf ("%s.%s" , f . file . getAlias ( toMsg .GoPackage ()) , name )
2262
2273
}
2263
2274
2264
2275
return & CastStruct {
@@ -2328,7 +2339,7 @@ func (f *CastField) ToOneof() *CastOneof {
2328
2339
name += "_"
2329
2340
}
2330
2341
if f .service .GoPackage ().ImportPath != msg .GoPackage ().ImportPath {
2331
- name = fmt .Sprintf ("%s.%s" , msg .GoPackage (). Name , name )
2342
+ name = fmt .Sprintf ("%s.%s" , f . file . getAlias ( msg .GoPackage ()) , name )
2332
2343
}
2333
2344
return & CastOneof {
2334
2345
Name : name ,
@@ -3075,7 +3086,7 @@ func (d *VariableDefinition) RequestType() string {
3075
3086
case expr .Call != nil :
3076
3087
request := expr .Call .Request
3077
3088
return fmt .Sprintf ("%s.%s" ,
3078
- request .Type .GoPackage (). Name ,
3089
+ d . file . getAlias ( request .Type .GoPackage ()) ,
3079
3090
util .ToPublicGoVariable (request .Type .Name ),
3080
3091
)
3081
3092
case expr .Message != nil :
@@ -3099,7 +3110,7 @@ func (d *VariableDefinition) ReturnType() string {
3099
3110
case expr .Call != nil :
3100
3111
response := expr .Call .Method .Response
3101
3112
return fmt .Sprintf ("%s.%s" ,
3102
- response .GoPackage (). Name ,
3113
+ d . file . getAlias ( response .GoPackage ()) ,
3103
3114
response .Name ,
3104
3115
)
3105
3116
case expr .Message != nil :
@@ -3673,7 +3684,7 @@ func toEnumValuePrefix(file *File, typ *resolver.Type) string {
3673
3684
if file .GoPackage .ImportPath == enum .GoPackage ().ImportPath {
3674
3685
return name
3675
3686
}
3676
- return fmt .Sprintf ("%s.%s" , enum .GoPackage (). Name , name )
3687
+ return fmt .Sprintf ("%s.%s" , file . getAlias ( enum .GoPackage ()) , name )
3677
3688
}
3678
3689
3679
3690
func toEnumValueText (enumValuePrefix string , value string ) string {
0 commit comments