Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: add collection support according to java implementation #161

Merged
merged 6 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,40 @@ The encoded bytes of the struct `MyUser` is as following:
00000040 0c 30 31 30 2d 31 32 33 34 35 36 37 38 |.010-12345678|
```

#### Using Java collections
By default, the output of Hessian Java impl of a Java collection like java.util.HashSet will be decoded as `[]interface{}` in `go-hessian2`.
To apply the one-to-one mapping relationship between certain Java collection class and your Go struct, examples are as follows:

```go
//use HashSet as example
//define your struct, which should implements hessian.JavaCollectionObject
type JavaHashSet struct {
value []interface{}
}

//get the inside slice value
func (j *JavaHashSet) Get() []interface{} {
return j.value
}

//set the inside slice value
func (j *JavaHashSet) Set(v []interface{}) {
j.value = v
}

//should be the same as the class name of the Java collection
func (j *JavaHashSet) JavaClassName() string {
return "java.util.HashSet"
}

func init() {
//register your struct so that hessian can recognized it when encoding and decoding
SetCollectionSerialize(&JavaHashSet{})
}
```



## Notice for inheritance

`go-hessian2` supports inheritance struct, but the following situations should be avoided.
Expand Down
139 changes: 139 additions & 0 deletions java_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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 (
perrors "github.com/pkg/errors"
fangyincheng marked this conversation as resolved.
Show resolved Hide resolved
"reflect"
)

type JavaCollectionObject interface {
Get() []interface{}
Set([]interface{})
JavaClassName() string
}

var collectionTypeMap = make(map[string]reflect.Type, 16)

func SetCollectionSerialize(collection JavaCollectionObject) {
name := collection.JavaClassName()
v := reflect.ValueOf(collection)
var typ reflect.Type
switch v.Kind() {
case reflect.Struct:
typ = v.Type()
case reflect.Ptr:
typ = v.Elem().Type()
default:
typ = reflect.TypeOf(collection)
}
SetSerializer(name, JavaCollectionSerializer{})
RegisterPOJO(collection)
collectionTypeMap[name] = typ
}

func getCollectionSerialize(name string) reflect.Type {
return collectionTypeMap[name]
}

func isCollectionSerialize(name string) bool {
return getCollectionSerialize(name) != nil
}

type JavaCollectionSerializer struct {
}

func (JavaCollectionSerializer) EncObject(e *Encoder, vv POJO) error {
var (
err error
)
v, ok := vv.(JavaCollectionObject)
if !ok {
return perrors.New("can not be converted into java collection object")
}
collectionName := v.JavaClassName()
if collectionName == "" {
return perrors.New("collection name empty")
}
list := v.Get()
length := len(list)
typeName := v.JavaClassName()
err = writeCollectionBegin(length, typeName, e)
if err != nil {
return err
}
for i := 0; i < length; i++ {
if err = e.Encode(list[i]); err != nil {
return err
}
}
return nil
}

func (JavaCollectionSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
//for the java impl of hessian encode collections as list, which will not be decoded as object in go impl, this method should not be called
return nil, perrors.New("unexpected collection decode call")
}

func (d *Decoder) decodeCollection(length int, listTyp string) (interface{}, error) {
typ := getCollectionSerialize(listTyp)
if typ == nil {
return nil, perrors.New("no collection deserialize set as " + listTyp)
}
v := reflect.New(typ).Interface()
collcetionV, ok := v.(JavaCollectionObject)
if !ok {
return nil, perrors.New("collection deserialize err " + listTyp)
}
list, err := d.readTypedListValue(length, "", false)
if err != nil {
return nil, err
}
listInterface, err := EnsureInterface(list, nil)
if err != nil {
return nil, err
}
listV, listOk := listInterface.([]interface{})
if !listOk {
return nil, perrors.New("collection deserialize err " + listTyp)
}
collcetionV.Set(listV)
return collcetionV, nil
}

func writeCollectionBegin(length int, typeName string, e *Encoder) error {
var err error
if length <= int(LIST_DIRECT_MAX) {
e.Append([]byte{BC_LIST_DIRECT + byte(length)})
err = e.Encode(typeName)
if err != nil {
return err
}
} else {
e.Append([]byte{BC_LIST_FIXED})
err = e.Encode(typeName)
if err != nil {
return err
}
err = e.Encode(int32(length))
if err != nil {
return nil
}
}
return nil
}
58 changes: 58 additions & 0 deletions java_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 (
"testing"
)

func init() {
SetCollectionSerialize(&JavaHashSet{})
}

type JavaHashSet struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在gost中我们已经实现了正在的hashset,这个是否可以复用?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边设计设想是依赖接口,集合的具体实现开放给用户或外部来实现;这里只是一个实现了接口的简单例子

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我到不觉得是例子,一旦加入实际上则成为了标准,用户也不会另外实现。 所以我也建议尝试复用gost里面的hashset。

value []interface{}
}

func (j *JavaHashSet) Get() []interface{} {
return j.value
}

func (j *JavaHashSet) Set(v []interface{}) {
j.value = v
}

func (j *JavaHashSet) JavaClassName() string {
return "java.util.HashSet"
}

func TestListJavaCollectionEncode(t *testing.T) {
inside := make([]interface{}, 2)
inside[0] = int32(0)
inside[1] = int32(1)
hashSet := JavaHashSet{value: inside}
testJavaDecode(t, "customArgTypedFixedList_HashSet", &hashSet)
}

func TestListJavaCollectionDecode(t *testing.T) {
inside := make([]interface{}, 2)
inside[0] = int32(0)
inside[1] = int32(1)
hashSet := JavaHashSet{value: inside}
testDecodeFramework(t, "customReplyTypedFixedList_HashSet", &hashSet)
}
6 changes: 6 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,13 @@ func (d *Decoder) readTypedList(tag byte) (interface{}, error) {
} else {
return nil, perrors.Errorf("error typed list tag: 0x%x", tag)
}
if isCollectionSerialize(listTyp) {
return d.decodeCollection(length, listTyp)
}
return d.readTypedListValue(length, listTyp, isVariableArr)
}

func (d *Decoder) readTypedListValue(length int, listTyp string, isVariableArr bool) (interface{}, error) {
// return when no element
if length < 0 {
return nil, nil
Expand Down
11 changes: 7 additions & 4 deletions test_hessian/src/main/java/test/TestCustomDecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
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 java.util.*;

import test.model.DateDemo;


Expand Down Expand Up @@ -201,4 +199,9 @@ public Object customArgString_emoji() throws Exception {
String o = (String) input.readObject();
return TestString.getEmojiTestString().equals(o);
}

public Object customArgTypedFixedList_HashSet() throws Exception {
HashSet o = (HashSet) input.readObject();
return o.contains(0) && o.contains(1);
}
}
19 changes: 19 additions & 0 deletions test_hessian/src/main/java/test/TestCustomReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import test.model.DateDemo;


Expand Down Expand Up @@ -437,6 +440,22 @@ public void customReplyExtendClassToSingleStruct() throws Exception {
output.writeObject(dog);
output.flush();
}

public void customReplyTypedFixedList_HashSet() throws Exception {
Set<Integer> set = new HashSet<>();
set.add(0);
set.add(1);
output.writeObject(set);
output.flush();
}

public void customReplyTypedFixedList_HashSetCustomObject() throws Exception {
Set<Object> set = new HashSet<>();
set.add(new BigInteger("1234"));
set.add(new BigDecimal("123.4"));
output.writeObject(set);
output.flush();
}
}

interface Leg {
Expand Down