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

wusheng 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 63bab93  Fix enhance method error when unknown parameter type (#160)
63bab93 is described below

commit 63bab93d8009e91686c853e4efb3c2cd8d57a151
Author: mrproliu <[email protected]>
AuthorDate: Wed Jan 10 14:50:20 2024 +0800

    Fix enhance method error when unknown parameter type (#160)
---
 CHANGES.md                                         |  3 ++-
 .../plugins/templates/method_intercept_after.tmpl  |  3 ++-
 .../plugins/templates/method_intercept_before.tmpl | 11 +++++----
 tools/go-agent/tools/enhancement.go                | 27 +++-------------------
 tools/go-agent/tools/enhancement_test.go           | 22 ++++++++----------
 5 files changed, 23 insertions(+), 43 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index f782716..4e98f9c 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -17,7 +17,8 @@ Release Notes.
 * Fix users can not use async api in toolkit-trace.
 * Fix cannot enhance the vendor management project.
 * Fix SW_AGENT_REPORTER_GRPC_MAX_SEND_QUEUE not working on metricsSendCh & 
logSendCh chans of gRPC reporter.
-* Fix ParseVendorModule error for special case in vendor/modules.txt
+* Fix ParseVendorModule error for special case in vendor/modules.txt.
+* Fix enhance method error when unknown parameter type.
 
 #### Issues and PR
 - All issues are 
[here](https://github.com/apache/skywalking/milestone/197?closed=1)
diff --git 
a/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl 
b/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl
index f17b10d..1aa0847 100644
--- a/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl
+++ b/tools/go-agent/instrument/plugins/templates/method_intercept_after.tmpl
@@ -21,7 +21,8 @@ if (invocation.isContinue) {
        if invocation.returnValues[{{$index}}] != nil {
         *ret_{{$index}} = 
(invocation.returnValues[{{$index}}]).({{$value.PackagedTypeName}})
     } else {
-        *ret_{{$index}} = {{$value.DefaultValueAsString}}
+        var tmp_{{$index}} {{$value.PackagedTypeName}}
+        *ret_{{$index}} = tmp_{{$index}}
     }
     {{- end }}
 }
\ No newline at end of file
diff --git 
a/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl 
b/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl
index f6cbd07..e29f34b 100644
--- a/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl
+++ b/tools/go-agent/instrument/plugins/templates/method_intercept_before.tmpl
@@ -25,18 +25,21 @@ invocation.changeArgCallback = func(index int, value 
interface{}) {
     panic("cannot found the argument index")
 }
 
+{{- range $index, $value := .Results }}
+var def_res_{{$index}} {{$value.PackagedTypeName}}
+{{- end}}
 // real invoke
 if err := {{.InterceptorVarName}}.BeforeInvoke(invocation); err != nil {
        // using go2sky log error
        log.Warnf("execute interceptor before invoke error, instrument name: 
%s, interceptor name: %s, function ID: %s, error: %v",
                    "{{.InstrumentName}}", "{{.InterceptorDefineName}}", 
"{{.FuncID}}", err)
+
        return {{ range $index, $value := .Results -}}
-{{$value.DefaultValueAsString}},
+def_res_{{$index}},
 {{- end}}invocation, false
 }
 if (invocation.isContinue) {
     {{- range $index, $value := .Results }}
-    var def_res_{{$index}} {{$value.PackagedTypeName}}
     if invocation.returnValues[{{$index}}] != nil {
         def_res_{{$index}} = 
(invocation.returnValues[{{$index}}]).({{$value.PackagedTypeName}})
     }
@@ -46,5 +49,5 @@ if (invocation.isContinue) {
 {{- end}}invocation, true
 }
 return {{ range $index, $value := .Results -}}
-{{- if ne $index 0}}, {{end}}{{$value.DefaultValueAsString }}
-{{- end}}{{if .Results}}, {{- end}}invocation, false
\ No newline at end of file
+def_res_{{$index}},
+{{- end}}invocation, false
\ No newline at end of file
diff --git a/tools/go-agent/tools/enhancement.go 
b/tools/go-agent/tools/enhancement.go
index 7b800fd..4171f61 100644
--- a/tools/go-agent/tools/enhancement.go
+++ b/tools/go-agent/tools/enhancement.go
@@ -19,8 +19,6 @@ package tools
 
 import (
        "fmt"
-       "go/token"
-       "strings"
 
        "github.com/dave/dst"
        "github.com/dave/dst/decorator"
@@ -31,10 +29,9 @@ const OtherPackageRefPrefix = "swref_"
 const parameterAppender = ", "
 
 type ParameterInfo struct {
-       Name                 string
-       Type                 dst.Expr
-       DefaultValueAsString string
-       TypeName             string
+       Name     string
+       Type     dst.Expr
+       TypeName string
 }
 
 type PackagedParameterInfo struct {
@@ -141,24 +138,6 @@ func newParameterInfo(name string, tp dst.Expr) 
*ParameterInfo {
                Type:     tp,
                TypeName: GenerateTypeNameByExp(tp),
        }
-       var defaultNil = "nil"
-       switch n := tp.(type) {
-       case *dst.Ident:
-               if n.Name == "string" {
-                       defaultNil = `""`
-               } else if n.Name == "bool" {
-                       defaultNil = "false"
-               } else if strings.HasPrefix(n.Name, "int") || 
strings.HasPrefix(n.Name, "uint") ||
-                       strings.HasPrefix(n.Name, "float") || n.Name == "byte" 
|| n.Name == "rune" {
-                       defaultNil = "0"
-               }
-       case *dst.UnaryExpr:
-               if n.Op == token.INT || n.Op == token.FLOAT {
-                       defaultNil = "0"
-               }
-       }
-       result.DefaultValueAsString = defaultNil
-
        return result
 }
 
diff --git a/tools/go-agent/tools/enhancement_test.go 
b/tools/go-agent/tools/enhancement_test.go
index ea9058d..e2a8c51 100644
--- a/tools/go-agent/tools/enhancement_test.go
+++ b/tools/go-agent/tools/enhancement_test.go
@@ -23,11 +23,10 @@ import (
        "github.com/dave/dst"
 )
 
-func buildParameterValidateInfo(name, typeName, defaultValue string) 
*ParameterInfo {
+func buildParameterValidateInfo(name, typeName string) *ParameterInfo {
        return &ParameterInfo{
-               Name:                 name,
-               TypeName:             typeName,
-               DefaultValueAsString: defaultValue,
+               Name:     name,
+               TypeName: typeName,
        }
 }
 
@@ -43,13 +42,13 @@ func TestEnhanceParameterNames(t *testing.T) {
                                return false
                        }`,
                        recvs: []*ParameterInfo{
-                               buildParameterValidateInfo("skywalking_recv_0", 
"*Example", "nil"),
+                               buildParameterValidateInfo("skywalking_recv_0", 
"*Example"),
                        },
                        params: []*ParameterInfo{
-                               
buildParameterValidateInfo("skywalking_param_0", "int", "0"),
+                               
buildParameterValidateInfo("skywalking_param_0", "int"),
                        },
                        results: []*ParameterInfo{
-                               
buildParameterValidateInfo("skywalking_result_0", "bool", "false"),
+                               
buildParameterValidateInfo("skywalking_result_0", "bool"),
                        },
                },
                {
@@ -57,13 +56,13 @@ func TestEnhanceParameterNames(t *testing.T) {
                                return false
 }`,
                        recvs: []*ParameterInfo{
-                               buildParameterValidateInfo("e", "*Example", 
"nil"),
+                               buildParameterValidateInfo("e", "*Example"),
                        },
                        params: []*ParameterInfo{
-                               buildParameterValidateInfo("i", "int", "0"),
+                               buildParameterValidateInfo("i", "int"),
                        },
                        results: []*ParameterInfo{
-                               buildParameterValidateInfo("b", "bool", 
"false"),
+                               buildParameterValidateInfo("b", "bool"),
                        },
                },
        }
@@ -95,8 +94,5 @@ func validateParameterInfo(t *testing.T, inx int, flistType 
FieldListType, actua
                if exp.TypeName != act.TypeName {
                        t.Errorf("case %d:%s: expected type %s , actual %s", 
inx, flistType, exp.TypeName, act.TypeName)
                }
-               if exp.DefaultValueAsString != act.DefaultValueAsString {
-                       t.Errorf("case %d:%s: expected default value %s , 
actual %s", inx, flistType, exp.DefaultValueAsString, act.DefaultValueAsString)
-               }
        }
 }

Reply via email to