-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum_replace.go
81 lines (65 loc) · 2.1 KB
/
enum_replace.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
package client
import "fmt"
type replaceMethodSet struct {
Random ReplaceMethod
Fake ReplaceMethod
Category ReplaceMethod
Mask ReplaceMethod
}
// ReplaceMethods represents the set of replace methods that can be used.
var ReplaceMethods = replaceMethodSet{
Random: newReplaceMethod("random"),
Fake: newReplaceMethod("fake"),
Category: newReplaceMethod("category"),
Mask: newReplaceMethod("mask"),
}
// Parse parses the string value and returns a replace method if one exists.
func (replaceMethodSet) Parse(value string) (ReplaceMethod, error) {
replaceMethod, exists := replaceMethods[value]
if !exists {
return ReplaceMethod{}, fmt.Errorf("invalid replace method %q", value)
}
return replaceMethod, nil
}
// MustParse parses the string value and returns a replace method if one exists.
// If an error occurs the function panics.
func (replaceMethodSet) MustParse(value string) ReplaceMethod {
replaceMethod, err := ReplaceMethods.Parse(value)
if err != nil {
panic(err)
}
return replaceMethod
}
// =============================================================================
// Set of known replace methods.
var replaceMethods = make(map[string]ReplaceMethod)
// ReplaceMethod represents a replace method in the system.
type ReplaceMethod struct {
value string
}
func newReplaceMethod(replaceMethod string) ReplaceMethod {
rm := ReplaceMethod{replaceMethod}
replaceMethods[replaceMethod] = rm
return rm
}
// String returns the name of the replace method.
func (rm ReplaceMethod) String() string {
return rm.value
}
// UnmarshalText implement the unmarshal interface for JSON conversions.
func (rm *ReplaceMethod) UnmarshalText(data []byte) error {
replaceMethod, err := ReplaceMethods.Parse(string(data))
if err != nil {
return err
}
rm.value = replaceMethod.value
return nil
}
// MarshalText implement the marshal interface for JSON conversions.
func (rm ReplaceMethod) MarshalText() ([]byte, error) {
return []byte(rm.value), nil
}
// Equal provides support for the go-cmp package and testing.
func (rm ReplaceMethod) Equal(rm2 ReplaceMethod) bool {
return rm.value == rm2.value
}