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 1 commit
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: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require (
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.4.0
)

go 1.13
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
22 changes: 22 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ func TestEncInt32Len2B(t *testing.T) {
t.Logf("decode(%#x) = %#x, %v\n", v, res, err)
}

func TestEncInt32ForAlias(t *testing.T) {
type JavaEnum int32
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()
t.Logf("decode(%v) = %v, %v\n", v, res, err)
}

func TestEncInt32Len4B(t *testing.T) {
var (
v int32
Expand Down