forked from abice/go-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommented_enum.go
More file actions
173 lines (148 loc) · 5.05 KB
/
commented_enum.go
File metadata and controls
173 lines (148 loc) · 5.05 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Code generated by go-enum DO NOT EDIT.
// Version: example
// Revision: example
// Build Date: example
// Built By: example
//go:build example
// +build example
package example
import (
"errors"
"fmt"
"strings"
)
const (
// CommentedValue1 is a Commented of type Value1.
// Commented value 1
CommentedValue1 Commented = iota
// CommentedValue2 is a Commented of type Value2.
CommentedValue2
// CommentedValue3 is a Commented of type Value3.
// Commented value 3
CommentedValue3
)
var ErrInvalidCommented = errors.New("not a valid Commented")
const _CommentedName = "value1value2value3"
var _CommentedMap = map[Commented]string{
CommentedValue1: _CommentedName[0:6],
CommentedValue2: _CommentedName[6:12],
CommentedValue3: _CommentedName[12:18],
}
// String implements the Stringer interface.
func (x Commented) String() string {
if str, ok := _CommentedMap[x]; ok {
return str
}
return fmt.Sprintf("Commented(%d)", x)
}
// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x Commented) IsValid() bool {
_, ok := _CommentedMap[x]
return ok
}
var _CommentedValue = map[string]Commented{
_CommentedName[0:6]: CommentedValue1,
strings.ToLower(_CommentedName[0:6]): CommentedValue1,
_CommentedName[6:12]: CommentedValue2,
strings.ToLower(_CommentedName[6:12]): CommentedValue2,
_CommentedName[12:18]: CommentedValue3,
strings.ToLower(_CommentedName[12:18]): CommentedValue3,
}
// ParseCommented attempts to convert a string to a Commented.
func ParseCommented(name string) (Commented, error) {
if x, ok := _CommentedValue[name]; ok {
return x, nil
}
return Commented(0), fmt.Errorf("%s is %w", name, ErrInvalidCommented)
}
// MarshalText implements the text marshaller method.
func (x Commented) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *Commented) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseCommented(name)
if err != nil {
return err
}
*x = tmp
return nil
}
// AppendText appends the textual representation of itself to the end of b
// (allocating a larger slice if necessary) and returns the updated slice.
//
// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
func (x *Commented) AppendText(b []byte) ([]byte, error) {
return append(b, x.String()...), nil
}
const (
// Skipped value.
// Placeholder with a ',' in it. (for harder testing)
_ ComplexCommented = iota
// ComplexCommentedValue1 is a ComplexCommented of type Value1.
// Commented value 1
ComplexCommentedValue1
// ComplexCommentedValue2 is a ComplexCommented of type Value2.
ComplexCommentedValue2
// ComplexCommentedValue3 is a ComplexCommented of type Value3.
// Commented value 3
ComplexCommentedValue3
)
var ErrInvalidComplexCommented = errors.New("not a valid ComplexCommented")
const _ComplexCommentedName = "value1value2value3"
var _ComplexCommentedMap = map[ComplexCommented]string{
ComplexCommentedValue1: _ComplexCommentedName[0:6],
ComplexCommentedValue2: _ComplexCommentedName[6:12],
ComplexCommentedValue3: _ComplexCommentedName[12:18],
}
// String implements the Stringer interface.
func (x ComplexCommented) String() string {
if str, ok := _ComplexCommentedMap[x]; ok {
return str
}
return fmt.Sprintf("ComplexCommented(%d)", x)
}
// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x ComplexCommented) IsValid() bool {
_, ok := _ComplexCommentedMap[x]
return ok
}
var _ComplexCommentedValue = map[string]ComplexCommented{
_ComplexCommentedName[0:6]: ComplexCommentedValue1,
strings.ToLower(_ComplexCommentedName[0:6]): ComplexCommentedValue1,
_ComplexCommentedName[6:12]: ComplexCommentedValue2,
strings.ToLower(_ComplexCommentedName[6:12]): ComplexCommentedValue2,
_ComplexCommentedName[12:18]: ComplexCommentedValue3,
strings.ToLower(_ComplexCommentedName[12:18]): ComplexCommentedValue3,
}
// ParseComplexCommented attempts to convert a string to a ComplexCommented.
func ParseComplexCommented(name string) (ComplexCommented, error) {
if x, ok := _ComplexCommentedValue[name]; ok {
return x, nil
}
return ComplexCommented(0), fmt.Errorf("%s is %w", name, ErrInvalidComplexCommented)
}
// MarshalText implements the text marshaller method.
func (x ComplexCommented) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *ComplexCommented) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseComplexCommented(name)
if err != nil {
return err
}
*x = tmp
return nil
}
// AppendText appends the textual representation of itself to the end of b
// (allocating a larger slice if necessary) and returns the updated slice.
//
// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
func (x *ComplexCommented) AppendText(b []byte) ([]byte, error) {
return append(b, x.String()...), nil
}