Skip to content

Commit b5c2155

Browse files
committed
Add collection clone method
1 parent cea8720 commit b5c2155

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ When *Fielder* is applied to a type, it will generate public functions/methods a
9595
* Method `<Type><Suffix>List::AddIfNotContains` adds the values to the collection if they are not already present.
9696
* Method `<Type><Suffix>List::Remove` removes the values from the collection.
9797
* Method `<Type><Suffix>List::Clear` clears the collection.
98+
* Method `<Type><Suffix>List::Clone` returns a pointer to a copy of the collection.
9899
* Method `<Type><Suffix>List::Strings` returns a slice with all the strings of the collection items.
99100
* Functions:
100101
* `<Type><Suffix>Values` returns a slice with all the values of the ENUM.

examples/simple/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"reflect"
65

76
"github.com/kpeu3i/fielder/examples/simple/models"
87
)
@@ -12,7 +11,7 @@ func main() {
1211
fmt.Println("enum validation:", models.UserAccountColumn("_INVALID_").IsValid()) // false
1312

1413
columns1 := models.NewUserAccountColumnList()
15-
columns2 := models.NewUserAccountColumnList()
14+
columns2 := *columns1.Clone()
1615

1716
fmt.Println("collection equality (1):", columns1.Equals(columns2)) // true
1817
fmt.Println("collection similarity (1):", columns1.Similar(columns2)) // true
@@ -39,8 +38,4 @@ func main() {
3938

4039
fmt.Println("collection_1:", columns1.Strings()) // [id]
4140
fmt.Println("collection_2:", columns2.Strings()) // [id created_at updated_at name surname email password]
42-
43-
r := reflect.ValueOf(&models.UserAccount{})
44-
f := reflect.Indirect(r).FieldByName("id")
45-
fmt.Println(f.String())
4641
}

examples/simple/models/user_account_fielder.go

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

output.tpl

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ func (x {{ $fieldType }}) String() string {
4343
type {{ $fieldListType }} []{{ $fieldType }}
4444

4545
// Len returns the number of values in the collection.
46-
func (l {{ $fieldListType}}) Len() int {
46+
func (l {{ $fieldListType }}) Len() int {
4747
return len(l)
4848
}
4949

5050
// Contains returns true if the collection contains the value.
51-
func (l {{ $fieldListType}}) Contains(v {{ $fieldType}}) bool {
51+
func (l {{ $fieldListType }}) Contains(v {{ $fieldType }}) bool {
5252
for _, x := range l {
5353
if x == v {
5454
return true
@@ -59,7 +59,7 @@ func (l {{ $fieldListType}}) Contains(v {{ $fieldType}}) bool {
5959
}
6060

6161
// Equals returns true if the two collections are equal.
62-
func (l {{ $fieldListType}}) Equals(other {{ $fieldListType}}) bool {
62+
func (l {{ $fieldListType }}) Equals(other {{ $fieldListType }}) bool {
6363
if len(l) != len(other) {
6464
return false
6565
}
@@ -74,7 +74,7 @@ func (l {{ $fieldListType}}) Equals(other {{ $fieldListType}}) bool {
7474
}
7575

7676
// Similar returns true if the two collections contain the same values.
77-
func (l {{ $fieldListType}}) Similar(other {{ $fieldListType}}) bool {
77+
func (l {{ $fieldListType }}) Similar(other {{ $fieldListType }}) bool {
7878
if len(l) != len(other) {
7979
return false
8080
}
@@ -89,14 +89,14 @@ func (l {{ $fieldListType}}) Similar(other {{ $fieldListType}}) bool {
8989
}
9090

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

9898
// AddIfNotContains adds the values to the collection if they are not already present.
99-
func (l *{{ $fieldListType}}) AddIfNotContains(v ...{{ $fieldType}}) *{{ $fieldListType}} {
99+
func (l *{{ $fieldListType }}) AddIfNotContains(v ...{{ $fieldType }}) *{{ $fieldListType }} {
100100
for _, x := range v {
101101
if !l.Contains(x) {
102102
l.Add(x)
@@ -107,7 +107,7 @@ func (l *{{ $fieldListType}}) AddIfNotContains(v ...{{ $fieldType}}) *{{ $fieldL
107107
}
108108

109109
// Remove removes the values from the collection.
110-
func (l *{{ $fieldListType}}) Remove(v ...{{ $fieldType}}) *{{ $fieldListType}} {
110+
func (l *{{ $fieldListType }}) Remove(v ...{{ $fieldType }}) *{{ $fieldListType }} {
111111
for _, x := range v {
112112
for i, y := range *l {
113113
if y == x {
@@ -122,14 +122,28 @@ func (l *{{ $fieldListType}}) Remove(v ...{{ $fieldType}}) *{{ $fieldListType}}
122122
}
123123

124124
// Clear clears the collection.
125-
func (l *{{ $fieldListType}}) Clear() *{{ $fieldListType}} {
126-
*l = []{{ $fieldType}}{}
125+
func (l *{{ $fieldListType }}) Clear() *{{ $fieldListType }} {
126+
*l = []{{ $fieldType }}{}
127127

128128
return l
129129
}
130130

131+
// Clone returns a pointer to a copy of the collection.
132+
func (l *{{ $fieldListType }}) Clone() *{{ $fieldListType }} {
133+
if l == nil {
134+
return nil
135+
}
136+
137+
items := make([]{{ $fieldType }}, len(*l))
138+
copy(items, *l)
139+
140+
result := {{ $fieldListType }}(items)
141+
142+
return &result
143+
}
144+
131145
// Strings returns a slice with all the strings of the collection items.
132-
func (l {{ $fieldListType}}) Strings() []string {
146+
func (l {{ $fieldListType }}) Strings() []string {
133147
strings := make([]string, 0, len(l))
134148
for _, x := range l {
135149
strings = append(strings, x.String())
@@ -167,7 +181,7 @@ func New{{ $fieldListType }}() {{ $fieldListType }} {
167181
}
168182

169183
// New{{ $fieldListType }}With returns a new {{ $fieldListType }} with the given values of the ENUM.
170-
func New{{ $fieldListType }}With(v ...{{ $fieldType}}) {{ $fieldListType }} {
184+
func New{{ $fieldListType }}With(v ...{{ $fieldType }}) {{ $fieldListType }} {
171185
result := {{ $fieldListType }}{}
172186
if len(v) > 0 {
173187
result.Add(v...)

0 commit comments

Comments
 (0)