This is an automated email from the ASF dual-hosted git repository.

wongoo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f1ca08  Ftr: support encoding *int (#142)
3f1ca08 is described below

commit 3f1ca08e3293ea4d011eaa1966c908d786fb59b7
Author: Patrick Jiang <patrickjiang0...@gmail.com>
AuthorDate: Thu Dec 12 11:30:57 2019 +0800

    Ftr: support encoding *int (#142)
    
    * *int to java int
    
    * fix bug if v is nil and add double pointer checking
---
 encode.go   |  6 ++++++
 int.go      | 13 +++++++++++++
 int_test.go | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/encode.go b/encode.go
index 9525bd1..4e4fc8e 100644
--- a/encode.go
+++ b/encode.go
@@ -161,6 +161,12 @@ func (e *Encoder) Encode(v interface{}) error {
                        } else {
                                e.buffer = encBool(e.buffer, false)
                        }
+               case reflect.Int32:
+                       var err error
+                       e.buffer, err = e.encTypeInt32(e.buffer, v)
+                       if err != nil {
+                               return err
+                       }
                default:
                        if p, ok := v.(POJOEnum); ok { // JavaEnum
                                return e.encObject(p)
diff --git a/int.go b/int.go
index 9d21568..b2b24a6 100644
--- a/int.go
+++ b/int.go
@@ -20,6 +20,7 @@ package hessian
 import (
        "encoding/binary"
        "io"
+       "reflect"
 )
 
 import (
@@ -105,3 +106,15 @@ func (d *Decoder) decInt32(flag int32) (int32, error) {
                return 0, perrors.Errorf("decInt32 integer wrong tag:%#x", tag)
        }
 }
+
+func (d *Encoder) encTypeInt32(b []byte, p interface{}) ([]byte, error) {
+       value := reflect.ValueOf(p)
+       if value.IsNil() {
+               return encNull(b), nil
+       }
+       value = UnpackPtrValue(value)
+       if value.Kind() != reflect.Int32 {
+               return nil, perrors.Errorf("encode reflect Int32 integer wrong, 
it's not int32 pointer")
+       }
+       return encInt32(b, int32(value.Int())), nil
+}
diff --git a/int_test.go b/int_test.go
index 22e2f2e..56c903e 100644
--- a/int_test.go
+++ b/int_test.go
@@ -121,3 +121,38 @@ func TestIntEncode(t *testing.T) {
        testJavaDecode(t, "argInt_m16", int32(-16))
        testJavaDecode(t, "argInt_m17", int32(-17))
 }
+
+func TestReflectIntEncode(t *testing.T) {
+       a1 := int32(0)
+       a2 := int32(0x30)
+       a3 := int32(0x3ffff)
+       a4 := int32(0x40000)
+       a5 := int32(0x7ff)
+       a6 := int32(0x7fffffff)
+       a7 := int32(0x800)
+       a8 := int32(1)
+       a9 := int32(47)
+       a10 := int32(-0x40000)
+       a11 := int32(-0x40001)
+       a12 := int32(-0x800)
+       a13 := int32(-0x80000000)
+       a14 := int32(-0x801)
+       a15 := int32(-16)
+       a16 := int32(-17)
+       testJavaDecode(t, "argInt_0", &a1)
+       testJavaDecode(t, "argInt_0x30", &a2)
+       testJavaDecode(t, "argInt_0x3ffff", &a3)
+       testJavaDecode(t, "argInt_0x40000", &a4)
+       testJavaDecode(t, "argInt_0x7ff", &a5)
+       testJavaDecode(t, "argInt_0x7fffffff", &a6)
+       testJavaDecode(t, "argInt_0x800", &a7)
+       testJavaDecode(t, "argInt_1", &a8)
+       testJavaDecode(t, "argInt_47", &a9)
+       testJavaDecode(t, "argInt_m0x40000", &a10)
+       testJavaDecode(t, "argInt_m0x40001", &a11)
+       testJavaDecode(t, "argInt_m0x800", &a12)
+       testJavaDecode(t, "argInt_m0x80000000", &a13)
+       testJavaDecode(t, "argInt_m0x801", &a14)
+       testJavaDecode(t, "argInt_m16", &a15)
+       testJavaDecode(t, "argInt_m17", &a16)
+}

Reply via email to