Skip to content

Commit

Permalink
funcstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Dec 13, 2024
1 parent 34d3fbb commit 90247e8
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 39 deletions.
30 changes: 15 additions & 15 deletions ignite/pkg/xast/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type (
FunctionOptions func(*functionOpts)

functionStruct struct {
structName string
paramName string
code string
index int
name string
param string
code string
index int
}
functionCall struct {
name string
Expand Down Expand Up @@ -104,16 +104,16 @@ func AppendInsideFuncCall(callName, code string, index int) FunctionOptions {
}
}

// AppendInsideFuncStruct add code inside another function call. For instances,
// AppendFuncStruct add code inside another function call. For instances,
// the struct have only one parameter 'Params{Param1: param1}' and we want to add
// the param2 the result will be 'Params{Param1: param1, Param2: param2}'.
func AppendInsideFuncStruct(structName, paramName, code string, index int) FunctionOptions {
func AppendFuncStruct(name, param, code string, index int) FunctionOptions {
return func(c *functionOpts) {
c.insideStruct = append(c.insideStruct, functionStruct{
structName: structName,
paramName: paramName,
code: code,
index: index,
name: name,
param: param,
code: code,
index: index,
})
}
}
Expand Down Expand Up @@ -198,12 +198,12 @@ func ModifyFunction(fileContent, functionName string, functions ...FunctionOptio
structMap := make(map[string][]functionStruct)
structMapCheck := make(map[string][]functionStruct)
for _, s := range opts.insideStruct {
structs, ok := structMap[s.structName]
structs, ok := structMap[s.name]
if !ok {
structs = []functionStruct{}
}
structMap[s.structName] = append(structs, s)
structMapCheck[s.structName] = append(structs, s)
structMap[s.name] = append(structs, s)
structMapCheck[s.name] = append(structs, s)
}

// Parse the Go code to insert.
Expand Down Expand Up @@ -359,9 +359,9 @@ func ModifyFunction(fileContent, functionName string, functions ...FunctionOptio
// Construct the new argument to be added
for _, s := range structs {
var newArg ast.Expr = ast.NewIdent(s.code)
if s.paramName != "" {
if s.param != "" {
newArg = &ast.KeyValueExpr{
Key: ast.NewIdent(s.paramName),
Key: ast.NewIdent(s.param),
Colon: token.Pos(s.index),
Value: ast.NewIdent(s.code),
}
Expand Down
8 changes: 4 additions & 4 deletions ignite/pkg/xast/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestValidate(t *testing.T) {
AppendInsideFuncCall("SimpleCall", "baz", 0),
AppendInsideFuncCall("SimpleCall", "bla", -1),
AppendInsideFuncCall("Println", strconv.Quote("test"), -1),
AppendInsideFuncStruct("Param", "Bar", strconv.Quote("bar"), -1),
AppendFuncStruct("Param", "Bar", strconv.Quote("bar"), -1),
AppendFuncTestCase(`{
desc: "valid first genesis state",
genState: GenesisState{},
Expand Down Expand Up @@ -448,9 +448,9 @@ func TestValidate(t *testing.T) {
`,
functionName: "anotherFunction",
functions: []FunctionOptions{
AppendInsideFuncStruct("Param", "Bar", "bar", -1),
AppendInsideFuncStruct("Param", "Bla", "bla", 1),
AppendInsideFuncStruct("Client", "", "bar", 0),
AppendFuncStruct("Param", "Bar", "bar", -1),
AppendFuncStruct("Param", "Bla", "bla", 1),
AppendFuncStruct("Client", "", "bar", 0),
},
},
want: `package main
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func genesisTypesModify(opts *CreateOptions) genny.RunFn {
content, err = xast.ModifyFunction(
content,
"DefaultGenesis",
xast.AppendInsideFuncStruct("GenesisState", "PortId", "PortID", -1),
xast.AppendFuncStruct("GenesisState", "PortId", "PortID", -1),
)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/module/create/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func paramsTypesModify(opts ParamsOptions) genny.RunFn {
newParamsModifier = append(
newParamsModifier,
xast.AppendFuncParams(param.Name.LowerCamel, param.DataType(), -1),
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"Params",
param.Name.UpperCamel,
param.Name.LowerCamel,
Expand Down
10 changes: 5 additions & 5 deletions ignite/templates/typed/list/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
}

// add parameter to the struct into the new method.
content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendInsideFuncStruct(
content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]%[1]v{}", opts.TypeName.UpperCamel),
Expand Down Expand Up @@ -199,13 +199,13 @@ require.Equal(t, genesisState.%[1]vCount, got.%[1]vCount)`, opts.TypeName.UpperC
content, err := xast.ModifyFunction(
f.String(),
"TestGenesis",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]types.%[1]v{{ Id: 0 }, { Id: 1 }}", opts.TypeName.UpperCamel),
-1,
),
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
Expand Down Expand Up @@ -272,13 +272,13 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"TestGenesisState_Validate",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]types.%[1]v{{ Id: 0 }, { Id: 1 }}", opts.TypeName.UpperCamel),
-1,
),
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func keeperModify(opts *typed.Options) genny.RunFn {
content, err = xast.ModifyFunction(
content,
"NewKeeper",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"Keeper",
fmt.Sprintf("%[1]vSeq", opts.TypeName.UpperCamel),
fmt.Sprintf(`collections.NewSequence(sb, types.%[1]vCountKey, "%[2]v_seq")`,
Expand All @@ -345,7 +345,7 @@ func keeperModify(opts *typed.Options) genny.RunFn {
),
-1,
),
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"Keeper",
opts.TypeName.UpperCamel,
fmt.Sprintf(`collections.NewMap(sb, types.%[1]vKey, "%[2]v", collections.Uint64Key, codec.CollValue[types.%[1]v](cdc))`,
Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/typed/list/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func moduleSimulationModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"GenerateGenesisState",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf(
Expand All @@ -35,7 +35,7 @@ func moduleSimulationModify(opts *typed.Options) genny.RunFn {
),
-1,
),
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vCount", opts.TypeName.UpperCamel),
"2",
Expand Down
8 changes: 4 additions & 4 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func keeperModify(opts *typed.Options) genny.RunFn {
content, err = xast.ModifyFunction(
content,
"NewKeeper",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"Keeper",
opts.TypeName.UpperCamel,
fmt.Sprintf(`collections.NewMap(sb, types.%[1]vKey, "%[2]v", %[3]v, codec.CollValue[types.%[1]v](cdc))`,
Expand Down Expand Up @@ -364,7 +364,7 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
return err
}

content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendInsideFuncStruct(
content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf("[]%[1]v{}", opts.TypeName.UpperCamel),
Expand Down Expand Up @@ -478,7 +478,7 @@ func genesisTestsModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"TestGenesis",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf(
Expand Down Expand Up @@ -537,7 +537,7 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"TestGenesisState_Validate",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/typed/map/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func moduleSimulationModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"GenerateGenesisState",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel),
fmt.Sprintf(
Expand Down
8 changes: 4 additions & 4 deletions ignite/templates/typed/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func keeperModify(opts *typed.Options) genny.RunFn {
content, err = xast.ModifyFunction(
content,
"NewKeeper",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"Keeper",
opts.TypeName.UpperCamel,
fmt.Sprintf(`collections.NewItem(sb, types.%[1]vKey, "%[2]v", codec.CollValue[types.%[1]v](cdc))`,
Expand Down Expand Up @@ -284,7 +284,7 @@ func genesisTypesModify(opts *typed.Options) genny.RunFn {
return err
}

content, err := xast.ModifyFunction(f.String(), "DefaultGenesis", xast.AppendInsideFuncStruct(
content, err := xast.ModifyFunction(f.String(), "DefaultGenesis", xast.AppendFuncStruct(
"GenesisState",
fmt.Sprintf("%[1]v", opts.TypeName.UpperCamel),
"nil",
Expand Down Expand Up @@ -320,7 +320,7 @@ func genesisTestsModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"TestGenesis",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
opts.TypeName.UpperCamel,
fmt.Sprintf("&types.%[1]v{ %[2]v }", opts.TypeName.UpperCamel, sampleFields),
Expand Down Expand Up @@ -361,7 +361,7 @@ func genesisTypesTestsModify(opts *typed.Options) genny.RunFn {
content, err := xast.ModifyFunction(
f.String(),
"TestGenesisState_Validate",
xast.AppendInsideFuncStruct(
xast.AppendFuncStruct(
"GenesisState",
opts.TypeName.UpperCamel,
fmt.Sprintf("&types.%[1]v{ %[2]v }", opts.TypeName.UpperCamel, sampleFields),
Expand Down

0 comments on commit 90247e8

Please sign in to comment.