Skip to content

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 Colors int
const (
	ColorsRed Colors = 0
	ColorsGreen Colors = 1
	ColorsBlue Colors = 2
)
var values_Colors = map[Colors]string{0: "Red", 1: "Green", 2: "Blue"}
var valueValues_Colors = []Colors{0, 1, 2}
var valueNames_Colors = []string{"Red", "Green", "Blue"}
func (v Colors) isDefined() bool {
	_, ok := values_Colors[v]
	return ok
}
func (v Colors) IsAColors() bool {
	return v.isDefined()
}
func (v Colors) String() string {
	name, ok := values_Colors[v]
	if ok {
		return name
	}
	return fmt.Sprintf("Colors(%d)", v)
}
func (v Colors) Title() string {
	if v.IsAColors() {
		return fmt.Sprintf("%s (%d)", v, v)
	}
	return v.String()
}
func ColorsStrings() []string {
	return valueNames_Colors
}
func ColorsValues() []Colors {
	return valueValues_Colors
}

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