{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Dhall.TH
(
staticDhallExpression
, dhall
, makeHaskellTypeFromUnion
, makeHaskellTypes
, makeHaskellTypesWith
, HaskellType(..)
, GenerateOptions(..)
, defaultGenerateOptions
) where
import Data.Text (Text)
import Dhall (FromDhall, ToDhall)
import Dhall.Syntax (Expr (..))
import GHC.Generics (Generic)
import Language.Haskell.TH.Quote (QuasiQuoter (..), dataToExpQ)
import Prettyprinter (Pretty)
import Language.Haskell.TH.Syntax
( Bang (..)
, Body (..)
, Con (..)
, Dec (..)
, Exp (..)
, Match (..)
, Pat (..)
, Q
, SourceStrictness (..)
, SourceUnpackedness (..)
, Type (..)
)
import Language.Haskell.TH.Syntax (DerivClause (..), DerivStrategy (..))
import qualified Data.List as List
import qualified Data.Set as Set
import qualified Data.Text as Text
import qualified Data.Typeable as Typeable
import qualified Dhall
import qualified Dhall.Core as Core
import qualified Dhall.Map
import qualified Dhall.Pretty
import qualified Dhall.Util
import qualified GHC.IO.Encoding
import qualified Language.Haskell.TH.Syntax as Syntax
import qualified Numeric.Natural
import qualified Prettyprinter.Render.String as Pretty
import qualified System.IO
staticDhallExpression :: Text -> Q Exp
staticDhallExpression :: Text -> Q Exp
staticDhallExpression Text
text = do
IO () -> Q ()
forall a. IO a -> Q a
Syntax.runIO (TextEncoding -> IO ()
GHC.IO.Encoding.setLocaleEncoding TextEncoding
System.IO.utf8)
Expr Src Void
expression <- IO (Expr Src Void) -> Q (Expr Src Void)
forall a. IO a -> Q a
Syntax.runIO (Text -> IO (Expr Src Void)
Dhall.inputExpr Text
text)
(forall b. Data b => b -> Maybe (Q Exp)) -> Expr Src Void -> Q Exp
forall a.
Data a =>
(forall b. Data b => b -> Maybe (Q Exp)) -> a -> Q Exp
dataToExpQ ((Text -> Q Exp) -> Maybe Text -> Maybe (Q Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Q Exp
liftText (Maybe Text -> Maybe (Q Exp))
-> (b -> Maybe Text) -> b -> Maybe (Q Exp)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe Text
forall a b. (Typeable a, Typeable b) => a -> Maybe b
Typeable.cast) Expr Src Void
expression
where
liftText :: Text -> Q Exp
liftText = (Exp -> Exp) -> Q Exp -> Q Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Exp -> Exp -> Exp
AppE (Name -> Exp
VarE 'Text.pack)) (Q Exp -> Q Exp) -> (Text -> Q Exp) -> Text -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Q Exp
forall t. Lift t => t -> Q Exp
Syntax.lift (String -> Q Exp) -> (Text -> String) -> Text -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack
dhall :: QuasiQuoter
dhall :: QuasiQuoter
dhall = QuasiQuoter :: (String -> Q Exp)
-> (String -> Q Pat)
-> (String -> Q Type)
-> (String -> Q [Dec])
-> QuasiQuoter
QuasiQuoter
{ quoteExp :: String -> Q Exp
quoteExp = Text -> Q Exp
staticDhallExpression (Text -> Q Exp) -> (String -> Text) -> String -> Q Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack
, quotePat :: String -> Q Pat
quotePat = Q Pat -> String -> Q Pat
forall a b. a -> b -> a
const (Q Pat -> String -> Q Pat) -> Q Pat -> String -> Q Pat
forall a b. (a -> b) -> a -> b
$ String -> Q Pat
forall a. HasCallStack => String -> a
error String
"dhall quasi-quoter: Quoting patterns is not supported!"
, quoteType :: String -> Q Type
quoteType = Q Type -> String -> Q Type
forall a b. a -> b -> a
const (Q Type -> String -> Q Type) -> Q Type -> String -> Q Type
forall a b. (a -> b) -> a -> b
$ String -> Q Type
forall a. HasCallStack => String -> a
error String
"dhall quasi-quoter: Quoting types is not supported!"
, quoteDec :: String -> Q [Dec]
quoteDec = Q [Dec] -> String -> Q [Dec]
forall a b. a -> b -> a
const (Q [Dec] -> String -> Q [Dec]) -> Q [Dec] -> String -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ String -> Q [Dec]
forall a. HasCallStack => String -> a
error String
"dhall quasi-quoter: Quoting declarations is not supported!"
}
toNestedHaskellType
:: (Eq a, Pretty a)
=> [HaskellType (Expr s a)]
-> Expr s a
-> Q Type
toNestedHaskellType :: [HaskellType (Expr s a)] -> Expr s a -> Q Type
toNestedHaskellType [HaskellType (Expr s a)]
haskellTypes = Expr s a -> Q Type
forall (m :: * -> *) s. MonadFail m => Expr s a -> m Type
loop
where
loop :: Expr s a -> m Type
loop Expr s a
dhallType = case Expr s a
dhallType of
Expr s a
Bool ->
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT ''Bool)
Expr s a
Double ->
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT ''Double)
Expr s a
Integer ->
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT ''Integer)
Expr s a
Natural ->
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT ''Numeric.Natural.Natural)
Expr s a
Text ->
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT ''Text)
App Expr s a
List Expr s a
dhallElementType -> do
Type
haskellElementType <- Expr s a -> m Type
loop Expr s a
dhallElementType
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
AppT (Name -> Type
ConT ''[]) Type
haskellElementType)
App Expr s a
Optional Expr s a
dhallElementType -> do
Type
haskellElementType <- Expr s a -> m Type
loop Expr s a
dhallElementType
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Type -> Type
AppT (Name -> Type
ConT ''Maybe) Type
haskellElementType)
Expr s a
_ | Just HaskellType (Expr s a)
haskellType <- (HaskellType (Expr s a) -> Bool)
-> [HaskellType (Expr s a)] -> Maybe (HaskellType (Expr s a))
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find HaskellType (Expr s a) -> Bool
forall s. HaskellType (Expr s a) -> Bool
predicate [HaskellType (Expr s a)]
haskellTypes -> do
let name :: Name
name = String -> Name
Syntax.mkName (Text -> String
Text.unpack (HaskellType (Expr s a) -> Text
forall code. HaskellType code -> Text
typeName HaskellType (Expr s a)
haskellType))
Type -> m Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> Type
ConT Name
name)
| Bool
otherwise -> do
let document :: Doc Ann
document =
[Doc Ann] -> Doc Ann
forall a. Monoid a => [a] -> a
mconcat
[ Doc Ann
"Unsupported nested type\n"
, Doc Ann
" \n"
, Doc Ann
"Explanation: Not all Dhall types can be nested within Haskell datatype \n"
, Doc Ann
"declarations. Specifically, only the following simple Dhall types are supported\n"
, Doc Ann
"as a nested type inside of a data declaration: \n"
, Doc Ann
" \n"
, Doc Ann
"• ❰Bool❱ \n"
, Doc Ann
"• ❰Double❱ \n"
, Doc Ann
"• ❰Integer❱ \n"
, Doc Ann
"• ❰Natural❱ \n"
, Doc Ann
"• ❰Text❱ \n"
, Doc Ann
"• ❰List a❱ (where ❰a❱ is also a valid nested type) \n"
, Doc Ann
"• ❰Optional a❱ (where ❰a❱ is also a valid nested type) \n"
, Doc Ann
"• Another matching datatype declaration \n"
, Doc Ann
" \n"
, Doc Ann
"The Haskell datatype generation logic encountered the following Dhall type: \n"
, Doc Ann
" \n"
, Doc Ann
" " Doc Ann -> Doc Ann -> Doc Ann
forall a. Semigroup a => a -> a -> a
<> Expr s a -> Doc Ann
forall a. Pretty a => a -> Doc Ann
Dhall.Util.insert Expr s a
dhallType Doc Ann -> Doc Ann -> Doc Ann
forall a. Semigroup a => a -> a -> a
<> Doc Ann
"\n"
, Doc Ann
" \n"
, Doc Ann
"... which did not fit any of the above criteria."
]
let message :: String
message = SimpleDocStream Ann -> String
forall ann. SimpleDocStream ann -> String
Pretty.renderString (Doc Ann -> SimpleDocStream Ann
forall ann. Doc ann -> SimpleDocStream ann
Dhall.Pretty.layout Doc Ann
document)
String -> m Type
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
message
where
predicate :: HaskellType (Expr s a) -> Bool
predicate HaskellType (Expr s a)
haskellType =
Expr s a -> Expr s a -> Bool
forall a s t. Eq a => Expr s a -> Expr t a -> Bool
Core.judgmentallyEqual (HaskellType (Expr s a) -> Expr s a
forall code. HaskellType code -> code
code HaskellType (Expr s a)
haskellType) Expr s a
dhallType
derivingGenericClause :: DerivClause
derivingGenericClause :: DerivClause
derivingGenericClause = Maybe DerivStrategy -> Cxt -> DerivClause
DerivClause (DerivStrategy -> Maybe DerivStrategy
forall a. a -> Maybe a
Just DerivStrategy
StockStrategy) [ Name -> Type
ConT ''Generic ]
fromDhallInstance
:: Syntax.Name
-> Q Exp
-> Q [Dec]
fromDhallInstance :: Name -> Q Exp -> Q [Dec]
fromDhallInstance Name
n Q Exp
interpretOptions = [d|
instance FromDhall $(pure $ ConT n) where
autoWith = Dhall.genericAutoWithInputNormalizer $(interpretOptions)
|]
toDhallInstance
:: Syntax.Name
-> Q Exp
-> Q [Dec]
toDhallInstance :: Name -> Q Exp -> Q [Dec]
toDhallInstance Name
n Q Exp
interpretOptions = [d|
instance ToDhall $(pure $ ConT n) where
injectWith = Dhall.genericToDhallWithInputNormalizer $(interpretOptions)
|]
toDeclaration
:: (Eq a, Pretty a)
=> GenerateOptions
-> [HaskellType (Expr s a)]
-> HaskellType (Expr s a)
-> Q [Dec]
toDeclaration :: GenerateOptions
-> [HaskellType (Expr s a)] -> HaskellType (Expr s a) -> Q [Dec]
toDeclaration generateOptions :: GenerateOptions
generateOptions@GenerateOptions{Bool
Text -> Text
generateToDhallInstance :: GenerateOptions -> Bool
generateFromDhallInstance :: GenerateOptions -> Bool
fieldModifier :: GenerateOptions -> Text -> Text
constructorModifier :: GenerateOptions -> Text -> Text
generateToDhallInstance :: Bool
generateFromDhallInstance :: Bool
fieldModifier :: Text -> Text
constructorModifier :: Text -> Text
..} [HaskellType (Expr s a)]
haskellTypes typ :: HaskellType (Expr s a)
typ@MultipleConstructors{Text
Expr s a
code :: Expr s a
typeName :: Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} =
case Expr s a
code of
Union Map Text (Maybe (Expr s a))
kts -> do
let name :: Name
name = String -> Name
Syntax.mkName (Text -> String
Text.unpack Text
typeName)
let derivingClauses :: [DerivClause]
derivingClauses =
[ DerivClause
derivingGenericClause | Bool
generateFromDhallInstance Bool -> Bool -> Bool
|| Bool
generateToDhallInstance ]
[Con]
constructors <- ((Text, Maybe (Expr s a)) -> Q Con)
-> [(Text, Maybe (Expr s a))] -> Q [Con]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
forall a s.
(Eq a, Pretty a) =>
GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
toConstructor GenerateOptions
generateOptions [HaskellType (Expr s a)]
haskellTypes Text
typeName) (Map Text (Maybe (Expr s a)) -> [(Text, Maybe (Expr s a))]
forall k v. Ord k => Map k v -> [(k, v)]
Dhall.Map.toList Map Text (Maybe (Expr s a))
kts)
let interpretOptions :: Q Exp
interpretOptions = GenerateOptions -> HaskellType (Expr s a) -> Q Exp
forall s a. GenerateOptions -> HaskellType (Expr s a) -> Q Exp
generateToInterpretOptions GenerateOptions
generateOptions HaskellType (Expr s a)
typ
([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Q [[Dec]] -> Q [Dec])
-> ([Q [Dec]] -> Q [[Dec]]) -> [Q [Dec]] -> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Q [Dec]] -> Q [[Dec]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Q [Dec]] -> Q [Dec]) -> [Q [Dec]] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$
[[Dec] -> Q [Dec]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Cxt
-> Name
-> [TyVarBndr]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataD [] Name
name [] Maybe Type
forall a. Maybe a
Nothing [Con]
constructors [DerivClause]
derivingClauses]] [Q [Dec]] -> [Q [Dec]] -> [Q [Dec]]
forall a. Semigroup a => a -> a -> a
<>
[ Name -> Q Exp -> Q [Dec]
fromDhallInstance Name
name Q Exp
interpretOptions | Bool
generateFromDhallInstance ] [Q [Dec]] -> [Q [Dec]] -> [Q [Dec]]
forall a. Semigroup a => a -> a -> a
<>
[ Name -> Q Exp -> Q [Dec]
toDhallInstance Name
name Q Exp
interpretOptions | Bool
generateToDhallInstance ]
Expr s a
_ -> do
let document :: Doc Ann
document =
[Doc Ann] -> Doc Ann
forall a. Monoid a => [a] -> a
mconcat
[ Doc Ann
"Dhall.TH.makeHaskellTypes: Not a union type\n"
, Doc Ann
" \n"
, Doc Ann
"Explanation: This function expects the ❰code❱ field of ❰MultipleConstructors❱ to\n"
, Doc Ann
"evaluate to a union type. \n"
, Doc Ann
" \n"
, Doc Ann
"For example, this is a valid Dhall union type that this function would accept: \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
" ┌──────────────────────────────────────────────────────────────────┐ \n"
, Doc Ann
" │ Dhall.TH.makeHaskellTypes (MultipleConstructors \"T\" \"< A | B >\") │ \n"
, Doc Ann
" └──────────────────────────────────────────────────────────────────┘ \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
"... which corresponds to this Haskell type declaration: \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
" ┌────────────────┐ \n"
, Doc Ann
" │ data T = A | B │ \n"
, Doc Ann
" └────────────────┘ \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
"... but the following Dhall type is rejected due to being a bare record type: \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
" ┌──────────────────────────────────────────────┐ \n"
, Doc Ann
" │ Dhall.TH.makeHaskellTypes \"T\" \"{ x : Bool }\" │ Not valid \n"
, Doc Ann
" └──────────────────────────────────────────────┘ \n"
, Doc Ann
" \n"
, Doc Ann
" \n"
, Doc Ann
"The Haskell datatype generation logic encountered the following Dhall type: \n"
, Doc Ann
" \n"
, Doc Ann
" " Doc Ann -> Doc Ann -> Doc Ann
forall a. Semigroup a => a -> a -> a
<> Expr s a -> Doc Ann
forall a. Pretty a => a -> Doc Ann
Dhall.Util.insert Expr s a
code Doc Ann -> Doc Ann -> Doc Ann
forall a. Semigroup a => a -> a -> a
<> Doc Ann
"\n"
, Doc Ann
" \n"
, Doc Ann
"... which is not a union type."
]
let message :: String
message = SimpleDocStream Ann -> String
forall ann. SimpleDocStream ann -> String
Pretty.renderString (Doc Ann -> SimpleDocStream Ann
forall ann. Doc ann -> SimpleDocStream ann
Dhall.Pretty.layout Doc Ann
document)
String -> Q [Dec]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
message
toDeclaration generateOptions :: GenerateOptions
generateOptions@GenerateOptions{Bool
Text -> Text
generateToDhallInstance :: Bool
generateFromDhallInstance :: Bool
fieldModifier :: Text -> Text
constructorModifier :: Text -> Text
generateToDhallInstance :: GenerateOptions -> Bool
generateFromDhallInstance :: GenerateOptions -> Bool
fieldModifier :: GenerateOptions -> Text -> Text
constructorModifier :: GenerateOptions -> Text -> Text
..} [HaskellType (Expr s a)]
haskellTypes typ :: HaskellType (Expr s a)
typ@SingleConstructor{Text
Expr s a
constructorName :: forall code. HaskellType code -> Text
code :: Expr s a
constructorName :: Text
typeName :: Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} = do
let name :: Name
name = String -> Name
Syntax.mkName (Text -> String
Text.unpack Text
typeName)
let derivingClauses :: [DerivClause]
derivingClauses =
[ DerivClause
derivingGenericClause | Bool
generateFromDhallInstance Bool -> Bool -> Bool
|| Bool
generateToDhallInstance ]
let interpretOptions :: Q Exp
interpretOptions = GenerateOptions -> HaskellType (Expr s a) -> Q Exp
forall s a. GenerateOptions -> HaskellType (Expr s a) -> Q Exp
generateToInterpretOptions GenerateOptions
generateOptions HaskellType (Expr s a)
typ
Con
constructor <- GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
forall a s.
(Eq a, Pretty a) =>
GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
toConstructor GenerateOptions
generateOptions [HaskellType (Expr s a)]
haskellTypes Text
typeName (Text
constructorName, Expr s a -> Maybe (Expr s a)
forall a. a -> Maybe a
Just Expr s a
code)
([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Q [[Dec]] -> Q [Dec])
-> ([Q [Dec]] -> Q [[Dec]]) -> [Q [Dec]] -> Q [Dec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Q [Dec]] -> Q [[Dec]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Q [Dec]] -> Q [Dec]) -> [Q [Dec]] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$
[[Dec] -> Q [Dec]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Cxt
-> Name
-> [TyVarBndr]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataD [] Name
name [] Maybe Type
forall a. Maybe a
Nothing [Con
constructor] [DerivClause]
derivingClauses]] [Q [Dec]] -> [Q [Dec]] -> [Q [Dec]]
forall a. Semigroup a => a -> a -> a
<>
[ Name -> Q Exp -> Q [Dec]
fromDhallInstance Name
name Q Exp
interpretOptions | Bool
generateFromDhallInstance ] [Q [Dec]] -> [Q [Dec]] -> [Q [Dec]]
forall a. Semigroup a => a -> a -> a
<>
[ Name -> Q Exp -> Q [Dec]
toDhallInstance Name
name Q Exp
interpretOptions | Bool
generateToDhallInstance ]
toConstructor
:: (Eq a, Pretty a)
=> GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
toConstructor :: GenerateOptions
-> [HaskellType (Expr s a)]
-> Text
-> (Text, Maybe (Expr s a))
-> Q Con
toConstructor GenerateOptions{Bool
Text -> Text
generateToDhallInstance :: Bool
generateFromDhallInstance :: Bool
fieldModifier :: Text -> Text
constructorModifier :: Text -> Text
generateToDhallInstance :: GenerateOptions -> Bool
generateFromDhallInstance :: GenerateOptions -> Bool
fieldModifier :: GenerateOptions -> Text -> Text
constructorModifier :: GenerateOptions -> Text -> Text
..} [HaskellType (Expr s a)]
haskellTypes Text
outerTypeName (Text
constructorName, Maybe (Expr s a)
maybeAlternativeType) = do
let name :: Name
name = String -> Name
Syntax.mkName (Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Text
constructorModifier Text
constructorName)
let bang :: Bang
bang = SourceUnpackedness -> SourceStrictness -> Bang
Bang SourceUnpackedness
NoSourceUnpackedness SourceStrictness
NoSourceStrictness
case Maybe (Expr s a)
maybeAlternativeType of
Just Expr s a
dhallType
| let predicate :: HaskellType (Expr s a) -> Bool
predicate HaskellType (Expr s a)
haskellType =
Expr s a -> Expr s a -> Bool
forall a s t. Eq a => Expr s a -> Expr t a -> Bool
Core.judgmentallyEqual (HaskellType (Expr s a) -> Expr s a
forall code. HaskellType code -> code
code HaskellType (Expr s a)
haskellType) Expr s a
dhallType
Bool -> Bool -> Bool
&& HaskellType (Expr s a) -> Text
forall code. HaskellType code -> Text
typeName HaskellType (Expr s a)
haskellType Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
outerTypeName
, Just HaskellType (Expr s a)
haskellType <- (HaskellType (Expr s a) -> Bool)
-> [HaskellType (Expr s a)] -> Maybe (HaskellType (Expr s a))
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find HaskellType (Expr s a) -> Bool
forall s. HaskellType (Expr s a) -> Bool
predicate [HaskellType (Expr s a)]
haskellTypes -> do
let innerName :: Name
innerName =
String -> Name
Syntax.mkName (Text -> String
Text.unpack (HaskellType (Expr s a) -> Text
forall code. HaskellType code -> Text
typeName HaskellType (Expr s a)
haskellType))
Con -> Q Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [BangType] -> Con
NormalC Name
name [ (Bang
bang, Name -> Type
ConT Name
innerName) ])
Just (Record Map Text (RecordField s a)
kts) -> do
let process :: (Text, Expr s a) -> Q (Name, Bang, Type)
process (Text
key, Expr s a
dhallFieldType) = do
Type
haskellFieldType <- [HaskellType (Expr s a)] -> Expr s a -> Q Type
forall a s.
(Eq a, Pretty a) =>
[HaskellType (Expr s a)] -> Expr s a -> Q Type
toNestedHaskellType [HaskellType (Expr s a)]
haskellTypes Expr s a
dhallFieldType
(Name, Bang, Type) -> Q (Name, Bang, Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Name
Syntax.mkName (Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Text
fieldModifier Text
key), Bang
bang, Type
haskellFieldType)
[(Name, Bang, Type)]
varBangTypes <- ((Text, Expr s a) -> Q (Name, Bang, Type))
-> [(Text, Expr s a)] -> Q [(Name, Bang, Type)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (Text, Expr s a) -> Q (Name, Bang, Type)
process (Map Text (Expr s a) -> [(Text, Expr s a)]
forall k v. Ord k => Map k v -> [(k, v)]
Dhall.Map.toList (Map Text (Expr s a) -> [(Text, Expr s a)])
-> Map Text (Expr s a) -> [(Text, Expr s a)]
forall a b. (a -> b) -> a -> b
$ RecordField s a -> Expr s a
forall s a. RecordField s a -> Expr s a
Core.recordFieldValue (RecordField s a -> Expr s a)
-> Map Text (RecordField s a) -> Map Text (Expr s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text (RecordField s a)
kts)
Con -> Q Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [(Name, Bang, Type)] -> Con
RecC Name
name [(Name, Bang, Type)]
varBangTypes)
Just Expr s a
dhallAlternativeType -> do
Type
haskellAlternativeType <- [HaskellType (Expr s a)] -> Expr s a -> Q Type
forall a s.
(Eq a, Pretty a) =>
[HaskellType (Expr s a)] -> Expr s a -> Q Type
toNestedHaskellType [HaskellType (Expr s a)]
haskellTypes Expr s a
dhallAlternativeType
Con -> Q Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [BangType] -> Con
NormalC Name
name [ (Bang
bang, Type
haskellAlternativeType) ])
Maybe (Expr s a)
Nothing ->
Con -> Q Con
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [BangType] -> Con
NormalC Name
name [])
makeHaskellTypeFromUnion
:: Text
-> Text
-> Q [Dec]
makeHaskellTypeFromUnion :: Text -> Text -> Q [Dec]
makeHaskellTypeFromUnion Text
typeName Text
code =
[HaskellType Text] -> Q [Dec]
makeHaskellTypes [ MultipleConstructors :: forall code. Text -> code -> HaskellType code
MultipleConstructors{Text
code :: Text
typeName :: Text
code :: Text
typeName :: Text
..} ]
data HaskellType code
= MultipleConstructors
{ HaskellType code -> Text
typeName :: Text
, HaskellType code -> code
code :: code
}
| SingleConstructor
{ typeName :: Text
, HaskellType code -> Text
constructorName :: Text
, code :: code
}
deriving (a -> HaskellType b -> HaskellType a
(a -> b) -> HaskellType a -> HaskellType b
(forall a b. (a -> b) -> HaskellType a -> HaskellType b)
-> (forall a b. a -> HaskellType b -> HaskellType a)
-> Functor HaskellType
forall a b. a -> HaskellType b -> HaskellType a
forall a b. (a -> b) -> HaskellType a -> HaskellType b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> HaskellType b -> HaskellType a
$c<$ :: forall a b. a -> HaskellType b -> HaskellType a
fmap :: (a -> b) -> HaskellType a -> HaskellType b
$cfmap :: forall a b. (a -> b) -> HaskellType a -> HaskellType b
Functor, HaskellType a -> Bool
(a -> m) -> HaskellType a -> m
(a -> b -> b) -> b -> HaskellType a -> b
(forall m. Monoid m => HaskellType m -> m)
-> (forall m a. Monoid m => (a -> m) -> HaskellType a -> m)
-> (forall m a. Monoid m => (a -> m) -> HaskellType a -> m)
-> (forall a b. (a -> b -> b) -> b -> HaskellType a -> b)
-> (forall a b. (a -> b -> b) -> b -> HaskellType a -> b)
-> (forall b a. (b -> a -> b) -> b -> HaskellType a -> b)
-> (forall b a. (b -> a -> b) -> b -> HaskellType a -> b)
-> (forall a. (a -> a -> a) -> HaskellType a -> a)
-> (forall a. (a -> a -> a) -> HaskellType a -> a)
-> (forall a. HaskellType a -> [a])
-> (forall a. HaskellType a -> Bool)
-> (forall a. HaskellType a -> Int)
-> (forall a. Eq a => a -> HaskellType a -> Bool)
-> (forall a. Ord a => HaskellType a -> a)
-> (forall a. Ord a => HaskellType a -> a)
-> (forall a. Num a => HaskellType a -> a)
-> (forall a. Num a => HaskellType a -> a)
-> Foldable HaskellType
forall a. Eq a => a -> HaskellType a -> Bool
forall a. Num a => HaskellType a -> a
forall a. Ord a => HaskellType a -> a
forall m. Monoid m => HaskellType m -> m
forall a. HaskellType a -> Bool
forall a. HaskellType a -> Int
forall a. HaskellType a -> [a]
forall a. (a -> a -> a) -> HaskellType a -> a
forall m a. Monoid m => (a -> m) -> HaskellType a -> m
forall b a. (b -> a -> b) -> b -> HaskellType a -> b
forall a b. (a -> b -> b) -> b -> HaskellType a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: HaskellType a -> a
$cproduct :: forall a. Num a => HaskellType a -> a
sum :: HaskellType a -> a
$csum :: forall a. Num a => HaskellType a -> a
minimum :: HaskellType a -> a
$cminimum :: forall a. Ord a => HaskellType a -> a
maximum :: HaskellType a -> a
$cmaximum :: forall a. Ord a => HaskellType a -> a
elem :: a -> HaskellType a -> Bool
$celem :: forall a. Eq a => a -> HaskellType a -> Bool
length :: HaskellType a -> Int
$clength :: forall a. HaskellType a -> Int
null :: HaskellType a -> Bool
$cnull :: forall a. HaskellType a -> Bool
toList :: HaskellType a -> [a]
$ctoList :: forall a. HaskellType a -> [a]
foldl1 :: (a -> a -> a) -> HaskellType a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> HaskellType a -> a
foldr1 :: (a -> a -> a) -> HaskellType a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> HaskellType a -> a
foldl' :: (b -> a -> b) -> b -> HaskellType a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> HaskellType a -> b
foldl :: (b -> a -> b) -> b -> HaskellType a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> HaskellType a -> b
foldr' :: (a -> b -> b) -> b -> HaskellType a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> HaskellType a -> b
foldr :: (a -> b -> b) -> b -> HaskellType a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> HaskellType a -> b
foldMap' :: (a -> m) -> HaskellType a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> HaskellType a -> m
foldMap :: (a -> m) -> HaskellType a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> HaskellType a -> m
fold :: HaskellType m -> m
$cfold :: forall m. Monoid m => HaskellType m -> m
Foldable, Functor HaskellType
Foldable HaskellType
Functor HaskellType
-> Foldable HaskellType
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HaskellType a -> f (HaskellType b))
-> (forall (f :: * -> *) a.
Applicative f =>
HaskellType (f a) -> f (HaskellType a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HaskellType a -> m (HaskellType b))
-> (forall (m :: * -> *) a.
Monad m =>
HaskellType (m a) -> m (HaskellType a))
-> Traversable HaskellType
(a -> f b) -> HaskellType a -> f (HaskellType b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
HaskellType (m a) -> m (HaskellType a)
forall (f :: * -> *) a.
Applicative f =>
HaskellType (f a) -> f (HaskellType a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HaskellType a -> m (HaskellType b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HaskellType a -> f (HaskellType b)
sequence :: HaskellType (m a) -> m (HaskellType a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
HaskellType (m a) -> m (HaskellType a)
mapM :: (a -> m b) -> HaskellType a -> m (HaskellType b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> HaskellType a -> m (HaskellType b)
sequenceA :: HaskellType (f a) -> f (HaskellType a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
HaskellType (f a) -> f (HaskellType a)
traverse :: (a -> f b) -> HaskellType a -> f (HaskellType b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> HaskellType a -> f (HaskellType b)
$cp2Traversable :: Foldable HaskellType
$cp1Traversable :: Functor HaskellType
Traversable)
data GenerateOptions = GenerateOptions
{ GenerateOptions -> Text -> Text
constructorModifier :: Text -> Text
, GenerateOptions -> Text -> Text
fieldModifier :: Text -> Text
, GenerateOptions -> Bool
generateFromDhallInstance :: Bool
, GenerateOptions -> Bool
generateToDhallInstance :: Bool
}
defaultGenerateOptions :: GenerateOptions
defaultGenerateOptions :: GenerateOptions
defaultGenerateOptions = GenerateOptions :: (Text -> Text) -> (Text -> Text) -> Bool -> Bool -> GenerateOptions
GenerateOptions
{ constructorModifier :: Text -> Text
constructorModifier = Text -> Text
forall a. a -> a
id
, fieldModifier :: Text -> Text
fieldModifier = Text -> Text
forall a. a -> a
id
, generateFromDhallInstance :: Bool
generateFromDhallInstance = Bool
True
, generateToDhallInstance :: Bool
generateToDhallInstance = Bool
True
}
generateToInterpretOptions :: GenerateOptions -> HaskellType (Expr s a) -> Q Exp
generateToInterpretOptions :: GenerateOptions -> HaskellType (Expr s a) -> Q Exp
generateToInterpretOptions GenerateOptions{Bool
Text -> Text
generateToDhallInstance :: Bool
generateFromDhallInstance :: Bool
fieldModifier :: Text -> Text
constructorModifier :: Text -> Text
generateToDhallInstance :: GenerateOptions -> Bool
generateFromDhallInstance :: GenerateOptions -> Bool
fieldModifier :: GenerateOptions -> Text -> Text
constructorModifier :: GenerateOptions -> Text -> Text
..} HaskellType (Expr s a)
haskellType = [| Dhall.InterpretOptions
{ Dhall.fieldModifier = \ $(pure nameP) ->
$(toCases fieldModifier $ fields haskellType)
, Dhall.constructorModifier = \ $(pure nameP) ->
$(toCases constructorModifier $ constructors haskellType)
, Dhall.singletonConstructors = Dhall.singletonConstructors Dhall.defaultInterpretOptions
}|]
where
constructors :: HaskellType (Expr s a) -> [Text]
constructors :: HaskellType (Expr s a) -> [Text]
constructors SingleConstructor{Text
Expr s a
code :: Expr s a
constructorName :: Text
typeName :: Text
constructorName :: forall code. HaskellType code -> Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} = [Text
constructorName]
constructors MultipleConstructors{Text
Expr s a
code :: Expr s a
typeName :: Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} | Union Map Text (Maybe (Expr s a))
kts <- Expr s a
code = Map Text (Maybe (Expr s a)) -> [Text]
forall k v. Map k v -> [k]
Dhall.Map.keys Map Text (Maybe (Expr s a))
kts
constructors HaskellType (Expr s a)
_ = []
fields :: HaskellType (Expr s a) -> [Text]
fields :: HaskellType (Expr s a) -> [Text]
fields SingleConstructor{Text
Expr s a
code :: Expr s a
constructorName :: Text
typeName :: Text
constructorName :: forall code. HaskellType code -> Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} | Record Map Text (RecordField s a)
kts <- Expr s a
code = Map Text (RecordField s a) -> [Text]
forall k v. Map k v -> [k]
Dhall.Map.keys Map Text (RecordField s a)
kts
fields MultipleConstructors{Text
Expr s a
code :: Expr s a
typeName :: Text
code :: forall code. HaskellType code -> code
typeName :: forall code. HaskellType code -> Text
..} | Union Map Text (Maybe (Expr s a))
kts <- Expr s a
code = Set Text -> [Text]
forall a. Set a -> [a]
Set.toList (Set Text -> [Text]) -> Set Text -> [Text]
forall a b. (a -> b) -> a -> b
$ [Set Text] -> Set Text
forall a. Monoid a => [a] -> a
mconcat
[ Map Text (RecordField s a) -> Set Text
forall k v. Map k v -> Set k
Dhall.Map.keysSet Map Text (RecordField s a)
kts'
| (Text
_, Just (Record Map Text (RecordField s a)
kts')) <- Map Text (Maybe (Expr s a)) -> [(Text, Maybe (Expr s a))]
forall k v. Ord k => Map k v -> [(k, v)]
Dhall.Map.toList Map Text (Maybe (Expr s a))
kts
]
fields HaskellType (Expr s a)
_ = []
toCases :: (Text -> Text) -> [Text] -> Q Exp
toCases :: (Text -> Text) -> [Text] -> Q Exp
toCases Text -> Text
f [Text]
xs = do
Exp
err <- [| Core.internalError $ "Unmatched " <> Text.pack (show $(pure nameE)) |]
Exp -> Q Exp
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ Exp -> [Match] -> Exp
CaseE Exp
nameE ([Match] -> Exp) -> [Match] -> Exp
forall a b. (a -> b) -> a -> b
$ (Text -> Match) -> [Text] -> [Match]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Match
mkMatch [Text]
xs [Match] -> [Match] -> [Match]
forall a. Semigroup a => a -> a -> a
<> [Pat -> Body -> [Dec] -> Match
Match Pat
WildP (Exp -> Body
NormalB Exp
err) []]
where
mkMatch :: Text -> Match
mkMatch Text
n = Pat -> Body -> [Dec] -> Match
Match (Text -> Pat
textToPat (Text -> Pat) -> Text -> Pat
forall a b. (a -> b) -> a -> b
$ Text -> Text
f Text
n) (Exp -> Body
NormalB (Exp -> Body) -> Exp -> Body
forall a b. (a -> b) -> a -> b
$ Text -> Exp
textToExp Text
n) []
nameE :: Exp
nameE :: Exp
nameE = Name -> Exp
Syntax.VarE (Name -> Exp) -> Name -> Exp
forall a b. (a -> b) -> a -> b
$ String -> Name
Syntax.mkName String
"n"
nameP :: Pat
nameP :: Pat
nameP = Name -> Pat
Syntax.VarP (Name -> Pat) -> Name -> Pat
forall a b. (a -> b) -> a -> b
$ String -> Name
Syntax.mkName String
"n"
textToExp :: Text -> Exp
textToExp :: Text -> Exp
textToExp = Lit -> Exp
Syntax.LitE (Lit -> Exp) -> (Text -> Lit) -> Text -> Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Lit
Syntax.StringL (String -> Lit) -> (Text -> String) -> Text -> Lit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack
textToPat :: Text -> Pat
textToPat :: Text -> Pat
textToPat = Lit -> Pat
Syntax.LitP (Lit -> Pat) -> (Text -> Lit) -> Text -> Pat
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Lit
Syntax.StringL (String -> Lit) -> (Text -> String) -> Text -> Lit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack
makeHaskellTypes :: [HaskellType Text] -> Q [Dec]
makeHaskellTypes :: [HaskellType Text] -> Q [Dec]
makeHaskellTypes = GenerateOptions -> [HaskellType Text] -> Q [Dec]
makeHaskellTypesWith GenerateOptions
defaultGenerateOptions
makeHaskellTypesWith :: GenerateOptions -> [HaskellType Text] -> Q [Dec]
makeHaskellTypesWith :: GenerateOptions -> [HaskellType Text] -> Q [Dec]
makeHaskellTypesWith GenerateOptions
generateOptions [HaskellType Text]
haskellTypes = do
IO () -> Q ()
forall a. IO a -> Q a
Syntax.runIO (TextEncoding -> IO ()
GHC.IO.Encoding.setLocaleEncoding TextEncoding
System.IO.utf8)
[HaskellType (Expr Src Void)]
haskellTypes' <- (HaskellType Text -> Q (HaskellType (Expr Src Void)))
-> [HaskellType Text] -> Q [HaskellType (Expr Src Void)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Text -> Q (Expr Src Void))
-> HaskellType Text -> Q (HaskellType (Expr Src Void))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (IO (Expr Src Void) -> Q (Expr Src Void)
forall a. IO a -> Q a
Syntax.runIO (IO (Expr Src Void) -> Q (Expr Src Void))
-> (Text -> IO (Expr Src Void)) -> Text -> Q (Expr Src Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO (Expr Src Void)
Dhall.inputExpr)) [HaskellType Text]
haskellTypes
[[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (HaskellType (Expr Src Void) -> Q [Dec])
-> [HaskellType (Expr Src Void)] -> Q [[Dec]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (GenerateOptions
-> [HaskellType (Expr Src Void)]
-> HaskellType (Expr Src Void)
-> Q [Dec]
forall a s.
(Eq a, Pretty a) =>
GenerateOptions
-> [HaskellType (Expr s a)] -> HaskellType (Expr s a) -> Q [Dec]
toDeclaration GenerateOptions
generateOptions [HaskellType (Expr Src Void)]
haskellTypes') [HaskellType (Expr Src Void)]
haskellTypes'