Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Rel8/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ testDBType getTestDatabase = testGroup "DBType instances"
, dbTypeTest "Value" genValue
, dbTypeTest "JSONEncoded" genJSONEncoded
, dbTypeTest "JSONBEncoded" genJSONBEncoded
, dbTypeTest "Object" genObject
]

where
Expand Down Expand Up @@ -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"
Expand Down
Loading