Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make option for adding strictness to TH-generated fields #2504

Merged
merged 2 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dhall/src/Dhall/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ toConstructor
toConstructor GenerateOptions{..} haskellTypes outerTypeName (constructorName, maybeAlternativeType) = do
let name = Syntax.mkName (Text.unpack $ constructorModifier constructorName)

let bang = Bang NoSourceUnpackedness NoSourceStrictness
let strictness = if makeStrict then SourceStrict else NoSourceStrictness

let bang = Bang NoSourceUnpackedness strictness

case maybeAlternativeType of
Just dhallType
Expand Down Expand Up @@ -400,6 +402,8 @@ data GenerateOptions = GenerateOptions
-- ^ Generate a `FromDhall` instance for the Haskell type
, generateToDhallInstance :: Bool
-- ^ Generate a `ToDhall` instance for the Haskell type
, makeStrict :: Bool
-- ^ Make all fields strict.
}

-- | A default set of options used by `makeHaskellTypes`. That means:
Expand All @@ -412,6 +416,7 @@ defaultGenerateOptions = GenerateOptions
, fieldModifier = id
, generateFromDhallInstance = True
, generateToDhallInstance = True
, makeStrict = False
}

-- | This function generates `Dhall.InterpretOptions` that can be used for the
Expand Down
16 changes: 12 additions & 4 deletions dhall/tests/Dhall/Test/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

module Dhall.Test.TH where

import Control.Exception (throwIO)
import Data.Either.Validation (Validation(..))
import Dhall.TH (HaskellType (..))
import Test.Tasty (TestTree)
import Control.Exception (throwIO)
import Data.Either.Validation (Validation (..))
import Dhall.TH (HaskellType (..))
import Test.Tasty (TestTree)

import qualified Data.Text
import qualified Dhall
Expand Down Expand Up @@ -153,3 +153,11 @@ Dhall.TH.makeHaskellTypesWith (Dhall.TH.defaultGenerateOptions
deriving instance Dhall.Generic NoInstancesT
instance Dhall.FromDhall NoInstancesT
instance Dhall.ToDhall NoInstancesT

Dhall.TH.makeHaskellTypesWith (Dhall.TH.defaultGenerateOptions
{ Dhall.TH.constructorModifier = ("Strict" <>)
, Dhall.TH.fieldModifier = ("strict" <>) . Data.Text.toTitle
, Dhall.TH.makeStrict = True
})
[ MultipleConstructors "StrictFields" "./tests/th/example.dhall"
Gabriella439 marked this conversation as resolved.
Show resolved Hide resolved
]