apache / dubbo-go-hessian2

caucho hessian2 implementation in Go for [apache/dubbo-go](https://github.com/apache/dubbo-go) which is compatible with [dubbo-hessian-lite](https://github.com/apache/dubbo-hessian-lite)
Apache License 2.0
209 stars 113 forks source link

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

Closed alpha-baby closed 2 years ago

alpha-baby commented 2 years ago

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?:

wongoo commented 2 years ago

see https://github.com/apache/dubbo-go-hessian2/releases/tag/v1.10.2

alpha-baby commented 2 years ago

see https://github.com/apache/dubbo-go-hessian2/releases/tag/v1.10.2

good job!

alpha-baby commented 2 years ago

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
    }
}
wongoo commented 2 years ago

@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 commented 2 years ago

@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.

https://github.com/apache/dubbo-go-hessian2/pull/303