From 3e94c7f443e9818d3f0c4432e5e849d05c20a421 Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 14:09:29 +0800 Subject: [PATCH 1/6] fix enum decode --- object.go | 15 ++++--- testcases/user/user.go | 85 +++++++++++++++++++++++++++++++++++++ testcases/user/user_test.go | 42 ++++++++++++++++++ 3 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 testcases/user/user.go create mode 100644 testcases/user/user_test.go diff --git a/object.go b/object.go index ab469876..62b8fc86 100644 --- a/object.go +++ b/object.go @@ -499,17 +499,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 00000000..cf4a7630 --- /dev/null +++ b/testcases/user/user.go @@ -0,0 +1,85 @@ +/* + * 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" + + 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" +} \ No newline at end of file diff --git a/testcases/user/user_test.go b/testcases/user/user_test.go new file mode 100644 index 00000000..cfd30018 --- /dev/null +++ b/testcases/user/user_test.go @@ -0,0 +1,42 @@ +/* + * 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" + + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/stretchr/testify/assert" +) + +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) +} From a3fe6e9015d96ae772af34d25ee250b8ef012c5f Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 14:25:51 +0800 Subject: [PATCH 2/6] fix duplicate name struct unit test --- hessian_test/dup_struct_name_test.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/hessian_test/dup_struct_name_test.go b/hessian_test/dup_struct_name_test.go index 14a0666f..0c4954f1 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) From 7420f80409804891bd2a7a1df1013bdcc1932a8e Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 14:29:22 +0800 Subject: [PATCH 3/6] add unit tests in sub directories --- .github/workflows/github-actions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 459f4f83..132f03e4 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) From 601a729ea5b8b935ee381d8c02b9644ec79ea0c8 Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 14:39:01 +0800 Subject: [PATCH 4/6] add unit test for type convert --- testcases/user/user_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/testcases/user/user_test.go b/testcases/user/user_test.go index cfd30018..aa7ba09e 100644 --- a/testcases/user/user_test.go +++ b/testcases/user/user_test.go @@ -25,6 +25,16 @@ 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} From cd46406ccc6bf3561ff63d64a0ac922945d87dfe Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 15:24:54 +0800 Subject: [PATCH 5/6] format code --- testcases/user/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcases/user/user.go b/testcases/user/user.go index cf4a7630..8a3018b1 100644 --- a/testcases/user/user.go +++ b/testcases/user/user.go @@ -82,4 +82,4 @@ func (u User) String() string { func (u *User) JavaClassName() string { return "org.apache.dubbo.sample.User" -} \ No newline at end of file +} From 7d37ede9a5ad5e98dd37d5a5de04f005bd0b9cd6 Mon Sep 17 00:00:00 2001 From: wongoo Date: Sat, 29 Oct 2022 22:54:25 +0800 Subject: [PATCH 6/6] format import --- testcases/user/user.go | 2 ++ testcases/user/user_test.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/testcases/user/user.go b/testcases/user/user.go index 8a3018b1..888951b6 100644 --- a/testcases/user/user.go +++ b/testcases/user/user.go @@ -21,7 +21,9 @@ import ( "fmt" "strconv" "time" +) +import ( hessian "github.com/apache/dubbo-go-hessian2" ) diff --git a/testcases/user/user_test.go b/testcases/user/user_test.go index aa7ba09e..3c7cf9df 100644 --- a/testcases/user/user_test.go +++ b/testcases/user/user_test.go @@ -20,8 +20,13 @@ package user import ( "testing" "time" +) +import ( hessian "github.com/apache/dubbo-go-hessian2" +) + +import ( "github.com/stretchr/testify/assert" )