Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions dhall/src/Dhall/Marshal/Decode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -61,6 +62,7 @@ module Dhall.Marshal.Decode
, localTime
, zonedTime
, utcTime
, dayOfWeek
-- ** Containers
, maybe
, pair
Expand Down Expand Up @@ -151,6 +153,7 @@ import Dhall.Syntax
( Chunks (..)
, DhallDouble (..)
, Expr (..)
, FieldSelection (..)
, FunctionBinding (..)
, Var (..)
)
Expand Down Expand Up @@ -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`.
-}
Expand Down Expand Up @@ -999,6 +1005,37 @@ zonedTime = record $
utcTime :: Decoder Time.UTCTime
utcTime = Time.zonedTimeToUTC <$> zonedTime

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"
Expand Down
31 changes: 31 additions & 0 deletions dhall/src/Dhall/Marshal/Encode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down