forked from circuithub/rel8
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…uithub#347) Resolves circuithub#344
- Loading branch information
Showing
5 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Fixed | ||
|
||
- `JSONEncoded` should be encoded as `json` not `jsonb`. Resolves #344 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,56 @@ | ||
{-# language StandaloneKindSignatures #-} | ||
{-# language OverloadedStrings #-} | ||
{-# language TypeApplications #-} | ||
|
||
module Rel8.Type.JSONEncoded ( JSONEncoded(..) ) where | ||
|
||
-- aeson | ||
import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON ) | ||
import Data.Aeson ( FromJSON, ToJSON, parseJSON ) | ||
import Data.Aeson.Types ( parseEither ) | ||
import qualified Data.Aeson as Aeson | ||
|
||
-- base | ||
import Data.Kind ( Type ) | ||
import Data.Bifunctor (first) | ||
import Prelude | ||
|
||
-- hasql | ||
import qualified Hasql.Decoders as Hasql | ||
|
||
-- rel8 | ||
import Rel8.Type ( DBType(..) ) | ||
import Rel8.Type.Information ( parseTypeInformation ) | ||
import Rel8.Type.Information ( TypeInformation(..) ) | ||
import Rel8.Type.Decoder ( Decoder(..) ) | ||
|
||
-- opaleye | ||
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye | ||
import qualified Opaleye.Internal.HaskellDB.Sql.Default as Opaleye ( quote ) | ||
|
||
-- text | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.Lazy as Lazy | ||
import qualified Data.Text.Lazy.Encoding as Lazy | ||
|
||
|
||
-- | A deriving-via helper type for column types that store a Haskell value | ||
-- using a JSON encoding described by @aeson@'s 'ToJSON' and 'FromJSON' type | ||
-- classes. | ||
type JSONEncoded :: Type -> Type | ||
newtype JSONEncoded a = JSONEncoded { fromJSONEncoded :: a } | ||
deriving (Show, Eq, Ord) | ||
|
||
|
||
instance (FromJSON a, ToJSON a) => DBType (JSONEncoded a) where | ||
typeInformation = parseTypeInformation f g typeInformation | ||
where | ||
f = fmap JSONEncoded . parseEither parseJSON | ||
g = toJSON . fromJSONEncoded | ||
typeInformation = TypeInformation | ||
{ encode = | ||
Opaleye.ConstExpr . Opaleye.OtherLit . Opaleye.quote . | ||
Lazy.unpack . Lazy.decodeUtf8 . | ||
Aeson.encode . fromJSONEncoded | ||
, decode = | ||
Decoder | ||
{ binary = Hasql.refine (first Text.pack . fmap JSONEncoded . parseEither parseJSON) Hasql.json | ||
, parser = fmap JSONEncoded . Aeson.eitherDecodeStrict | ||
, delimiter = ',' | ||
} | ||
, typeName = "json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters