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 d520464  Fix decode enum not correctly (#332)
d520464 is described below

commit d520464544d64e8dc00278d38786d0264c6cf8c6
Author: 望哥 <[email protected]>
AuthorDate: Mon Oct 31 17:01:23 2022 +0800

    Fix decode enum not correctly (#332)
    
    * fix enum decode
    
    * fix duplicate name struct unit test
    
    * add unit tests in sub directories
    
    * add unit test for type convert
    
    * format code
    
    * format import
---
 .github/workflows/github-actions.yml |  2 +-
 hessian_test/dup_struct_name_test.go | 14 ------
 object.go                            | 15 ++++---
 testcases/user/user.go               | 87 ++++++++++++++++++++++++++++++++++++
 testcases/user/user_test.go          | 57 +++++++++++++++++++++++
 5 files changed, 154 insertions(+), 21 deletions(-)

diff --git a/.github/workflows/github-actions.yml 
b/.github/workflows/github-actions.yml
index 459f4f8..132f03e 100644
--- a/.github/workflows/github-actions.yml
+++ b/.github/workflows/github-actions.yml
@@ -80,7 +80,7 @@ jobs:
       run: golangci-lint run --timeout=10m -v
 
     - name: Go Test
-      run: GO111MODULE=on && go mod vendor && go test -race -v && go test 
-bench . -race -coverprofile=coverage.txt
+      run: GO111MODULE=on && go mod vendor && go test ./... -race -v && go 
test -bench . -race -coverprofile=coverage.txt
 
     - name: Coverage
       run: bash <(curl -s https://codecov.io/bash)
diff --git a/hessian_test/dup_struct_name_test.go 
b/hessian_test/dup_struct_name_test.go
index 14a0666..0c4954f 100644
--- a/hessian_test/dup_struct_name_test.go
+++ b/hessian_test/dup_struct_name_test.go
@@ -106,13 +106,6 @@ func TestDupStructNameResponse(t *testing.T) {
        assert.Nil(t, err)
 
        decodedResponse := &hessian.Response{}
-       decodedResponse.RspObj = &dupclass.CaseZ{}
-       err = codecR.ReadBody(decodedResponse)
-       assert.NotNil(t, err)
-       assert.Equal(t, ExpectedErrorMsg, err.Error())
-
-       decodedResponse = &hessian.Response{}
-       decodedResponse.RspObj = &CaseZ{}
        err = codecR.ReadBody(decodedResponse)
        assert.Nil(t, err)
 
@@ -134,13 +127,6 @@ func TestDupStructNameResponse2(t *testing.T) {
        assert.Nil(t, err)
 
        decodedResponse := &hessian.Response{}
-       decodedResponse.RspObj = &CaseZ{}
-       err = codecR.ReadBody(decodedResponse)
-       assert.NotNil(t, err)
-       assert.Equal(t, ExpectedErrorMsg, err.Error())
-
-       decodedResponse = &hessian.Response{}
-       decodedResponse.RspObj = &dupclass.CaseZ{}
        err = codecR.ReadBody(decodedResponse)
        assert.Nil(t, err)
 
diff --git a/object.go b/object.go
index 3d16043..f5898cf 100644
--- a/object.go
+++ b/object.go
@@ -508,17 +508,20 @@ func (d *Decoder) decInstance(typ reflect.Type, cls 
*ClassInfo) (interface{}, er
                        if err != nil {
                                // java enum
                                if fldRawValue.Type().Implements(javaEnumType) {
-                                       d.unreadByte() // Enum parsing, 
decInt64 above has read a byte, so you need to return a byte here
-                                       s, decErr := d.DecodeValue()
+                                       _ = d.unreadByte() // Enum parsing, 
decInt64 above has read a byte, so you need to return a byte here
+                                       enumVal, decErr := d.DecodeValue()
                                        if decErr != nil {
                                                return nil, 
perrors.Wrapf(decErr, "decInstance->decObject field name:%s", fieldName)
                                        }
-                                       enumValue, _ := s.(JavaEnum)
-                                       num = int32(enumValue)
-                               } else {
-                                       return nil, perrors.Wrapf(err, 
"decInstance->decInt32, field name:%s", fieldName)
+
+                                       SetValue(fldRawValue, 
reflect.ValueOf(enumVal))
+
+                                       continue
                                }
+
+                               return nil, perrors.Wrapf(err, 
"decInstance->decInt32, field name:%s", fieldName)
                        }
+
                        fldRawValue.SetInt(int64(num))
                case reflect.Uint16, reflect.Uint8:
                        num, err := d.decInt32(TAG_READ)
diff --git a/testcases/user/user.go b/testcases/user/user.go
new file mode 100644
index 0000000..888951b
--- /dev/null
+++ b/testcases/user/user.go
@@ -0,0 +1,87 @@
+/*
+ * 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 user
+
+import (
+       "fmt"
+       "strconv"
+       "time"
+)
+
+import (
+       hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+type Gender hessian.JavaEnum
+
+const (
+       MAN Gender = iota
+       WOMAN
+)
+
+var genderName = map[Gender]string{
+       MAN:   "MAN",
+       WOMAN: "WOMAN",
+}
+
+var genderValue = map[string]Gender{
+       "MAN":   MAN,
+       "WOMAN": WOMAN,
+}
+
+func (g Gender) JavaClassName() string {
+       return "org.apache.dubbo.sample.Gender"
+}
+
+func (g Gender) String() string {
+       s, ok := genderName[g]
+       if ok {
+               return s
+       }
+
+       return strconv.Itoa(int(g))
+}
+
+func (g Gender) EnumValue(s string) hessian.JavaEnum {
+       v, ok := genderValue[s]
+       if ok {
+               return hessian.JavaEnum(v)
+       }
+
+       return hessian.InvalidJavaEnum
+}
+
+type User struct {
+       // !!! Cannot define lowercase names of variable
+       ID   string
+       Name string
+       Age  int32
+       Time time.Time
+       Sex  Gender // notice: java enum Object <--> go string
+}
+
+func (u User) String() string {
+       return fmt.Sprintf(
+               "User{ID:%s, Name:%s, Age:%d, Time:%s, Sex:%s}",
+               u.ID, u.Name, u.Age, u.Time, u.Sex,
+       )
+}
+
+func (u *User) JavaClassName() string {
+       return "org.apache.dubbo.sample.User"
+}
diff --git a/testcases/user/user_test.go b/testcases/user/user_test.go
new file mode 100644
index 0000000..3c7cf9d
--- /dev/null
+++ b/testcases/user/user_test.go
@@ -0,0 +1,57 @@
+/*
+ * 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 user
+
+import (
+       "testing"
+       "time"
+)
+
+import (
+       hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+func TestEnumConvert(t *testing.T) {
+       var g interface{}
+       g = WOMAN
+
+       // new defined type cant be converted to the original type.
+       failConvertedValue, ok := g.(hessian.JavaEnum)
+       assert.False(t, ok)
+       assert.Equal(t, hessian.JavaEnum(0), failConvertedValue)
+}
+
+func TestUserEncodeDecode(t *testing.T) {
+       ts, _ := time.Parse("2006-01-02 15:04:05", "2019-01-01 12:34:56")
+       u1 := &User{ID: "001", Name: "Lily", Age: 18, Time: ts.Local(), Sex: 
WOMAN}
+       hessian.RegisterPOJO(u1)
+
+       encoder := hessian.NewEncoder()
+       err := encoder.Encode(u1)
+       assert.Nil(t, err)
+
+       buf := encoder.Buffer()
+       decoder := hessian.NewDecoder(buf)
+       dec, err := decoder.Decode()
+       assert.Nil(t, err)
+       assert.Equal(t, u1, dec)
+}

Reply via email to