From 4e23133a28eba3bc73591bb7b6db548adfbf6e80 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Wed, 18 Jan 2017 20:13:14 -0800 Subject: [PATCH] Add json to the default set of mapped types and make the map public --- .travis.yml | 4 ++++ build.sbt | 3 ++- .../twitter/finagle/postgres/PostgresClient.scala | 3 ++- .../twitter/finagle/postgres/values/Types.scala | 1 + .../finagle/postgres/values/ValuesSpec.scala | 15 ++++++++++++++- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5f617902..114b8cee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: trusty language: scala scala: @@ -7,6 +8,9 @@ scala: jdk: - oraclejdk8 +addons: + postgresql: "9.5" + services: - postgresql diff --git a/build.sbt b/build.sbt index 9ec1d82b..3870b8e9 100644 --- a/build.sbt +++ b/build.sbt @@ -14,7 +14,8 @@ val baseSettings = Seq( "com.twitter" %% "finagle-netty3" % "18.2.0", "org.scalatest" %% "scalatest" % "3.0.4" % "test,it", "org.scalacheck" %% "scalacheck" % "1.13.5" % "test,it", - "org.scalamock" %% "scalamock-scalatest-support" % "3.4.2" % "test,it" + "org.scalamock" %% "scalamock-scalatest-support" % "3.4.2" % "test,it", + "io.circe" %% "circe-testing" % "0.8.0" % "test,it" ) ) diff --git a/src/main/scala/com/twitter/finagle/postgres/PostgresClient.scala b/src/main/scala/com/twitter/finagle/postgres/PostgresClient.scala index 6e84ba2f..c9a9f2be 100644 --- a/src/main/scala/com/twitter/finagle/postgres/PostgresClient.scala +++ b/src/main/scala/com/twitter/finagle/postgres/PostgresClient.scala @@ -81,7 +81,7 @@ object PostgresClient { case class TypeSpecifier(receiveFunction: String, typeName: String, elemOid: Long = 0) - private[finagle] val defaultTypes = Map( + val defaultTypes = Map( Types.BOOL -> TypeSpecifier("boolrecv", "bool"), Types.BYTE_A -> TypeSpecifier("bytearecv", "bytea"), Types.CHAR -> TypeSpecifier("charrecv", "char"), @@ -95,6 +95,7 @@ object PostgresClient { Types.TID -> TypeSpecifier("tidrecv", "tid"), Types.XID -> TypeSpecifier("xidrecv", "xid"), Types.CID -> TypeSpecifier("cidrecv", "cid"), + Types.JSON -> TypeSpecifier("json_recv", "json"), Types.XML -> TypeSpecifier("xml_recv", "xml"), Types.POINT -> TypeSpecifier("point_recv", "point"), Types.L_SEG -> TypeSpecifier("lseg_recv", "lseg"), diff --git a/src/main/scala/com/twitter/finagle/postgres/values/Types.scala b/src/main/scala/com/twitter/finagle/postgres/values/Types.scala index c97a16ab..f6d08da1 100644 --- a/src/main/scala/com/twitter/finagle/postgres/values/Types.scala +++ b/src/main/scala/com/twitter/finagle/postgres/values/Types.scala @@ -17,6 +17,7 @@ object Types { val TID = 27 val XID = 28 val CID = 29 + val JSON = 114 val XML = 142 val POINT = 600 val L_SEG = 601 diff --git a/src/test/scala/com/twitter/finagle/postgres/values/ValuesSpec.scala b/src/test/scala/com/twitter/finagle/postgres/values/ValuesSpec.scala index b72ce2c8..7e5f0df2 100644 --- a/src/test/scala/com/twitter/finagle/postgres/values/ValuesSpec.scala +++ b/src/test/scala/com/twitter/finagle/postgres/values/ValuesSpec.scala @@ -15,8 +15,10 @@ import com.twitter.finagle.postgres.messages.DataRow import com.twitter.finagle.postgres.messages.Field import com.twitter.util.Await import org.jboss.netty.buffer.ChannelBuffers -import org.scalacheck.Arbitrary +import org.scalacheck.{Arbitrary, Gen} +import Arbitrary.arbitrary import org.scalatest.prop.GeneratorDrivenPropertyChecks +import io.circe.testing.instances.arbitraryJson class ValuesSpec extends Spec with GeneratorDrivenPropertyChecks { @@ -156,6 +158,17 @@ class ValuesSpec extends Spec with GeneratorDrivenPropertyChecks { val decoded = ValueDecoder[JSONB].decodeBinary("", createBuffer(), Charset.defaultCharset()).get() JSONB.stringify(decoded) must equal(json) } + + "parse json" in test(ValueDecoder.string, ValueEncoder.string)("json_send", "json")( + Arbitrary( + Gen.oneOf( + arbitraryJson.arbitrary.map(_.noSpaces), + arbitraryJson.arbitrary.map(_.spaces4), + arbitraryJson.arbitrary.map(_.spaces2) + ).map(_.replace("\u0000", "\\u0000")) + ), + client + ) } } }