Skip to content
Permalink
Browse files
fix getting nil result for decoding list to generic interface field (#…
  • Loading branch information
wongoo committed Jul 5, 2021
1 parent 8dc4b20 commit e7144303cf1a338c905e31a574e57bf3effee6a5
Showing 5 changed files with 78 additions and 16 deletions.
@@ -397,14 +397,7 @@ func SetSlice(dest reflect.Value, objects interface{}) error {
}

if ref, ok := objects.(*_refHolder); ok {
v, err := ConvertSliceValueType(destTyp, ref.value)
if err != nil {
return err
}
SetValue(dest, v)
ref.change(v) // change finally
ref.notify() // delay set value to all destinations
return nil
return unpackRefHolder(dest, destTyp, ref)
}

v := EnsurePackValue(objects)
@@ -422,9 +415,21 @@ func SetSlice(dest reflect.Value, objects interface{}) error {
return nil
}

// unpackRefHolder unpack the ref holder when decoding slice finished.
func unpackRefHolder(dest reflect.Value, destTyp reflect.Type, ref *_refHolder) error {
v, err := ConvertSliceValueType(destTyp, ref.value)
if err != nil {
return err
}
SetValue(dest, v)
ref.change(v) // change finally
ref.notify() // delay set value to all destinations
return nil
}

// ConvertSliceValueType convert to slice of destination type
func ConvertSliceValueType(destTyp reflect.Type, v reflect.Value) (reflect.Value, error) {
if destTyp == v.Type() {
if destTyp == v.Type() || destTyp.Kind() == reflect.Interface {
return v, nil
}

@@ -539,8 +539,12 @@ func (d *Decoder) decInstance(typ reflect.Type, cls *classInfo) (interface{}, er
return nil, perrors.WithStack(err)
}
if s != nil {
// set value which accepting pointers
SetValue(fldRawValue, EnsurePackValue(s))
if ref, ok := s.(*_refHolder); ok {
_ = unpackRefHolder(fldRawValue, fldTyp, ref)
} else {
// set value which accepting pointers
SetValue(fldRawValue, EnsurePackValue(s))
}
}
default:
return nil, perrors.Errorf("unknown struct member type: %v %v", kind, typ.Name()+"."+fieldStruct.Name)
@@ -812,3 +812,38 @@ func TestCustomReplyGenericResponseBusinessData(t *testing.T) {

testDecodeFramework(t, "customReplyGenericResponseBusinessData", res)
}

func TestCustomReplyGenericResponseList(t *testing.T) {
data := []*BusinessData{
{
Name: "apple",
Count: 5,
},
{
Name: "banana",
Count: 6,
},
}
res := &GenericResponse{
Code: 202,
Data: data,
}
RegisterPOJO(data[0])
RegisterPOJO(res)

testDecodeFrameworkFunc(t, "customReplyGenericResponseList", func(r interface{}) {
expect, ok := r.(*GenericResponse)
if !ok {
t.Errorf("expect *GenericResponse, but get %v", r)
return
}
list, dataOk := expect.Data.([]interface{})
if !dataOk {
t.Errorf("expect []interface{}, but get %v", expect.Data)
return
}
assert.Equal(t, res.Code, expect.Code)
assert.True(t, reflect.DeepEqual(data[0], list[0]))
assert.True(t, reflect.DeepEqual(data[1], list[1]))
})
}
@@ -27,13 +27,13 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hessian-lite</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.60</version>
<version>4.0.65</version>
<scope>compile</scope>
</dependency>
<dependency>
@@ -50,12 +50,12 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -31,7 +31,16 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


public class TestCustomReply {
@@ -599,6 +608,15 @@ public void customReplyGenericResponseBusinessData() throws Exception {
output.flush();
}

public void customReplyGenericResponseList() throws Exception {
List<BusinessData> list = new ArrayList<>();
list.add(new BusinessData("apple", 5));
list.add(new BusinessData("banana", 6));
Response<List<BusinessData>> response = new Response<>(202, list);
output.writeObject(response);
output.flush();
}

public void customReplyUUID() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
UUID uuid1 = new UUID(459021424248441700L, -7160773830801198154L);

0 comments on commit e714430

Please sign in to comment.