Skip to content

Commit

Permalink
feat(templates): add CollectionsKeyValueName to field.DataType (#…
Browse files Browse the repository at this point in the history
…4131)

* feat(templates): add `CollectionsKeyValueName` to `field.DataType`

* pr number

* typo

* imports

Co-authored-by: Danilo Pantani <[email protected]>

* fix cache

* feedback

---------

Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
julienrbrt and Pantani authored May 15, 2024
1 parent a8956c8 commit ae58e83
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 78 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,4 @@ jobs:
install-mode: goinstall
args: --timeout 10m
github-token: ${{ secrets.github_token }}
skip-go-installation: true
skip-pkg-cache: true
skip-build-cache: true
skip-save-cache: true
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [#4110](https://github.com/ignite/cli/pull/4110) Scaffold a consumer chain with `interchain-security` v5.0.0-rc0.
- [#4111](https://github.com/ignite/cli/pull/4111) Remove vuex generation
- [#4117](https://github.com/ignite/cli/pull/4117), [#4125](https://github.com/ignite/cli/pull/4125) Support relative path when installing local plugins
- [#4131](https://github.com/ignite/cli/pull/4131) Support `bytes` as data type in the `scaffold` commands

### Changes

Expand Down
11 changes: 6 additions & 5 deletions ignite/templates/field/datatype/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (

// DataBool bool data type definition.
var DataBool = DataType{
DataType: func(string) string { return "bool" },
DefaultTestValue: "false",
ValueLoop: "false",
ValueIndex: "false",
ValueInvalidIndex: "false",
DataType: func(string) string { return "bool" },
CollectionsKeyValueName: func(string) string { return "collections.BoolKey" },
DefaultTestValue: "false",
ValueLoop: "false",
ValueIndex: "false",
ValueInvalidIndex: "false",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("bool %s = %d", name, index)
},
Expand Down
36 changes: 36 additions & 0 deletions ignite/templates/field/datatype/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datatype

import (
"fmt"

"github.com/emicklei/proto"

"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil"
)

// DataBytes is a string data type definition.
var DataBytes = DataType{
DataType: func(string) string { return "[]byte" },
CollectionsKeyValueName: func(string) string { return "collections.BytesKey" },
DefaultTestValue: "[]byte{1, 2, 3, 4, 5}",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("bytes %s = %d", name, index)
},
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: []byte(\"%d\"),\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf("%s%s := []byte(args[%d])", prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return name
},
ToString: func(name string) string {
return fmt.Sprintf("string(%s)", name)
},
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "bytes", index)
},
NonIndex: true,
}
10 changes: 6 additions & 4 deletions ignite/templates/field/datatype/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
var (
// DataCoin coin data type definition.
DataCoin = DataType{
DataType: func(string) string { return "sdk.Coin" },
DefaultTestValue: "10token",
DataType: func(string) string { return "sdk.Coin" },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "10token",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]",
name, index)
Expand All @@ -38,8 +39,9 @@ var (

// DataCoinSlice is a coin array data type definition.
DataCoinSlice = DataType{
DataType: func(string) string { return "sdk.Coins" },
DefaultTestValue: "10token,20stake",
DataType: func(string) string { return "sdk.Coins" },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "10token,20stake",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("repeated cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]",
name, index)
Expand Down
5 changes: 3 additions & 2 deletions ignite/templates/field/datatype/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

// DataCustom is a custom data type definition.
var DataCustom = DataType{
DataType: func(datatype string) string { return fmt.Sprintf("*%s", datatype) },
DefaultTestValue: "null",
DataType: func(datatype string) string { return fmt.Sprintf("*%s", datatype) },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "null",
ProtoType: func(datatype, name string, index int) string {
return fmt.Sprintf("%s %s = %d", datatype, name, index)
},
Expand Down
16 changes: 9 additions & 7 deletions ignite/templates/field/datatype/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
var (
// DataInt is an int data type definition.
DataInt = DataType{
DataType: func(string) string { return "int32" },
DefaultTestValue: "111",
ValueLoop: "int32(i)",
ValueIndex: "0",
ValueInvalidIndex: "100000",
DataType: func(string) string { return "int32" },
CollectionsKeyValueName: func(string) string { return "collections.Int32Key" },
DefaultTestValue: "111",
ValueLoop: "int32(i)",
ValueIndex: "0",
ValueInvalidIndex: "100000",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("int32 %s = %d", name, index)
},
Expand Down Expand Up @@ -45,8 +46,9 @@ var (

// DataIntSlice is an int array data type definition.
DataIntSlice = DataType{
DataType: func(string) string { return "[]int32" },
DefaultTestValue: "1,2,3,4,5",
DataType: func(string) string { return "[]int32" },
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)
},
Expand Down
18 changes: 10 additions & 8 deletions ignite/templates/field/datatype/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
var (
// DataString is a string data type definition.
DataString = DataType{
DataType: func(string) string { return "string" },
DefaultTestValue: "xyz",
ValueLoop: "strconv.Itoa(i)",
ValueIndex: "strconv.Itoa(0)",
ValueInvalidIndex: "strconv.Itoa(100000)",
DataType: func(string) string { return "string" },
CollectionsKeyValueName: func(string) string { return "collections.StringKey" },
DefaultTestValue: "xyz",
ValueLoop: "strconv.Itoa(i)",
ValueIndex: "strconv.Itoa(0)",
ValueInvalidIndex: "strconv.Itoa(100000)",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("string %s = %d", name, index)
},
Expand All @@ -39,8 +40,9 @@ var (

// DataStringSlice is a string array data type definition.
DataStringSlice = DataType{
DataType: func(string) string { return "[]string" },
DefaultTestValue: "abc,xyz",
DataType: func(string) string { return "[]string" },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "abc,xyz",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("repeated string %s = %d", name, index)
},
Expand All @@ -52,9 +54,9 @@ var (
prefix, name.UpperCamel, argIndex)
},
GoCLIImports: []GoImport{{Name: "strings"}},
NonIndex: true,
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "string", index, protoutil.Repeated())
},
NonIndex: true,
}
)
34 changes: 20 additions & 14 deletions ignite/templates/field/datatype/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
Coin Name = "coin"
// Coins represents the coin array type name.
Coins Name = "array.coin"
// Bytes represents the bytes type name.
Bytes Name = "bytes"
// Custom represents the custom type name.
Custom Name = Name(TypeCustom)

Expand All @@ -42,10 +44,13 @@ const (

// TypeCustom represents the string type name id.
TypeCustom = "customstarporttype"

collectionValueComment = "/* Add collection key value */"
)

// supportedTypes all support data types and definitions.
var supportedTypes = map[Name]DataType{
Bytes: DataBytes,
String: DataString,
StringSlice: DataStringSlice,
StringSliceAlias: DataStringSlice,
Expand All @@ -67,20 +72,21 @@ type Name string

// DataType represents the data types for code replacement.
type DataType struct {
DataType func(datatype string) string
ProtoType func(datatype, name string, index int) string
GenesisArgs func(name multiformatname.Name, value int) string
ProtoImports []string
GoCLIImports []GoImport
DefaultTestValue string
ValueLoop string
ValueIndex string
ValueInvalidIndex string
ToBytes func(name string) string
ToString func(name string) string
ToProtoField func(datatype, name string, index int) *proto.NormalField
CLIArgs func(name multiformatname.Name, datatype, prefix string, argIndex int) string
NonIndex bool
DataType func(datatype string) string
ProtoType func(datatype, name string, index int) string
CollectionsKeyValueName func(datatype string) string
GenesisArgs func(name multiformatname.Name, value int) string
ProtoImports []string
GoCLIImports []GoImport
DefaultTestValue string
ValueLoop string
ValueIndex string
ValueInvalidIndex string
ToBytes func(name string) string
ToString func(name string) string
ToProtoField func(datatype, name string, index int) *proto.NormalField
CLIArgs func(name multiformatname.Name, datatype, prefix string, argIndex int) string
NonIndex bool
}

// GoImport represents the go import repo name with the alias.
Expand Down
5 changes: 5 additions & 0 deletions ignite/templates/field/datatype/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func TestIsSupportedType(t *testing.T) {
typename: datatype.Coins,
ok: true,
},
{
name: "bytes",
typename: datatype.Bytes,
ok: true,
},
{
name: "custom",
typename: datatype.Custom,
Expand Down
16 changes: 9 additions & 7 deletions ignite/templates/field/datatype/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
var (
// DataUint uint data type definition.
DataUint = DataType{
DataType: func(string) string { return "uint64" },
DefaultTestValue: "111",
ValueLoop: "uint64(i)",
ValueIndex: "0",
ValueInvalidIndex: "100000",
DataType: func(string) string { return "uint64" },
CollectionsKeyValueName: func(string) string { return "collections.Uint64Key" },
DefaultTestValue: "111",
ValueLoop: "uint64(i)",
ValueIndex: "0",
ValueInvalidIndex: "100000",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("uint64 %s = %d", name, index)
},
Expand Down Expand Up @@ -45,8 +46,9 @@ var (

// DataUintSlice uint array data type definition.
DataUintSlice = DataType{
DataType: func(string) string { return "[]uint64" },
DefaultTestValue: "1,2,3,4,5",
DataType: func(string) string { return "[]uint64" },
CollectionsKeyValueName: func(string) string { return collectionValueComment },
DefaultTestValue: "1,2,3,4,5",
ProtoType: func(_, name string, index int) string {
return fmt.Sprintf("repeated uint64 %s = %d", name, index)
},
Expand Down
10 changes: 10 additions & 0 deletions ignite/templates/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func (f Field) ProtoType(index int) string {
return dt.ProtoType(f.Datatype, f.ProtoFieldName(), index)
}

// CollectionsKeyValueType returns the field collections key value type.
func (f Field) CollectionsKeyValueType() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.CollectionsKeyValueName(f.Datatype)
}

// DefaultTestValue returns the Datatype value default.
func (f Field) DefaultTestValue() string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
Expand Down Expand Up @@ -96,6 +105,7 @@ func (f Field) GenesisArgs(value int) string {
}

// CLIArgs returns the Datatype CLI args.
// TODO(@julienrbrt): Once unused and fully replaced by AutoCLI, remove CLIArgs from DataType.
func (f Field) CLIArgs(prefix string, argIndex int) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
Expand Down
29 changes: 1 addition & 28 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil"
"github.com/ignite/cli/v29/ignite/pkg/xast"
"github.com/ignite/cli/v29/ignite/pkg/xgenny"
"github.com/ignite/cli/v29/ignite/templates/field"
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
"github.com/ignite/cli/v29/ignite/templates/module"
"github.com/ignite/cli/v29/ignite/templates/typed"
Expand Down Expand Up @@ -143,7 +142,7 @@ func keeperModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunF
typed.PlaceholderCollectionInstantiate,
opts.TypeName.UpperCamel,
opts.TypeName.LowerCamel,
dataTypeToCollectionKeyValue(opts.Index),
opts.Index.CollectionsKeyValueType(),
)
content = replacer.Replace(content, typed.PlaceholderCollectionInstantiate, replacementInstantiate)

Expand Down Expand Up @@ -728,29 +727,3 @@ func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny.
return r.File(newFile)
}
}

// dataTypeToCollectionKeyValue returns the date type of the collection value.
// TODO(@julienrbrt): extend support of dataTypeToCollectionKeyValue.
func dataTypeToCollectionKeyValue(f field.Field) string {
var collectionKeyValue string
switch f.DataType() {
case "string":
collectionKeyValue = "collections.StringKey"
case "int32":
collectionKeyValue = "collections.Int32Key"
case "int64":
collectionKeyValue = "collections.Int64Key"
case "uint32":
collectionKeyValue = "collections.Uint32Key"
case "uint64":
collectionKeyValue = "collections.Uint64Key"
case "byte":
collectionKeyValue = "collections.BytesKey"
case "bool":
collectionKeyValue = "collections.BoolKey"
default:
collectionKeyValue = "/* Add collection key value */"
}

return collectionKeyValue
}

0 comments on commit ae58e83

Please sign in to comment.