Skip to content

Commit ffd1ba9

Browse files
authored
add TimeZone/Date/TimeOfDay to TH (#2512)
1 parent 61c5be9 commit ffd1ba9

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

dhall/src/Dhall/TH.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import qualified Data.List as List
4747
import qualified Data.Map as Map
4848
import qualified Data.Set as Set
4949
import qualified Data.Text as Text
50+
import qualified Data.Time as Time
5051
import qualified Data.Typeable as Typeable
5152
import qualified Dhall
5253
import qualified Dhall.Core as Core
@@ -145,6 +146,9 @@ toNestedHaskellType typeParams haskellTypes = loop
145146
, "• ❰Integer❱ \n"
146147
, "• ❰Natural❱ \n"
147148
, "• ❰Text❱ \n"
149+
, "• ❰Date❱ \n"
150+
, "• ❰TimeOfDay❱ \n"
151+
, "• ❰TimeZone❱ \n"
148152
, "• ❰List a❱ (where ❰a❱ is also a valid nested type) \n"
149153
, "• ❰Optional a❱ (where ❰a❱ is also a valid nested type) \n"
150154
, "• Another matching datatype declaration \n"
@@ -175,6 +179,15 @@ toNestedHaskellType typeParams haskellTypes = loop
175179
Text ->
176180
return (ConT ''Text)
177181

182+
Date ->
183+
return (ConT ''Time.Day)
184+
185+
Time ->
186+
return (ConT ''Time.TimeOfDay)
187+
188+
TimeZone ->
189+
return (ConT ''Time.TimeZone)
190+
178191
App List dhallElementType -> do
179192
haskellElementType <- loop dhallElementType
180193

dhall/tests/Dhall/Test/TH.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Dhall.Test.TH where
1010

1111
import Control.Exception (throwIO)
1212
import Data.Either.Validation (Validation (..))
13+
import Data.Time (TimeOfDay (..), TimeZone (..), fromGregorian)
1314
import Dhall.TH (HaskellType (..))
1415
import Test.Tasty (TestTree)
1516

@@ -27,6 +28,7 @@ deriving instance Show T
2728
Dhall.TH.makeHaskellTypes
2829
[ MultipleConstructors "Department" "./tests/th/Department.dhall"
2930
, SingleConstructor "Employee" "MakeEmployee" "./tests/th/Employee.dhall"
31+
, SingleConstructor "TimeExample" "TimeExample" "./tests/th/Time.dhall"
3032
]
3133

3234
deriving instance Eq Department
@@ -35,6 +37,9 @@ deriving instance Show Department
3537
deriving instance Eq Employee
3638
deriving instance Show Employee
3739

40+
deriving instance Eq TimeExample
41+
deriving instance Show TimeExample
42+
3843
Dhall.TH.makeHaskellTypes
3944
[ SingleConstructor "Bar" "MakeBar" "(./tests/th/issue2066.dhall).Bar"
4045
, SingleConstructor "Foo" "MakeFoo" "(./tests/th/issue2066.dhall).Foo"
@@ -75,6 +80,16 @@ makeHaskellTypeFromUnion = Tasty.HUnit.testCase "makeHaskellTypeFromUnion" $ do
7580

7681
Tasty.HUnit.assertEqual "" qux (Foo MakeFoo{ foo = 2, bar = MakeBar{ baz = 3 } })
7782

83+
timex <- Dhall.input Dhall.auto "let T = ./tests/th/Time.dhall in { txTime = 21:12:00, txDate = 1976-04-01, txTimeZone = +05:00 } : T"
84+
85+
Tasty.HUnit.assertEqual "" timex TimeExample { txTime = tod, txDate = day, txTimeZone = tz}
86+
87+
where
88+
tod = TimeOfDay { todHour = 21, todMin = 12, todSec = 0 }
89+
day = fromGregorian 1976 4 1
90+
tz = TimeZone { timeZoneMinutes = 300, timeZoneSummerOnly = False, timeZoneName = "" }
91+
92+
7893
Dhall.TH.makeHaskellTypesWith (Dhall.TH.defaultGenerateOptions
7994
{ Dhall.TH.constructorModifier = ("My" <>)
8095
, Dhall.TH.fieldModifier = ("my" <>) . Data.Text.toTitle

dhall/tests/th/Time.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ txTime : Time, txDate : Date, txTimeZone : TimeZone }

0 commit comments

Comments
 (0)