From 5cd468ea02efd8513e5040ce6cf23874d9524f79 Mon Sep 17 00:00:00 2001 From: Pantani Date: Thu, 23 Nov 2023 00:23:35 +0100 Subject: [PATCH] add parameters placeholders and modifier --- ignite/templates/field/field.go | 8 ++ .../{{moduleName}}/params.proto.plush | 1 - .../x/{{moduleName}}/types/params.go.plush | 35 +++-- ignite/templates/module/create/options.go | 7 + ignite/templates/module/create/params.go | 122 ++++++++++++++++++ ignite/templates/module/placeholders.go | 9 ++ 6 files changed, 169 insertions(+), 13 deletions(-) create mode 100644 ignite/templates/module/create/params.go diff --git a/ignite/templates/field/field.go b/ignite/templates/field/field.go index 62fdee9115..5922f05c95 100644 --- a/ignite/templates/field/field.go +++ b/ignite/templates/field/field.go @@ -155,3 +155,11 @@ func (f Field) ProtoImports() []string { } return dt.ProtoImports } + +// Value returns the field assign value. +func (f Field) Value() string { + if f.DataType() == "string" { + return f.Name.Snake + } + return f.ValueIndex() +} diff --git a/ignite/templates/module/create/files/base/proto/{{appName}}/{{moduleName}}/params.proto.plush b/ignite/templates/module/create/files/base/proto/{{appName}}/{{moduleName}}/params.proto.plush index 1790a2ee02..05eb8fdc68 100644 --- a/ignite/templates/module/create/files/base/proto/{{appName}}/{{moduleName}}/params.proto.plush +++ b/ignite/templates/module/create/files/base/proto/{{appName}}/{{moduleName}}/params.proto.plush @@ -10,7 +10,6 @@ option go_package = "<%= modulePath %>/x/<%= moduleName %>/types"; message Params { option (amino.name) = "<%= appName %>/x/<%= moduleName %>/Params"; option (gogoproto.equal) = true; - <%= for (i, param) in params { %> <%= param.ProtoType(i+1) %> [(gogoproto.moretags) = "yaml:\"<%= param.Name.Snake %>\""];<% } %> } \ No newline at end of file diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush index dbe9b6c49b..8cd9614a0a 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/params.go.plush @@ -10,53 +10,62 @@ var _ paramtypes.ParamSet = (*Params)(nil) <%= for (param) in params { %> var ( - Key<%= param.Name.UpperCamel %> = []byte("<%= param.Name.UpperCamel %>")<%= if (param.DataType() == "string") { %> - // TODO: Determine the default value - Default<%= param.Name.UpperCamel %> <%= param.DataType() %> = "<%= param.Name.Snake %>"<% } else { %> - // TODO: Determine the default value - Default<%= param.Name.UpperCamel %> <%= param.DataType() %> = <%= param.ValueIndex() %><% } %> + // Key<%= param.Name.UpperCamel %> represents the <%= param.Name.UpperCamel %> parameter. + Key<%= param.Name.UpperCamel %> = []byte("<%= param.Name.UpperCamel %>") + // TODO: Determine the default value. + // Default<%= param.Name.UpperCamel %> represents the <%= param.Name.UpperCamel %> default value. + Default<%= param.Name.UpperCamel %> <%= param.DataType() %> = <%= param.Value() %> ) <% } %> -// ParamKeyTable the param key table for launch module +// this line is used by starport scaffolding # types/params/vars + +// ParamKeyTable the param key table for launch module. func ParamKeyTable() paramtypes.KeyTable { return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) } -// NewParams creates a new Params instance +// NewParams creates a new Params instance. func NewParams(<%= for (param) in params { %> <%= param.Name.LowerCamel %> <%= param.DataType() %>,<% } %> + // this line is used by starport scaffolding # types/params/new/parameter ) Params { return Params{<%= for (param) in params { %> <%= param.Name.UpperCamel %>: <%= param.Name.LowerCamel %>,<% } %> + // this line is used by starport scaffolding # types/params/new/struct } } -// DefaultParams returns a default set of parameters +// DefaultParams returns a default set of parameters. func DefaultParams() Params { return NewParams(<%= for (param) in params { %> Default<%= param.Name.UpperCamel %>,<% } %> + // this line is used by starport scaffolding # types/params/default ) } -// ParamSetPairs get the params.ParamSet +// ParamSetPairs get the params.ParamSet. func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{<%= for (param) in params { %> paramtypes.NewParamSetPair(Key<%= param.Name.UpperCamel %>, &p.<%= param.Name.UpperCamel %>, validate<%= param.Name.UpperCamel %>),<% } %> + // this line is used by starport scaffolding # types/params/setpairs } } -// Validate validates the set of params +// Validate validates the set of params. func (p Params) Validate() error {<%= for (param) in params { %> if err := validate<%= param.Name.UpperCamel %>(p.<%= param.Name.UpperCamel %>); err != nil { return err } <% } %> + + // this line is used by starport scaffolding # types/params/validate + return nil } <%= for (param) in params { %> -// validate<%= param.Name.UpperCamel %> validates the <%= param.Name.UpperCamel %> param +// validate<%= param.Name.UpperCamel %> validates the <%= param.Name.UpperCamel %> parameter. func validate<%= param.Name.UpperCamel %>(v interface{}) error { <%= param.Name.LowerCamel %>, ok := v.(<%= param.DataType() %>) if !ok { @@ -68,4 +77,6 @@ func validate<%= param.Name.UpperCamel %>(v interface{}) error { return nil } -<% } %> \ No newline at end of file +<% } %> + +// this line is used by starport scaffolding # types/params/validation \ No newline at end of file diff --git a/ignite/templates/module/create/options.go b/ignite/templates/module/create/options.go index fc09404a0b..3baaf8c809 100644 --- a/ignite/templates/module/create/options.go +++ b/ignite/templates/module/create/options.go @@ -9,6 +9,13 @@ import ( ) type ( + // ParamsOptions represents the options to scaffold a Cosmos SDK module parameters. + ParamsOptions struct { + ModuleName string + AppPath string + Params field.Fields + } + // CreateOptions represents the options to scaffold a Cosmos SDK module. CreateOptions struct { ModuleName string diff --git a/ignite/templates/module/create/params.go b/ignite/templates/module/create/params.go new file mode 100644 index 0000000000..659d4bb1d6 --- /dev/null +++ b/ignite/templates/module/create/params.go @@ -0,0 +1,122 @@ +package modulecreate + +import ( + "fmt" + "path/filepath" + + "github.com/gobuffalo/genny/v2" + + "github.com/ignite/cli/ignite/pkg/placeholder" + "github.com/ignite/cli/ignite/templates/module" +) + +func paramsTypesModify(replacer placeholder.Replacer, opts ParamsOptions) genny.RunFn { + return func(r *genny.Runner) error { + path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/params.go") + f, err := r.Disk.Find(path) + if err != nil { + return err + } + + content := f.String() + for _, param := range opts.Params { + // param key and default value. + templateVars := `var ( + // Key%[2]v represents the %[2]v parameter. + Key%[2]v = []byte("%[2]v") + // TODO: Determine the default value + // Default%[2]v represents the %[2]v default value. + Default%[2]v %[3]v = %[4]v +) + +%[1]v` + replacementVars := fmt.Sprintf( + templateVars, + module.PlaceholderParamsVars, + param.Name.UpperCamel, + param.DataType(), + param.Value(), + ) + content = replacer.Replace(content, module.PlaceholderParamsVars, replacementVars) + + // add parameter to the new method. + templateNewParam := "%[2]v %[3]v,\n%[1]v" + replacementNewParam := fmt.Sprintf( + templateNewParam, + module.PlaceholderParamsNewParam, + param.Name.LowerCamel, + param.DataType(), + ) + content = replacer.Replace(content, module.PlaceholderParamsNewParam, replacementNewParam) + + // add parameter to the struct into the new method. + templateNewStruct := "%[2]v,\n%[1]v" + replacementNewStruct := fmt.Sprintf( + templateNewStruct, + module.PlaceholderParamsNewStruct, + param.Name.LowerCamel, + ) + content = replacer.Replace(content, module.PlaceholderParamsNewStruct, replacementNewStruct) + + // add default parameter. + templateDefault := `Default%[2]v, +%[1]v` + replacementDefault := fmt.Sprintf( + templateDefault, + module.PlaceholderParamsDefault, + param.Name.UpperCamel, + ) + content = replacer.Replace(content, module.PlaceholderParamsDefault, replacementDefault) + + // add new param set pair. + templateSetPairs := `paramtypes.NewParamSetPair(Key%[2]v, &p.%[2]v, validate%[2]v), +%[1]v` + replacementSetPairs := fmt.Sprintf( + templateSetPairs, + module.PlaceholderParamsSetPairs, + param.Name.UpperCamel, + ) + content = replacer.Replace(content, module.PlaceholderParamsSetPairs, replacementSetPairs) + + // add param field to the validate method. + templateValidate := `if err := validate%[2]v(p.%[2]v); err != nil { + return err + } + %[1]v` + replacementValidate := fmt.Sprintf( + templateValidate, + module.PlaceholderParamsValidate, + param.Name.UpperCamel, + ) + content = replacer.Replace(content, module.PlaceholderParamsValidate, replacementValidate) + + // add param field to the validate method. + templateValidation := `// validate%[2]v validates the %[2]v parameter. +func validate%[2]v(v interface{}) error { + %[3]v, ok := v.(%[4]v) + if !ok { + return fmt.Errorf("invalid parameter type: %T", v) + } + + // TODO implement validation + _ = %[3]v + + return nil +} + +%[1]v` + replacementValidation := fmt.Sprintf( + templateValidation, + module.PlaceholderParamsValidation, + param.Name.UpperCamel, + param.Name.LowerCamel, + param.DataType(), + ) + content = replacer.Replace(content, module.PlaceholderParamsValidation, replacementValidation) + + } + + newFile := genny.NewFileS(path, content) + return r.File(newFile) + } +} diff --git a/ignite/templates/module/placeholders.go b/ignite/templates/module/placeholders.go index d8cf25041a..7ece674d37 100644 --- a/ignite/templates/module/placeholders.go +++ b/ignite/templates/module/placeholders.go @@ -27,4 +27,13 @@ const ( PlaceholderTypesGenesisValidField = "// this line is used by starport scaffolding # types/genesis/validField" PlaceholderGenesisTestState = "// this line is used by starport scaffolding # genesis/test/state" PlaceholderGenesisTestAssert = "// this line is used by starport scaffolding # genesis/test/assert" + + // Params + PlaceholderParamsVars = "// this line is used by starport scaffolding # types/params/vars" + PlaceholderParamsNewParam = "// this line is used by starport scaffolding # types/params/new/parameter" + PlaceholderParamsNewStruct = "// this line is used by starport scaffolding # types/params/new/struct" + PlaceholderParamsDefault = "// this line is used by starport scaffolding # types/params/default" + PlaceholderParamsSetPairs = "// this line is used by starport scaffolding # types/params/setpairs" + PlaceholderParamsValidate = "// this line is used by starport scaffolding # types/params/validate" + PlaceholderParamsValidation = "// this line is used by starport scaffolding # types/params/validation" )