Skip to content
Merged
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

## Unreleased

### Features

- [#4790](https://github.com/ignite/cli/pull/4790) Remove global vars and struct placeholders.

## [`v29.3.1`](https://github.com/ignite/cli/releases/tag/v29.3.1)

### Fixes

- [#4793](https://github.com/ignite/cli/pull/4793) Use latest `bytedance/sonic` version to support Go 1.25.


## [`v29.3.0`](https://github.com/ignite/cli/releases/tag/v29.3.0)

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ var (
{Account: ibctransfertypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
{Account: ibcfeetypes.ModuleName},
{Account: icatypes.ModuleName},
// this line is used by starport scaffolding # stargate/app/maccPerms
}

// blocked account addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ var (
{Account: ibctransfertypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
{Account: ibcfeetypes.ModuleName},
{Account: icatypes.ModuleName},
// this line is used by starport scaffolding # stargate/app/maccPerms
}

// blocked account addresses
Expand Down
117 changes: 116 additions & 1 deletion ignite/pkg/xast/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type (
values []structValue
}

// StructOpts configures code generation.
// StructOpts configures struct changes.
StructOpts func(*structOpts)

structValue struct {
Expand Down Expand Up @@ -286,3 +286,118 @@ func ModifyStruct(fileContent, structName string, options ...StructOpts) (string
// Return the modified content.
return buf.String(), nil
}

type (
// globalArrayOpts represent the options for globar array variables.
globalArrayOpts struct {
values []string
}

// GlobalArrayOpts configures global array variable changes.
GlobalArrayOpts func(*globalArrayOpts)
)

// AppendGlobalArrayValue add a new value inside a global array variable. For instances,
// the variable have only one field '[]]' and we want to add
// the `test2 int` the result will be 'test struct{ test1 string, test int }'.
func AppendGlobalArrayValue(value string) GlobalArrayOpts {
return func(c *globalArrayOpts) {
c.values = append(c.values, value)
}
}

func newGlobalArrayOptions() globalArrayOpts {
return globalArrayOpts{
values: make([]string, 0),
}
}

// ModifyGlobalArrayVar modifies an array global array variable in the provided Go source code by appending new values.
func ModifyGlobalArrayVar(fileContent, globalName string, options ...GlobalArrayOpts) (string, error) {
opts := newGlobalArrayOptions()
for _, o := range options {
o(&opts)
}

fileSet := token.NewFileSet()

f, err := parser.ParseFile(fileSet, "", fileContent, parser.ParseComments)
if err != nil {
return "", err
}
cmap := ast.NewCommentMap(fileSet, f, f.Comments)

var found bool
ast.Inspect(f, func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
return true
}

for _, spec := range genDecl.Specs {
valueSpec, ok := spec.(*ast.ValueSpec)
if !ok || len(valueSpec.Names) == 0 || valueSpec.Names[0].Name != globalName {
continue
}

if len(valueSpec.Values) == 0 {
continue
}

compLit, ok := valueSpec.Values[0].(*ast.CompositeLit)
if !ok {
continue
}

file := fileSet.File(compLit.Pos())
maxOffset := file.Offset(compLit.Rbrace)
for _, elt := range compLit.Elts {
if pos := elt.End(); pos.IsValid() {
offset := file.Offset(pos)
if offset > maxOffset {
maxOffset = offset
}
}
}

for i, v := range opts.values {
// Advance position
insertOffset := maxOffset + i
insertPos := file.Pos(insertOffset)

value := ast.NewIdent(v)
value.NamePos = insertPos

compLit.Elts = append(compLit.Elts, value)
compLit.Rbrace += token.Pos(i + 1)
}

// Ensure closing brace is on a new line and add comma after last element
if len(compLit.Elts) > 0 {
last := compLit.Elts[len(compLit.Elts)-1]
if file.Line(compLit.Rbrace) == file.Line(last.End())-1 {
// Add comma after last element
file.AddLine(file.Offset(compLit.Rbrace))
compLit.Rbrace += token.Pos(1)
}
}

found = true
return false // Stop searching once we modify the struct.
}
return true
})

if !found {
return "", errors.Errorf("global array %q not found in file content", globalName)
}

f.Comments = cmap.Filter(f).Comments()

var buf bytes.Buffer
if err := format.Node(&buf, fileSet, f); err != nil {
return "", err
}

return buf.String(), nil
}
128 changes: 128 additions & 0 deletions ignite/pkg/xast/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ import (
var myIntVar int = 42

// This is a comment
`,
},
{
name: "Insert global int var without type",
args: args{
fileContent: `package main

import (
"fmt"
)

`,
globalType: GlobalTypeVar,
globals: []GlobalOptions{
WithGlobal("myIntVar", "", "42"),
},
},
want: `package main

import (
"fmt"
)

var myIntVar = 42
`,
},
{
Expand Down Expand Up @@ -540,3 +564,107 @@ type MyStruct struct {
})
}
}

func TestModifyGlobalArrayVar(t *testing.T) {
type args struct {
fileContent string
globalName string
options []GlobalArrayOpts
}
tests := []struct {
name string
args args
want string
err error
}{
{
name: "Add field to custom variable array",
args: args{
fileContent: `package app
var (
moduleAccPerms = []*authmodulev1.ModuleAccountPermission{
{Account: nft.ModuleName},
{Account: ibctransfertypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
}
)
`,
globalName: "moduleAccPerms",
options: []GlobalArrayOpts{AppendGlobalArrayValue("{Account: icatypes.ModuleName}")},
},
want: `package app

var (
moduleAccPerms = []*authmodulev1.ModuleAccountPermission{
{Account: nft.ModuleName},
{Account: ibctransfertypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
{Account: icatypes.ModuleName},
}
)
`,
},
{
name: "Add field to string variable array",
args: args{
fileContent: `package app

var (
blockAccAddrs = []string{
authtypes.FeeCollectorName,
distrtypes.ModuleName,
minttypes.ModuleName,
stakingtypes.BondedPoolName,
stakingtypes.NotBondedPoolName,
}
)
`,
globalName: "blockAccAddrs",
options: []GlobalArrayOpts{AppendGlobalArrayValue("nft.ModuleName")},
},
want: `package app

var (
blockAccAddrs = []string{
authtypes.FeeCollectorName,
distrtypes.ModuleName,
minttypes.ModuleName,
stakingtypes.BondedPoolName,
stakingtypes.NotBondedPoolName,
nft.ModuleName,
}
)
`,
},
{
name: "name not found",
args: args{
fileContent: `package app

var (
blockAccAddrs = []string{
authtypes.FeeCollectorName,
distrtypes.ModuleName,
minttypes.ModuleName,
stakingtypes.BondedPoolName,
stakingtypes.NotBondedPoolName,
}
)
`,
globalName: "notFound",
options: []GlobalArrayOpts{AppendGlobalArrayValue("nft.ModuleName")},
},
err: errors.New("global array \"notFound\" not found in file content"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ModifyGlobalArrayVar(tt.args.fileContent, tt.args.globalName, tt.args.options...)
if tt.err != nil {
require.Error(t, err)
require.Equal(t, tt.err.Error(), err.Error())
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
2 changes: 0 additions & 2 deletions ignite/templates/app/files-minimal/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ type App struct {
StakingKeeper *stakingkeeper.Keeper
DistrKeeper distrkeeper.Keeper

// this line is used by starport scaffolding # stargate/app/keeperDeclaration

// simulation manager
sm *module.SimulationManager
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
{Account: stakingtypes.NotBondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
// this line is used by starport scaffolding # stargate/app/maccPerms
}

// blocked account addresses
Expand Down
2 changes: 0 additions & 2 deletions ignite/templates/app/files/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ type App struct {
ICAHostKeeper icahostkeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper

// this line is used by starport scaffolding # stargate/app/keeperDeclaration

// simulation manager
sm *module.SimulationManager
}
Expand Down
1 change: 0 additions & 1 deletion ignite/templates/app/files/app/app_config.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ var (
{Account: nft.ModuleName},
{Account: ibctransfertypes.ModuleName, Permissions: []string{authtypes.Minter, authtypes.Burner}},
{Account: icatypes.ModuleName},
// this line is used by starport scaffolding # stargate/app/maccPerms
}

// blocked account addresses
Expand Down
24 changes: 14 additions & 10 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Gener
g := genny.New()
g.RunFn(moduleModify(replacer, opts))
g.RunFn(protoModify(opts))
g.RunFn(eventModify(replacer, opts))
g.RunFn(eventModify(opts))
if err := g.OnlyFS(subPacketComponent, nil, nil); err != nil {
return g, err
}
Expand Down Expand Up @@ -261,23 +261,27 @@ func protoModify(opts *PacketOptions) genny.RunFn {
}
}

func eventModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn {
func eventModify(opts *PacketOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join("x", opts.ModuleName, "types/events_ibc.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}

template := `EventType%[2]vPacket = "%[3]v_packet"
%[1]v`
replacement := fmt.Sprintf(
template,
PlaceholderIBCPacketEvent,
opts.PacketName.UpperCamel,
opts.PacketName.LowerCamel,
// Keeper declaration
content, err := xast.InsertGlobal(
f.String(),
xast.GlobalTypeConst,
xast.WithGlobal(
fmt.Sprintf("EventType%[1]vPacket", opts.PacketName.UpperCamel),
"",
fmt.Sprintf(`"%[1]v_packet"`, opts.PacketName.LowerCamel),
),
)
content := replacer.Replace(f.String(), PlaceholderIBCPacketEvent, replacement)
if err != nil {
return err
}

newFile := genny.NewFileS(path, content)
return r.File(newFile)
Expand Down
1 change: 0 additions & 1 deletion ignite/templates/ibc/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ibc
//nolint:godot
const (
// Placeholders IBC packets
PlaceholderIBCPacketEvent = "// this line is used by starport scaffolding # ibc/packet/event"
PlaceholderIBCPacketModuleRecv = "// this line is used by starport scaffolding # ibc/packet/module/recv"
PlaceholderIBCPacketModuleAck = "// this line is used by starport scaffolding # ibc/packet/module/ack"
PlaceholderIBCPacketModuleTimeout = "// this line is used by starport scaffolding # ibc/packet/module/timeout"
Expand Down
Loading
Loading