Repository: apache/dubbo-go-hessian2
Pull request #224 (2020-09-04 16:06:38)

Fix: revert zhantu interface{} attachment
<!--  Thanks for sending a pull request! 
-->

**What this PR does**:

revert pr https://github.com/apache/dubbo-go-hessian2/pull/218



Patch:
diff --git a/contributing.md b/contributing.md
index 15c9610..ec5b83f 100644
--- a/contributing.md
+++ b/contributing.md
@@ -1,4 +1,4 @@
-Contributing to Hessian2 Protocol Go Implementation
+Contributing to Dubbo-go-hessian2
 
 ## 1. Branch
 
@@ -24,3 +24,41 @@ The title format of the pull request `MUST` follow the following rules:
   >- Start with `Dep:` for adding depending libs.
   >- Start with `Rem:` for removing feature/struct/function/member/files.
 
+## 3. Code Style
+
+### 3.1 log
+
+>- 1 when logging the function's input parameter, you should add '@' before input parameter name.
+
+### 3.2 naming
+
+>- 1 do not use an underscore in package name, such as `filter_impl`.
+>- 2 do not use an underscore in constants, such as `DUBBO_PROTOCOL`. use 'DubboProtocol' instead.
+
+### 3.3 comment
+
+>- 1 there should be comment for every export func/var.
+>- 2 the comment should begin with function name/var name.
+
+### 3.4 import
+
+We dubbogo import blocks should be splited into 3 blocks.
+
+```Go
+// block 1: the go internal package
+import (
+  "fmt"
+)
+
+// block 2: the third package
+import (
+  "github.com/dubbogo/xxx"
+
+  "github.com/RoaringBitmap/roaring"
+)
+
+// block 3: the dubbo-go package
+import (
+  "github.com/apache/dubbo-go/common"
+)
+```
diff --git a/hessian.go b/hessian.go
index 6527604..d4fb135 100644
--- a/hessian.go
+++ b/hessian.go
@@ -218,7 +218,7 @@ func (h *HessianCodec) ReadBody(rspObj interface{}) error {
 }
 
 // ignore body, but only read attachments
-func (h *HessianCodec) ReadAttachments() (map[string]interface{}, error) {
+func (h *HessianCodec) ReadAttachments() (map[string]string, error) {
 	if h.reader.Buffered() < h.bodyLen {
 		return nil, ErrBodyNotEnough
 	}
@@ -237,7 +237,7 @@ func (h *HessianCodec) ReadAttachments() (map[string]interface{}, error) {
 		if err = unpackRequestBody(NewDecoderWithSkip(buf[:]), rspObj); err != nil {
 			return nil, perrors.WithStack(err)
 		}
-		return rspObj[6].(map[string]interface{}), nil
+		return rspObj[6].(map[string]string), nil
 	case PackageResponse:
 		rspObj := &Response{}
 		if err = unpackResponseBody(NewDecoderWithSkip(buf[:]), rspObj); err != nil {
diff --git a/request.go b/request.go
index c668fcb..25908cb 100644
--- a/request.go
+++ b/request.go
@@ -151,13 +151,13 @@ func getArgsTypeList(args []interface{}) (string, error) {
 
 type Request struct {
 	Params      interface{}
-	Attachments map[string]interface{}
+	Attachments map[string]string
 }
 
 // NewRequest create a new Request
-func NewRequest(params interface{}, atta map[string]interface{}) *Request {
+func NewRequest(params interface{}, atta map[string]string) *Request {
 	if atta == nil {
-		atta = make(map[string]interface{})
+		atta = make(map[string]string)
 	}
 	return &Request{
 		Params:      params,
@@ -327,22 +327,24 @@ func unpackRequestBody(decoder *Decoder, reqObj interface{}) error {
 	}
 	if v, ok := attachments.(map[interface{}]interface{}); ok {
 		v[DUBBO_VERSION_KEY] = dubboVersion
-		req[6] = ToMapStringInterface(v)
+		req[6] = ToMapStringString(v)
 		return nil
 	}
 
 	return perrors.Errorf("get wrong attachments: %+v", attachments)
 }
 
-func ToMapStringInterface(origin map[interface{}]interface{}) map[string]interface{} {
-	dest := make(map[string]interface{})
+func ToMapStringString(origin map[interface{}]interface{}) map[string]string {
+	dest := make(map[string]string, len(origin))
 	for k, v := range origin {
 		if kv, ok := k.(string); ok {
 			if v == nil {
 				dest[kv] = ""
 				continue
 			}
-			dest[kv] = v
+			if vv, ok := v.(string); ok {
+				dest[kv] = vv
+			}
 		}
 	}
 	return dest
diff --git a/response.go b/response.go
index 910323b..ea7408f 100644
--- a/response.go
+++ b/response.go
@@ -37,13 +37,13 @@ import (
 type Response struct {
 	RspObj      interface{}
 	Exception   error
-	Attachments map[string]interface{}
+	Attachments map[string]string
 }
 
 // NewResponse create a new Response
-func NewResponse(rspObj interface{}, exception error, attachments map[string]interface{}) *Response {
+func NewResponse(rspObj interface{}, exception error, attachments map[string]string) *Response {
 	if attachments == nil {
-		attachments = make(map[string]interface{})
+		attachments = make(map[string]string, 8)
 	}
 	return &Response{
 		RspObj:      rspObj,
@@ -176,7 +176,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error {
 				return perrors.WithStack(err)
 			}
 			if v, ok := attachments.(map[interface{}]interface{}); ok {
-				atta := ToMapStringInterface(v)
+				atta := ToMapStringString(v)
 				response.Attachments = atta
 			} else {
 				return perrors.Errorf("get wrong attachments: %+v", attachments)
@@ -201,7 +201,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error {
 				return perrors.WithStack(err)
 			}
 			if v, ok := attachments.(map[interface{}]interface{}); ok {
-				response.Attachments = ToMapStringInterface(v)
+				response.Attachments = ToMapStringString(v)
 			} else {
 				return perrors.Errorf("get wrong attachments: %+v", attachments)
 			}
@@ -222,7 +222,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error {
 				return perrors.WithStack(err)
 			}
 			if v, ok := attachments.(map[interface{}]interface{}); ok {
-				atta := ToMapStringInterface(v)
+				atta := ToMapStringString(v)
 				response.Attachments = atta
 			} else {
 				return perrors.Errorf("get wrong attachments: %+v", attachments)
@@ -338,9 +338,8 @@ var versionInt = make(map[string]int)
 
 // https://github.com/apache/dubbo/blob/dubbo-2.7.1/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java#L96
 // isSupportResponseAttachment is for compatibility among some dubbo version
-func isSupportResponseAttachment(ver interface{}) bool {
-	version, ok := ver.(string)
-	if !ok || len(version) == 0 {
+func isSupportResponseAttachment(version string) bool {
+	if len(version) == 0 {
 		return false
 	}
 
@@ -358,9 +357,8 @@ func isSupportResponseAttachment(ver interface{}) bool {
 	return v >= LOWEST_VERSION_FOR_RESPONSE_ATTACHMENT
 }
 
-func version2Int(ver interface{}) int {
-	version, ok := ver.(string)
-	if !ok || len(version) == 0 {
+func version2Int(version string) int {
+	if len(version) == 0 {
 		return 0
 	}
 	var v = 0


Test patch:
diff --git a/hessian_test.go b/hessian_test.go
index 6f89f0b..47726a2 100644
--- a/hessian_test.go
+++ b/hessian_test.go
@@ -192,11 +192,10 @@ func TestRequest(t *testing.T) {
 }
 
 func TestHessianCodec_ReadAttachments(t *testing.T) {
-	RegisterPOJO(&AttachObject{})
 	body := &Response{
 		RspObj:      &CaseB{A: "A", B: CaseA{A: "a", B: 1, C: Case{A: "c", B: 2}}},
 		Exception:   nil,
-		Attachments: map[string]interface{}{DUBBO_VERSION_KEY: "2.6.4", "att": AttachObject{Id: 23, Name: "haha"}},
+		Attachments: map[string]string{DUBBO_VERSION_KEY: "2.6.4"},
 	}
 	resp, err := doTestHessianEncodeHeader(t, PackageResponse, Response_OK, body)
 	assert.NoError(t, err)
@@ -214,17 +213,6 @@ func TestHessianCodec_ReadAttachments(t *testing.T) {
 	attrs, err := codecR2.ReadAttachments()
 	assert.NoError(t, err)
 	assert.Equal(t, "2.6.4", attrs[DUBBO_VERSION_KEY])
-	assert.Equal(t, AttachObject{Id: 23, Name: "haha"}, *(attrs["att"].(*AttachObject)))
-	assert.NotEqual(t, AttachObject{Id: 24, Name: "nohaha"}, *(attrs["att"].(*AttachObject)))
 
 	t.Log(attrs)
 }
-
-type AttachObject struct {
-	Id   int32
-	Name string `dubbo:name`
-}
-
-func (AttachObject) JavaClassName() string {
-	return "com.test.Test"
-}
diff --git a/request_test.go b/request_test.go
index 37ec48d..2b7f1f3 100644
--- a/request_test.go
+++ b/request_test.go
@@ -129,7 +129,7 @@ func TestIssue192(t *testing.T) {
 	tests := []struct {
 		name string
 		args args
-		want map[string]interface{}
+		want map[string]string
 	}{
 		{
 			name: "not null",
@@ -140,7 +140,7 @@ func TestIssue192(t *testing.T) {
 					"":  "",
 				},
 			},
-			want: map[string]interface{}{
+			want: map[string]string{
 				"1": "",
 				"2": "3",
 				"":  "",
@@ -149,8 +149,8 @@ func TestIssue192(t *testing.T) {
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			if got := ToMapStringInterface(tt.args.origin); !reflect.DeepEqual(got, tt.want) {
-				t.Errorf("ToMapStringInterface() = %v, want %v", got, tt.want)
+			if got := ToMapStringString(tt.args.origin); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("ToMapStringString() = %v, want %v", got, tt.want)
 			}
 		})
 	}
diff --git a/response_test.go b/response_test.go
index 5f0e07a..7299389 100644
--- a/response_test.go
+++ b/response_test.go
@@ -168,10 +168,7 @@ func TestCopySlice(t *testing.T) {
 }
 
 func TestIsSupportResponseAttachment(t *testing.T) {
-	is := isSupportResponseAttachment("2.X")
-	assert.False(t, is)
-
-	is = isSupportResponseAttachment("2.0.10")
+	is := isSupportResponseAttachment("2.0.10")
 	assert.False(t, is)
 
 	is = isSupportResponseAttachment("2.5.3")