| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Dhall.Marshal.Encode
Description
Please read the Dhall.Tutorial module, which contains a tutorial explaining how to use the language, the compiler, and this library
Synopsis
- data Encoder a = Encoder {}
- class ToDhall a where
- injectWith :: InputNormalizer -> Encoder a
- type Inject = ToDhall
- inject :: ToDhall a => Encoder a
- newtype RecordEncoder a = RecordEncoder (Map Text (Encoder a))
- recordEncoder :: RecordEncoder a -> Encoder a
- encodeField :: ToDhall a => Text -> RecordEncoder a
- encodeFieldWith :: Text -> Encoder a -> RecordEncoder a
- newtype UnionEncoder a = UnionEncoder (Product (Const (Map Text (Expr Src Void))) (Op (Text, Expr Src Void)) a)
- unionEncoder :: UnionEncoder a -> Encoder a
- encodeConstructor :: ToDhall a => Text -> UnionEncoder a
- encodeConstructorWith :: Text -> Encoder a -> UnionEncoder a
- (>|<) :: UnionEncoder a -> UnionEncoder b -> UnionEncoder (Either a b)
- class GenericToDhall f where
- genericToDhallWithNormalizer :: InputNormalizer -> InterpretOptions -> State Int (Encoder (f a))
- genericToDhall :: (Generic a, GenericToDhall (Rep a)) => Encoder a
- genericToDhallWith :: (Generic a, GenericToDhall (Rep a)) => InterpretOptions -> Encoder a
- genericToDhallWithInputNormalizer :: (Generic a, GenericToDhall (Rep a)) => InterpretOptions -> InputNormalizer -> Encoder a
- data InterpretOptions = InterpretOptions {}
- data SingletonConstructors
- defaultInterpretOptions :: InterpretOptions
- newtype InputNormalizer = InputNormalizer {}
- defaultInputNormalizer :: InputNormalizer
- data Result f
- (>$<) :: Contravariant f => (a -> b) -> f b -> f a
- (>*<) :: Divisible f => f a -> f b -> f (a, b)
- data Natural
- data Seq a
- data Text
- data Vector a
- class Generic a
General
An (Encoder a) represents a way to marshal a value of type 'a' from
Haskell into Dhall.
Constructors
| Encoder | |
class ToDhall a where Source #
This class is used by FromDhall instance for functions:
instance (ToDhall a, FromDhall b) => FromDhall (a -> b)
You can convert Dhall functions with "simple" inputs (i.e. instances of this class) into Haskell functions. This works by:
- Marshaling the input to the Haskell function into a Dhall expression (i.e.
x :: Expr Src Void) - Applying the Dhall function (i.e.
f :: Expr Src Void) to the Dhall input (i.e.App f x) - Normalizing the syntax tree (i.e.
normalize (App f x)) - Marshaling the resulting Dhall expression back into a Haskell value
This class auto-generates a default implementation for types that
implement Generic. This does not auto-generate an instance for recursive
types.
The default instance can be tweaked using genericToDhallWith/genericToDhallWithInputNormalizer
and custom InterpretOptions, or using
DerivingVia
and Codec from Dhall.Deriving.
Minimal complete definition
Nothing
Methods
injectWith :: InputNormalizer -> Encoder a Source #
default injectWith :: (Generic a, GenericToDhall (Rep a)) => InputNormalizer -> Encoder a Source #
Instances
inject :: ToDhall a => Encoder a Source #
Use the default input normalizer for injecting a value.
inject = injectWith defaultInputNormalizer
Building encoders
Records
newtype RecordEncoder a Source #
The RecordEncoder divisible (contravariant) functor allows you to build
an Encoder for a Dhall record.
For example, let's take the following Haskell data type:
>>>:{data Project = Project { projectName :: Text , projectDescription :: Text , projectStars :: Natural } :}
And assume that we have the following Dhall record that we would like to
parse as a Project:
{ name =
"dhall-haskell"
, description =
"A configuration language guaranteed to terminate"
, stars =
289
}Our encoder has type Encoder Project, but we can't build that out of any
smaller encoders, as Encoders cannot be combined (they are only Contravariants).
However, we can use an RecordEncoder to build an Encoder for Project:
>>>:{injectProject :: Encoder Project injectProject = recordEncoder ( adapt >$< encodeFieldWith "name" inject >*< encodeFieldWith "description" inject >*< encodeFieldWith "stars" inject ) where adapt (Project{..}) = (projectName, (projectDescription, projectStars)) :}
Or, since we are simply using the ToDhall instance to inject each field, we could write
>>>:{injectProject :: Encoder Project injectProject = recordEncoder ( adapt >$< encodeField "name" >*< encodeField "description" >*< encodeField "stars" ) where adapt (Project{..}) = (projectName, (projectDescription, projectStars)) :}
Constructors
| RecordEncoder (Map Text (Encoder a)) |
Instances
| Contravariant RecordEncoder Source # | |
Defined in Dhall.Marshal.Encode Methods contramap :: (a' -> a) -> RecordEncoder a -> RecordEncoder a' Source # (>$) :: b -> RecordEncoder b -> RecordEncoder a Source # | |
| Divisible RecordEncoder Source # | |
Defined in Dhall.Marshal.Encode Methods divide :: (a -> (b, c)) -> RecordEncoder b -> RecordEncoder c -> RecordEncoder a Source # conquer :: RecordEncoder a Source # | |
recordEncoder :: RecordEncoder a -> Encoder a Source #
Convert a RecordEncoder into the equivalent Encoder.
encodeField :: ToDhall a => Text -> RecordEncoder a Source #
Specify how to encode one field of a record using the default ToDhall
instance for that type.
encodeFieldWith :: Text -> Encoder a -> RecordEncoder a Source #
Specify how to encode one field of a record by supplying an explicit
Encoder for that field.
Unions
newtype UnionEncoder a Source #
UnionEncoder allows you to build an Encoder for a Dhall record.
For example, let's take the following Haskell data type:
>>>:{data Status = Queued Natural | Result Text | Errored Text :}
And assume that we have the following Dhall union that we would like to
parse as a Status:
< Result : Text | Queued : Natural | Errored : Text >.Result "Finish successfully"
Our encoder has type Encoder Status, but we can't build that out of any
smaller encoders, as Encoders cannot be combined.
However, we can use an UnionEncoder to build an Encoder for Status:
>>>:{injectStatus :: Encoder Status injectStatus = adapt >$< unionEncoder ( encodeConstructorWith "Queued" inject >|< encodeConstructorWith "Result" inject >|< encodeConstructorWith "Errored" inject ) where adapt (Queued n) = Left n adapt (Result t) = Right (Left t) adapt (Errored e) = Right (Right e) :}
Or, since we are simply using the ToDhall instance to inject each branch, we could write
>>>:{injectStatus :: Encoder Status injectStatus = adapt >$< unionEncoder ( encodeConstructor "Queued" >|< encodeConstructor "Result" >|< encodeConstructor "Errored" ) where adapt (Queued n) = Left n adapt (Result t) = Right (Left t) adapt (Errored e) = Right (Right e) :}
Instances
| Contravariant UnionEncoder Source # | |
Defined in Dhall.Marshal.Encode Methods contramap :: (a' -> a) -> UnionEncoder a -> UnionEncoder a' Source # (>$) :: b -> UnionEncoder b -> UnionEncoder a Source # | |
unionEncoder :: UnionEncoder a -> Encoder a Source #
Convert a UnionEncoder into the equivalent Encoder.
encodeConstructor :: ToDhall a => Text -> UnionEncoder a Source #
Specify how to encode an alternative by using the default ToDhall instance
for that type.
encodeConstructorWith :: Text -> Encoder a -> UnionEncoder a Source #
Specify how to encode an alternative by providing an explicit Encoder
for that alternative.
(>|<) :: UnionEncoder a -> UnionEncoder b -> UnionEncoder (Either a b) infixr 5 Source #
Combines two UnionEncoder values. See UnionEncoder for usage
notes.
Ideally, this matches chosen;
however, this allows UnionEncoder to not need a Divisible instance
itself (since no instance is possible).
Generic encoding
class GenericToDhall f where Source #
This is the underlying class that powers the FromDhall class's support
for automatically deriving a generic implementation.
Methods
genericToDhallWithNormalizer :: InputNormalizer -> InterpretOptions -> State Int (Encoder (f a)) Source #
Instances
genericToDhall :: (Generic a, GenericToDhall (Rep a)) => Encoder a Source #
Use the default options for injecting a value, whose structure is determined generically.
This can be used when you want to use ToDhall on types that you don't
want to define orphan instances for.
genericToDhallWith :: (Generic a, GenericToDhall (Rep a)) => InterpretOptions -> Encoder a Source #
Use custom options for injecting a value, whose structure is determined generically.
This can be used when you want to use ToDhall on types that you don't
want to define orphan instances for.
genericToDhallWithInputNormalizer :: (Generic a, GenericToDhall (Rep a)) => InterpretOptions -> InputNormalizer -> Encoder a Source #
genericToDhallWithInputNormalizer is like genericToDhallWith, but
instead of using the defaultInputNormalizer it expects an custom
InputNormalizer.
data InterpretOptions Source #
Use these options to tweak how Dhall derives a generic implementation of
FromDhall.
Constructors
| InterpretOptions | |
Fields
| |
data SingletonConstructors Source #
This type specifies how to model a Haskell constructor with 1 field in Dhall
For example, consider the following Haskell datatype definition:
data Example = Foo { x :: Double } | Bar DoubleDepending on which option you pick, the corresponding Dhall type could be:
< Foo : Double | Bar : Double > -- Bare
< Foo : { x : Double } | Bar : { _1 : Double } > -- Wrapped< Foo : { x : Double } | Bar : Double > -- SmartConstructors
| Bare | Never wrap the field in a record |
| Wrapped | Always wrap the field in a record |
| Smart | Only fields in a record if they are named |
Instances
| ToSingletonConstructors a => ModifyOptions (SetSingletonConstructors a :: Type) Source # | |
Defined in Dhall.Deriving Methods modifyOptions :: InterpretOptions -> InterpretOptions Source # | |
defaultInterpretOptions :: InterpretOptions Source #
Default interpret options for generics-based instances, which you can tweak or override, like this:
genericAutoWith
(defaultInterpretOptions { fieldModifier = Data.Text.Lazy.dropWhile (== '_') })Miscellaneous
newtype InputNormalizer Source #
This is only used by the FromDhall instance for
functions in order to normalize the function input before marshaling the
input into a Dhall expression.
Constructors
| InputNormalizer | |
Fields | |
defaultInputNormalizer :: InputNormalizer Source #
Default normalization-related settings (no custom normalization)
This type is exactly the same as Fix except with a different
FromDhall instance. This intermediate type
simplifies the implementation of the inner loop for the
FromDhall instance for Fix.
Instances
| FromDhall (f (Result f)) => FromDhall (Result f) Source # | |
Defined in Dhall.Marshal.Decode | |
| ToDhall (f (Result f)) => ToDhall (Result f) Source # | |
Defined in Dhall.Marshal.Encode Methods injectWith :: InputNormalizer -> Encoder (Result f) Source # | |
(>$<) :: Contravariant f => (a -> b) -> f b -> f a infixl 4 Source #
This is an infix alias for contramap.
Re-exports
Natural number
Invariant: numbers <= 0xffffffffffffffff use the NS constructor
Instances
General-purpose finite sequences.
Instances
| FromJSON1 Seq | |
| ToJSON1 Seq | |
Defined in Data.Aeson.Types.ToJSON Methods liftToJSON :: (a -> Value) -> ([a] -> Value) -> Seq a -> Value Source # liftToJSONList :: (a -> Value) -> ([a] -> Value) -> [Seq a] -> Value Source # liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> Seq a -> Encoding Source # liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [Seq a] -> Encoding Source # | |
| MonadFix Seq | Since: containers-0.5.11 |
| MonadZip Seq |
|
| Foldable Seq | |
Defined in Data.Sequence.Internal Methods fold :: Monoid m => Seq m -> m Source # foldMap :: Monoid m => (a -> m) -> Seq a -> m Source # foldMap' :: Monoid m => (a -> m) -> Seq a -> m Source # foldr :: (a -> b -> b) -> b -> Seq a -> b Source # foldr' :: (a -> b -> b) -> b -> Seq a -> b Source # foldl :: (b -> a -> b) -> b -> Seq a -> b Source # foldl' :: (b -> a -> b) -> b -> Seq a -> b Source # foldr1 :: (a -> a -> a) -> Seq a -> a Source # foldl1 :: (a -> a -> a) -> Seq a -> a Source # toList :: Seq a -> [a] Source # null :: Seq a -> Bool Source # length :: Seq a -> Int Source # elem :: Eq a => a -> Seq a -> Bool Source # maximum :: Ord a => Seq a -> a Source # minimum :: Ord a => Seq a -> a Source # | |
| Eq1 Seq | Since: containers-0.5.9 |
| Ord1 Seq | Since: containers-0.5.9 |
Defined in Data.Sequence.Internal | |
| Read1 Seq | Since: containers-0.5.9 |
Defined in Data.Sequence.Internal Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Seq a) Source # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Seq a] Source # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Seq a) Source # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Seq a] Source # | |
| Show1 Seq | Since: containers-0.5.9 |
| Traversable Seq | |
| Alternative Seq | Since: containers-0.5.4 |
| Applicative Seq | Since: containers-0.5.4 |
| Functor Seq | |
| Monad Seq | |
| MonadPlus Seq | |
| UnzipWith Seq | |
Defined in Data.Sequence.Internal Methods unzipWith' :: (x -> (a, b)) -> Seq x -> (Seq a, Seq b) | |
| Hashable1 Seq | Since: hashable-1.3.4.0 |
Defined in Data.Hashable.Class | |
| FoldableWithIndex Int Seq | |
Defined in WithIndex Methods ifoldMap :: Monoid m => (Int -> a -> m) -> Seq a -> m Source # ifoldMap' :: Monoid m => (Int -> a -> m) -> Seq a -> m Source # ifoldr :: (Int -> a -> b -> b) -> b -> Seq a -> b Source # ifoldl :: (Int -> b -> a -> b) -> b -> Seq a -> b Source # | |
| FunctorWithIndex Int Seq | The position in the |
| TraversableWithIndex Int Seq | |
| Lift a => Lift (Seq a :: Type) | Since: containers-0.6.6 |
| FromJSON a => FromJSON (Seq a) | |
| ToJSON a => ToJSON (Seq a) | |
| Data a => Data (Seq a) | |
Defined in Data.Sequence.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Seq a -> c (Seq a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Seq a) Source # toConstr :: Seq a -> Constr Source # dataTypeOf :: Seq a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Seq a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Seq a)) Source # gmapT :: (forall b. Data b => b -> b) -> Seq a -> Seq a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Seq a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Seq a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Seq a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Seq a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Seq a -> m (Seq a) Source # | |
| a ~ Char => IsString (Seq a) | Since: containers-0.5.7 |
Defined in Data.Sequence.Internal Methods fromString :: String -> Seq a Source # | |
| Monoid (Seq a) | |
| Semigroup (Seq a) | Since: containers-0.5.7 |
| IsList (Seq a) | |
| Read a => Read (Seq a) | |
| Show a => Show (Seq a) | |
| NFData a => NFData (Seq a) | |
Defined in Data.Sequence.Internal | |
| FromDhall a => FromDhall (Seq a) Source # | |
Defined in Dhall.Marshal.Decode | |
| ToDhall a => ToDhall (Seq a) Source # | |
Defined in Dhall.Marshal.Encode Methods injectWith :: InputNormalizer -> Encoder (Seq a) Source # | |
| Eq a => Eq (Seq a) | |
| Ord a => Ord (Seq a) | |
Defined in Data.Sequence.Internal | |
| Hashable v => Hashable (Seq v) | Since: hashable-1.3.4.0 |
| Ord a => Stream (Seq a) | Since: megaparsec-9.0.0 |
Defined in Text.Megaparsec.Stream Methods tokenToChunk :: Proxy (Seq a) -> Token (Seq a) -> Tokens (Seq a) Source # tokensToChunk :: Proxy (Seq a) -> [Token (Seq a)] -> Tokens (Seq a) Source # chunkToTokens :: Proxy (Seq a) -> Tokens (Seq a) -> [Token (Seq a)] Source # chunkLength :: Proxy (Seq a) -> Tokens (Seq a) -> Int Source # chunkEmpty :: Proxy (Seq a) -> Tokens (Seq a) -> Bool Source # take1_ :: Seq a -> Maybe (Token (Seq a), Seq a) Source # takeN_ :: Int -> Seq a -> Maybe (Tokens (Seq a), Seq a) Source # takeWhile_ :: (Token (Seq a) -> Bool) -> Seq a -> (Tokens (Seq a), Seq a) Source # | |
| Serialise a => Serialise (Seq a) | Since: serialise-0.2.0.0 |
| type Item (Seq a) | |
Defined in Data.Sequence.Internal | |
| type Token (Seq a) | |
Defined in Text.Megaparsec.Stream | |
| type Tokens (Seq a) | |
Defined in Text.Megaparsec.Stream | |
A space efficient, packed, unboxed Unicode text type.
Instances
Boxed vectors, supporting efficient slicing.
Instances
Representable types of kind *.
This class is derivable in GHC with the DeriveGeneric flag on.
A Generic instance must satisfy the following laws:
from.to≡idto.from≡id