-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
value_gen.go
103 lines (89 loc) · 2.09 KB
/
value_gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Code generated from "enum.go.tmpl" - DO NOT EDIT.
/*
* Copyright ©1998-2023 by Richard A. Wilkes. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, version 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, version 2.0.
*/
package emcost
import (
"strings"
"github.com/richardwilkes/toolbox/i18n"
)
// Possible values.
const (
Addition Value = iota
Percentage
Multiplier
CostFactor
)
// LastValue is the last valid value.
const LastValue Value = CostFactor
// Values holds all possible values.
var Values = []Value{
Addition,
Percentage,
Multiplier,
CostFactor,
}
// Value describes how an Equipment Modifier's cost value is applied.
type Value byte
// EnsureValid ensures this is of a known value.
func (enum Value) EnsureValid() Value {
if enum <= CostFactor {
return enum
}
return 0
}
// Key returns the key used in serialization.
func (enum Value) Key() string {
switch enum {
case Addition:
return "+"
case Percentage:
return "%"
case Multiplier:
return "x"
case CostFactor:
return "cf"
default:
return Value(0).Key()
}
}
// String implements fmt.Stringer.
func (enum Value) String() string {
switch enum {
case Addition:
return i18n.Text("+")
case Percentage:
return i18n.Text("%")
case Multiplier:
return i18n.Text("x")
case CostFactor:
return i18n.Text("CF")
default:
return Value(0).String()
}
}
// MarshalText implements the encoding.TextMarshaler interface.
func (enum Value) MarshalText() (text []byte, err error) {
return []byte(enum.Key()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (enum *Value) UnmarshalText(text []byte) error {
*enum = ExtractValue(string(text))
return nil
}
// ExtractValue extracts the value from a string.
func ExtractValue(str string) Value {
for _, enum := range Values {
if strings.EqualFold(enum.Key(), str) {
return enum
}
}
return 0
}