Skip to content

Commit

Permalink
fix go type name for list (#290)
Browse files Browse the repository at this point in the history
* fix go name of POJO for list

* split import

* rename var

* re-order imports

* rename
  • Loading branch information
wongoo authored Nov 18, 2021
1 parent 9653a02 commit 5d3d18b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ lint2:
golangci-lint run

format:
go fmt
gofmt -l -w .

test:
go test
go test ./... -v

all: format test
5 changes: 5 additions & 0 deletions hessian_test/dup_struct_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
}
2 changes: 1 addition & 1 deletion java_unknown_exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 22 additions & 18 deletions pojo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package hessian
import (
"fmt"
"reflect"
"regexp"
"strings"
"sync"
"unicode"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
46 changes: 46 additions & 0 deletions pojo_test.go
Original file line number Diff line number Diff line change
@@ -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{}))
}

0 comments on commit 5d3d18b

Please sign in to comment.