Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug for encTypeInt32 #148

Merged
merged 4 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func (e *Encoder) Encode(v interface{}) error {
case map[interface{}]interface{}:
return e.encUntypedMap(val)

case POJOEnum:
if p, ok := v.(POJOEnum); ok {
return e.encObject(p)
}

default:
t := UnpackPtrType(reflect.TypeOf(v))
switch t.Kind() {
Expand Down Expand Up @@ -168,9 +173,6 @@ func (e *Encoder) Encode(v interface{}) error {
return err
}
default:
if p, ok := v.(POJOEnum); ok { // JavaEnum
return e.encObject(p)
}
return perrors.Errorf("type not supported! %s", t.Kind().String())
}
}
Expand Down
2 changes: 1 addition & 1 deletion int.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (d *Decoder) decInt32(flag int32) (int32, error) {

func (d *Encoder) encTypeInt32(b []byte, p interface{}) ([]byte, error) {
value := reflect.ValueOf(p)
if value.IsNil() {
if PackPtr(value).IsNil() {
return encNull(b), nil
}
value = UnpackPtrValue(value)
Expand Down
28 changes: 28 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package hessian

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

Expand All @@ -39,6 +40,7 @@ func TestEncInt32Len1B(t *testing.T) {
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Equal(t, v, res)
t.Logf("decode(%v) = %v, %v\n", v, res, err)
}

Expand All @@ -60,9 +62,34 @@ func TestEncInt32Len2B(t *testing.T) {
t.Logf("%#v\n", e.buffer)
d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Nil(t, err)
assert.Equal(t, v, res)
t.Logf("decode(%#x) = %#x, %v\n", v, res, err)
}

func TestEncInt32ForAlias(t *testing.T) {
var (
v JavaEnum
err error
e *Encoder
d *Decoder
res interface{}
)

v = 0xe6
// var v int32 = 0xf016
e = NewEncoder()
e.Encode(v)
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Nil(t, err)
assert.Equal(t, int32(v), res)
t.Logf("decode(%v) = %v, %v\n", v, res, err)
}

func TestEncInt32Len4B(t *testing.T) {
var (
v int32
Expand All @@ -81,6 +108,7 @@ func TestEncInt32Len4B(t *testing.T) {

d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Nil(t, err)
t.Logf("decode(%v) = %v, %v\n", v, res, err)
}

Expand Down