Skip to content

Commit b8fd86e

Browse files
committed
Dhall.TH: Configurable {From,To}Dhall instances
This commit adds two fields to both constructors of `Dhall.TH.HaskellType`: One flag to control whether a `FromDhall` instance will be generated and one to control whether a `ToDhall` instance will be generated.
1 parent fa11f0c commit b8fd86e

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

dhall/src/Dhall/TH.hs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
189192
toDeclaration
@@ -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{..} =
245253
toDeclaration 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{..} ]
310325
makeHaskellTypeFromUnion
311326
:: Text
312327
-- ^ Name of the generated Haskell type
313328
-> Text
314329
-- ^ Dhall code that evaluates to a union type
315330
-> Q [Dec]
316331
makeHaskellTypeFromUnion 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
320337
data 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
}

dhall/tests/Dhall/Test/TH.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ deriving instance Eq T
2121
deriving instance Show T
2222

2323
Dhall.TH.makeHaskellTypes
24-
[ MultipleConstructors "Department" "./tests/th/Department.dhall"
25-
, SingleConstructor "Employee" "MakeEmployee" "./tests/th/Employee.dhall"
24+
[ MultipleConstructors "Department" True True "./tests/th/Department.dhall"
25+
, SingleConstructor "Employee" "MakeEmployee" True True "./tests/th/Employee.dhall"
2626
]
2727

2828
deriving instance Eq Department
@@ -32,9 +32,9 @@ deriving instance Eq Employee
3232
deriving instance Show Employee
3333

3434
Dhall.TH.makeHaskellTypes
35-
[ SingleConstructor "Bar" "MakeBar" "(./tests/th/issue2066.dhall).Bar"
36-
, SingleConstructor "Foo" "MakeFoo" "(./tests/th/issue2066.dhall).Foo"
37-
, MultipleConstructors "Qux" "(./tests/th/issue2066.dhall).Qux"
35+
[ SingleConstructor "Bar" "MakeBar" True True "(./tests/th/issue2066.dhall).Bar"
36+
, SingleConstructor "Foo" "MakeFoo" True True "(./tests/th/issue2066.dhall).Foo"
37+
, MultipleConstructors "Qux" True True "(./tests/th/issue2066.dhall).Qux"
3838
]
3939

4040
deriving instance Eq Bar

0 commit comments

Comments
 (0)