-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.tpl
196 lines (152 loc) · 4.54 KB
/
output.tpl
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Code generated by "{{ .CMD }}"; DO NOT EDIT.
{{ $fieldType := print .Type .Suffix }}
{{ $fieldListType := print $fieldType "List" }}
{{ $fieldValues := print "_" $fieldType "Values" }}
package {{ .Package }}
// {{ $fieldType }} represents the values of the ENUM.
type {{ $fieldType }} string
const (
{{ range .Fields -}}
{{ $fieldType }}{{ .Name }} {{ $fieldType }} = "{{ .Alias }}"
{{ end }}
)
var {{ $fieldValues }} = [...]{{ $fieldType }}{
{{ range .Fields -}}
{{ $fieldType }}{{ .Name }},
{{ end }}
}
// IsValid returns true if the value is a valid ENUM.
func (x {{ $fieldType }}) IsValid() bool {
for _, v := range {{ $fieldValues }} {
if v == x {
return true
}
}
return false
}
// String returns the string representation of the value.
func (x {{ $fieldType }}) String() string {
return string(x)
}
// *********************************************************************************************************************
// {{ $fieldListType }} represents a collection of {{ $fieldType }}.
type {{ $fieldListType }} []{{ $fieldType }}
// Len returns the number of values in the collection.
func (l {{ $fieldListType }}) Len() int {
return len(l)
}
// IsEmpty returns true if the collection is empty.
func (l {{ $fieldListType }}) IsEmpty() bool {
return l.Len() == 0
}
// Contains returns true if the collection contains the value.
func (l {{ $fieldListType }}) Contains(v {{ $fieldType }}) bool {
for _, x := range l {
if x == v {
return true
}
}
return false
}
// Equals returns true if the two collections are equal.
func (l {{ $fieldListType }}) Equals(other {{ $fieldListType }}) bool {
if len(l) != len(other) {
return false
}
for i, x := range l {
if x != other[i] {
return false
}
}
return true
}
// Similar returns true if the two collections contain the same values.
func (l {{ $fieldListType }}) Similar(other {{ $fieldListType }}) bool {
if len(l) != len(other) {
return false
}
for _, x := range l {
if !other.Contains(x) {
return false
}
}
return true
}
// Add adds the values to the collection.
func (l *{{ $fieldListType }}) Add(v ...{{ $fieldType }}) *{{ $fieldListType }} {
*l = append(*l, v...)
return l
}
// AddIfNotContains adds the values to the collection if they are not already present.
func (l *{{ $fieldListType }}) AddIfNotContains(v ...{{ $fieldType }}) *{{ $fieldListType }} {
for _, x := range v {
if !l.Contains(x) {
l.Add(x)
}
}
return l
}
// Remove removes the values from the collection.
func (l *{{ $fieldListType }}) Remove(v ...{{ $fieldType }}) *{{ $fieldListType }} {
for _, x := range v {
for i, y := range *l {
if y == x {
*l = append((*l)[:i], (*l)[i+1:]...)
break
}
}
}
return l
}
// Clear clears the collection.
func (l *{{ $fieldListType }}) Clear() *{{ $fieldListType }} {
*l = []{{ $fieldType }}{}
return l
}
// Clone returns a pointer to a copy of the collection.
func (l *{{ $fieldListType }}) Clone() *{{ $fieldListType }} {
if l == nil {
return nil
}
items := make([]{{ $fieldType }}, len(*l))
copy(items, *l)
result := {{ $fieldListType }}(items)
return &result
}
// Strings returns a slice with all the strings of the collection items.
func (l {{ $fieldListType }}) Strings() []string {
strings := make([]string, 0, len(l))
for _, x := range l {
strings = append(strings, x.String())
}
return strings
}
// *********************************************************************************************************************
// {{ $fieldType }}Values returns a slice with all the values of the ENUM.
func {{ $fieldType }}Values() []{{ $fieldType }} {
result := make([]{{ $fieldType }}, len({{ $fieldValues }}))
copy(result, {{ $fieldValues }}[:])
return result
}
// {{ $fieldType }}Strings returns a slice with all the strings of the ENUM.
func {{ $fieldType }}Strings() []string {
strings := make([]string, 0, len({{ $fieldValues }}))
for _, v := range {{ $fieldValues }} {
strings = append(strings, v.String())
}
return strings
}
// New{{ $fieldListType }} returns a new {{ $fieldListType }} with all the values of the ENUM.
func New{{ $fieldListType }}() {{ $fieldListType }} {
result := make([]{{ $fieldType }}, len({{ $fieldValues }}))
copy(result, {{ $fieldValues }}[:])
return result
}
// New{{ $fieldListType }}With returns a new {{ $fieldListType }} with the given values of the ENUM.
func New{{ $fieldListType }}With(v ...{{ $fieldType }}) {{ $fieldListType }} {
result := {{ $fieldListType }}{}
if len(v) > 0 {
result.Add(v...)
}
return result
}