Documentation ¶
Overview ¶
A struct based golang enum package that handles stringifying, marshalling, and unmarshalling. All examples will be built from the following example types
type CurrencyCodes struct { enum.Enum USD enum.Const Custom enum.Const `enum:"CUSTOM"` } type Money struct { CurrencyCode CurrencyCode `json:"currency_code"` Amount int `json:"amount"` }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Instantiates the enum if that hasn't been done and validates that its current value is valid. Commonly used after unmarshalling an enum like so
func main() { var money Money json.Unmarshal([]byte("{\"currency_code\":\"USD\",\"amount\":5}"), &money) enum.Validate(&money.CurrencyCode) // <-- Must be run after unmarshal fmt.Println(money) // Prints "{USD 5}" }
Types ¶
type Const ¶
type Const string
The base value for all Enum fields. The name of the field on the enum struct will be the default value for the enum const. For example The value of the USD const is "USD". In order to customize this value, add the tag enum:"<name>" for example
type CurrencyCodes struct { enum.Enum USD enum.Const `enum:"not usd"` }
The value of the USD const is now "not usd"
type Enum ¶
type Enum struct {
// contains filtered or unexported fields
}
The base type for all Enums. Stores the current value and keeps track of all valid values.
func (Enum) MarshalJSON ¶
func (*Enum) UnmarshalJSON ¶
Unmarshalls the string into an Enum. NOTE: you must run enum.Validate after unmarshalling a string like so
func main() { var money Money json.Unmarshal([]byte("{\"currency_code\":\"USD\",\"amount\":5}"), &money) enum.Validate(&money.CurrencyCode) // <-- Must be run after unmarshal fmt.Println(money) // Prints "{USD 5}" }
type Enummer ¶
type Enummer interface { Get() Const Set(c Const) error MustSet(c Const) GetAll() []Const // contains filtered or unexported methods }
func Construct ¶
Instantiates an Enum with the provided value. If the value is invalid, an error is returned otherwise, an Enummer is returned with a nil error
cc, err := enum.Construct(new(CurrencyCodes), enum.Const("USD")) if err != nil { panic(err) } cc = cc.(*CurrencyCodes)
func MustConstruct ¶
Instantiates an Enum with the provided value. If the value is invalid, a panic occurs
cc := enum.MustConstruct(new(CurrencyCodes), enum.Const("USD")).(*CurrencyCodes)