This is an automated email from the ASF dual-hosted git repository.

liuhan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 3db8143  Enhance the plugin rewrite ability (#57)
3db8143 is described below

commit 3db8143cb658f0ee4fa7d5a363cfa1e6ca62dfda
Author: mrproliu <[email protected]>
AuthorDate: Wed Jun 7 07:25:07 2023 +0000

    Enhance the plugin rewrite ability (#57)
---
 CHANGES.md                                           | 13 +++++++++++++
 tools/go-agent/instrument/plugins/rewrite/context.go | 12 ++++++++++--
 tools/go-agent/instrument/plugins/rewrite/func.go    | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index b7bf214..1140f2b 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -2,6 +2,19 @@ Changes by Version
 ==================
 Release Notes.
 
+0.2.0
+------------------
+#### Features
+* Enhance the plugin rewrite ability to support `switch` and `if/else` in the 
plugin codes.
+
+#### Plugins
+
+#### Documentation
+
+#### Issues and PR
+- All issues are 
[here](https://github.com/apache/skywalking/milestone/180?closed=1)
+- All and pull requests are 
[here](https://github.com/apache/skywalking-go/milestone/2?closed=1)
+
 0.1.0
 ------------------
 
diff --git a/tools/go-agent/instrument/plugins/rewrite/context.go 
b/tools/go-agent/instrument/plugins/rewrite/context.go
index d3807f7..c8dd04a 100644
--- a/tools/go-agent/instrument/plugins/rewrite/context.go
+++ b/tools/go-agent/instrument/plugins/rewrite/context.go
@@ -270,6 +270,14 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType 
dst.Expr, parent dst.Node
                        p.Elt = generateExpr()
                case *dst.ValueSpec:
                        p.Type = generateExpr()
+               case *dst.BinaryExpr:
+                       if argIndex == 0 {
+                               p.X = generateExpr()
+                       } else if argIndex == 1 {
+                               p.Y = generateExpr()
+                       } else {
+                               panic("binary expr arg index error")
+                       }
                }
        case *dst.StarExpr:
                return c.enhanceTypeNameWhenRewrite(t.X, t, -1)
@@ -315,8 +323,8 @@ func (c *Context) enhanceTypeNameWhenRewrite(fieldType 
dst.Expr, parent dst.Node
                        c.enhanceFuncStmt(stmt)
                }
        case *dst.BinaryExpr:
-               c.enhanceTypeNameWhenRewrite(t.X, t, -1)
-               c.enhanceTypeNameWhenRewrite(t.Y, t, -1)
+               c.enhanceTypeNameWhenRewrite(t.X, t, 0)
+               c.enhanceTypeNameWhenRewrite(t.Y, t, 1)
        case *dst.ParenExpr:
                c.rewriteVarIfExistingMapping(t.X, t)
        case *dst.MapType:
diff --git a/tools/go-agent/instrument/plugins/rewrite/func.go 
b/tools/go-agent/instrument/plugins/rewrite/func.go
index 5f6f64b..4ba8926 100644
--- a/tools/go-agent/instrument/plugins/rewrite/func.go
+++ b/tools/go-agent/instrument/plugins/rewrite/func.go
@@ -75,6 +75,7 @@ func (c *Context) enhanceFuncStmt(stmt dst.Stmt) {
        subCallTypes := []reflect.Type{
                reflect.TypeOf(&dst.IfStmt{}),
                reflect.TypeOf(&dst.BlockStmt{}),
+               reflect.TypeOf(&dst.TypeSwitchStmt{}),
        }
        dstutil.Apply(stmt, func(cursor *dstutil.Cursor) bool {
                for _, t := range subCallTypes {
@@ -144,6 +145,21 @@ func (c *Context) enhanceFuncStmt(stmt dst.Stmt) {
                        }
                case *dst.ValueSpec:
                        c.Var(n, false)
+               case *dst.TypeSwitchStmt:
+                       c.enhanceFuncStmt(n.Init)
+                       c.enhanceFuncStmt(n.Assign)
+                       if n.Body != nil {
+                               for _, stmt := range n.Body.List {
+                                       c.enhanceFuncStmt(stmt)
+                               }
+                       }
+               case *dst.CaseClause:
+                       for _, stmt := range n.List {
+                               c.enhanceTypeNameWhenRewrite(stmt, n, -1)
+                       }
+                       for _, stmt := range n.Body {
+                               c.enhanceFuncStmt(stmt)
+                       }
                default:
                        return true
                }

Reply via email to