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 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- [#4166](https://github.com/ignite/cli/issues/4166) Migrate buf config files to v2

### 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
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)
})
}
}
Loading