union package:dhall

Combine two Maps, preferring keys from the first Map
union = unionWith (\v _ -> v)
>>> union (fromList [("D",1),("C",2)]) (fromList [("B",3),("A",4)])
fromList [("D",1),("C",2),("B",3),("A",4)]

>>> union (fromList [("D",1),("C",2)]) (fromList [("C",3),("A",4)])
fromList [("D",1),("C",2),("A",4)]
Run a UnionDecoder to build a Decoder.
Union        [(k1, Just t1), (k2, Nothing)] ~  < k1 : t1 | k2 >
Combine two Maps using a combining function for colliding keys
>>> unionWith (+) (fromList [("D",1),("C",2)]) (fromList [("B",3),("A",4)])
fromList [("D",1),("C",2),("B",3),("A",4)]

>>> unionWith (+) (fromList [("D",1),("C",2)]) (fromList [("C",3),("A",4)])
fromList [("D",1),("C",5),("A",4)]
Convert a UnionEncoder into the equivalent Encoder.
The UnionDecoder monoid allows you to build a Decoder from a Dhall union. 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 decoder has type Decoder Status, but we can't build that out of any smaller decoders, as Decoders cannot be combined (they are only Functors). However, we can use a UnionDecoder to build a Decoder for Status:
>>> :{
status :: Decoder Status
status = union
(  ( Queued  <$> constructor "Queued"  natural )
<> ( Result  <$> constructor "Result"  strictText )
<> ( Errored <$> constructor "Errored" strictText )
)
:}
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)
:}
This is the underlying class that powers the FromDhall class's support for automatically deriving a generic implementation for a union type.
Generate a Haskell datatype declaration from a Dhall union type where each union alternative corresponds to a Haskell constructor For example, this Template Haskell splice:
Dhall.TH.makeHaskellTypeFromUnion "T" "< A : { x : Bool } | B >"
... generates this Haskell code:
data T = A {x :: GHC.Types.Bool} | B
This is a special case of makeHaskellTypes:
makeHaskellTypeFromUnion typeName code =
makeHaskellTypes [ MultipleConstructors{..} ]