diff --git a/src/Rel8/Type.hs b/src/Rel8/Type.hs index f49835ba..0e934322 100644 --- a/src/Rel8/Type.hs +++ b/src/Rel8/Type.hs @@ -16,7 +16,7 @@ module Rel8.Type where -- aeson -import Data.Aeson ( Value ) +import Data.Aeson ( Value, Object ) import qualified Data.Aeson as Aeson import qualified Data.Aeson.Text as Aeson @@ -63,7 +63,7 @@ import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation ) import Rel8.Type.Decimal (PowerOf10, resolution) import Rel8.Type.Decoder (Decoder (..)) import Rel8.Type.Encoder (Encoder (..)) -import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation ) +import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation, parseTypeInformation ) import Rel8.Type.Name (TypeName (..)) import Rel8.Type.Parser (parse) import qualified Rel8.Type.Builder.ByteString as Builder @@ -521,6 +521,16 @@ instance DBType Value where , typeName = "jsonb" } +-- | Corresponds to @jsonb@ +instance DBType Object where + typeInformation = parseTypeInformation + (aesonResultToEither . Aeson.fromJSON) + Aeson.Object + typeInformation + where + aesonResultToEither = \case + Aeson.Success o -> Right o + Aeson.Error e -> Left e -- | Corresponds to @inet@ instance DBType IPRange where diff --git a/tests/Main.hs b/tests/Main.hs index 8ba5cd58..f26a580a 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -585,6 +585,7 @@ testDBType getTestDatabase = testGroup "DBType instances" , dbTypeTest "Value" genValue , dbTypeTest "JSONEncoded" genJSONEncoded , dbTypeTest "JSONBEncoded" genJSONBEncoded + , dbTypeTest "Object" genObject ] where @@ -733,13 +734,16 @@ testDBType getTestDatabase = testGroup "DBType instances" , Aeson.Number <$> genScientific , Aeson.String <$> genText ] - [ Aeson.Object . Aeson.KeyMap.fromMap <$> Gen.map (Range.linear 0 10) ((,) <$> genKey <*> genValue) + [ Aeson.Object <$> genObject , Aeson.Array . Vector.fromList <$> Gen.list (Range.linear 0 10) genValue ] genJSONEncoded = Rel8.JSONEncoded <$> genValue genJSONBEncoded = Rel8.JSONBEncoded <$> genValue + genObject :: Gen Aeson.Object + genObject = Aeson.KeyMap.fromMap <$> Gen.map (Range.linear 0 10) ((,) <$> genKey <*> genValue) + testDBEq :: IO TmpPostgres.DB -> TestTree testDBEq getTestDatabase = testGroup "DBEq instances"