diff --git a/dhall/src/Dhall/Marshal/Decode.hs b/dhall/src/Dhall/Marshal/Decode.hs index c3f996275..0398382b7 100644 --- a/dhall/src/Dhall/Marshal/Decode.hs +++ b/dhall/src/Dhall/Marshal/Decode.hs @@ -7,6 +7,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} @@ -61,6 +62,7 @@ module Dhall.Marshal.Decode , localTime , zonedTime , utcTime + , dayOfWeek -- ** Containers , maybe , pair @@ -151,6 +153,7 @@ import Dhall.Syntax ( Chunks (..) , DhallDouble (..) , Expr (..) + , FieldSelection (..) , FunctionBinding (..) , Var (..) ) @@ -336,6 +339,9 @@ instance FromDhall Time.ZonedTime where instance FromDhall Time.UTCTime where autoWith _ = utcTime +instance FromDhall Time.DayOfWeek where + autoWith _ = dayOfWeek + {-| Note that this instance will throw errors in the presence of duplicates in the list. To ignore duplicates, use `setIgnoringDuplicates`. -} @@ -999,6 +1005,42 @@ zonedTime = record $ utcTime :: Decoder Time.UTCTime utcTime = Time.zonedTimeToUTC <$> zonedTime +{-| Decode `Time.DayOfWeek` + +>>> input dayOfWeek "< Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday >.Monday" +Monday +-} +dayOfWeek :: Decoder Time.DayOfWeek +dayOfWeek = Decoder{..} + where + extract expr@(Field _ FieldSelection{ fieldSelectionLabel }) = + case fieldSelectionLabel of + "Sunday" -> pure Time.Sunday + "Monday" -> pure Time.Monday + "Tuesday" -> pure Time.Tuesday + "Wednesday" -> pure Time.Wednesday + "Thursday" -> pure Time.Thursday + "Friday" -> pure Time.Friday + "Saturday" -> pure Time.Saturday + _ -> typeError expected expr + extract expr = + typeError expected expr + + expected = + pure + (Union + (Dhall.Map.fromList + [ ("Sunday", Nothing) + , ("Monday", Nothing) + , ("Tuesday", Nothing) + , ("Wednesday", Nothing) + , ("Thursday", Nothing) + , ("Friday", Nothing) + , ("Saturday", Nothing) + ] + ) + ) + {-| 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 28246dce6..20a195dbb 100644 --- a/dhall/src/Dhall/Marshal/Encode.hs +++ b/dhall/src/Dhall/Marshal/Encode.hs @@ -382,6 +382,37 @@ instance ToDhall Time.ZonedTime where instance ToDhall Time.UTCTime where injectWith = contramap (Time.utcToZonedTime Time.utc) . injectWith +instance ToDhall Time.DayOfWeek where + injectWith _ = Encoder{..} + where + embed Time.Sunday = + Field declared (Core.makeFieldSelection "Sunday") + embed Time.Monday = + Field declared (Core.makeFieldSelection "Monday" ) + embed Time.Tuesday = + Field declared (Core.makeFieldSelection "Tuesday") + embed Time.Wednesday = + Field declared (Core.makeFieldSelection "Wednesday") + embed Time.Thursday = + Field declared (Core.makeFieldSelection "Thursday") + embed Time.Friday = + Field declared (Core.makeFieldSelection "Friday") + embed Time.Saturday = + Field declared (Core.makeFieldSelection "Saturday") + + declared = + Union + (Dhall.Map.fromList + [ ("Sunday", Nothing) + , ("Monday", Nothing) + , ("Tuesday", Nothing) + , ("Wednesday", Nothing) + , ("Thursday", Nothing) + , ("Friday", Nothing) + , ("Saturday", Nothing) + ] + ) + {-| Note that the output list will be sorted. >>> let x = Data.Set.fromList ["mom", "hi" :: Text]