Commit ed3fa8fa authored by Hugo Moreau's avatar Hugo Moreau

refacto global transform

 * transform/global.go,
   transform/global2.go,
   transform/global3.go : Here.
parent b841a7a0
Pipeline #26289 failed with stage
in 38 seconds
......@@ -161,13 +161,17 @@ func (t *Global) Post(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.File:
for _, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if val, ok2 := t.funcArgToAdd[f.Name.Name]; ok2 {
f.Type.Params.List = append(f.Type.Params.List, val)
}
addArg(node, t.funcArgToAdd)
}
return true
}
func addArg(node *ast.File, funcs map[string]*ast.Field) {
for _, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if val, ok2 := funcs[f.Name.Name]; ok2 {
f.Type.Params.List = append(f.Type.Params.List, val)
}
}
}
return true
}
......@@ -98,13 +98,7 @@ func (t *Global2) Post(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.File:
for _, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if val, ok2 := t.funcArgGetToAdd[f.Name.Name]; ok2 {
f.Type.Params.List = append(f.Type.Params.List, val)
}
}
}
addArg(node, t.funcArgGetToAdd)
}
return true
}
......@@ -46,36 +46,26 @@ func (t *Global3) Post(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.File:
for i, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if f.Name.Name == t.FirstFunc {
newDecls := []ast.Decl{}
for j := 0; j < i; j++ {
newDecls = append(newDecls, node.Decls[j])
}
for j := 0; j < len(t.FuncsSetter); j++ {
newDecls = append(newDecls, t.FuncsSetter[j])
}
newDecls = append(newDecls, node.Decls[i:]...)
node.Decls = newDecls
InjectFunc(node, t.FuncsSetter, t.FirstFunc)
InjectFunc(node, t.FuncsGetter, t.FirstFunc)
}
return true
}
func InjectFunc(node *ast.File, slice []*ast.FuncDecl, firstFunc string) {
for i, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if f.Name.Name == firstFunc {
newDecls := []ast.Decl{}
for j := 0; j < i; j++ {
newDecls = append(newDecls, node.Decls[j])
}
}
}
for i, d := range node.Decls {
if f, ok := d.(*ast.FuncDecl); ok {
if f.Name.Name == t.FirstFunc {
newDecls := []ast.Decl{}
for j := 0; j < i; j++ {
newDecls = append(newDecls, node.Decls[j])
}
for j := 0; j < len(t.FuncsGetter); j++ {
newDecls = append(newDecls, t.FuncsGetter[j])
}
newDecls = append(newDecls, node.Decls[i:]...)
node.Decls = newDecls
for j := 0; j < len(slice); j++ {
newDecls = append(newDecls, slice[j])
}
newDecls = append(newDecls, node.Decls[i:]...)
node.Decls = newDecls
}
}
}
return true
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment