From c62eff76bbf61ac9b5d7b4779cfbd6c65fdb2a60 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 16 Nov 2021 16:44:19 -0500 Subject: [PATCH] better enums --- gamestatus_enumer.go | 73 ++++++++++++++++++++++++++++++++++++++++++ targetstatus_enumer.go | 73 ++++++++++++++++++++++++++++++++++++++++++ util.go | 10 ++++-- 3 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 gamestatus_enumer.go create mode 100644 targetstatus_enumer.go diff --git a/gamestatus_enumer.go b/gamestatus_enumer.go new file mode 100644 index 0000000..41d4c54 --- /dev/null +++ b/gamestatus_enumer.go @@ -0,0 +1,73 @@ +// Code generated by "enumer -type=GameStatus -json"; DO NOT EDIT. + +// +package tome_lib + +import ( + "encoding/json" + "fmt" +) + +const _GameStatusName = "StatusLobbyStatusReadyStatusPlayingStatusStopStatusSentinalWinStatusScourgeWinStatusDraw" + +var _GameStatusIndex = [...]uint8{0, 11, 22, 35, 45, 62, 78, 88} + +func (i GameStatus) String() string { + if i < 0 || i >= GameStatus(len(_GameStatusIndex)-1) { + return fmt.Sprintf("GameStatus(%d)", i) + } + return _GameStatusName[_GameStatusIndex[i]:_GameStatusIndex[i+1]] +} + +var _GameStatusValues = []GameStatus{0, 1, 2, 3, 4, 5, 6} + +var _GameStatusNameToValueMap = map[string]GameStatus{ + _GameStatusName[0:11]: 0, + _GameStatusName[11:22]: 1, + _GameStatusName[22:35]: 2, + _GameStatusName[35:45]: 3, + _GameStatusName[45:62]: 4, + _GameStatusName[62:78]: 5, + _GameStatusName[78:88]: 6, +} + +// GameStatusString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func GameStatusString(s string) (GameStatus, error) { + if val, ok := _GameStatusNameToValueMap[s]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to GameStatus values", s) +} + +// GameStatusValues returns all values of the enum +func GameStatusValues() []GameStatus { + return _GameStatusValues +} + +// IsAGameStatus returns "true" if the value is listed in the enum definition. "false" otherwise +func (i GameStatus) IsAGameStatus() bool { + for _, v := range _GameStatusValues { + if i == v { + return true + } + } + return false +} + +// MarshalJSON implements the json.Marshaler interface for GameStatus +func (i GameStatus) MarshalJSON() ([]byte, error) { + return json.Marshal(i.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface for GameStatus +func (i *GameStatus) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return fmt.Errorf("GameStatus should be a string, got %s", data) + } + + var err error + *i, err = GameStatusString(s) + return err +} diff --git a/targetstatus_enumer.go b/targetstatus_enumer.go new file mode 100644 index 0000000..649a86b --- /dev/null +++ b/targetstatus_enumer.go @@ -0,0 +1,73 @@ +// Code generated by "enumer -type=TargetStatus -json"; DO NOT EDIT. + +// +package tome_lib + +import ( + "encoding/json" + "fmt" +) + +const _TargetStatusName = "TargetSelfTargetOwnTargetOwnEmptyTargetOppTargetOppEmptyTargetAnyTargetNone" + +var _TargetStatusIndex = [...]uint8{0, 10, 19, 33, 42, 56, 65, 75} + +func (i TargetStatus) String() string { + if i < 0 || i >= TargetStatus(len(_TargetStatusIndex)-1) { + return fmt.Sprintf("TargetStatus(%d)", i) + } + return _TargetStatusName[_TargetStatusIndex[i]:_TargetStatusIndex[i+1]] +} + +var _TargetStatusValues = []TargetStatus{0, 1, 2, 3, 4, 5, 6} + +var _TargetStatusNameToValueMap = map[string]TargetStatus{ + _TargetStatusName[0:10]: 0, + _TargetStatusName[10:19]: 1, + _TargetStatusName[19:33]: 2, + _TargetStatusName[33:42]: 3, + _TargetStatusName[42:56]: 4, + _TargetStatusName[56:65]: 5, + _TargetStatusName[65:75]: 6, +} + +// TargetStatusString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func TargetStatusString(s string) (TargetStatus, error) { + if val, ok := _TargetStatusNameToValueMap[s]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to TargetStatus values", s) +} + +// TargetStatusValues returns all values of the enum +func TargetStatusValues() []TargetStatus { + return _TargetStatusValues +} + +// IsATargetStatus returns "true" if the value is listed in the enum definition. "false" otherwise +func (i TargetStatus) IsATargetStatus() bool { + for _, v := range _TargetStatusValues { + if i == v { + return true + } + } + return false +} + +// MarshalJSON implements the json.Marshaler interface for TargetStatus +func (i TargetStatus) MarshalJSON() ([]byte, error) { + return json.Marshal(i.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface for TargetStatus +func (i *TargetStatus) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return fmt.Errorf("TargetStatus should be a string, got %s", data) + } + + var err error + *i, err = TargetStatusString(s) + return err +} diff --git a/util.go b/util.go index 217d70a..1708ad9 100644 --- a/util.go +++ b/util.go @@ -1,10 +1,10 @@ package tome_lib +//go:generate enumer -type=GameStatus -json type GameStatus int -type TargetStatus int const ( - StatusLobby = iota + StatusLobby GameStatus = iota StatusReady StatusPlaying StatusStop @@ -13,8 +13,12 @@ const ( StatusDraw ) +//go:generate enumer -type=TargetStatus -json + +type TargetStatus int + const ( - TargetSelf = iota + TargetSelf TargetStatus = iota TargetOwn TargetOwnEmpty TargetOpp