From 53266f029fbb23a31220663ac094869ed4701a0f Mon Sep 17 00:00:00 2001 From: Diego Becciolini Date: Mon, 25 Apr 2022 12:53:15 +0100 Subject: [PATCH] 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 --- hstore.go | 3 ++- hstore_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/hstore.go b/hstore.go index 706a3964..d21af7bc 100644 --- a/hstore.go +++ b/hstore.go @@ -90,7 +90,8 @@ func (src *Hstore) AssignTo(dst interface{}) error { case Null: (*v)[k] = nil case Present: - (*v)[k] = &val.String + str := val.String + (*v)[k] = &str default: return fmt.Errorf("cannot decode %#v into %T", src, dst) } diff --git a/hstore_test.go b/hstore_test.go index 73ee0612..32a8f015 100644 --- a/hstore_test.go +++ b/hstore_test.go @@ -181,13 +181,14 @@ func TestHstoreAssignTo(t *testing.T) { func TestHstoreAssignToNullable(t *testing.T) { var m map[string]*string + strPtr := func(str string) *string { return &str } simpleTests := []struct { src pgtype.Hstore dst *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))}, }