Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add field max length #4480

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [#4436](https://github.com/ignite/cli/pull/4436) Return tx hash to the faucet API
- [#4437](https://github.com/ignite/cli/pull/4437) Remove module placeholders
- [#4289](https://github.com/ignite/cli/pull/4289), [#4423](https://github.com/ignite/cli/pull/4423), [#4432](https://github.com/ignite/cli/pull/4432) Cosmos SDK v0.52 support
- [#4480](https://github.com/ignite/cli/pull/4480) Add field max length

### Changes

Expand Down
2 changes: 1 addition & 1 deletion ignite/services/scaffolder/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func checkGoReservedWord(name string) error {
"uintptr":
return errors.Errorf("%s is a Go built-in identifier", name)
}
return nil
return checkMaxLength(name)
}

// containsCustomTypes returns true if the list of fields contains at least one custom type.
Expand Down
10 changes: 10 additions & 0 deletions ignite/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/ignite/cli/v29/ignite/templates/typed/singleton"
)

const maxLength = 126
Pantani marked this conversation as resolved.
Show resolved Hide resolved

// AddTypeOption configures options for AddType.
type AddTypeOption func(*addTypeOptions)

Expand Down Expand Up @@ -214,6 +216,14 @@ func (s Scaffolder) AddType(
return s.Run(append(gens, g)...)
}

// checkMaxLength checks if the index length exceeds the maximum allowed length
func checkMaxLength(name string) error {
if len(name) > maxLength {
return errors.Errorf("index exceeds maximum allowed length of %d characters", maxLength)
}
return nil
}

// checkForbiddenTypeIndex returns true if the name is forbidden as a index name.
func checkForbiddenTypeIndex(index string) error {
indexSplit := strings.Split(index, datatype.Separator)
Expand Down
34 changes: 33 additions & 1 deletion ignite/services/scaffolder/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/pkg/randstr"
"github.com/ignite/cli/v29/ignite/templates/field"
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
)
Expand Down Expand Up @@ -216,5 +217,36 @@ func TestCheckForbiddenTypeIndexField(t *testing.T) {
}
}

func TestAddType(_ *testing.T) {
func Test_checkMaxLength(t *testing.T) {
tests := []struct {
desc string
name string
shouldError bool
}{
{
desc: "should pass with valid name",
name: "validName",
shouldError: false,
},
{
desc: "should fail with name exceeding max length",
name: randstr.Runes(257),
shouldError: true,
},
{
desc: "should pass with name at max length",
name: randstr.Runes(256),
shouldError: false,
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
err := checkMaxLength(tc.name)
if tc.shouldError {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
Loading