Skip to content

Commit

Permalink
Merge pull request #350 from wongoo/wrapper_type_dev
Browse files Browse the repository at this point in the history
support java wrapper types
  • Loading branch information
AlexStocks authored Feb 21, 2023
2 parents 4820c03 + a03ceac commit 6ea8710
Show file tree
Hide file tree
Showing 24 changed files with 862 additions and 149 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ thanks to [micln](https://github.com/micln), [pantianying](https://github.com/pa

* [All JDK Exceptions](https://github.com/apache/dubbo-go-hessian2/issues/59)
* [Field Alias By Alias](https://github.com/apache/dubbo-go-hessian2/issues/19)
* [Java wrapper type](https://github.com/apache/dubbo-go-hessian2/issues/349)
* [Java Bigdecimal](https://github.com/apache/dubbo-go-hessian2/issues/89)
* [Java Date & Time](https://github.com/apache/dubbo-go-hessian2/issues/90)
* [java8 time.Date](https://github.com/apache/dubbo-go-hessian2/pull/212)
Expand All @@ -41,25 +42,34 @@ Cross languages message definition should be careful, the following situations s

So we can maintain a cross language type mapping:

| hessian type | java type | golang type |
| --- | --- | --- |
| **null** | null | nil |
| **binary** | byte[] | []byte |
| **boolean** | boolean | bool |
| **date** | java.util.Date | time.Time |
| **double** | double | float64 |
| **int** | int | int32 |
| **long** | long | int64 |
| **string** | java.lang.String | string |
| **list** | java.util.List | slice |
| **map** | java.util.Map | map |
| **object** | custom define object | custom define struct|
| **OTHER COMMON USING TYPE** | | |
| **big decimal** | java.math.BigDecimal | github.com/dubbogo/gost/math/big/Decimal |
| **big integer** | java.math.BigInteger | github.com/dubbogo/gost/math/big/Integer |
| **date** | java.sql.Date | github.com/apache/dubbo-go-hessian2/java_sql_time/Date |
| **date** | java.sql.Time | github.com/apache/dubbo-go-hessian2/java_sql_time/Time |
| **date** | all java8 sdk time | github.com/apache/dubbo-go-hessian2/java8_time |
| hessian type | java type | golang type |
|-----------------------------|----------------------|--------------------------------------------------------|
| **null** | null | nil |
| **binary** | byte[] | []byte |
| **boolean** | boolean | bool |
| **date** | java.util.Date | time.Time |
| **double** | double | float64 |
| **int** | int | int32 |
| **long** | long | int64 |
| **string** | java.lang.String | string |
| **list** | java.util.List | slice |
| **map** | java.util.Map | map |
| **object** | custom define object | custom define struct |
| **big decimal** | java.math.BigDecimal | github.com/dubbogo/gost/math/big/Decimal |
| **big integer** | java.math.BigInteger | github.com/dubbogo/gost/math/big/Integer |
| **date** | java.sql.Date | github.com/apache/dubbo-go-hessian2/java_sql_time/Date |
| **date** | java.sql.Time | github.com/apache/dubbo-go-hessian2/java_sql_time/Time |
| **date** | all java8 sdk time | github.com/apache/dubbo-go-hessian2/java8_time |
| **Integer** | java.lang.Integer | *int32 |
| **Byte** | java.lang.Byte | *byte |
| **Short** | java.lang.Short | *int16 |
| **Boolean** | java.lang.Boolean | *bool |
| **Long** | java.lang.Long | *int64 |
| **Float** | java.lang.Float | *float32 |
| **Double** | java.lang.Double | *float64 |
| **Character** | java.lang.Character | *hessian.Rune |
| **OTHER COMMON USING TYPE** | | |


## reference

Expand Down
21 changes: 9 additions & 12 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ import (
"strings"
)

func init() {
SetCollectionSerialize(&IntegerArray{})
SetCollectionSerialize(&ByteArray{})
SetCollectionSerialize(&ShortArray{})
SetCollectionSerialize(&BooleanArray{})
SetCollectionSerialize(&LongArray{})
SetCollectionSerialize(&FloatArray{})
SetCollectionSerialize(&DoubleArray{})
SetCollectionSerialize(&CharacterArray{})
}

// BooleanArray Boolean[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type BooleanArray struct {
Values []bool
}
Expand Down Expand Up @@ -60,7 +50,8 @@ func (*BooleanArray) JavaClassName() string {
return "[java.lang.Boolean"
}

// IntegerArray Integer[]
// IntegerArray Integer[].
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type IntegerArray struct {
Values []int32
}
Expand Down Expand Up @@ -89,6 +80,7 @@ func (*IntegerArray) JavaClassName() string {
}

// ByteArray Byte[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type ByteArray struct {
Values []uint8
}
Expand Down Expand Up @@ -117,6 +109,7 @@ func (*ByteArray) JavaClassName() string {
}

// ShortArray Short[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type ShortArray struct {
Values []int16
}
Expand Down Expand Up @@ -145,6 +138,7 @@ func (*ShortArray) JavaClassName() string {
}

// LongArray Long[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type LongArray struct {
Values []int64
}
Expand Down Expand Up @@ -173,6 +167,7 @@ func (*LongArray) JavaClassName() string {
}

// FloatArray Float[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type FloatArray struct {
Values []float32
}
Expand Down Expand Up @@ -201,6 +196,7 @@ func (*FloatArray) JavaClassName() string {
}

// DoubleArray Double[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type DoubleArray struct {
Values []float64
}
Expand Down Expand Up @@ -229,6 +225,7 @@ func (*DoubleArray) JavaClassName() string {
}

// CharacterArray Character[]
// Deprecated: it will not be supported in next major version, being replaced by a slice type instead.
type CharacterArray struct {
Values string
}
Expand Down
142 changes: 77 additions & 65 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,131 +18,143 @@
package hessian

import (
"github.com/stretchr/testify/assert"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestBooleanArray(t *testing.T) {
booleanArray := &BooleanArray{[]bool{true, false}}
e := &Encoder{}
a := assert.New(t)
var x, y = true, false
booleanArray := []*bool{&x, &y}
e := NewEncoder()

err := e.Encode(booleanArray)
a.Nil(err)
assert.Nil(t, err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(booleanArray.Values, decodeValue.(*BooleanArray).Values)
decodeValue, err := decoder.Decode()
assert.Nil(t, err)
assert.Equal(t, booleanArray, decodeValue)
}

func TestIntegerArray(t *testing.T) {
ia := &IntegerArray{[]int32{1, 2, 3}}
a := assert.New(t)
var a, b, c int32 = 1, 2, 3
ia := []*int32{&a, &b, &c}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(ia)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(ia.Values, decodeValue.(*IntegerArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(ia, decodeValue)

// 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{}
//bigIa := &IntegerArray{[]int32{1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}}
bigIa := []*int32{&a, &b, &c, &a, &b, &c, &a, &b, &c, &a, &b, &c, &a, &b, &c, &a, &b, &c}
ne := NewEncoder()
err = ne.Encode(bigIa)
a.Nil(err)
tt.Nil(err)

decoder = NewDecoder(e.buffer)
decodeValue, err = decoder.DecodeValue()
a.Nil(err)
a.Equal(ia.Values, decodeValue.(*IntegerArray).Values)
decodeValue, err = decoder.Decode()
tt.Nil(err)
tt.Equal(ia, decodeValue)
}

func TestByteArray(t *testing.T) {
ba := &ByteArray{}
ba.Values = []uint8{1, 2, 3}
a := assert.New(t)
var a, b, c uint8 = 1, 2, 3
ba := []*byte{&a, &b, &c}

tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(ba)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(ba.Values, decodeValue.(*ByteArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(ba, decodeValue)
}

func TestShortArray(t *testing.T) {
sa := &ShortArray{}
sa.Values = []int16{1, 2, 3}
a := assert.New(t)
var a, b, c int16 = 1, 2, 3
sa := []*int16{&a, &b, &c}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(sa)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(sa.Values, decodeValue.(*ShortArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(sa, decodeValue)
}

func TestLongArray(t *testing.T) {
la := &LongArray{[]int64{1, 2, 3, 4}}
a := assert.New(t)
var a, b, c, d int64 = 1, 2, 3, 4
la := []*int64{&a, &b, &c, &d}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(la)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(la.Values, decodeValue.(*LongArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(la, decodeValue)
}

func TestFloatArray(t *testing.T) {
fa := &FloatArray{[]float32{1, 2, 3, 4}}
a := assert.New(t)
var a, b, c, d float32 = 1, 2, 3, 4
fa := []*float32{&a, &b, &c, &d}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(fa)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(fa.Values, decodeValue.(*FloatArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(fa, decodeValue)
}

func TestDoubleArray(t *testing.T) {
da := &DoubleArray{[]float64{1, 2, 3, 4}}
a := assert.New(t)
var a, b, c, d float64 = 1, 2, 3, 4
da := []*float64{&a, &b, &c, &d}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(da)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(da.Values, decodeValue.(*DoubleArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)
tt.Equal(da, decodeValue)
}

func TestCharacterArray(t *testing.T) {
ca := &CharacterArray{"hello world"}
a := assert.New(t)
var r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11 Rune = 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'
ca := []*Rune{&r1, &r2, &r3, &r4, &r5, &r6, &r7, &r8, &r9, &r10, &r11}
tt := assert.New(t)

e := &Encoder{}
e := NewEncoder()
err := e.Encode(ca)
a.Nil(err)
tt.Nil(err)

decoder := NewDecoder(e.buffer)
decodeValue, err := decoder.DecodeValue()
a.Nil(err)
a.Equal(ca.Values, decodeValue.(*CharacterArray).Values)
decodeValue, err := decoder.Decode()
tt.Nil(err)

expected := []*int32{(*int32)(&r1), (*int32)(&r2), (*int32)(&r3), (*int32)(&r4), (*int32)(&r5), (*int32)(&r6), (*int32)(&r7), (*int32)(&r8), (*int32)(&r9), (*int32)(&r10), (*int32)(&r11)}
tt.Equal(expected, decodeValue)
}
Loading

0 comments on commit 6ea8710

Please sign in to comment.