This is an automated email from the ASF dual-hosted git repository. wongoo 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 d24d44e Ftr: add BigInteger support (#141) d24d44e is described below commit d24d44ed7246be32f055c3ba9b58938d04b60a38 Author: huiren <zhr...@gmail.com> AuthorDate: Sat Dec 14 18:26:44 2019 +0800 Ftr: add BigInteger support (#141) add biginteger support --- date_test.go | 2 +- go.mod | 4 +- go.sum | 12 ++- serialize.go | 31 ++++++ serialize_test.go | 107 ++++++++++++++++++++- .../src/main/java/test/TestCustomDecode.java | 24 ++++- .../src/main/java/test/TestCustomReply.java | 55 ++++++++++- 7 files changed, 218 insertions(+), 17 deletions(-) diff --git a/date_test.go b/date_test.go index 0bb6640..c55df10 100644 --- a/date_test.go +++ b/date_test.go @@ -142,7 +142,7 @@ func TestDateNulJavaDecode(t *testing.T) { Name: "zs", Date: ZeroDate, } - testJavaDecode(t, "customArgTypedFixedList_DateNull", date) + testJavaDecode(t, "customArgTypedFixed_DateNull", date) } func TestDateNilDecode(t *testing.T) { diff --git a/go.mod b/go.mod index 5a38e9e..a5576d6 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/apache/dubbo-go-hessian2 require ( - github.com/dubbogo/gost v1.1.1 + github.com/dubbogo/gost v1.4.0 github.com/pkg/errors v0.8.1 - github.com/stretchr/testify v1.3.0 + github.com/stretchr/testify v1.4.0 ) diff --git a/go.sum b/go.sum index 526c431..2686d90 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,15 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dubbogo/gost v1.1.1 h1:JCM7vx5edPIjDA5ovJTuzEEXuw2t7xLyrlgi2mi5jHI= -github.com/dubbogo/gost v1.1.1/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg= +github.com/dubbogo/gost v1.4.0 h1:7YoSpTVvceK9OcZKeUB5stW1IfTPkZOVdJmsqWTu1D0= +github.com/dubbogo/gost v1.4.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/serialize.go b/serialize.go index 08cc45e..bb5860f 100644 --- a/serialize.go +++ b/serialize.go @@ -25,7 +25,12 @@ import ( big "github.com/dubbogo/gost/math/big" ) +type bigInteger = big.Integer + func init() { + RegisterPOJO(&bigInteger{}) + SetSerializer("java.math.BigInteger", IntegerSerializer{}) + RegisterPOJO(&big.Decimal{}) SetSerializer("java.math.BigDecimal", DecimalSerializer{}) } @@ -46,6 +51,32 @@ func GetSerializer(key string) (Serializer, bool) { return codec, ok } +type IntegerSerializer struct{} + +func (IntegerSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) { + bigInt, err := d.decInstance(typ, cls) + if err != nil { + return nil, err + } + + result, ok := bigInt.(*bigInteger) + if !ok { + panic("result type is not Integer, please check the whether the conversion is ok") + } + + result.FromSignAndMag(result.Signum, result.Mag) + return result, nil +} + +func (IntegerSerializer) EncObject(e *Encoder, v POJO) error { + bigInt, ok := v.(bigInteger) + if !ok { + return e.encObject(v) + } + bigInt.Signum, bigInt.Mag = bigInt.GetSignAndMag() + return e.encObject(bigInt) +} + type DecimalSerializer struct{} func (DecimalSerializer) EncObject(e *Encoder, v POJO) error { diff --git a/serialize_test.go b/serialize_test.go index 1dff269..f521a18 100644 --- a/serialize_test.go +++ b/serialize_test.go @@ -18,6 +18,7 @@ package hessian import ( + "fmt" "reflect" "testing" ) @@ -53,20 +54,92 @@ func TestDecimalGoDecode(t *testing.T) { var d big.Decimal _ = d.FromString("100.256") d.Value = d.String() - doTestDecimal(t, "customReplyTypedFixedDecimal", "100.256") + doTestStringer(t, "customReplyTypedFixedDecimal", "100.256") } func TestDecimalJavaDecode(t *testing.T) { var d big.Decimal _ = d.FromString("100.256") d.Value = d.String() - testJavaDecode(t, "customArgTypedFixedList_Decimal", d) + testJavaDecode(t, "customArgTypedFixed_Decimal", d) } -func doTestDecimal(t *testing.T, method, content string) { +func TestEncodeDecodeInteger(t *testing.T) { + var bigInt bigInteger + //bigInt := new(bigInteger) + _ = bigInt.FromString("100256") + e := NewEncoder() + err := e.Encode(bigInt) + if err != nil { + t.Error(err) + t.FailNow() + } + + d := NewDecoder(e.buffer) + bigIntObj, err := d.Decode() + if err != nil { + t.Error(err) + t.FailNow() + } + + if !reflect.DeepEqual(bigInt.String(), bigIntObj.(*bigInteger).String()) { + t.Errorf("expect: %v, but get: %v", bigInt, bigIntObj) + } +} + +func TestIntegerGoDecode(t *testing.T) { + doTestStringer(t, "customReplyTypedFixedIntegerZero", "0") + doTestStringer(t, "customReplyTypedFixedInteger", "4294967298") + doTestStringer(t, "customReplyTypedFixedIntegerSigned", "-4294967298") +} + +func TestIntegerJavaDecode(t *testing.T) { + var i bigInteger + _ = i.FromString("4294967298") + testJavaDecode(t, "customArgTypedFixed_Integer", i) + + _ = i.FromString("0") + testJavaDecode(t, "customArgTypedFixed_IntegerZero", i) + + _ = i.FromString("-4294967298") + testJavaDecode(t, "customArgTypedFixed_IntegerSigned", i) +} + +func TestIntegerListGoDecode(t *testing.T) { + data := []string{ + "1234", + "12347890", + "123478901234", + "1234789012345678", + "123478901234567890", + "1234789012345678901234", + "12347890123456789012345678", + "123478901234567890123456781234", + "1234789012345678901234567812345678", + "12347890123456789012345678123456781234", + "-12347890123456789012345678123456781234", + "0", + } + + out, err := decodeJavaResponse(`customReplyTypedFixedList_BigInteger`, ``, false) + if err != nil { + t.Errorf("%#v %v", out, err) + return + } + + resp := out.([]*big.Integer) + for i := range data { + gotInteger := resp[i] + if gotInteger.String() != data[i] { + t.Errorf("java: %s go: %s", gotInteger.String(), data[i]) + } + } +} + +func doTestStringer(t *testing.T, method, content string) { testDecodeFrameworkFunc(t, method, func(r interface{}) { t.Logf("%#v", r) - assert.Equal(t, content, r.(*big.Decimal).String()) + assert.Equal(t, content, r.(fmt.Stringer).String()) }) } @@ -91,3 +164,29 @@ func TestDecimalListGoDecode(t *testing.T) { } } } + +func TestObjectListGoDecode(t *testing.T) { + data := []string{ + "1234", + "-12347890", + "0", + "123.4", + "-123.45", + "0", + } + + out, err := decodeJavaResponse(`customReplyTypedFixedList_CustomObject`, ``, false) + if err != nil { + t.Error(err) + return + } + + resp := out.([]Object) + for i := range data { + elem := resp[i] + if elem.(fmt.Stringer).String() != data[i] { + t.Logf("%T %#v", elem, elem) + t.Errorf("java: %s go: %s", elem.(fmt.Stringer).String(), data[i]) + } + } +} diff --git a/test_hessian/src/main/java/test/TestCustomDecode.java b/test_hessian/src/main/java/test/TestCustomDecode.java index e9a928f..9e72a0a 100644 --- a/test_hessian/src/main/java/test/TestCustomDecode.java +++ b/test_hessian/src/main/java/test/TestCustomDecode.java @@ -20,14 +20,15 @@ package test; import com.caucho.hessian.io.Hessian2Input; import com.caucho.hessian.test.A0; import com.caucho.hessian.test.A1; -import test.model.DateDemo; - import java.io.InputStream; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import test.model.DateDemo; + public class TestCustomDecode { @@ -166,12 +167,27 @@ public class TestCustomDecode { return Arrays.deepEquals(list, (Object[]) o); } - public Object customArgTypedFixedList_Decimal() throws Exception { + public Object customArgTypedFixed_Integer() throws Exception { + BigInteger o = (BigInteger) input.readObject(); + return o.toString().equals("4294967298"); + } + + public Object customArgTypedFixed_IntegerZero() throws Exception { + BigInteger o = (BigInteger) input.readObject(); + return o.toString().equals("0"); + } + + public Object customArgTypedFixed_IntegerSigned() throws Exception { + BigInteger o = (BigInteger) input.readObject(); + return o.toString().equals("-4294967298"); + } + + public Object customArgTypedFixed_Decimal() throws Exception { BigDecimal o = (BigDecimal) input.readObject(); return o.toString().equals("100.256"); } - public Object customArgTypedFixedList_DateNull() throws Exception { + public Object customArgTypedFixed_DateNull() throws Exception { DateDemo o = (DateDemo) input.readObject(); return o.getDate() == null && o.getDate1() == null; } diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java index 1f32a7e..a260f02 100644 --- a/test_hessian/src/main/java/test/TestCustomReply.java +++ b/test_hessian/src/main/java/test/TestCustomReply.java @@ -20,13 +20,14 @@ package test; import com.alibaba.com.caucho.hessian.io.Hessian2Output; import com.caucho.hessian.test.A0; import com.caucho.hessian.test.A1; -import test.model.DateDemo; - import java.io.OutputStream; import java.io.Serializable; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Date; import java.util.HashMap; +import test.model.DateDemo; + public class TestCustomReply { @@ -344,6 +345,56 @@ public class TestCustomReply { output.flush(); } + public void customReplyTypedFixedInteger() throws Exception { + BigInteger integer = new BigInteger("4294967298"); + output.writeObject(integer); + output.flush(); + } + + public void customReplyTypedFixedList_BigInteger() throws Exception { + BigInteger[] integers = new BigInteger[] { + new BigInteger("1234"), + new BigInteger("12347890"), + new BigInteger("123478901234"), + new BigInteger("1234789012345678"), + new BigInteger("123478901234567890"), + new BigInteger("1234789012345678901234"), + new BigInteger("12347890123456789012345678"), + new BigInteger("123478901234567890123456781234"), + new BigInteger("1234789012345678901234567812345678"), + new BigInteger("12347890123456789012345678123456781234"), + new BigInteger("-12347890123456789012345678123456781234"), + new BigInteger("0"), + }; + output.writeObject(integers); + output.flush(); + } + + public void customReplyTypedFixedList_CustomObject() throws Exception { + Object[] objects = new Object[] { + new BigInteger("1234"), + new BigInteger("-12347890"), + new BigInteger("0"), + new BigDecimal("123.4"), + new BigDecimal("-123.45"), + new BigDecimal("0"), + }; + output.writeObject(objects); + output.flush(); + } + + public void customReplyTypedFixedIntegerZero() throws Exception { + BigInteger integer = new BigInteger("0"); + output.writeObject(integer); + output.flush(); + } + + public void customReplyTypedFixedIntegerSigned() throws Exception { + BigInteger integer = new BigInteger("-4294967298"); + output.writeObject(integer); + output.flush(); + } + public void customReplyTypedFixedDecimal() throws Exception { BigDecimal decimal = new BigDecimal("100.256"); output.writeObject(decimal);