Skip to content
Permalink
Browse files
feat(*): support wrapper classes for Java basic types
  • Loading branch information
dongjianhui03 committed Sep 13, 2021
1 parent 4d74555 commit bc7edd2bf1c292812dfefd90e000f0027bc19be4
Showing 2 changed files with 342 additions and 13 deletions.
199 array.go
@@ -1,31 +1,222 @@
/*
* 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
}
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)
}
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"
}
@@ -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)
}

0 comments on commit bc7edd2

Please sign in to comment.