Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose interpretOptions so that custom HasInterpretOptions instances can be created #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3.1

- Expose HasInterpretOptions(interpretOptions) so that custom instances of HasInterpretOptions can be used.

# 0.3

- Use `dhall ==1.29`
Expand Down
12 changes: 6 additions & 6 deletions servant-dhall.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: >=1.10
name: servant-dhall
version: 0.3
version: 0.3.1
synopsis: Servant Dhall content-type
description:
Servant Dhall bindings.
Expand Down Expand Up @@ -31,15 +31,15 @@ source-repository head
library
exposed-modules: Servant.Dhall
build-depends:
base >=4.9 && <4.14
base >=4.9 && <4.15
, base-compat >=0.10.1 && <0.12
, bytestring >=0.10.4.0 && <0.11
, dhall >=1.29.0 && <1.30
, dhall >=1.29.0 && <1.37
, either >=5.0.1.1 && <5.1
, http-media >=0.7.1.2 && <0.9
, megaparsec >=7.0.4 && <8.1
, megaparsec >=7.0.4 && <10.0
, prettyprinter >=1.5.1 && <1.7
, servant >=0.17 && <0.18
, servant >=0.17 && <0.19
, text >=1.2.3.0 && <1.3

hs-source-dirs: src
Expand All @@ -59,7 +59,7 @@ test-suite example
, http-media
, servant
, servant-dhall
, servant-server >=0.12 && <0.18
, servant-server >=0.12 && <0.19
, wai >=3.0.3.0 && <3.3
, warp >=3.0.13.1 && <3.4

Expand Down
28 changes: 13 additions & 15 deletions src/Servant/Dhall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- | A @DHALL@ empty datatype with `MimeRender` and `MimeUnrender` instances for
-- /Dhall/'s 'Interpret' and 'Inject' classes.
Expand All @@ -18,7 +19,7 @@
module Servant.Dhall (
DHALL,
DHALL',
HasInterpretOptions,
HasInterpretOptions(..),
DefaultInterpretOptions,
) where

Expand All @@ -28,7 +29,9 @@ import Prelude.Compat
import Control.Monad
(unless)
import Data.Either.Validation
(Validation (..))
(Validation (..), validationToEither)
import Data.Function
((&))
import Data.Proxy
(Proxy (..))
import Data.Text.Encoding.Error
Expand All @@ -47,7 +50,7 @@ import Data.Traversable
import Data.Typeable
(Typeable)
import Dhall
(ToDhall (..), Encoder (..), FromDhall (..),
(auto, ToDhall (..), Encoder (..), FromDhall (..), inject,
InterpretOptions, Decoder (..), defaultInterpretOptions)
import qualified Dhall.Core
import Dhall.Parser
Expand All @@ -68,31 +71,29 @@ instance Accept (DHALL' opts) where
-- Encoding
-------------------------------------------------------------------------------

instance (ToDhall a, HasInterpretOptions opts) => MimeRender (DHALL' opts) a where
instance ToDhall a => MimeRender (DHALL' opts) a where
mimeRender _ x
= TLE.encodeUtf8
$ renderLazy
$ layoutSmart defaultLayoutOptions
$ (`mappend` line)
$ pretty
$ embed ty x
where
ty :: Encoder a
ty = injectWith (interpretOptions (Proxy :: Proxy opts))
$ embed inject x

-------------------------------------------------------------------------------
-- Decoding
-------------------------------------------------------------------------------

instance (FromDhall a, HasInterpretOptions opts) => MimeUnrender (DHALL' opts) a where
instance FromDhall a => MimeUnrender (DHALL' opts) a where
mimeUnrender _ lbs = do
expr0 <- firstEither showParseError $ exprFromText "(input)" te
expr1 <- for expr0 $ \i -> Left $ "Import found: " ++ ppExpr i
tyExpr <- firstEither showTypeError $ Dhall.TypeCheck.typeOf expr1
unless (Dhall.Core.judgmentallyEqual tyExpr $ expected ty) $
tyExprExpected <- expected (auto @a) & validationToEither & firstEither show
unless (Dhall.Core.judgmentallyEqual tyExpr $ tyExprExpected) $
Left $ "Expected and actual types don't match : "
++ ppExpr (expected ty) ++ " /= " ++ ppExpr tyExpr
case extract ty (Dhall.Core.normalizeWith Nothing expr1) of
++ ppExpr tyExprExpected ++ " /= " ++ ppExpr tyExpr
case extract auto (Dhall.Core.normalizeWith Nothing expr1) of
Success x -> Right x
Failure _ -> Left "Invalid type"
where
Expand All @@ -102,9 +103,6 @@ instance (FromDhall a, HasInterpretOptions opts) => MimeUnrender (DHALL' opts) a
te = TL.toStrict $
TLE.decodeUtf8With lenientDecode lbs

ty :: Decoder a
ty = autoWith (interpretOptions (Proxy :: Proxy opts))

ppExpr :: Pretty pp => pp -> String
ppExpr = renderString . layoutPretty defaultLayoutOptions . pretty

Expand Down