From 90247e8d97f10d2ee1a959dd1463df6a72f40d06 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 13 Dec 2024 00:44:47 -0300 Subject: [PATCH] funcstruct --- ignite/pkg/xast/function.go | 30 +++++++++---------- ignite/pkg/xast/function_test.go | 8 ++--- ignite/templates/module/create/ibc.go | 2 +- ignite/templates/module/create/params.go | 2 +- ignite/templates/typed/list/genesis.go | 10 +++---- ignite/templates/typed/list/list.go | 4 +-- ignite/templates/typed/list/simulation.go | 4 +-- ignite/templates/typed/map/map.go | 8 ++--- ignite/templates/typed/map/simulation.go | 2 +- ignite/templates/typed/singleton/singleton.go | 8 ++--- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/ignite/pkg/xast/function.go b/ignite/pkg/xast/function.go index cde783d968..bd6f238630 100644 --- a/ignite/pkg/xast/function.go +++ b/ignite/pkg/xast/function.go @@ -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 @@ -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, }) } } @@ -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. @@ -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), } diff --git a/ignite/pkg/xast/function_test.go b/ignite/pkg/xast/function_test.go index 111cf89e5c..1853ac5003 100644 --- a/ignite/pkg/xast/function_test.go +++ b/ignite/pkg/xast/function_test.go @@ -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{}, @@ -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 diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 45d2d2f695..e51b2d7050 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -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 diff --git a/ignite/templates/module/create/params.go b/ignite/templates/module/create/params.go index d6b89a4103..e8c2575f9e 100644 --- a/ignite/templates/module/create/params.go +++ b/ignite/templates/module/create/params.go @@ -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, diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index ca088ab170..48fa10663c 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -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), @@ -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", @@ -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", diff --git a/ignite/templates/typed/list/list.go b/ignite/templates/typed/list/list.go index 09174380fd..0ed1cf6843 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -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")`, @@ -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))`, diff --git a/ignite/templates/typed/list/simulation.go b/ignite/templates/typed/list/simulation.go index f509019a77..56b17f8bdd 100644 --- a/ignite/templates/typed/list/simulation.go +++ b/ignite/templates/typed/list/simulation.go @@ -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( @@ -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", diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 5b25895856..bfa5b50b23 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -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))`, @@ -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), @@ -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( @@ -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( diff --git a/ignite/templates/typed/map/simulation.go b/ignite/templates/typed/map/simulation.go index c951c2f994..ccd96dc1a1 100644 --- a/ignite/templates/typed/map/simulation.go +++ b/ignite/templates/typed/map/simulation.go @@ -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( diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index 8d2279ddfa..736bf193c2 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -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))`, @@ -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", @@ -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), @@ -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),