2
0

Hstore: fix AssignTo

Hstore.AssignTo a map of string pointers takes the address of the loop variable, thus setting all the entries to the same string pointer.

extend TestHstoreAssignToNullable

assert fix
This commit is contained in:
Diego Becciolini
2022-04-25 12:53:15 +01:00
committed by Jack Christensen
parent c5a0faca99
commit 53266f029f
2 changed files with 4 additions and 2 deletions
+2 -1
View File
@@ -90,7 +90,8 @@ func (src *Hstore) AssignTo(dst interface{}) error {
case Null: case Null:
(*v)[k] = nil (*v)[k] = nil
case Present: case Present:
(*v)[k] = &val.String str := val.String
(*v)[k] = &str
default: default:
return fmt.Errorf("cannot decode %#v into %T", src, dst) return fmt.Errorf("cannot decode %#v into %T", src, dst)
} }
+2 -1
View File
@@ -181,13 +181,14 @@ func TestHstoreAssignTo(t *testing.T) {
func TestHstoreAssignToNullable(t *testing.T) { func TestHstoreAssignToNullable(t *testing.T) {
var m map[string]*string var m map[string]*string
strPtr := func(str string) *string { return &str }
simpleTests := []struct { simpleTests := []struct {
src pgtype.Hstore src pgtype.Hstore
dst *map[string]*string dst *map[string]*string
expected map[string]*string expected map[string]*string
}{ }{
{src: pgtype.Hstore{Map: map[string]pgtype.Text{"foo": {Status: pgtype.Null}}, Status: pgtype.Present}, dst: &m, expected: map[string]*string{"foo": nil}}, {src: pgtype.Hstore{Map: map[string]pgtype.Text{"foo": {Status: pgtype.Null}, "bar": {String: "1", Status: pgtype.Present}, "baz": {String: "2", Status: pgtype.Present}}, Status: pgtype.Present}, dst: &m, expected: map[string]*string{"foo": nil, "bar": strPtr("1"), "baz": strPtr("2")}},
{src: pgtype.Hstore{Status: pgtype.Null}, dst: &m, expected: ((map[string]*string)(nil))}, {src: pgtype.Hstore{Status: pgtype.Null}, dst: &m, expected: ((map[string]*string)(nil))},
} }