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 null encoding for nil pointer #368

Merged
merged 2 commits into from
Nov 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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