Skip to content

Commit

Permalink
fix: Fixed NULL not being formatted for slices/maps.
Browse files Browse the repository at this point in the history
When a query parameter was a nil slice or a nil map, the value `NULL`
would not be appended to the query. Instead `'null'` would be appended
to the query which is not correct.

Resolves go-pg#1908
  • Loading branch information
elliotcourant committed Nov 20, 2021
1 parent 4d138f1 commit be3aae4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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())
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))
})
}

0 comments on commit be3aae4

Please sign in to comment.