-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_wire_bud.go
77 lines (66 loc) · 1.48 KB
/
auto_wire_bud.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
package factory
import (
"errors"
"fmt"
"strings"
)
const (
WireValueSelf WireValue = "self"
WireValueAuto WireValue = "auto"
WireValueType WireValue = "type"
WireValueName WireValue = "name"
WireValueValue WireValue = "value"
)
var ErrInvalidWireValue = errors.New("not a valid WireValue")
var _WireValueNameMap = map[string]WireValue{
"self": WireValueSelf,
"auto": WireValueAuto,
"type": WireValueType,
"name": WireValueName,
"value": WireValueValue,
}
func (x WireValue) IsValid() bool {
_, ok := _WireValueNameMap[string(x)]
return ok
}
func WireValueValues() []WireValue {
return []WireValue{
WireValueSelf,
WireValueAuto,
WireValueType,
WireValueName,
WireValueValue,
}
}
func (x WireValue) Name() string {
if v, ok := _WireValueNameMap[string(x)]; ok {
return string(v)
}
panic(ErrInvalidWireValue)
}
func (x WireValue) String() string {
if v, ok := _WireValueNameMap[string(x)]; ok {
return string(v)
}
return fmt.Sprintf("WireValue(%s)", string(x))
}
func ParseWireValue(value string) (WireValue, error) {
if x, ok := _WireValueNameMap[value]; ok {
return x, nil
}
if x, ok := _WireValueNameMap[strings.ToLower(value)]; ok {
return x, nil
}
return "", fmt.Errorf("%s is %w", value, ErrInvalidWireValue)
}
func (x WireValue) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *WireValue) UnmarshalText(text []byte) error {
val, err := ParseWireValue(string(text))
if err != nil {
return err
}
*x = val
return nil
}