Skip to content

Commit

Permalink
envoyproxy#GH-585 fix generated enum type name
Browse files Browse the repository at this point in the history
Signed-off-by: Elliot Jackson <[email protected]>
  • Loading branch information
Elliot Jackson committed Sep 15, 2022
1 parent b12d42f commit 64bd88a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion templates/go/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
_ = sort.Sort
{{ range $pkg, $path := enumPackages (externalEnums .) }}
_ = {{ $pkg }}.{{ enumName (index (externalEnums $) 0) }}(0)
_ = {{ $pkg }}.{{ enumType (index (externalEnums $) 0) }}(0)
{{ end }}
)
Expand Down
20 changes: 20 additions & 0 deletions templates/goshared/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Register(tpl *template.Template, params pgs.Parameters) {
"externalEnums": fns.externalEnums,
"enumName": fns.enumName,
"enumPackages": fns.enumPackages,
"enumType": fns.enumType,
})

template.Must(tpl.New("msg").Parse(msgTpl))
Expand Down Expand Up @@ -365,6 +366,25 @@ func (fns goSharedFuncs) enumPackages(enums []pgs.Enum) map[pgs.Name]pgs.FilePat
return out
}

// recursively walks up the parents of the enum until it reaches the root file
// prepends 'Message_' for each message it sees on the way. This builds up the generated enum type name.
//
// intended to be used by the enumType function to build the generate type name.
func walkEnumParents(parent pgs.ParentEntity, name string) string {
switch t := parent.(type) {
case pgs.Message:
return walkEnumParents(t.Parent(), string(t.Name())+"_"+name)
case pgs.File:
return name
default:
panic("enum parent can only resolve to message or file")
}
}

func (fns goSharedFuncs) enumType(enum pgs.Enum) string {
return walkEnumParents(enum.Parent(), string(enum.Name()))
}

func (fns goSharedFuncs) snakeCase(name string) string {
return strcase.ToSnake(name)
}

0 comments on commit 64bd88a

Please sign in to comment.