You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/10/22 05:29:24 UTC

[dubbo-go-hessian2] branch master updated: notify all ref holder at the end of decoding

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

alexstocks pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git


The following commit(s) were added to refs/heads/master by this push:
     new 4055e70  notify all ref holder at the end of decoding
     new 542bcf0  Merge pull request #324 from wongoo/fix-issue-323
4055e70 is described below

commit 4055e70d154cfc1a2392e29ce4a2edf891fadd29
Author: wongoo <wo...@apache.org>
AuthorDate: Wed Oct 19 19:35:34 2022 +0800

    notify all ref holder at the end of decoding
---
 decode.go      | 16 +++++++++++++---
 decode_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ref.go         |  2 ++
 3 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/decode.go b/decode.go
index d0b67a8..0e7932e 100644
--- a/decode.go
+++ b/decode.go
@@ -30,8 +30,9 @@ import (
 
 // Decoder struct
 type Decoder struct {
-	reader *bufio.Reader
-	refs   []interface{}
+	reader     *bufio.Reader
+	refs       []interface{}
+	refHolders []*_refHolder
 	// record type refs, both list and map need it
 	typeRefs      *TypeRefs
 	classInfoList []*ClassInfo
@@ -236,7 +237,16 @@ func (d *Decoder) decMapType() (reflect.Type, error) {
 
 // Decode parse hessian data, and ensure the reflection value unpacked
 func (d *Decoder) Decode() (interface{}, error) {
-	return EnsureInterface(d.DecodeValue())
+	v, err := d.DecodeValue()
+	if err != nil {
+		return nil, err
+	}
+
+	for _, holder := range d.refHolders {
+		holder.notify()
+	}
+
+	return EnsureRawAny(v), nil
 }
 
 func (d *Decoder) Buffered() int { return d.reader.Buffered() }
diff --git a/decode_test.go b/decode_test.go
index 99db40b..ccdd092 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -30,6 +30,7 @@ import (
 	"os/exec"
 	"reflect"
 	"testing"
+	"unsafe"
 )
 
 import (
@@ -257,3 +258,56 @@ func TestIssue299HessianDecode(t *testing.T) {
 		return
 	}
 }
+
+type Issue323B struct {
+	Num int
+}
+
+func (b *Issue323B) JavaClassName() string {
+	return "B"
+}
+
+type Issue323BB struct {
+	List1 []*Issue323B
+	List2 []*Issue323B
+}
+
+func (bb *Issue323BB) JavaClassName() string {
+	return "BB"
+}
+
+func TestIssue323(t *testing.T) {
+	RegisterPOJO(&Issue323B{})
+	RegisterPOJO(&Issue323BB{})
+	a1 := &Issue323B{
+		Num: 1,
+	}
+	a2 := &Issue323B{
+		Num: 2,
+	}
+	list := []*Issue323B{a1, a2}
+	b := &Issue323BB{
+		List1: list,
+		List2: list,
+	}
+	e := NewEncoder()
+	err := e.Encode(b)
+	assert.Nil(t, err)
+	fmt.Println(b)
+
+	d := NewDecoder(e.Buffer())
+	res, err := d.Decode()
+	assert.Nil(t, err)
+	fmt.Println(res)
+	assert.True(t, reflect.DeepEqual(b, res))
+
+	decodB, ok := res.(*Issue323BB)
+	if !ok {
+		t.Log("res is not Issue323BB")
+		t.FailNow()
+	}
+
+	// list1 and list2 should be reference to the same one.
+	assert.Equal(t, unsafe.Pointer(reflect.ValueOf(decodB.List1).Pointer()),
+		unsafe.Pointer(reflect.ValueOf(decodB.List2).Pointer()))
+}
diff --git a/ref.go b/ref.go
index 09cd380..e23d688 100644
--- a/ref.go
+++ b/ref.go
@@ -78,6 +78,8 @@ func (d *Decoder) appendRefs(v interface{}) *_refHolder {
 		}
 		// pack holder value
 		v = reflect.ValueOf(holder)
+
+		d.refHolders = append(d.refHolders, holder)
 	}
 
 	d.refs = append(d.refs, v)