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

FromDhall / ToDhall instances for temporal values #2294

Merged
merged 4 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 53 additions & 0 deletions dhall/src/Dhall/Marshal/Decode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ module Dhall.Marshal.Decode
, string
, lazyText
, strictText
-- ** Time
, timeOfDay
, day
, timeZone
-- ** Containers
, maybe
, pair
Expand Down Expand Up @@ -168,6 +172,7 @@ import qualified Data.Sequence
import qualified Data.Set
import qualified Data.Text
import qualified Data.Text.Lazy
import qualified Data.Time as Time
import qualified Data.Vector
import qualified Dhall.Core as Core
import qualified Dhall.Map
Expand Down Expand Up @@ -312,6 +317,15 @@ instance FromDhall a => FromDhall [a] where
instance FromDhall a => FromDhall (Vector a) where
autoWith opts = vector (autoWith opts)

instance FromDhall Time.TimeOfDay where
autoWith _ = timeOfDay

instance FromDhall Time.Day where
autoWith _ = day

instance FromDhall Time.TimeZone where
autoWith _ = timeZone

{-| Note that this instance will throw errors in the presence of duplicates in
the list. To ignore duplicates, use `setIgnoringDuplicates`.
-}
Expand Down Expand Up @@ -897,6 +911,45 @@ strictText = Decoder {..}

expected = pure Text

{-| Decode `Time.TimeOfDay`

>>> input timeOfDay "00:00:00"
00:00:00
-}
timeOfDay :: Decoder Time.TimeOfDay
timeOfDay = Decoder {..}
where
extract (TimeLiteral t _) = pure t
extract expr = typeError expected expr

expected = pure Time

{-| Decode `Time.Day`

>>> input day "2000-01-01"
2000-01-01
-}
day :: Decoder Time.Day
day = Decoder {..}
where
extract (DateLiteral d) = pure d
extract expr = typeError expected expr

expected = pure Date

{-| Decode `Time.TimeZone`

>>> input timeZone "+00:00"
+0000
-}
timeZone :: Decoder Time.TimeZone
timeZone = Decoder {..}
where
extract (TimeZoneLiteral z) = pure z
extract expr = typeError expected expr

expected = pure TimeZone

{-| Decode a `Maybe`.

>>> input (maybe natural) "Some 1"
Expand Down
22 changes: 22 additions & 0 deletions dhall/src/Dhall/Marshal/Encode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import qualified Data.Sequence
import qualified Data.Set
import qualified Data.Text
import qualified Data.Text.Lazy
import qualified Data.Time as Time
import qualified Data.Vector
import qualified Data.Void
import qualified Dhall.Core as Core
Expand Down Expand Up @@ -311,6 +312,27 @@ instance ToDhall a => ToDhall [a] where
instance ToDhall a => ToDhall (Vector a) where
injectWith = fmap (contramap Data.Vector.toList) injectWith

instance ToDhall Time.TimeOfDay where
injectWith _ = Encoder {..}
where
embed timeOfDay = TimeLiteral timeOfDay 12

declared = Time

instance ToDhall Time.Day where
injectWith _ = Encoder {..}
where
embed = DateLiteral

declared = Date

instance ToDhall Time.TimeZone where
injectWith _ = Encoder {..}
where
embed = TimeZoneLiteral

declared = TimeZone

{-| Note that the output list will be sorted.

>>> let x = Data.Set.fromList ["mom", "hi" :: Text]
Expand Down