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

[Improvement] returned a reflect.value when decoding #28

Closed u0x01 closed 5 years ago

u0x01 commented 5 years ago

https://github.com/dubbogo/hessian2/blob/57ac2e777dc6d853bdcf74b5c697b6a98a728be9/object.go#L305 https://github.com/dubbogo/hessian2/blob/57ac2e777dc6d853bdcf74b5c697b6a98a728be9/object.go#L440 that lines should return vRef.Interface() of value itself instead vRef of reflect.value.

Its effect user decoding what he want, eg: I registered a POJO named MyUser, when I decode a hessian should return a MyUser type struct to me, but I must doing type assertion twice now for getting type of MyUser:

    RegisterPOJO(new(MyUser))

    d := hessian.NewDecoder(b)
    res, err := d.Decode()
    if err != nil {
        panic(err)
    }

    t.Logf("decode => %+v %v", res.(reflect.Value).Interface(), err)

    user := res.(reflect.Value).Interface().(*MyUser)

Its should using like this for normal user:

    RegisterPOJO(new(MyUser))

    d := hessian.NewDecoder(b)
    res, err := d.Decode()
    if err != nil {
        panic(err)
    }

    t.Logf("decode => %+v %v", res, err)

    user := res.(*MyUser)
AlexStocks commented 5 years ago

A good suggestion. @wongoo so what is your opinion?

wongoo commented 5 years ago

@AlexStocks it's good absolutely. I used to worry about it may not able to ref to the same of object when exists ref, but it won't happen. @u0x01 please submit a PR for it. Thanks!

wongoo commented 5 years ago

@u0x01 it's an improvement, not a bug.

u0x01 commented 5 years ago

already merge pr.