Skip to content

Commit 3b4229b

Browse files
authored
refactor(templates): harden IsSlice (#4667)
* refactor(templates): harden `IsSlice` * updates * lint
1 parent 62c0ab9 commit 3b4229b

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [#4658](https://github.com/ignite/cli/pull/4658) Fix indentation for params scaffolded into a struct.
3333
- [#4582](https://github.com/ignite/cli/issues/4582) Fix xast misplacing comments.
3434
- [#4660](https://github.com/ignite/cli/pull/4660) Fix xast test case indentation.
35+
- [#4667](https://github.com/ignite/cli/pull/4667) Harden `IsSlice`
3536

3637
## [`v29.0.0-beta.1`](https://github.com/ignite/cli/releases/tag/v29.0.0-beta.1)
3738

ignite/templates/field/field.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,37 @@ func (f Field) DataType() string {
2828
}
2929

3030
// IsSlice returns true if the field is a slice.
31-
// It assumes that a non indexable type is a slice.
3231
func (f Field) IsSlice() bool {
3332
dt, ok := datatype.IsSupportedType(f.DatatypeName)
3433
if !ok {
3534
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
3635
}
3736

38-
return dt.NonIndex
37+
switch f.DatatypeName {
38+
case datatype.StringSlice,
39+
datatype.IntSlice,
40+
datatype.UintSlice,
41+
datatype.Coins,
42+
datatype.StringSliceAlias,
43+
datatype.IntSliceAlias,
44+
datatype.UintSliceAlias,
45+
datatype.CoinSliceAlias,
46+
datatype.Bytes:
47+
return true
48+
case
49+
datatype.String,
50+
datatype.Bool,
51+
datatype.Int,
52+
datatype.Int64,
53+
datatype.Uint,
54+
datatype.Uint64,
55+
datatype.Coin,
56+
datatype.Custom:
57+
return false
58+
default:
59+
// For other types, we assume that it is a slice if non indexable.
60+
return dt.NonIndex
61+
}
3962
}
4063

4164
// ProtoFieldName returns the field name used in proto.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package field
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
9+
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
10+
)
11+
12+
// TestField_IsSlice tests the IsSlice method of Field struct.
13+
func TestField_IsSlice(t *testing.T) {
14+
testCases := []struct {
15+
name string
16+
field Field
17+
expected bool
18+
}{
19+
{
20+
name: "array type should be slice",
21+
field: Field{
22+
Name: multiformatname.Name{},
23+
DatatypeName: datatype.IntSlice,
24+
Datatype: string(datatype.IntSlice),
25+
},
26+
expected: true,
27+
},
28+
{
29+
name: "array type should be slice",
30+
field: Field{
31+
Name: multiformatname.Name{},
32+
DatatypeName: datatype.Bytes,
33+
Datatype: string(datatype.Bytes),
34+
},
35+
expected: true,
36+
},
37+
{
38+
name: "array type should be slice",
39+
field: Field{
40+
Name: multiformatname.Name{},
41+
DatatypeName: datatype.CoinSliceAlias,
42+
Datatype: string(datatype.CoinSliceAlias),
43+
},
44+
expected: true,
45+
},
46+
{
47+
name: "coin type should not be slice",
48+
field: Field{
49+
Name: multiformatname.Name{},
50+
DatatypeName: datatype.Coin,
51+
Datatype: string(datatype.Coin),
52+
},
53+
expected: false,
54+
},
55+
{
56+
name: "string type should not be slice",
57+
field: Field{
58+
Name: multiformatname.Name{},
59+
DatatypeName: datatype.String,
60+
Datatype: "",
61+
},
62+
expected: false,
63+
},
64+
{
65+
name: "int type should not be slice",
66+
field: Field{
67+
Name: multiformatname.Name{},
68+
DatatypeName: datatype.Int,
69+
Datatype: "",
70+
},
71+
expected: false,
72+
},
73+
}
74+
75+
for _, tc := range testCases {
76+
t.Run(tc.name, func(t *testing.T) {
77+
require.Equal(t, tc.expected, tc.field.IsSlice())
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)