diff --git a/pgtype/json.go b/pgtype/json.go index b05aba6b..377a1546 100644 --- a/pgtype/json.go +++ b/pgtype/json.go @@ -72,14 +72,20 @@ func (dst *JSON) Get() interface{} { func (src *JSON) AssignTo(dst interface{}) error { switch v := dst.(type) { case *string: - if src.Status != Present { - v = nil - } else { + if src.Status == Present { *v = string(src.Bytes) + } else { + return errors.Errorf("cannot assign non-present status to %T", dst) } case **string: - *v = new(string) - return src.AssignTo(*v) + if src.Status == Present { + s := string(src.Bytes) + *v = &s + return nil + } else { + *v = nil + return nil + } case *[]byte: if src.Status != Present { *v = nil diff --git a/pgtype/json_test.go b/pgtype/json_test.go index 82c02539..38494841 100644 --- a/pgtype/json_test.go +++ b/pgtype/json_test.go @@ -129,7 +129,7 @@ func TestJSONAssignTo(t *testing.T) { t.Errorf("%d: %v", i, err) } - if *tt.dst == tt.expected { + if *tt.dst != tt.expected { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) } } diff --git a/pgtype/jsonb_test.go b/pgtype/jsonb_test.go index 1a9a3056..ab743151 100644 --- a/pgtype/jsonb_test.go +++ b/pgtype/jsonb_test.go @@ -135,7 +135,7 @@ func TestJSONBAssignTo(t *testing.T) { t.Errorf("%d: %v", i, err) } - if *tt.dst == tt.expected { + if *tt.dst != tt.expected { t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst) } }