Skip to content

Commit

Permalink
add parameters placeholders and modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Nov 22, 2023
1 parent 9fe1f4c commit 5cd468e
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 13 deletions.
8 changes: 8 additions & 0 deletions ignite/templates/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>\""];<% } %>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -68,4 +77,6 @@ func validate<%= param.Name.UpperCamel %>(v interface{}) error {

return nil
}
<% } %>
<% } %>

// this line is used by starport scaffolding # types/params/validation
7 changes: 7 additions & 0 deletions ignite/templates/module/create/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
122 changes: 122 additions & 0 deletions ignite/templates/module/create/params.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 9 additions & 0 deletions ignite/templates/module/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 5cd468e

Please sign in to comment.