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: Fixed NULL not being formatted for slices/maps. #1930

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions types/append_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ func ptrAppenderFunc(typ reflect.Type) AppenderFunc {
}

func appendValue(b []byte, v reflect.Value, flags int) []byte {
if v.Kind() == reflect.Ptr && v.IsNil() {
return AppendNull(b, flags)
switch v.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map:
// When the value is a pointer, slice or map; then we can assert if the value provided is nil.
// When the value is properly nil; then we can append NULL instead of a string representation of the provided
// value.
if v.IsNil() {
return AppendNull(b, flags)
}
}
appender := Appender(v.Type())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that you should be able to call Appender once for a type and then use it to append different values of the given type (nil or non-nil). So the function returned by appender should handle nil values, not appendValue itself.

I guess it is not worth fixing this properly so let's keep this code, but at least we should have an end-to-end test that inserts and selects slice/map from a real table...

return appender(b, v, flags)
Expand Down
27 changes: 27 additions & 0 deletions types/append_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAppend(t *testing.T) {
t.Run("null pointer", func(t *testing.T) {
var a *struct{}
result := Append(nil, a, 1)
assert.Equal(t, "NULL", string(result))
})

t.Run("null map", func(t *testing.T) {
var a map[string]int
result := Append(nil, a, 1)
assert.Equal(t, "NULL", string(result))
})

t.Run("null string array", func(t *testing.T) {
var a []string
result := Append(nil, a, 1)
assert.Equal(t, "NULL", string(result))
})
}