Skip to content

Commit dfb4b7c

Browse files
committed
Add custom constructor for the collection and extend existing collection methods
1 parent c5eb8a3 commit dfb4b7c

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ When *Fielder* is applied to a type, it will generate public functions/methods a
9191
* Method `<Type><Suffix>List::Contains` returns true if the collection contains the value.
9292
* Method `<Type><Suffix>List::Equals` returns true if the two collections are equal.
9393
* Method `<Type><Suffix>List::Similar` returns true if the two collections contain the same values.
94-
* Method `<Type><Suffix>List::Add` adds the value to the collection.
95-
* Method `<Type><Suffix>List::AddIfNotContains` adds the value to the collection if it is not already in the collection.
96-
* Method `<Type><Suffix>List::Remove` removes the value from the collection.
94+
* Method `<Type><Suffix>List::Add` adds the values to the collection.
95+
* Method `<Type><Suffix>List::AddIfNotContains` adds the values to the collection if they are not already present.
96+
* Method `<Type><Suffix>List::Remove` removes the values from the collection.
9797
* Method `<Type><Suffix>List::Clear` clears the collection.
9898
* Method `<Type><Suffix>List::Strings` returns a slice with all the strings of the collection items.
9999
* Functions:
100100
* `<Type><Suffix>Values` returns a slice with all the values of the ENUM.
101101
* `<Type><Suffix>Strings` returns a slice with all the strings of the ENUM.
102102
* `New<Type><Suffix>` returns a new collection with all the values of the ENUM.
103+
* `New<Type><Suffix>With` returns a new collection with the given values of the ENUM.
103104

104105
## Inspiring projects
105106
* [enumer](https://github.com/dmarkham/enumer)

examples/simple/models/user_account_fielder.go

Lines changed: 22 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

output.tpl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func (l {{ $fieldListType}}) Similar(other {{ $fieldListType}}) bool {
8888
return true
8989
}
9090

91-
// Add adds the value to the collection.
92-
func (l *{{ $fieldListType}}) Add(v {{ $fieldType}}) *{{ $fieldListType}} {
93-
*l = append(*l, v)
91+
// Add adds the values to the collection.
92+
func (l *{{ $fieldListType}}) Add(v ...{{ $fieldType}}) *{{ $fieldListType}} {
93+
*l = append(*l, v...)
9494
9595
return l
9696
}
9797

98-
// AddIfNotContains adds the value to the collection if it is not already in the collection.
98+
// AddIfNotContains adds the values to the collection if they are not already present.
9999
func (l *{{ $fieldListType}}) AddIfNotContains(v ...{{ $fieldType}}) *{{ $fieldListType}} {
100100
for _, x := range v {
101101
if !l.Contains(x) {
@@ -106,13 +106,15 @@ func (l *{{ $fieldListType}}) AddIfNotContains(v ...{{ $fieldType}}) *{{ $fieldL
106106
return l
107107
}
108108

109-
// Remove removes the value from the collection.
110-
func (l *{{ $fieldListType}}) Remove(v {{ $fieldType}}) *{{ $fieldListType}} {
111-
for i, x := range *l {
112-
if x == v {
113-
*l = append((*l)[:i], (*l)[i+1:]...)
109+
// Remove removes the values from the collection.
110+
func (l *{{ $fieldListType}}) Remove(v ...{{ $fieldType}}) *{{ $fieldListType}} {
111+
for _, x := range v {
112+
for i, y := range *l {
113+
if y == x {
114+
*l = append((*l)[:i], (*l)[i+1:]...)
114115
115-
return l
116+
break
117+
}
116118
}
117119
}
118120

@@ -163,3 +165,13 @@ func New{{ $fieldListType }}() {{ $fieldListType }} {
163165

164166
return result
165167
}
168+
169+
// New{{ $fieldListType }}With returns a new {{ $fieldListType }} with the given values of the ENUM.
170+
func New{{ $fieldListType }}With(v ...{{ $fieldType}}) {{ $fieldListType }} {
171+
result := {{ $fieldListType }}{}
172+
if len(v) > 0 {
173+
result.Add(v...)
174+
}
175+
176+
return result
177+
}

0 commit comments

Comments
 (0)