Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Add helpers for defined enum types #316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kendallgoto
Copy link

@kendallgoto kendallgoto commented Mar 6, 2025

Adds various basic helpers to compiled enums, including

  • enumValue.String() -> prints enum instance name
  • enumValue.IsAEnumType() -> true if enumValue is valid instance
  • EnumTypeStrings() -> []string of enum value names
  • EnumTypeValues() -> []EnumType array of enum values

Based on the enum definitions typically provided by Golang enum helpers, such as dmarkham/enumer

Given a simple example such as:

meta:
  id: demo
enums:
  colors:
    0: red
    1: green
    2: blue

Previously, the Go implementation would generate a new Enum type for colors, and appropriately named constants for the values. However, it did not generate any exposed methods related to this type or attached to it, preventing the use of basic operations such as converting the enum value to a readable string, validating enum bounds, etc:

type Demo_Colors int
const (
	Demo_Colors__Red Demo_Colors = 0
	Demo_Colors__Green Demo_Colors = 1
	Demo_Colors__Blue Demo_Colors = 2
)
var values_Demo_Colors = map[Demo_Colors]struct{}{0: {}, 1: {}, 2: {}}
func (v Demo_Colors) isDefined() bool {
	_, ok := values_Demo_Colors[v]
	return ok
}

With this change, additional helper methods are automatically generated for each enum, allowing more flexibility for their usage.

fmt.Println("Name of color:", Demo_Colors__Red) // Name of color: Red
fmt.Println("Name of unknown color:", Demo_Colors(10)) // Name of unknown color: Demo_Colors(10)
fmt.Printf("Value of color: %d\n", Demo_Colors__Red) // Value of color: 0
fmt.Println("Valid color:", Demo_Colors__Red.IsADemo_Colors()) // Valid color: true
fmt.Println("Valid color:", Demo_Colors(10).IsADemo_Colors()) // Valid color: false
fmt.Println("All known colors:", Demo_ColorsStrings()) // All known colors: [Red Green Blue]

The specific generated code in this code expands as such:

type Demo_Colors int
const (
	Demo_Colors__Red Demo_Colors = 0
	Demo_Colors__Green Demo_Colors = 1
	Demo_Colors__Blue Demo_Colors = 2
)
var valueNames_Demo_Colors = map[Demo_Colors]string{0: "Red", 1: "Green", 2: "Blue"}
func (v Demo_Colors) isDefined() bool {
	_, ok := valueNames_Demo_Colors[v]
	return ok
}
func (v Demo_Colors) IsADemo_Colors() bool {
	return v.isDefined()
}
func (v Demo_Colors) String() string {
	name, ok := valueNames_Demo_Colors[v]
	if ok {
		return name
	}
	return fmt.Sprintf("Demo_Colors(%d)", v)
}
func Demo_ColorsStrings() []string {
	strings := make([]string, 0, len(valueNames_Demo_Colors))
	for _, name := range valueNames_Demo_Colors {
		strings = append(strings, name)
	}
	return strings
}
func Demo_ColorsValues() []Demo_Colors {
	values := make([]Demo_Colors, 0, len(valueNames_Demo_Colors))
	for value := range valueNames_Demo_Colors {
		values = append(values, value)
	}
	return values
}

This implementation makes generated enums a lot more versatile and replicates some of the functionality e.g. of IntEnum in the compiled python3 code, which allows you to iterate over a list of defined enums, and convert enums to their string name.

Adds basic string conversion / lookup helpers for compiled enums

Signed-off-by: Kendall Goto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant