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(templates): scaffold an array of custom type #4081

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 6 additions & 0 deletions ignite/services/scaffolder/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,15 @@ func checkCustomTypes(ctx context.Context, appPath, appName, protoDir, module st
}

if _, ok := datatype.IsSupportedType(datatype.Name(ft)); !ok {
// sanitize 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, path, customFieldTypes...)
}

Expand Down
24 changes: 24 additions & 0 deletions ignite/templates/field/datatype/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,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,
}
7 changes: 5 additions & 2 deletions ignite/templates/field/datatype/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
Bytes Name = "bytes"
// 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"
Expand All @@ -42,8 +44,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"

collectionValueComment = "/* Add collection key value */"
)
Expand All @@ -65,6 +67,7 @@ var supportedTypes = map[Name]DataType{
Coins: DataCoinSlice,
CoinSliceAlias: DataCoinSlice,
Custom: DataCustom,
CustomSlice: DataCustomSlice,
}

// Name represents the Alias Name for the data type.
Expand Down
Loading