From 73403d9c236cce99608b08faca9e599fb0fb099d Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Mon, 23 Aug 2021 21:02:06 -0700 Subject: [PATCH 1/2] `FromDhall` / `ToDhall` instances for temporal values Fixes https://github.com/dhall-lang/dhall-haskell/issues/2286 --- dhall/src/Dhall/Marshal/Decode.hs | 44 +++++++++++++++++++++++++++++++ dhall/src/Dhall/Marshal/Encode.hs | 22 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/dhall/src/Dhall/Marshal/Decode.hs b/dhall/src/Dhall/Marshal/Decode.hs index 6bbdffc9a..8ceb5cafd 100644 --- a/dhall/src/Dhall/Marshal/Decode.hs +++ b/dhall/src/Dhall/Marshal/Decode.hs @@ -56,6 +56,10 @@ module Dhall.Marshal.Decode , string , lazyText , strictText + -- ** Time + , timeOfDay + , day + , timeZone -- ** Containers , maybe , pair @@ -164,6 +168,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 @@ -893,6 +898,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" diff --git a/dhall/src/Dhall/Marshal/Encode.hs b/dhall/src/Dhall/Marshal/Encode.hs index 4cca05f6b..545ae6f82 100644 --- a/dhall/src/Dhall/Marshal/Encode.hs +++ b/dhall/src/Dhall/Marshal/Encode.hs @@ -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 @@ -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] From 4946c115f90dddb5fd8421141cc4aad87fa468d6 Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Wed, 1 Sep 2021 21:40:26 -0700 Subject: [PATCH 2/2] Add missing `FromDhall` instances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … as caught by @sjakobi --- dhall/src/Dhall/Marshal/Decode.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dhall/src/Dhall/Marshal/Decode.hs b/dhall/src/Dhall/Marshal/Decode.hs index 8ceb5cafd..d66338902 100644 --- a/dhall/src/Dhall/Marshal/Decode.hs +++ b/dhall/src/Dhall/Marshal/Decode.hs @@ -313,6 +313,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`. -}