This is an automated email from the ASF dual-hosted git repository.
wongoo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git
The following commit(s) were added to refs/heads/master by this push:
new 0db9c6b fix bug: support java enum variable list (#330)
0db9c6b is described below
commit 0db9c6b3e27301bf5b49b3f37643705065e345bb
Author: shenchao861129 <[email protected]>
AuthorDate: Mon Oct 24 08:49:27 2022 +0800
fix bug: support java enum variable list (#330)
Co-authored-by: shenchao <[email protected]>
---
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 0ce242b..af60860 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 54526ae..ab46987 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 b7afa11..7006849 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 8d22362..2a28037 100644
--- a/test_hessian/src/main/java/test/TestCustomReply.java
+++ b/test_hessian/src/main/java/test/TestCustomReply.java
@@ -693,6 +693,15 @@ public class TestCustomReply {
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 {