From a7af2bd0655fd96e56d81297ba879a7e2390fdf6 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 15 Apr 2024 13:59:54 +0200 Subject: [PATCH 1/2] feat(templates): scaffold an array of custom type --- ignite/services/scaffolder/component.go | 6 ++++++ ignite/templates/field/datatype/custom.go | 24 +++++++++++++++++++++++ ignite/templates/field/datatype/types.go | 7 +++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ignite/services/scaffolder/component.go b/ignite/services/scaffolder/component.go index 1c4cf3f64a..abad4f4e7b 100644 --- a/ignite/services/scaffolder/component.go +++ b/ignite/services/scaffolder/component.go @@ -119,9 +119,15 @@ func checkCustomTypes(ctx context.Context, path, appName, module string, fields } if _, ok := datatype.IsSupportedType(datatype.Name(ft)); !ok { + // sanatized the custom type name + if str := strings.Split(ft, "."); len(str) > 1 { // <- sanitize in IsSupportedType + ft = str[1] + } + customFieldTypes = append(customFieldTypes, ft) } } + return protoanalysis.HasMessages(ctx, protoPath, customFieldTypes...) } diff --git a/ignite/templates/field/datatype/custom.go b/ignite/templates/field/datatype/custom.go index 3c4dc00681..0d5ad3f819 100644 --- a/ignite/templates/field/datatype/custom.go +++ b/ignite/templates/field/datatype/custom.go @@ -32,3 +32,27 @@ var DataCustom = DataType{ GoCLIImports: []GoImport{{Name: "encoding/json"}}, NonIndex: true, } + +// DataCustomSlice is a custom data array type definition. +var DataCustomSlice = DataType{ + DataType: func(datatype string) string { return fmt.Sprintf("[]*%s", datatype) }, + DefaultTestValue: "null", + ProtoType: func(datatype, name string, index int) string { + return fmt.Sprintf("repeated %s %s = %d", datatype, name, index) + }, + GenesisArgs: func(name multiformatname.Name, value int) string { + return fmt.Sprintf("%s: []types.%s{},\n", name.UpperCamel, name.UpperCamel) + }, + CLIArgs: func(name multiformatname.Name, datatype, prefix string, argIndex int) string { + return fmt.Sprintf(`%[1]v%[2]v := []types.%[3]v{} + err = json.Unmarshal([]byte(args[%[4]v]), %[1]v%[2]v) + if err != nil { + return err + }`, prefix, name.UpperCamel, datatype, argIndex) + }, + ToProtoField: func(datatype, name string, index int) *proto.NormalField { + return protoutil.NewField(name, datatype, index, protoutil.Repeated()) + }, + GoCLIImports: []GoImport{{Name: "encoding/json"}}, + NonIndex: true, +} diff --git a/ignite/templates/field/datatype/types.go b/ignite/templates/field/datatype/types.go index f0d55b455b..04f1d1db26 100644 --- a/ignite/templates/field/datatype/types.go +++ b/ignite/templates/field/datatype/types.go @@ -30,6 +30,8 @@ const ( Coins Name = "array.coin" // Custom represents the custom type name. Custom Name = Name(TypeCustom) + // CustomSlice represents the custom array type name. + CustomSlice Name = "array." + TypeCustom // StringSliceAlias represents the string array type name alias. StringSliceAlias Name = "strings" @@ -40,8 +42,8 @@ const ( // CoinSliceAlias represents the coin array type name alias. CoinSliceAlias Name = "coins" - // TypeCustom represents the string type name id. - TypeCustom = "customstarporttype" + // TypeCustom represents the custom type name. + TypeCustom = "customignitetype" ) // supportedTypes all support data types and definitions. @@ -60,6 +62,7 @@ var supportedTypes = map[Name]DataType{ Coins: DataCoinSlice, CoinSliceAlias: DataCoinSlice, Custom: DataCustom, + CustomSlice: DataCustomSlice, } // Name represents the Alias Name for the data type. From 78342c0ba55da17b7350fe8369a403f2c8b22aa0 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 10 May 2024 14:57:59 +0200 Subject: [PATCH 2/2] typo --- ignite/services/scaffolder/component.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ignite/services/scaffolder/component.go b/ignite/services/scaffolder/component.go index db13a1043a..c5b478f339 100644 --- a/ignite/services/scaffolder/component.go +++ b/ignite/services/scaffolder/component.go @@ -117,7 +117,7 @@ func checkCustomTypes(ctx context.Context, appPath, appName, protoDir, module st } if _, ok := datatype.IsSupportedType(datatype.Name(ft)); !ok { - // sanatized the custom type name + // sanitize the custom type name if str := strings.Split(ft, "."); len(str) > 1 { // <- sanitize in IsSupportedType ft = str[1] } @@ -126,7 +126,7 @@ func checkCustomTypes(ctx context.Context, appPath, appName, protoDir, module st } } - return protoanalysis.HasMessages(ctx, path, customFieldTypes...) + return protoanalysis.HasMessages(ctx, path, customFieldTypes...) } // checkForbiddenComponentName returns true if the name is forbidden as a component name.