better enums

This commit is contained in:
stryan 2021-11-16 16:44:19 -05:00
parent a17f5bfa95
commit c62eff76bb
3 changed files with 153 additions and 3 deletions

73
gamestatus_enumer.go Normal file
View File

@ -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
}

73
targetstatus_enumer.go Normal file
View File

@ -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
}

10
util.go
View File

@ -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