apache/dubbo-go-hessian2

结构体序列化再反序列化,不能完全还原

alpha-baby opened this issue · 5 comments

What happened:

func init() {
	RegisterPOJO(new(Args1))
       RegisterPOJO(new(MockData))
}

type Args1 struct {
	Label string
	Key   string
}

func (Args1) JavaClassName() string {
	return "com.test.Args1"
}

type MockData struct {
	Args []interface{}
}

func (MockData) JavaClassName() string {
	return "com.Mock"
}

func TestHessianEncode(t *testing.T) {
	d := &MockData{
		Args: []interface{}{
			[]*Args1{
				{Label: "1", Key: "2"},
			},
		},
	}

	encoder := NewEncoder()
	err := encoder.Encode(d)
	if err != nil {
		t.Errorf("encode obj error: %v", err)
		return
	}
	decoder := NewDecoder(encoder.Buffer())
	doInterface, err := decoder.Decode()
	if err != nil {
		t.Errorf("decode obj error: %v", err)
		return
	}
	do := doInterface.(*MockData)
	if !reflect.DeepEqual(d, do) {
		t.Errorf("not equal d: %+v, do: %+v", d, do)
		return
	}
}

unit test log

=== RUN   TestHessianEncode
    string_test.go:319: not equal d: &{Args:[[0xc000069100]]}, do: &{Args:[0xc0001097d0]}
--- FAIL: TestHessianEncode (0.00s)

What you expected to happen:

这个场景可以做到前后对象的一致吗?

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

func init() {
	RegisterPOJO(new(Args1))
	RegisterPOJO(new(MockData))
}

type Args1 struct {
	Label string
	Key   string
}

func (Args1) JavaClassName() string {
	return "com.test.Args1"
}

func init() {
	RegisterPOJO(new(MockData))
}

type MockData struct {
	Args map[string]interface{}
}

func (MockData) JavaClassName() string {
	return "com.Mock"
}

func TestHessianEncodeMap1(t *testing.T) {
	meta := make(map[string]interface{})
	meta["interface"] = []interface{}{
		"MyName",
	}
	in := &MockData{
		Args: meta,
	}

	encoder := NewEncoder()

	err := encoder.Encode(in)
	if err != nil {
		t.Errorf("encode Invocation obj error: %v", err)
		return
	}
	data := encoder.Buffer()
	decoder := NewDecoder(data)
	outI, err := decoder.Decode()
	if err != nil {
		t.Errorf("hessian decode error: %+v", outI)
		return
	}
	out := outI.(*MockData)
	t.Logf("out: %+v", out)
	if !reflect.DeepEqual(out, in) {
		t.Errorf("got: %#v, want: %#v", out, in)
		return
	}
}

func TestHessianEncodeMap2(t *testing.T) {
	meta := make(map[string]interface{})
	meta["map"] = map[string]interface{}{
		"k1": "v1",
	}
	in := &MockData{
		Args: meta,
	}

	encoder := NewEncoder()

	err := encoder.Encode(in)
	if err != nil {
		t.Errorf("encode Invocation obj error: %v", err)
		return
	}
	data := encoder.Buffer()
	decoder := NewDecoder(data)
	outI, err := decoder.Decode()
	if err != nil {
		t.Errorf("hessian decode error: %+v", outI)
		return
	}
	out := outI.(*MockData)
	t.Logf("out: %+v", out)
	if !reflect.DeepEqual(out, in) {
		t.Errorf("got: %#v, want: %#v", out, in)
		return
	}
}

@alpha-baby is the issue bug still exists? you can try to update the unit test https://github.com/apache/dubbo-go-hessian2/blob/master/decode_test.go#L211, and submit a PR to show that.

@alpha-baby is the issue bug still exists? you can try to update the unit test https://github.com/apache/dubbo-go-hessian2/blob/master/decode_test.go#L211, and submit a PR to show that.

#303