From 4987d5425e3e3cffd3f4c7b0555c80fbd7e55ee6 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 21 Sep 2015 13:40:47 -0500 Subject: [PATCH] Fix JSON encoding of *string --- conn.go | 2 ++ values_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/conn.go b/conn.go index 2cfc87cc..b204697b 100644 --- a/conn.go +++ b/conn.go @@ -798,6 +798,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{} continue } + encode: switch arg := arguments[i].(type) { case Encoder: err = arg.Encode(wbuf, oid) @@ -810,6 +811,7 @@ func (c *Conn) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{} continue } else { arguments[i] = v.Elem().Interface() + goto encode } } switch oid { diff --git a/values_test.go b/values_test.go index 490a358e..e7d52e6d 100644 --- a/values_test.go +++ b/values_test.go @@ -89,6 +89,8 @@ func TestJsonAndJsonbTranscode(t *testing.T) { } typename := conn.PgTypes[oid].Name + testJsonString(t, conn, typename) + testJsonStringPointer(t, conn, typename) testJsonSingleLevelStringMap(t, conn, typename) testJsonNestedMap(t, conn, typename) testJsonStringArray(t, conn, typename) @@ -98,6 +100,38 @@ func TestJsonAndJsonbTranscode(t *testing.T) { } } +func testJsonString(t *testing.T, conn *pgx.Conn, typename string) { + input := `{"key": "value"}` + expectedOutput := map[string]string{"key": "value"} + var output map[string]string + err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + if err != nil { + t.Errorf("%s: QueryRow Scan failed: %v", typename, err) + return + } + + if !reflect.DeepEqual(expectedOutput, output) { + t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, expectedOutput, output) + return + } +} + +func testJsonStringPointer(t *testing.T, conn *pgx.Conn, typename string) { + input := `{"key": "value"}` + expectedOutput := map[string]string{"key": "value"} + var output map[string]string + err := conn.QueryRow("select $1::"+typename, &input).Scan(&output) + if err != nil { + t.Errorf("%s: QueryRow Scan failed: %v", typename, err) + return + } + + if !reflect.DeepEqual(expectedOutput, output) { + t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, expectedOutput, output) + return + } +} + func testJsonSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) { input := map[string]string{"key": "value"} var output map[string]string