package gen import ( "fmt" "sort" "strings" "sources.truenas.cloud/code/avo/internal/api" "sources.truenas.cloud/code/avo/internal/inst" ) // Enum is a generated enumeration type. This assists with mapping between the // conceptual values of the enum, and it's materialization as Go code. type Enum struct { name string doc []string values []string } // NewEnum initializes an empty enum type with the given name. func NewEnum(name string) *Enum { return &Enum{name: name} } // Name returns the type name. func (e *Enum) Name() string { return e.name } // Receiver returns the receiver variable name. func (e *Enum) Receiver() string { return strings.ToLower(e.name[:1]) } // SetDoc sets type documentation, as a list of lines. func (e *Enum) SetDoc(doc ...string) { e.doc = doc } // Doc returns the type documentation. func (e *Enum) Doc() []string { return e.doc } // AddValue adds a named enumerator. func (e *Enum) AddValue(value string) { e.values = append(e.values, value) } // Values returns all enumerators. func (e *Enum) Values() []string { return e.values } // None returns the name of the "unset" constant of this enumeration. func (e *Enum) None() string { return e.ConstName("None") } // ConstName returns the constant name that refers to the given enumerator // value. func (e *Enum) ConstName(value string) string { return e.name + value } // ConstNames returns the constant names for all enumerator values. func (e *Enum) ConstNames() []string { var consts []string for _, v := range e.values { consts = append(consts, e.ConstName(v)) } return consts } // MaxName returns the name of the constant that represents the maximum // enumerator. This value is placed at the very end of the enum, so all values // will be between the None and Max enumerators. func (e *Enum) MaxName() string { return strings.ToLower(e.ConstName("max")) } // Max returns the value of the maximum enumerator. func (e *Enum) Max() int { return len(e.values) } // UnderlyingType returns the underlying unsigned integer type used for the // enumeration. This will be the smallest type that can represent all the // values. func (e *Enum) UnderlyingType() string { b := uint(8) for ; b < 64 && e.Max() > ((1<