From ae58e8306fa7362be426e43c6e9478fb6d60a704 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 15 May 2024 13:46:53 +0200 Subject: [PATCH] feat(templates): add `CollectionsKeyValueName` to `field.DataType` (#4131) * feat(templates): add `CollectionsKeyValueName` to `field.DataType` * pr number * typo * imports Co-authored-by: Danilo Pantani * fix cache * feedback --------- Co-authored-by: Danilo Pantani --- .github/workflows/test-lint.yml | 4 +-- changelog.md | 1 + ignite/templates/field/datatype/bool.go | 11 +++--- ignite/templates/field/datatype/bytes.go | 36 +++++++++++++++++++ ignite/templates/field/datatype/coin.go | 10 +++--- ignite/templates/field/datatype/custom.go | 5 +-- ignite/templates/field/datatype/int.go | 16 +++++---- ignite/templates/field/datatype/string.go | 18 +++++----- ignite/templates/field/datatype/types.go | 34 ++++++++++-------- ignite/templates/field/datatype/types_test.go | 5 +++ ignite/templates/field/datatype/uint.go | 16 +++++---- ignite/templates/field/field.go | 10 ++++++ ignite/templates/typed/map/map.go | 29 +-------------- 13 files changed, 117 insertions(+), 78 deletions(-) create mode 100644 ignite/templates/field/datatype/bytes.go diff --git a/.github/workflows/test-lint.yml b/.github/workflows/test-lint.yml index 3bdfdaab3b..aad15e59e4 100644 --- a/.github/workflows/test-lint.yml +++ b/.github/workflows/test-lint.yml @@ -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 diff --git a/changelog.md b/changelog.md index 4049c2b245..9274a7e071 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/ignite/templates/field/datatype/bool.go b/ignite/templates/field/datatype/bool.go index 96852439fe..063a4f3dc4 100644 --- a/ignite/templates/field/datatype/bool.go +++ b/ignite/templates/field/datatype/bool.go @@ -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) }, diff --git a/ignite/templates/field/datatype/bytes.go b/ignite/templates/field/datatype/bytes.go new file mode 100644 index 0000000000..eee3fafc49 --- /dev/null +++ b/ignite/templates/field/datatype/bytes.go @@ -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, +} diff --git a/ignite/templates/field/datatype/coin.go b/ignite/templates/field/datatype/coin.go index 7310c30daf..38279af97a 100644 --- a/ignite/templates/field/datatype/coin.go +++ b/ignite/templates/field/datatype/coin.go @@ -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) @@ -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) diff --git a/ignite/templates/field/datatype/custom.go b/ignite/templates/field/datatype/custom.go index 6c1877b1be..614967e7ed 100644 --- a/ignite/templates/field/datatype/custom.go +++ b/ignite/templates/field/datatype/custom.go @@ -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) }, diff --git a/ignite/templates/field/datatype/int.go b/ignite/templates/field/datatype/int.go index 2ca5fd1dba..f1bd859e8d 100644 --- a/ignite/templates/field/datatype/int.go +++ b/ignite/templates/field/datatype/int.go @@ -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) }, @@ -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) }, diff --git a/ignite/templates/field/datatype/string.go b/ignite/templates/field/datatype/string.go index 374e6f856d..d1253e2d1c 100644 --- a/ignite/templates/field/datatype/string.go +++ b/ignite/templates/field/datatype/string.go @@ -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) }, @@ -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) }, @@ -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, } ) diff --git a/ignite/templates/field/datatype/types.go b/ignite/templates/field/datatype/types.go index f0d55b455b..c817cea413 100644 --- a/ignite/templates/field/datatype/types.go +++ b/ignite/templates/field/datatype/types.go @@ -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) @@ -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, @@ -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. diff --git a/ignite/templates/field/datatype/types_test.go b/ignite/templates/field/datatype/types_test.go index fc16e824d4..5cf3530816 100644 --- a/ignite/templates/field/datatype/types_test.go +++ b/ignite/templates/field/datatype/types_test.go @@ -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, diff --git a/ignite/templates/field/datatype/uint.go b/ignite/templates/field/datatype/uint.go index 6d050f7046..4eb08f8836 100644 --- a/ignite/templates/field/datatype/uint.go +++ b/ignite/templates/field/datatype/uint.go @@ -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) }, @@ -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) }, diff --git a/ignite/templates/field/field.go b/ignite/templates/field/field.go index dd17af2903..13a4d44ebc 100644 --- a/ignite/templates/field/field.go +++ b/ignite/templates/field/field.go @@ -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) @@ -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 { diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 9efc0509ca..9b746a95cb 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -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" @@ -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) @@ -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 -}