Repository: apache/dubbo-go-hessian2
Pull request #290 (2021-11-16 11:00:01)

fix go type name for list
**What this PR does**:
- fix go type name for list


**Does this PR introduce a user-facing change?**:

```release-note
- fix go type name for list
```

codecov-commenter: # [Codecov](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#290](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (2e8ef9a) into [master](https://codecov.io/gh/apache/dubbo-go-hessian2/commit/9653a02986fcf3c6f20d2aff634475ad7bfa35e5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (9653a02) will **increase** coverage by `0.02%`.
> The diff coverage is `94.11%`.

> :exclamation: Current head 2e8ef9a differs from pull request most recent head cd6e712. Consider uploading reports for the commit cd6e712 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290/graphs/tree.svg?width=650&height=150&src=pr&token=Yh82j7lS1W&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)

```diff
@@            Coverage Diff             @@
##           master     #290      +/-   ##
==========================================
+ Coverage   70.07%   70.10%   +0.02%     
==========================================
  Files          27       27              
  Lines        3041     3044       +3     
==========================================
+ Hits         2131     2134       +3     
  Misses        683      683              
  Partials      227      227              
```


| [Impacted Files](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [pojo.go](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cG9qby5nbw==) | `64.62% <93.33%> (+0.50%)` | :arrow_up: |
| [list.go](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bGlzdC5nbw==) | `84.61% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [9653a02...cd6e712](https://codecov.io/gh/apache/dubbo-go-hessian2/pull/290?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).


Patch:
diff --git a/Makefile b/Makefile
index 0123897..c81a38a 100644
--- a/Makefile
+++ b/Makefile
@@ -20,9 +20,9 @@ lint2:
 	golangci-lint run
 
 format:
-	go fmt
+	gofmt -l -w .
 
 test:
-	go test
+	go test ./... -v
 
 all: format test
\ No newline at end of file
diff --git a/list.go b/list.go
index 94603c8..0eeef74 100644
--- a/list.go
+++ b/list.go
@@ -70,9 +70,9 @@ func init() {
 	listTypeNameMapper.Store("float64", "[double")
 	listTypeNameMapper.Store("bool", "[boolean")
 	listTypeNameMapper.Store("time.Time", "[date")
-	listTypeNameMapper.Store("java_exception.Throwabler", "[java.lang.Throwable")
+	listTypeNameMapper.Store("github.com/apache/dubbo-go-hessian2/java_exception/java_exception.Throwabler", "[java.lang.Throwable")
 
-	listTypeNameMapper.Store("hessian.Object", "[object")
+	listTypeNameMapper.Store("github.com/apache/dubbo-go-hessian2/hessian.Object", "[object")
 }
 
 func registerTypeName(gotype, javatype string) {
@@ -161,7 +161,7 @@ func (e *Encoder) writeTypedList(v interface{}) error {
 
 	value = UnpackPtrValue(value)
 	goType := UnpackPtrType(value.Type().Elem())
-	totype := combineGoName(goType)
+	totype := combineGoTypeName(goType)
 	typeName := getListTypeName(totype)
 	if typeName == "" {
 		return perrors.New("no this type name: " + totype)
diff --git a/pojo.go b/pojo.go
index 1257e9d..273ef7c 100644
--- a/pojo.go
+++ b/pojo.go
@@ -20,7 +20,6 @@ package hessian
 import (
 	"fmt"
 	"reflect"
-	"regexp"
 	"strings"
 	"sync"
 	"unicode"
@@ -102,9 +101,6 @@ var (
 	}
 	pojoType     = reflect.TypeOf((*POJO)(nil)).Elem()
 	javaEnumType = reflect.TypeOf((*POJOEnum)(nil)).Elem()
-
-	goPkgPathWhiteListRegexp = regexp.MustCompile(`^(github\.com/apache/dubbo-go-hessian2|time)`)
-	goPkgPathBlackListRegexp = regexp.MustCompile(`^(github\.com/apache/dubbo-go-hessian2/hessian_test)`)
 )
 
 // struct parsing
@@ -148,7 +144,7 @@ func RegisterPOJOMapping(javaClassName string, o interface{}) int {
 	)
 
 	sttInfo.typ = obtainValueType(o)
-	sttInfo.goName = getGoName(o)
+	sttInfo.goName = GetGoType(o)
 	sttInfo.javaName = javaClassName
 	sttInfo.inst = o
 	pojoRegistry.j2g[sttInfo.javaName] = sttInfo.goName
@@ -229,7 +225,7 @@ func unRegisterPOJO(o POJO) int {
 	pojoRegistry.Lock()
 	defer pojoRegistry.Unlock()
 
-	goName := getGoName(o)
+	goName := GetGoType(o)
 
 	if structInfo, ok := pojoRegistry.registry[goName]; ok {
 		delete(pojoRegistry.j2g, structInfo.javaName)
@@ -244,20 +240,28 @@ func unRegisterPOJO(o POJO) int {
 	return -1
 }
 
-func getGoName(o interface{}) string {
-	goType := reflect.TypeOf(o)
-	for reflect.Ptr == goType.Kind() {
-		goType = goType.Elem()
-	}
-	return combineGoName(goType)
+// GetGoType get the raw go type name with package.
+func GetGoType(o interface{}) string {
+	return combineGoTypeName(reflect.TypeOf(o))
 }
 
-func combineGoName(t reflect.Type) string {
+func combineGoTypeName(t reflect.Type) string {
+	for reflect.Ptr == t.Kind() {
+		t = t.Elem()
+	}
+
+	if reflect.Slice == t.Kind() {
+		goName := t.String()
+		sliceArrayPrefixIndex := strings.LastIndex(goName, "]")
+		for reflect.Slice == t.Kind() {
+			t = t.Elem()
+		}
+		return goName[:sliceArrayPrefixIndex+1] + combineGoTypeName(t)
+	}
+
 	pkgPath := t.PkgPath()
 	goName := t.String()
-	if pkgPath == "" ||
-		(goPkgPathWhiteListRegexp.Match([]byte(pkgPath)) &&
-			!goPkgPathBlackListRegexp.Match([]byte(pkgPath))) {
+	if pkgPath == "" || strings.HasPrefix(goName, pkgPath) {
 		return goName
 	}
 	return pkgPath + "/" + goName
@@ -312,7 +316,7 @@ func RegisterJavaEnum(o POJOEnum) int {
 		default:
 			t.typ = reflect.TypeOf(o)
 		}
-		t.goName = getGoName(o)
+		t.goName = GetGoType(o)
 		t.javaName = o.JavaClassName()
 		t.inst = o
 		pojoRegistry.j2g[t.javaName] = t.goName
@@ -355,7 +359,7 @@ func loadPOJORegistry(v interface{}) (*structInfo, bool) {
 		ok bool
 		s  *structInfo
 	)
-	goName := getGoName(v)
+	goName := GetGoType(v)
 	pojoRegistry.RLock()
 	s, ok = pojoRegistry.registry[goName]
 	pojoRegistry.RUnlock()


Test patch:
diff --git a/hessian_test/dup_struct_name_test.go b/hessian_test/dup_struct_name_test.go
index 5e8f149..0c34d44 100644
--- a/hessian_test/dup_struct_name_test.go
+++ b/hessian_test/dup_struct_name_test.go
@@ -181,3 +181,8 @@ func checkResponseBody(t *testing.T, decodedResponse *hessian.Response, h *hessi
 	out, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(decodedResponse.RspObj)), nil)
 	assert.Equal(t, in, out)
 }
+
+func TestDuplicatedClassGetGoType(t *testing.T) {
+	assert.Equal(t, "github.com/apache/dubbo-go-hessian2/hessian_test_test/hessian_test.CaseZ", hessian.GetGoType(&CaseZ{}))
+	assert.Equal(t, "github.com/apache/dubbo-go-hessian2/hessian_test/hessian_test/hessian_test.CaseZ", hessian.GetGoType(&dupclass.CaseZ{}))
+}
diff --git a/java_unknown_exception_test.go b/java_unknown_exception_test.go
index 8f2743e..264beaf 100644
--- a/java_unknown_exception_test.go
+++ b/java_unknown_exception_test.go
@@ -34,7 +34,7 @@ func TestCheckAndGetException(t *testing.T) {
 	assert.True(t, b)
 
 	assert.Equal(t, s.javaName, "com.test.UserDefinedException")
-	assert.Equal(t, s.goName, "hessian.UnknownException")
+	assert.Equal(t, s.goName, "github.com/apache/dubbo-go-hessian2/hessian.UnknownException")
 
 	clazzInfo2 := &classInfo{
 		javaName:      "com.test.UserDefinedException",
diff --git a/pojo_test.go b/pojo_test.go
new file mode 100644
index 0000000..cf839b1
--- /dev/null
+++ b/pojo_test.go
@@ -0,0 +1,46 @@
+/*
+ * 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 hessian
+
+import (
+	"bytes"
+	"container/list"
+	"testing"
+	"time"
+)
+
+import (
+	"github.com/stretchr/testify/assert"
+)
+
+import (
+	"github.com/apache/dubbo-go-hessian2/java_exception"
+	"github.com/apache/dubbo-go-hessian2/java_util"
+)
+
+func TestGetGoType(t *testing.T) {
+	assert.Equal(t, "time.Time", GetGoType(time.Now()))
+	assert.Equal(t, "bytes.Buffer", GetGoType(bytes.Buffer{}))
+	assert.Equal(t, "container/list/list.List", GetGoType(list.New()))
+	assert.Equal(t, "github.com/apache/dubbo-go-hessian2/hessian.BusinessData", GetGoType(&BusinessData{}))
+	assert.Equal(t, "github.com/apache/dubbo-go-hessian2/java_util/java_util.UUID", GetGoType(&java_util.UUID{}))
+	assert.Equal(t, "github.com/apache/dubbo-go-hessian2/java_exception/java_exception.ClassNotFoundException", GetGoType(&java_exception.ClassNotFoundException{}))
+
+	assert.Equal(t, "[]github.com/apache/dubbo-go-hessian2/hessian.BusinessData", GetGoType([]*BusinessData{}))
+	assert.Equal(t, "[][]github.com/apache/dubbo-go-hessian2/hessian.BusinessData", GetGoType([][]*BusinessData{}))
+}