Skip to content

Commit

Permalink
Merge pull request #368 from ahaostudy/master
Browse files Browse the repository at this point in the history
support java null encoding
  • Loading branch information
AlexStocks committed Nov 27, 2023
2 parents 6c40744 + ebf53db commit 333dcbc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
9 changes: 7 additions & 2 deletions encode.go
Expand Up @@ -201,7 +201,7 @@ func (e *Encoder) Encode(v interface{}) error {
if vv != nil {
e.buffer = encBool(e.buffer, *vv)
} else {
e.buffer = encBool(e.buffer, false)
e.buffer = EncNull(e.buffer)
}
case reflect.Int32:
if t == _typeOfRune {
Expand All @@ -219,7 +219,12 @@ func (e *Encoder) Encode(v interface{}) error {
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() {
if reflect.Ptr == vVal.Kind() {
if vVal.IsNil() {
e.buffer = EncNull(e.buffer)
return nil
}

return e.Encode(vVal.Elem().Interface())
}
default:
Expand Down
2 changes: 1 addition & 1 deletion int.go
Expand Up @@ -112,7 +112,7 @@ func (d *Decoder) decInt32(flag int32) (int32, error) {

func (e *Encoder) encTypeInt32(b []byte, p interface{}) ([]byte, error) {
value := reflect.ValueOf(p)
if PackPtr(value).IsNil() {
if value.Kind() == reflect.Ptr && value.IsNil() || PackPtr(value).IsNil() {
return EncNull(b), nil
}
value = UnpackPtrValue(value)
Expand Down
74 changes: 73 additions & 1 deletion null_test.go
Expand Up @@ -34,6 +34,78 @@ func TestNull(t *testing.T) {
testDecodeFramework(t, "replyNull", nil)
}

func TestNulEncode(t *testing.T) {
func TestNullEncode(t *testing.T) {
testJavaDecode(t, "argNull", nil)
}

func TestNullIntPtr(t *testing.T) {
e := NewEncoder()
var null *int = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullBoolPtr(t *testing.T) {
e := NewEncoder()
var null *bool = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullInt32Ptr(t *testing.T) {
e := NewEncoder()
var null *int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullSlice(t *testing.T) {
e := NewEncoder()
var null []int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullMap(t *testing.T) {
e := NewEncoder()
var null map[bool]int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

type NullFieldStruct struct {
Int *int
Bool *bool
Int32 *int32
Slice []int32
Map map[bool]int32
}

func (*NullFieldStruct) JavaClassName() string {
return "NullFieldStruct"
}

func TestNullFieldStruct(t *testing.T) {
e := NewEncoder()
req := &NullFieldStruct{}
e.Encode(req)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("NNNNN"), e.buffer[len(e.buffer)-5:], t)
}
6 changes: 1 addition & 5 deletions object_test.go
Expand Up @@ -681,11 +681,7 @@ func TestBasePointer(t *testing.T) {
base = BasePointer{
A: nil,
}
expectedF := false
expectedBase := BasePointer{
A: &expectedF,
}
doTestBasePointer(t, &base, &expectedBase)
doTestBasePointer(t, &base, &base)
}

func doTestBasePointer(t *testing.T, base *BasePointer, expected *BasePointer) {
Expand Down

0 comments on commit 333dcbc

Please sign in to comment.