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

refactor(templates)!: scaffold int64 instead of int32 #4167

Merged
merged 5 commits into from
May 28, 2024
Merged
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 @@ -35,6 +35,7 @@
- [#4075](https://github.com/ignite/cli/pull/4075) Use `gopkg.in/yaml.v3` instead `gopkg.in/yaml.v2`
- [#4118](https://github.com/ignite/cli/pull/4118) Version scaffolded protos as `v1` to follow SDK structure.
- [#4149](https://github.com/ignite/cli/pull/4149) Bump cometbft to `v0.38.7`
- [#4167](https://github.com/ignite/cli/pull/4167) Scaffold `int64` instead of `int32` when a field type is `int`
- [#4168](https://github.com/ignite/cli/pull/4168) Bump IBC to `v8.3.1`

### Fixes
Expand Down
4 changes: 2 additions & 2 deletions ignite/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Currently supports:
| string | - | yes | string | Text type |
| array.string | strings | no | []string | List of text type |
| bool | - | yes | bool | Boolean type |
| int | - | yes | int32 | Integer type |
| array.int | ints | no | []int32 | List of integers types |
| int | - | yes | int64 | Integer type |
| array.int | ints | no | []int64 | List of integers types |
| uint | - | yes | uint64 | Unsigned integer type |
| array.uint | uints | no | []uint64 | List of unsigned integers types |
| coin | - | no | sdk.Coin | Cosmos SDK coin type |
Expand Down
12 changes: 6 additions & 6 deletions ignite/pkg/protoanalysis/protoutil/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ func WithFieldOptions(options ...*proto.Option) FieldSpecOptions {

// NewField creates a new field statement node:
//
// // int32 Foo = 1;
// field := NewField("Foo", "int32", 1)
// // int64 Foo = 1;
// field := NewField("Foo", "int64", 1)
//
// Fields aren't marked as repeated, required or optional. Use Repeated, Optional
// and Required to mark the field as such.
//
// // repeated int32 Foo = 1;
// field := NewField("Foo", "int32", 1, Repeated())
// // repeated int64 Foo = 1;
// field := NewField("Foo", "int64", 1, Repeated())
func NewField(name, typename string, sequence int, opts ...FieldSpecOptions) *proto.NormalField {
f := FieldSpec{name: name, typename: typename, sequence: sequence}
for _, opt := range opts {
Expand Down Expand Up @@ -401,10 +401,10 @@ func Extend() MessageSpecOptions {
//
// // message Foo {
// // option (foo) = 1;
// // int32 Bar = 1;
// // int64 Bar = 1;
// // }
// opt := NewOption("foo", "1")
// field := NewField("int32", "Bar", 1)
// field := NewField("int64", "Bar", 1)
// message := NewMessage("Foo", WithMessageOptions(opt), WithFields(field))
//
// By default, options are added first, then fields and then enums.
Expand Down
28 changes: 14 additions & 14 deletions ignite/templates/field/datatype/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,62 @@ import (
var (
// DataInt is an int data type definition.
DataInt = DataType{
DataType: func(string) string { return "int32" },
CollectionsKeyValueName: func(string) string { return "collections.Int32Key" },
DataType: func(string) string { return "int64" },
CollectionsKeyValueName: func(string) string { return "collections.Int64Key" },
DefaultTestValue: "111",
ValueLoop: "int32(i)",
ValueLoop: "int64(i)",
ValueIndex: "0",
ValueInvalidIndex: "100000",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("int32 %s = %d", name, index)
return fmt.Sprintf("int64 %s = %d", name, index)
},
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: %d,\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := cast.ToInt32E(args[%d])
return fmt.Sprintf(`%s%s, err := cast.ToInt64E(args[%d])
if err != nil {
return err
}`,
prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return fmt.Sprintf(`%[1]vBytes := make([]byte, 4)
binary.BigEndian.PutUint32(%[1]vBytes, uint32(%[1]v))`, name)
binary.BigEndian.PutUint64(%[1]vBytes, uint64(%[1]v))`, name)
},
ToString: func(name string) string {
return fmt.Sprintf("strconv.Itoa(int(%s))", name)
return fmt.Sprintf("strconv.FormatInt(%s, 10)", name)
},
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "int32", index)
return protoutil.NewField(name, "int64", index)
},
GoCLIImports: []GoImport{{Name: "github.com/spf13/cast"}},
}

// DataIntSlice is an int array data type definition.
DataIntSlice = DataType{
DataType: func(string) string { return "[]int32" },
DataType: func(string) string { return "[]int64" },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "1,2,3,4,5",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("repeated int32 %s = %d", name, index)
return fmt.Sprintf("repeated int64 %s = %d", name, index)
},
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: []int32{%d},\n", name.UpperCamel, value)
return fmt.Sprintf("%s: []int64{%d},\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%[1]vCast%[2]v := strings.Split(args[%[3]v], listSeparator)
%[1]v%[2]v := make([]int32, len(%[1]vCast%[2]v))
%[1]v%[2]v := make([]int64, len(%[1]vCast%[2]v))
for i, arg := range %[1]vCast%[2]v {
value, err := cast.ToInt32E(arg)
value, err := cast.ToInt64E(arg)
if err != nil {
return err
}
%[1]v%[2]v[i] = value
}`, prefix, name.UpperCamel, argIndex)
},
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "int32", index, protoutil.Repeated())
return protoutil.NewField(name, "int64", index, protoutil.Repeated())
},
GoCLIImports: []GoImport{{Name: "github.com/spf13/cast"}, {Name: "strings"}},
NonIndex: true,
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn
content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault)

// lines of code to call the key function with the indexes of the element
keyCall := fmt.Sprintf("string(elem.%s)", opts.Index.Name.UpperCamel)
keyCall := fmt.Sprintf(`fmt.Sprint(elem.%s)`, opts.Index.Name.UpperCamel)

templateTypesValidate := `// Check for duplicated index in %[2]v
%[2]vIndexMap := make(map[string]struct{})
Expand Down
Loading