Skip to content

Commit 8ab5529

Browse files
FromDhall / ToDhall instances for temporal values (#2294)
* `FromDhall` / `ToDhall` instances for temporal values Fixes #2286 * Add missing `FromDhall` instances … as caught by @sjakobi Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 355a2ad commit 8ab5529

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

dhall/src/Dhall/Marshal/Decode.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ module Dhall.Marshal.Decode
5656
, string
5757
, lazyText
5858
, strictText
59+
-- ** Time
60+
, timeOfDay
61+
, day
62+
, timeZone
5963
-- ** Containers
6064
, maybe
6165
, pair
@@ -168,6 +172,7 @@ import qualified Data.Sequence
168172
import qualified Data.Set
169173
import qualified Data.Text
170174
import qualified Data.Text.Lazy
175+
import qualified Data.Time as Time
171176
import qualified Data.Vector
172177
import qualified Dhall.Core as Core
173178
import qualified Dhall.Map
@@ -312,6 +317,15 @@ instance FromDhall a => FromDhall [a] where
312317
instance FromDhall a => FromDhall (Vector a) where
313318
autoWith opts = vector (autoWith opts)
314319

320+
instance FromDhall Time.TimeOfDay where
321+
autoWith _ = timeOfDay
322+
323+
instance FromDhall Time.Day where
324+
autoWith _ = day
325+
326+
instance FromDhall Time.TimeZone where
327+
autoWith _ = timeZone
328+
315329
{-| Note that this instance will throw errors in the presence of duplicates in
316330
the list. To ignore duplicates, use `setIgnoringDuplicates`.
317331
-}
@@ -897,6 +911,45 @@ strictText = Decoder {..}
897911

898912
expected = pure Text
899913

914+
{-| Decode `Time.TimeOfDay`
915+
916+
>>> input timeOfDay "00:00:00"
917+
00:00:00
918+
-}
919+
timeOfDay :: Decoder Time.TimeOfDay
920+
timeOfDay = Decoder {..}
921+
where
922+
extract (TimeLiteral t _) = pure t
923+
extract expr = typeError expected expr
924+
925+
expected = pure Time
926+
927+
{-| Decode `Time.Day`
928+
929+
>>> input day "2000-01-01"
930+
2000-01-01
931+
-}
932+
day :: Decoder Time.Day
933+
day = Decoder {..}
934+
where
935+
extract (DateLiteral d) = pure d
936+
extract expr = typeError expected expr
937+
938+
expected = pure Date
939+
940+
{-| Decode `Time.TimeZone`
941+
942+
>>> input timeZone "+00:00"
943+
+0000
944+
-}
945+
timeZone :: Decoder Time.TimeZone
946+
timeZone = Decoder {..}
947+
where
948+
extract (TimeZoneLiteral z) = pure z
949+
extract expr = typeError expected expr
950+
951+
expected = pure TimeZone
952+
900953
{-| Decode a `Maybe`.
901954
902955
>>> input (maybe natural) "Some 1"

dhall/src/Dhall/Marshal/Encode.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import qualified Data.Sequence
8282
import qualified Data.Set
8383
import qualified Data.Text
8484
import qualified Data.Text.Lazy
85+
import qualified Data.Time as Time
8586
import qualified Data.Vector
8687
import qualified Data.Void
8788
import qualified Dhall.Core as Core
@@ -311,6 +312,27 @@ instance ToDhall a => ToDhall [a] where
311312
instance ToDhall a => ToDhall (Vector a) where
312313
injectWith = fmap (contramap Data.Vector.toList) injectWith
313314

315+
instance ToDhall Time.TimeOfDay where
316+
injectWith _ = Encoder {..}
317+
where
318+
embed timeOfDay = TimeLiteral timeOfDay 12
319+
320+
declared = Time
321+
322+
instance ToDhall Time.Day where
323+
injectWith _ = Encoder {..}
324+
where
325+
embed = DateLiteral
326+
327+
declared = Date
328+
329+
instance ToDhall Time.TimeZone where
330+
injectWith _ = Encoder {..}
331+
where
332+
embed = TimeZoneLiteral
333+
334+
declared = TimeZone
335+
314336
{-| Note that the output list will be sorted.
315337
316338
>>> let x = Data.Set.fromList ["mom", "hi" :: Text]

0 commit comments

Comments
 (0)