Skip to content

Commit

Permalink
fix bug: support java enum variable list (#330)
Browse files Browse the repository at this point in the history
Co-authored-by: shenchao <shenchao@zongheng.com>
  • Loading branch information
shenchao861129 and shenchao authored Oct 24, 2022
1 parent e13beea commit 0db9c6b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
2 changes: 0 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,6 @@ func (d *Decoder) readTypedListValue(length int, listTyp string, isVariableArr b
} else {
if it != nil {
aryValue.Index(j).Set(EnsureRawValue(it))
} else {
SetValue(aryValue.Index(j), EnsureRawValue(it))
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func (d *Decoder) getStructDefByIndex(idx int) (reflect.Type, *ClassInfo, error)
return s.typ, cls, nil
}

func (d *Decoder) decEnum(javaName string, flag int32) (JavaEnum, error) {
func (d *Decoder) decEnum(javaName string, flag int32) (interface{}, error) {
var (
err error
enumName string
Expand All @@ -678,8 +678,9 @@ func (d *Decoder) decEnum(javaName string, flag int32) (JavaEnum, error) {
}

enumValue = info.inst.(POJOEnum).EnumValue(enumName)
d.appendRefs(enumValue)
return enumValue, nil
enumVal := PackPtr(reflect.ValueOf(enumValue).Convert(info.typ)).Interface()
d.appendRefs(enumVal)
return enumVal, nil
}

// skip this object
Expand Down
57 changes: 57 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"math"
"reflect"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -1061,3 +1062,59 @@ func TestDecodeSliceIntegerHasNull(t *testing.T) {
RegisterPOJO(&User{})
testDecodeFramework(t, "customReplyTypedListIntegerHasNull", &User{Id: 0, List: []int32{1, 0}})
}

func TestDecodeCustomReplyEnumVariableList(t *testing.T) {
for v := range _LocaleCategoryEnumValues {
RegisterJavaEnum(v)
}

got, err := decodeJavaResponse(`customReplyEnumVariableList`, ``, false)
assert.NoError(t, err)
t.Logf("customReplyEnumVariableList %T %+v", got, got)
enumList, ok := got.([]*LocaleCategoryEnum)
if !ok {
t.Errorf("expect []LocaleCategoryEnum, but get %v", got)
return
}
assert.Equal(t, LocaleCategoryEnumDisplay, *enumList[0])
assert.Nil(t, enumList[1])
assert.Equal(t, LocaleCategoryEnumFormat, *enumList[2])
}

const (
LocaleCategoryEnumDisplay LocaleCategoryEnum = iota
LocaleCategoryEnumFormat
)

var (
_LocaleCategoryEnumValues = map[LocaleCategoryEnum]string{
LocaleCategoryEnumDisplay: "DISPLAY",
LocaleCategoryEnumFormat: "FORMAT",
}
_LocaleCategoryEnumEntities = map[string]LocaleCategoryEnum{
"DISPLAY": LocaleCategoryEnumDisplay,
"FORMAT": LocaleCategoryEnumFormat,
}
)

type LocaleCategoryEnum JavaEnum

func (e LocaleCategoryEnum) JavaClassName() string {
return "java.util.Locale$Category"
}

func (e LocaleCategoryEnum) String() string {
if v, ok := _LocaleCategoryEnumValues[e]; ok {
return v
}

return strconv.Itoa(int(e))
}

func (e LocaleCategoryEnum) EnumValue(s string) JavaEnum {
if v, ok := _LocaleCategoryEnumEntities[s]; ok {
return JavaEnum(v)
}

return InvalidJavaEnum
}
9 changes: 9 additions & 0 deletions test_hessian/src/main/java/test/TestCustomReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,15 @@ public void customReplyEnumSet() throws Exception {
output.writeObject(map);
output.flush();
}

public void customReplyEnumVariableList() throws Exception {
List<Locale.Category> enumList = new ArrayList<>();
enumList.add(Locale.Category.DISPLAY);
enumList.add(null);
enumList.add(Locale.Category.FORMAT);
output.writeObject(enumList.toArray(new Locale.Category[enumList.size()]));
output.flush();
}
}

interface Leg {
Expand Down

0 comments on commit 0db9c6b

Please sign in to comment.