@@ -179,11 +179,14 @@ toNestedHaskellType haskellTypes = loop
179179 predicate haskellType =
180180 Core. judgmentallyEqual (code haskellType) dhallType
181181
182- derivingClauses :: [DerivClause ]
183- derivingClauses =
184- [ DerivClause (Just StockStrategy ) [ ConT ''Generic ]
185- , DerivClause (Just AnyclassStrategy ) [ ConT ''FromDhall, ConT ''ToDhall ]
186- ]
182+ derivingGenericClause :: DerivClause
183+ derivingGenericClause = DerivClause (Just StockStrategy ) [ ConT ''Generic ]
184+
185+ derivingFromClause :: DerivClause
186+ derivingFromClause = DerivClause (Just AnyclassStrategy ) [ ConT ''FromDhall ]
187+
188+ derivingToClause :: DerivClause
189+ derivingToClause = DerivClause (Just AnyclassStrategy ) [ ConT ''ToDhall ]
187190
188191-- | Convert a Dhall type to the corresponding Haskell datatype declaration
189192toDeclaration
@@ -196,6 +199,11 @@ toDeclaration haskellTypes MultipleConstructors{..} =
196199 Union kts -> do
197200 let name = Syntax. mkName (Text. unpack typeName)
198201
202+ let derivingClauses =
203+ [ derivingGenericClause | generateFromDhallInstance || generateToDhallInstance ] <>
204+ [ derivingFromClause | generateFromDhallInstance ] <>
205+ [ derivingToClause | generateToDhallInstance ]
206+
199207 constructors <- traverse (toConstructor haskellTypes typeName) (Dhall.Map. toList kts )
200208
201209 return (DataD [] name [] Nothing constructors derivingClauses)
@@ -245,6 +253,11 @@ toDeclaration haskellTypes MultipleConstructors{..} =
245253toDeclaration haskellTypes SingleConstructor {.. } = do
246254 let name = Syntax. mkName (Text. unpack typeName)
247255
256+ let derivingClauses =
257+ [ derivingGenericClause | generateFromDhallInstance || generateToDhallInstance ] <>
258+ [ derivingFromClause | generateFromDhallInstance ] <>
259+ [ derivingToClause | generateToDhallInstance ]
260+
248261 constructor <- toConstructor haskellTypes typeName (constructorName, Just code)
249262
250263 return (DataD [] name [] Nothing [constructor] derivingClauses)
@@ -306,15 +319,19 @@ toConstructor haskellTypes outerTypeName (constructorName, maybeAlternativeType)
306319-- This is a special case of `Dhall.TH.makeHaskellTypes`:
307320--
308321-- > makeHaskellTypeFromUnion typeName code =
309- -- > makeHaskellTypes [ MultipleConstructors{..} ]
322+ -- > let generateFromDhallInstance = True
323+ -- > generateToDhallInstance = True
324+ -- > in makeHaskellTypes [ MultipleConstructors{..} ]
310325makeHaskellTypeFromUnion
311326 :: Text
312327 -- ^ Name of the generated Haskell type
313328 -> Text
314329 -- ^ Dhall code that evaluates to a union type
315330 -> Q [Dec ]
316331makeHaskellTypeFromUnion typeName code =
317- makeHaskellTypes [ MultipleConstructors {.. } ]
332+ let generateFromDhallInstance = True
333+ generateToDhallInstance = True
334+ in makeHaskellTypes [ MultipleConstructors {.. } ]
318335
319336-- | Used by `makeHaskellTypes` to specify how to generate Haskell types
320337data HaskellType code
@@ -323,6 +340,10 @@ data HaskellType code
323340 = MultipleConstructors
324341 { typeName :: Text
325342 -- ^ Name of the generated Haskell type
343+ , generateFromDhallInstance :: Bool
344+ -- ^ Generate a `FromDhall` instance for the Haskell type
345+ , generateToDhallInstance :: Bool
346+ -- ^ Generate a `ToDhall` instance for the Haskell type
326347 , code :: code
327348 -- ^ Dhall code that evaluates to a union type
328349 }
@@ -335,6 +356,10 @@ data HaskellType code
335356 -- ^ Name of the generated Haskell type
336357 , constructorName :: Text
337358 -- ^ Name of the constructor
359+ , generateFromDhallInstance :: Bool
360+ -- ^ Generate a `FromDhall` instance for the Haskell type
361+ , generateToDhallInstance :: Bool
362+ -- ^ Generate a `ToDhall` instance for the Haskell type
338363 , code :: code
339364 -- ^ Dhall code that evaluates to a type
340365 }
0 commit comments