genbindings/go: simpler enum naming

This commit is contained in:
mappu 2024-09-20 19:13:15 +12:00
parent d0e28fa69c
commit 6a958309b0

View File

@ -405,8 +405,18 @@ import "C"
continue // Removed by transformRedundant AST pass continue // Removed by transformRedundant AST pass
} }
// Fully qualified name of the enum itself
goEnumName := cabiClassName(e.EnumName) goEnumName := cabiClassName(e.EnumName)
// Shorter name, so that enum elements are reachable from the surrounding
// namespace
// Strip back one single :: pair from the generated variable name
nameParts := strings.Split(e.EnumName, `::`)
if len(nameParts) > 1 {
nameParts = nameParts[0 : len(nameParts)-1]
}
goEnumShortName := cabiClassName(strings.Join(nameParts, `::`))
ret.WriteString(` ret.WriteString(`
type ` + goEnumName + ` ` + e.UnderlyingType.RenderTypeGo() + ` type ` + goEnumName + ` ` + e.UnderlyingType.RenderTypeGo() + `
`) `)
@ -416,7 +426,7 @@ import "C"
ret.WriteString("const (\n") ret.WriteString("const (\n")
for _, ee := range e.Entries { for _, ee := range e.Entries {
ret.WriteString(cabiClassName(goEnumName+"::"+ee.EntryName) + " " + goEnumName + " = " + ee.EntryValue + "\n") ret.WriteString(titleCase(cabiClassName(goEnumShortName+"::"+ee.EntryName)) + " " + goEnumName + " = " + ee.EntryValue + "\n")
} }
ret.WriteString("\n)\n\n") ret.WriteString("\n)\n\n")