From 4d7455534f6582fcf28110dc6f17b7befeb15ab1 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Mon, 13 Sep 2021 14:17:01 +0800 Subject: [PATCH 1/6] feat(*): add Integer[] serializer --- array.go | 31 +++++++++++++++++++++++++++++++ array_test.go | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 array.go create mode 100644 array_test.go diff --git a/array.go b/array.go new file mode 100644 index 00000000..22c39931 --- /dev/null +++ b/array.go @@ -0,0 +1,31 @@ +package hessian + +func init() { + SetCollectionSerialize(&IntegerArray{}) +} + +type IntegerArray struct { + Values []int32 +} + +func (ia IntegerArray) Get() []interface{} { + res := make([]interface{}, len(ia.Values)) + for i, v := range ia.Values { + res[i] = v + } + return res +} + +func (ia IntegerArray) Set(vs []interface{}) { + values := make([]int32, len(vs)) + for i, v := range vs { + values[i] = v.(int32) + } + ia.Values = values +} + +func (IntegerArray) JavaClassName() string { + return "[java.lang.Integer" +} + +type ArraySerializer JavaCollectionSerializer diff --git a/array_test.go b/array_test.go new file mode 100644 index 00000000..04fbfa94 --- /dev/null +++ b/array_test.go @@ -0,0 +1,18 @@ +package hessian + +import ( + "fmt" + "testing" +) + +func Test(t *testing.T) { + var intArr = []int32{1, 2, 3} + integerArray := IntegerArray{intArr} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + err := jcs.EncObject(e, integerArray) + if err != nil { + return + } + fmt.Printf("%v\n", e.buffer) +} From bc7edd2bf1c292812dfefd90e000f0027bc19be4 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Mon, 13 Sep 2021 22:15:54 +0800 Subject: [PATCH 2/6] feat(*): support wrapper classes for Java basic types --- array.go | 199 +++++++++++++++++++++++++++++++++++++++++++++++++- array_test.go | 156 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 342 insertions(+), 13 deletions(-) diff --git a/array.go b/array.go index 22c39931..4bd1fdac 100644 --- a/array.go +++ b/array.go @@ -1,14 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package hessian func init() { SetCollectionSerialize(&IntegerArray{}) + SetCollectionSerialize(&ByteArray{}) + SetCollectionSerialize(&ShortArray{}) + SetCollectionSerialize(&BooleanArray{}) + SetCollectionSerialize(&LongArray{}) + SetCollectionSerialize(&FloatArray{}) + SetCollectionSerialize(&DoubleArray{}) + SetCollectionSerialize(&CharacterArray{}) +} + +//BooleanArray Boolean[] +type BooleanArray struct { + Values []bool +} + +func (ba *BooleanArray) Get() []interface{} { + res := make([]interface{}, len(ba.Values)) + for i, v := range ba.Values { + res[i] = v + } + return res +} + +func (ba *BooleanArray) Set(vs []interface{}) { + values := make([]bool, len(vs)) + for i, v := range vs { + values[i] = v.(bool) + } + ba.Values = values } +func (*BooleanArray) JavaClassName() string { + return "[java.lang.Boolean" +} + +//IntegerArray Integer[] type IntegerArray struct { Values []int32 } -func (ia IntegerArray) Get() []interface{} { +func (ia *IntegerArray) Get() []interface{} { res := make([]interface{}, len(ia.Values)) for i, v := range ia.Values { res[i] = v @@ -16,7 +66,7 @@ func (ia IntegerArray) Get() []interface{} { return res } -func (ia IntegerArray) Set(vs []interface{}) { +func (ia *IntegerArray) Set(vs []interface{}) { values := make([]int32, len(vs)) for i, v := range vs { values[i] = v.(int32) @@ -24,8 +74,149 @@ func (ia IntegerArray) Set(vs []interface{}) { ia.Values = values } -func (IntegerArray) JavaClassName() string { +func (*IntegerArray) JavaClassName() string { return "[java.lang.Integer" } -type ArraySerializer JavaCollectionSerializer +//ByteArray Byte[] +type ByteArray struct { + IntegerArray +} + +func (ba *ByteArray) Get() []interface{} { + res := make([]interface{}, len(ba.Values)) + for i, v := range ba.Values { + res[i] = v + } + return res +} + +func (ba *ByteArray) Set(vs []interface{}) { + values := make([]int32, len(vs)) + for i, v := range vs { + values[i] = v.(int32) + } + ba.Values = values +} + +func (*ByteArray) JavaClassName() string { + return "[java.lang.Byte" +} + +//ShortArray Short[] +type ShortArray struct { + IntegerArray +} + +func (sa *ShortArray) Get() []interface{} { + res := make([]interface{}, len(sa.Values)) + for i, v := range sa.Values { + res[i] = v + } + return res +} + +func (sa *ShortArray) Set(vs []interface{}) { + values := make([]int32, len(vs)) + for i, v := range vs { + values[i] = v.(int32) + } + sa.Values = values +} + +func (*ShortArray) JavaClassName() string { + return "[java.lang.Short" +} + +//LongArray Long[] +type LongArray struct { + Values []int64 +} + +func (ba *LongArray) Get() []interface{} { + res := make([]interface{}, len(ba.Values)) + for i, v := range ba.Values { + res[i] = v + } + return res +} + +func (ba *LongArray) Set(vs []interface{}) { + values := make([]int64, len(vs)) + for i, v := range vs { + values[i] = v.(int64) + } + ba.Values = values +} + +func (*LongArray) JavaClassName() string { + return "[java.lang.Long" +} + +//FloatArray Float[] +type FloatArray struct { + Values []float32 +} + +func (fa *FloatArray) Get() []interface{} { + res := make([]interface{}, len(fa.Values)) + for i, v := range fa.Values { + res[i] = v + } + return res +} + +func (fa *FloatArray) Set(vs []interface{}) { + values := make([]float32, len(vs)) + for i, v := range vs { + values[i] = float32(v.(float64)) + } + fa.Values = values +} + +func (*FloatArray) JavaClassName() string { + return "[java.lang.Float" +} + +// DoubleArray Double[] +type DoubleArray struct { + Values []float64 +} + +func (da *DoubleArray) Get() []interface{} { + res := make([]interface{}, len(da.Values)) + for i, v := range da.Values { + res[i] = v + } + return res +} + +func (da *DoubleArray) Set(vs []interface{}) { + values := make([]float64, len(vs)) + for i, v := range vs { + values[i] = v.(float64) + } + da.Values = values +} + +func (*DoubleArray) JavaClassName() string { + return "[java.lang.Double" +} + +// CharacterArray Character[] +type CharacterArray struct { + Values string +} + +func (ca *CharacterArray) Get() []interface{} { + res := []interface{}{ca.Values} + return res +} + +func (ca *CharacterArray) Set(vs []interface{}) { + ca.Values = vs[0].(string) +} + +func (*CharacterArray) JavaClassName() string { + return "[java.lang.Character" +} diff --git a/array_test.go b/array_test.go index 04fbfa94..92aafd65 100644 --- a/array_test.go +++ b/array_test.go @@ -1,18 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package hessian import ( - "fmt" + "github.com/stretchr/testify/assert" "testing" ) -func Test(t *testing.T) { - var intArr = []int32{1, 2, 3} - integerArray := IntegerArray{intArr} +func TestBooleanArray(t *testing.T) { + booleanArray := &BooleanArray{[]bool{true, false}} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, booleanArray) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(booleanArray.Values, decodeValue.(*BooleanArray).Values) +} + +func TestIntegerArray(t *testing.T) { + ia := &IntegerArray{[]int32{1, 2, 3}} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, ia) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(ia.Values, decodeValue.(*IntegerArray).Values) + + // Integer[] that length > 7 + bigIa := &IntegerArray{[]int32{1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}} + ne := &Encoder{} + err = jcs.EncObject(ne, bigIa) + a.Nil(err) + + decoder = NewDecoder(e.buffer) + decodeValue, err = decoder.DecodeValue() + a.Nil(err) + a.Equal(ia.Values, decodeValue.(*IntegerArray).Values) +} + +func TestByteArray(t *testing.T) { + ba := &ByteArray{} + ba.Values = []int32{1, 2, 3} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, ba) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(ba.Values, decodeValue.(*ByteArray).Values) +} + +func TestShortArray(t *testing.T) { + sa := &ShortArray{} + sa.Values = []int32{1, 2, 3} jcs := JavaCollectionSerializer{} e := &Encoder{} - err := jcs.EncObject(e, integerArray) - if err != nil { - return - } - fmt.Printf("%v\n", e.buffer) + a := assert.New(t) + + err := jcs.EncObject(e, sa) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(sa.Values, decodeValue.(*ShortArray).Values) +} + +func TestLongArray(t *testing.T) { + la := &LongArray{[]int64{1, 2, 3, 4}} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, la) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(la.Values, decodeValue.(*LongArray).Values) +} + +func TestFloatArray(t *testing.T) { + fa := &FloatArray{[]float32{1, 2, 3, 4}} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, fa) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(fa.Values, decodeValue.(*FloatArray).Values) +} + +func TestDoubleArray(t *testing.T) { + da := &DoubleArray{[]float64{1, 2, 3, 4}} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, da) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(da.Values, decodeValue.(*DoubleArray).Values) +} + +func TestCharacterArray(t *testing.T) { + ca := &CharacterArray{"hello world"} + jcs := JavaCollectionSerializer{} + e := &Encoder{} + a := assert.New(t) + + err := jcs.EncObject(e, ca) + a.Nil(err) + + decoder := NewDecoder(e.buffer) + decodeValue, err := decoder.DecodeValue() + a.Nil(err) + a.Equal(ca.Values, decodeValue.(*CharacterArray).Values) } From 8512ed30da14df6be6538efe2f84396a49784e71 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Tue, 14 Sep 2021 10:47:38 +0800 Subject: [PATCH 3/6] style(*): add nolint --- array.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/array.go b/array.go index 4bd1fdac..99f8560a 100644 --- a/array.go +++ b/array.go @@ -28,11 +28,12 @@ func init() { SetCollectionSerialize(&CharacterArray{}) } -//BooleanArray Boolean[] +// BooleanArray Boolean[] type BooleanArray struct { Values []bool } +// nolint func (ba *BooleanArray) Get() []interface{} { res := make([]interface{}, len(ba.Values)) for i, v := range ba.Values { @@ -41,6 +42,7 @@ func (ba *BooleanArray) Get() []interface{} { return res } +// nolint func (ba *BooleanArray) Set(vs []interface{}) { values := make([]bool, len(vs)) for i, v := range vs { @@ -49,6 +51,7 @@ func (ba *BooleanArray) Set(vs []interface{}) { ba.Values = values } +// nolint func (*BooleanArray) JavaClassName() string { return "[java.lang.Boolean" } @@ -58,6 +61,7 @@ type IntegerArray struct { Values []int32 } +// nolint func (ia *IntegerArray) Get() []interface{} { res := make([]interface{}, len(ia.Values)) for i, v := range ia.Values { @@ -66,6 +70,7 @@ func (ia *IntegerArray) Get() []interface{} { return res } +// nolint func (ia *IntegerArray) Set(vs []interface{}) { values := make([]int32, len(vs)) for i, v := range vs { @@ -74,15 +79,17 @@ func (ia *IntegerArray) Set(vs []interface{}) { ia.Values = values } +// nolint func (*IntegerArray) JavaClassName() string { return "[java.lang.Integer" } -//ByteArray Byte[] +// ByteArray Byte[] type ByteArray struct { IntegerArray } +// nolint func (ba *ByteArray) Get() []interface{} { res := make([]interface{}, len(ba.Values)) for i, v := range ba.Values { @@ -91,6 +98,7 @@ func (ba *ByteArray) Get() []interface{} { return res } +// nolint func (ba *ByteArray) Set(vs []interface{}) { values := make([]int32, len(vs)) for i, v := range vs { @@ -99,15 +107,17 @@ func (ba *ByteArray) Set(vs []interface{}) { ba.Values = values } +// nolint func (*ByteArray) JavaClassName() string { return "[java.lang.Byte" } -//ShortArray Short[] +// ShortArray Short[] type ShortArray struct { IntegerArray } +// nolint func (sa *ShortArray) Get() []interface{} { res := make([]interface{}, len(sa.Values)) for i, v := range sa.Values { @@ -116,6 +126,7 @@ func (sa *ShortArray) Get() []interface{} { return res } +// nolint func (sa *ShortArray) Set(vs []interface{}) { values := make([]int32, len(vs)) for i, v := range vs { @@ -124,15 +135,17 @@ func (sa *ShortArray) Set(vs []interface{}) { sa.Values = values } +// nolint func (*ShortArray) JavaClassName() string { return "[java.lang.Short" } -//LongArray Long[] +// LongArray Long[] type LongArray struct { Values []int64 } +// nolint func (ba *LongArray) Get() []interface{} { res := make([]interface{}, len(ba.Values)) for i, v := range ba.Values { @@ -141,6 +154,7 @@ func (ba *LongArray) Get() []interface{} { return res } +// nolint func (ba *LongArray) Set(vs []interface{}) { values := make([]int64, len(vs)) for i, v := range vs { @@ -149,15 +163,17 @@ func (ba *LongArray) Set(vs []interface{}) { ba.Values = values } +// nolint func (*LongArray) JavaClassName() string { return "[java.lang.Long" } -//FloatArray Float[] +// FloatArray Float[] type FloatArray struct { Values []float32 } +// nolint func (fa *FloatArray) Get() []interface{} { res := make([]interface{}, len(fa.Values)) for i, v := range fa.Values { @@ -166,6 +182,7 @@ func (fa *FloatArray) Get() []interface{} { return res } +// nolint func (fa *FloatArray) Set(vs []interface{}) { values := make([]float32, len(vs)) for i, v := range vs { @@ -174,6 +191,7 @@ func (fa *FloatArray) Set(vs []interface{}) { fa.Values = values } +// nolint func (*FloatArray) JavaClassName() string { return "[java.lang.Float" } @@ -183,6 +201,7 @@ type DoubleArray struct { Values []float64 } +// nolint func (da *DoubleArray) Get() []interface{} { res := make([]interface{}, len(da.Values)) for i, v := range da.Values { @@ -191,6 +210,7 @@ func (da *DoubleArray) Get() []interface{} { return res } +// nolint func (da *DoubleArray) Set(vs []interface{}) { values := make([]float64, len(vs)) for i, v := range vs { @@ -199,6 +219,7 @@ func (da *DoubleArray) Set(vs []interface{}) { da.Values = values } +// nolint func (*DoubleArray) JavaClassName() string { return "[java.lang.Double" } @@ -208,15 +229,18 @@ type CharacterArray struct { Values string } +// nolint func (ca *CharacterArray) Get() []interface{} { res := []interface{}{ca.Values} return res } +// nolint func (ca *CharacterArray) Set(vs []interface{}) { ca.Values = vs[0].(string) } +// nolint func (*CharacterArray) JavaClassName() string { return "[java.lang.Character" } From b470210f81c3777ba5ede691f89ffb20621753df Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Tue, 14 Sep 2021 21:34:08 +0800 Subject: [PATCH 4/6] feat(*): support java Character[] & format unit test --- array.go | 15 +++++++++++++-- array_test.go | 40 ++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/array.go b/array.go index 99f8560a..c85f059e 100644 --- a/array.go +++ b/array.go @@ -17,6 +17,10 @@ package hessian +import ( + "strings" +) + func init() { SetCollectionSerialize(&IntegerArray{}) SetCollectionSerialize(&ByteArray{}) @@ -231,13 +235,20 @@ type CharacterArray struct { // nolint func (ca *CharacterArray) Get() []interface{} { - res := []interface{}{ca.Values} + length := len(ca.Values) + charArr := strings.Split(ca.Values, "") + res := make([]interface{}, length) + for i := 0; i < length; i++ { + res[i] = charArr[i] + } return res } // nolint func (ca *CharacterArray) Set(vs []interface{}) { - ca.Values = vs[0].(string) + for _, v := range vs { + ca.Values = ca.Values + v.(string) + } } // nolint diff --git a/array_test.go b/array_test.go index 92aafd65..034d06ff 100644 --- a/array_test.go +++ b/array_test.go @@ -24,11 +24,10 @@ import ( func TestBooleanArray(t *testing.T) { booleanArray := &BooleanArray{[]bool{true, false}} - jcs := JavaCollectionSerializer{} e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, booleanArray) + err := e.Encode(booleanArray) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -39,11 +38,10 @@ func TestBooleanArray(t *testing.T) { func TestIntegerArray(t *testing.T) { ia := &IntegerArray{[]int32{1, 2, 3}} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, ia) + e := &Encoder{} + err := e.Encode(ia) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -54,7 +52,7 @@ func TestIntegerArray(t *testing.T) { // Integer[] that length > 7 bigIa := &IntegerArray{[]int32{1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}} ne := &Encoder{} - err = jcs.EncObject(ne, bigIa) + err = ne.Encode(bigIa) a.Nil(err) decoder = NewDecoder(e.buffer) @@ -66,11 +64,10 @@ func TestIntegerArray(t *testing.T) { func TestByteArray(t *testing.T) { ba := &ByteArray{} ba.Values = []int32{1, 2, 3} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, ba) + e := &Encoder{} + err := e.Encode(ba) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -82,11 +79,10 @@ func TestByteArray(t *testing.T) { func TestShortArray(t *testing.T) { sa := &ShortArray{} sa.Values = []int32{1, 2, 3} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, sa) + e := &Encoder{} + err := e.Encode(sa) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -97,11 +93,10 @@ func TestShortArray(t *testing.T) { func TestLongArray(t *testing.T) { la := &LongArray{[]int64{1, 2, 3, 4}} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, la) + e := &Encoder{} + err := e.Encode(la) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -112,11 +107,10 @@ func TestLongArray(t *testing.T) { func TestFloatArray(t *testing.T) { fa := &FloatArray{[]float32{1, 2, 3, 4}} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, fa) + e := &Encoder{} + err := e.Encode(fa) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -127,11 +121,10 @@ func TestFloatArray(t *testing.T) { func TestDoubleArray(t *testing.T) { da := &DoubleArray{[]float64{1, 2, 3, 4}} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, da) + e := &Encoder{} + err := e.Encode(da) a.Nil(err) decoder := NewDecoder(e.buffer) @@ -142,11 +135,10 @@ func TestDoubleArray(t *testing.T) { func TestCharacterArray(t *testing.T) { ca := &CharacterArray{"hello world"} - jcs := JavaCollectionSerializer{} - e := &Encoder{} a := assert.New(t) - err := jcs.EncObject(e, ca) + e := &Encoder{} + err := e.Encode(ca) a.Nil(err) decoder := NewDecoder(e.buffer) From feebbd1d90b370a1b6ca6c213a04acd880d91ab1 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Wed, 15 Sep 2021 20:40:18 +0800 Subject: [PATCH 5/6] feat(*): use uint8 as Byte & use int16 as Short --- array.go | 12 ++++++------ array_test.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/array.go b/array.go index c85f059e..1376a7a6 100644 --- a/array.go +++ b/array.go @@ -90,7 +90,7 @@ func (*IntegerArray) JavaClassName() string { // ByteArray Byte[] type ByteArray struct { - IntegerArray + Values []uint8 } // nolint @@ -104,9 +104,9 @@ func (ba *ByteArray) Get() []interface{} { // nolint func (ba *ByteArray) Set(vs []interface{}) { - values := make([]int32, len(vs)) + values := make([]uint8, len(vs)) for i, v := range vs { - values[i] = v.(int32) + values[i] = uint8(v.(int32)) } ba.Values = values } @@ -118,7 +118,7 @@ func (*ByteArray) JavaClassName() string { // ShortArray Short[] type ShortArray struct { - IntegerArray + Values []int16 } // nolint @@ -132,9 +132,9 @@ func (sa *ShortArray) Get() []interface{} { // nolint func (sa *ShortArray) Set(vs []interface{}) { - values := make([]int32, len(vs)) + values := make([]int16, len(vs)) for i, v := range vs { - values[i] = v.(int32) + values[i] = int16(v.(int32)) } sa.Values = values } diff --git a/array_test.go b/array_test.go index 034d06ff..f53256f5 100644 --- a/array_test.go +++ b/array_test.go @@ -63,7 +63,7 @@ func TestIntegerArray(t *testing.T) { func TestByteArray(t *testing.T) { ba := &ByteArray{} - ba.Values = []int32{1, 2, 3} + ba.Values = []uint8{1, 2, 3} a := assert.New(t) e := &Encoder{} @@ -78,7 +78,7 @@ func TestByteArray(t *testing.T) { func TestShortArray(t *testing.T) { sa := &ShortArray{} - sa.Values = []int32{1, 2, 3} + sa.Values = []int16{1, 2, 3} a := assert.New(t) e := &Encoder{} From 9aafe3ba6a455307b11841ac3e4636188c8d9758 Mon Sep 17 00:00:00 2001 From: dongjianhui03 Date: Fri, 17 Sep 2021 14:28:59 +0800 Subject: [PATCH 6/6] feat(*): add cross language encode/decode test --- object_test.go | 50 +++++++++++ output/output.go | 8 ++ output/testfuncs/wrapper_class_array.go | 86 +++++++++++++++++++ .../main/java/test/TestWrapperClassArray.java | 53 ++++++++++++ .../java/unit/GoWrapperClassArrayTest.java | 44 ++++++++++ 5 files changed, 241 insertions(+) create mode 100644 output/testfuncs/wrapper_class_array.go create mode 100644 test_hessian/src/main/java/test/TestWrapperClassArray.java create mode 100644 test_hessian/src/test/java/unit/GoWrapperClassArrayTest.java diff --git a/object_test.go b/object_test.go index e5b51f1e..f2d1e4f3 100644 --- a/object_test.go +++ b/object_test.go @@ -847,3 +847,53 @@ func TestCustomReplyGenericResponseList(t *testing.T) { assert.True(t, reflect.DeepEqual(data[1], list[1])) }) } + +func TestWrapperClassArray(t *testing.T) { + got, err := decodeJavaResponse(`byteArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + ba := &ByteArray{Values: []byte{byte(1), byte(100), byte(200)}} + assert.True(t, reflect.DeepEqual(got, ba)) + + got, err = decodeJavaResponse(`shortArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + sa := &ShortArray{Values: []int16{1, 100, 10000}} + assert.True(t, reflect.DeepEqual(got, sa)) + + got, err = decodeJavaResponse(`integerArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + ia := &IntegerArray{Values: []int32{1, 100, 10000}} + assert.True(t, reflect.DeepEqual(got, ia)) + + got, err = decodeJavaResponse(`longArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + la := &LongArray{Values: []int64{1, 100, 10000}} + assert.True(t, reflect.DeepEqual(got, la)) + + got, err = decodeJavaResponse(`characterArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + ca := &CharacterArray{Values: "hello world"} + assert.True(t, reflect.DeepEqual(got, ca)) + + got, err = decodeJavaResponse(`booleanArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + bla := &BooleanArray{Values: []bool{true, false, true}} + assert.True(t, reflect.DeepEqual(got, bla)) + + got, err = decodeJavaResponse(`floatArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + fa := &FloatArray{Values: []float32{1.0, 100.0, 10000.1}} + assert.True(t, reflect.DeepEqual(got, fa)) + + got, err = decodeJavaResponse(`doubleArray`, `test.TestWrapperClassArray`, false) + assert.NoError(t, err) + t.Logf("%T %+v", got, got) + da := &DoubleArray{Values: []float64{1.0, 100.0, 10000.1}} + assert.True(t, reflect.DeepEqual(got, da)) +} diff --git a/output/output.go b/output/output.go index f619a1aa..a27afe08 100644 --- a/output/output.go +++ b/output/output.go @@ -42,6 +42,14 @@ func init() { funcMap["JavaException"] = testfuncs.JavaException funcMap["UserArray"] = testfuncs.UserArray funcMap["UserList"] = testfuncs.UserList + funcMap["ByteArray"] = testfuncs.ByteArray + funcMap["ShortArray"] = testfuncs.ShortArray + funcMap["IntegerArray"] = testfuncs.IntegerArray + funcMap["LongArray"] = testfuncs.LongArray + funcMap["BooleanArray"] = testfuncs.BooleanArray + funcMap["CharacterArray"] = testfuncs.CharacterArray + funcMap["FloatArray"] = testfuncs.FloatArray + funcMap["DoubleArray"] = testfuncs.DoubleArray } func main() { diff --git a/output/testfuncs/wrapper_class_array.go b/output/testfuncs/wrapper_class_array.go new file mode 100644 index 00000000..a3cd3986 --- /dev/null +++ b/output/testfuncs/wrapper_class_array.go @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package testfuncs + +import ( + hessian "github.com/apache/dubbo-go-hessian2" +) + +// nolint +func ByteArray() []byte { + e := hessian.NewEncoder() + ba := &hessian.ByteArray{Values: []byte{byte(1), byte(100), byte(200)}} + e.Encode(ba) + return e.Buffer() +} + +// nolint +func ShortArray() []byte { + e := hessian.NewEncoder() + sa := &hessian.ShortArray{Values: []int16{1, 100, 10000}} + e.Encode(sa) + return e.Buffer() +} + +// nolint +func IntegerArray() []byte { + e := hessian.NewEncoder() + ia := &hessian.IntegerArray{Values: []int32{1, 100, 10000}} + e.Encode(ia) + return e.Buffer() +} + +// nolint +func LongArray() []byte { + e := hessian.NewEncoder() + la := &hessian.LongArray{Values: []int64{1, 100, 10000}} + e.Encode(la) + return e.Buffer() +} + +// nolint +func BooleanArray() []byte { + e := hessian.NewEncoder() + ba := &hessian.BooleanArray{Values: []bool{true, false, true}} + e.Encode(ba) + return e.Buffer() +} + +// nolint +func CharacterArray() []byte { + e := hessian.NewEncoder() + ca := &hessian.CharacterArray{Values: "hello world"} + e.Encode(ca) + return e.Buffer() +} + +// nolint +func FloatArray() []byte { + e := hessian.NewEncoder() + fa := &hessian.FloatArray{Values: []float32{1.0, 100.0, 10000.1}} + e.Encode(fa) + return e.Buffer() +} + +// nolint +func DoubleArray() []byte { + e := hessian.NewEncoder() + da := &hessian.DoubleArray{Values: []float64{1.0, 100.0, 10000.1}} + e.Encode(da) + return e.Buffer() +} diff --git a/test_hessian/src/main/java/test/TestWrapperClassArray.java b/test_hessian/src/main/java/test/TestWrapperClassArray.java new file mode 100644 index 00000000..21859942 --- /dev/null +++ b/test_hessian/src/main/java/test/TestWrapperClassArray.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test; + +public class TestWrapperClassArray { + + public static Byte[] byteArray() { + return new Byte[]{1, 100, -56}; + } + + public static Short[] shortArray() { + return new Short[]{1, 100, 10000}; + } + + public static Integer[] integerArray() { + return new Integer[]{1, 100, 10000}; + } + + public static Long[] longArray() { + return new Long[]{1L, 100L, 10000L}; + } + + public static Boolean[] booleanArray() { + return new Boolean[]{true, false, true}; + } + + public static Float[] floatArray() { + return new Float[]{1.0f, 100.0f, 10000.1f}; + } + + public static Double[] doubleArray() { + return new Double[]{1.0, 100.0, 10000.1}; + } + + public static Character[] characterArray() { + return new Character[]{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}; + } +} diff --git a/test_hessian/src/test/java/unit/GoWrapperClassArrayTest.java b/test_hessian/src/test/java/unit/GoWrapperClassArrayTest.java new file mode 100644 index 00000000..ae6ebcdc --- /dev/null +++ b/test_hessian/src/test/java/unit/GoWrapperClassArrayTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package unit; + +import org.junit.Assert; +import org.junit.Test; + +public class GoWrapperClassArrayTest { + + @Test + public void test() { + Assert.assertArrayEquals(new Byte[]{1, 100, -56} + , (Byte[])GoTestUtil.readGoObject("ByteArray")); + Assert.assertArrayEquals(new Short[]{1, 100, 10000} + , (Short[])GoTestUtil.readGoObject("ShortArray")); + Assert.assertArrayEquals(new Integer[]{1, 100, 10000} + , (Integer[])GoTestUtil.readGoObject("IntegerArray")); + Assert.assertArrayEquals(new Long[]{1L, 100L, 10000L} + , (Long[])GoTestUtil.readGoObject("LongArray")); + Assert.assertArrayEquals(new Boolean[]{true, false, true} + , (Boolean[])GoTestUtil.readGoObject("BooleanArray")); + Assert.assertArrayEquals(new Float[]{1.0f, 100.0f, 10000.1f} + , (Float[])GoTestUtil.readGoObject("FloatArray")); + Assert.assertArrayEquals(new Double[]{1.0, 100.0, 10000.1} + , (Double[])GoTestUtil.readGoObject("DoubleArray")); + Assert.assertArrayEquals(new Character[]{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'} + , (Character[])GoTestUtil.readGoObject("CharacterArray")); + } +}