Skip to content

Commit

Permalink
fix bug for encTypeInt32 (#148)
Browse files Browse the repository at this point in the history
* fix bug for encTypeInt32

* modify go.mod

* add assert

* compare the response
  • Loading branch information
fangyincheng authored and wongoo committed Jan 11, 2020
1 parent d583a57 commit c039b90
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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

0 comments on commit c039b90

Please sign in to comment.