Skip to content
Permalink
Browse files
Merge pull request #283 from chans-open-source/feature/fix_encode_type
fix issue #282
  • Loading branch information
zouyx committed Oct 13, 2021
2 parents 1ec5c50 + 43d1925 commit ea64fc75b879f4b588a196040aac3976422bba91
Showing 5 changed files with 61 additions and 0 deletions.
@@ -100,3 +100,12 @@ func TestDoubleEncode(t *testing.T) {
testJavaDecode(t, "argDouble_m129_0", -129.0)
testJavaDecode(t, "argDouble_m32768_0", -32768.0)
}

func TestDoublePrtEncode(t *testing.T) {
var (
f0 float32 = 3.14
f1 float64 = 1024.1024102410241024
)
testSimpleEncode(t, &f0)
testSimpleEncode(t, &f1)
}
@@ -21,7 +21,9 @@ import (
"reflect"
"time"
"unsafe"
)

import (
perrors "github.com/pkg/errors"
)

@@ -193,6 +195,14 @@ func (e *Encoder) Encode(v interface{}) error {
if err != nil {
return err
}
case reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64: // resolve base type
vVal := reflect.ValueOf(v)
if reflect.Ptr == vVal.Kind() && !vVal.IsNil() {
return e.Encode(vVal.Elem().Interface())
}
default:
return perrors.Errorf("type not supported! %s", t.Kind().String())
}
@@ -23,6 +23,10 @@ import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

var assertEqual = func(want, got []byte, t *testing.T) {
if !bytes.Equal(want, got) {
t.Fatalf("want %v , got %v", want, got)
@@ -77,3 +81,9 @@ func testJavaDecode(t *testing.T, method string, target interface{}) {
t.Errorf("%s: encode %v to bytes wrongly, result: %s", method, target, result)
}
}

func testSimpleEncode(t *testing.T, v interface{}) {
e := NewEncoder()
err := e.Encode(v)
assert.Nil(t, err)
}
@@ -153,6 +153,33 @@ func TestIntEncode(t *testing.T) {
testJavaDecode(t, "argInt_m17", int32(-17))
}

func TestIntPrtEncode(t *testing.T) {
var (
i0 int = 0
i1 int8 = 1
i2 int16 = 2
i3 int32 = 3
i4 int64 = 4
ui0 uint = 10
ui1 uint8 = 11
ui2 uint16 = 12
ui3 uint32 = 13
ui4 uint64 = 14
e0 JavaEnum = 0xe6
)
testSimpleEncode(t, &i0)
testSimpleEncode(t, &i1)
testSimpleEncode(t, &i2)
testSimpleEncode(t, &i3)
testSimpleEncode(t, &i4)
testSimpleEncode(t, &ui0)
testSimpleEncode(t, &ui1)
testSimpleEncode(t, &ui2)
testSimpleEncode(t, &ui3)
testSimpleEncode(t, &ui4)
testSimpleEncode(t, &e0)
}

func TestReflectIntEncode(t *testing.T) {
a1 := int32(0)
a2 := int32(0x30)
@@ -175,6 +175,11 @@ func TestStringEncode(t *testing.T) {
testJavaDecode(t, "argString_65536", s65560[:65536])
}

func TestStringPrtEncode(t *testing.T) {
str := "dubbo-go-hessian2"
testSimpleEncode(t, &str)
}

var decodePool = &sync.Pool{
New: func() interface{} {
return NewCheapDecoderWithSkip([]byte{})

0 comments on commit ea64fc7

Please sign in to comment.