From 0db9c6b3e27301bf5b49b3f37643705065e345bb Mon Sep 17 00:00:00 2001 From: shenchao861129 Date: Mon, 24 Oct 2022 08:49:27 +0800 Subject: [PATCH] fix bug: support java enum variable list (#330) Co-authored-by: shenchao --- list.go | 2 - object.go | 7 ++- object_test.go | 57 +++++++++++++++++++ .../src/main/java/test/TestCustomReply.java | 9 +++ 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/list.go b/list.go index 0ce242b8..af608605 100644 --- a/list.go +++ b/list.go @@ -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)) } } } diff --git a/object.go b/object.go index 54526ae4..ab469876 100644 --- a/object.go +++ b/object.go @@ -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 @@ -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 diff --git a/object_test.go b/object_test.go index b7afa118..7006849a 100644 --- a/object_test.go +++ b/object_test.go @@ -21,6 +21,7 @@ import ( "encoding/json" "math" "reflect" + "strconv" "testing" "time" @@ -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 +} diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java index 8d22362e..2a280371 100644 --- a/test_hessian/src/main/java/test/TestCustomReply.java +++ b/test_hessian/src/main/java/test/TestCustomReply.java @@ -693,6 +693,15 @@ public void customReplyEnumSet() throws Exception { output.writeObject(map); output.flush(); } + + public void customReplyEnumVariableList() throws Exception { + List 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 {