Skip to content

Commit

Permalink
feat: add field max length (#4480)
Browse files Browse the repository at this point in the history
* add field max lenght

* add changelog

* Update ignite/services/scaffolder/type.go

* Update ignite/services/scaffolder/type.go

* fix unit tests

* fix comments

---------

Co-authored-by: Danny <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2025
1 parent 63e1a7b commit b3ab467
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
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
- [#4477](https://github.com/ignite/cli/pull/4477) IBC v10 support
- [#4166](https://github.com/ignite/cli/issues/4166) Migrate buf config files to v2
- [#4494](https://github.com/ignite/cli/pull/4494) Automatic migrate the buf configs to v2
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
12 changes: 11 additions & 1 deletion 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 = 64

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

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

// checkForbiddenTypeIndex returns true if the name is forbidden as a index name.
// 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 an index name.
func checkForbiddenTypeIndex(index string) error {
indexSplit := strings.Split(index, datatype.Separator)
if len(indexSplit) > 1 {
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(maxLength + 1),
shouldError: true,
},
{
desc: "should pass with name at max length",
name: randstr.Runes(maxLength),
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)
})
}
}

0 comments on commit b3ab467

Please sign in to comment.