module TD.Data.Date
( Date(..)
, defaultDate
) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
data Date
= Date
{ Date -> Maybe Int
day :: Maybe Int
, Date -> Maybe Int
month :: Maybe Int
, Date -> Maybe Int
year :: Maybe Int
}
deriving (Date -> Date -> Bool
(Date -> Date -> Bool) -> (Date -> Date -> Bool) -> Eq Date
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Date -> Date -> Bool
== :: Date -> Date -> Bool
$c/= :: Date -> Date -> Bool
/= :: Date -> Date -> Bool
Eq, Int -> Date -> ShowS
[Date] -> ShowS
Date -> String
(Int -> Date -> ShowS)
-> (Date -> String) -> ([Date] -> ShowS) -> Show Date
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Date -> ShowS
showsPrec :: Int -> Date -> ShowS
$cshow :: Date -> String
show :: Date -> String
$cshowList :: [Date] -> ShowS
showList :: [Date] -> ShowS
Show)
instance I.ShortShow Date where
shortShow :: Date -> String
shortShow Date
{ day :: Date -> Maybe Int
day = Maybe Int
day_
, month :: Date -> Maybe Int
month = Maybe Int
month_
, year :: Date -> Maybe Int
year = Maybe Int
year_
}
= String
"Date"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"day" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
day_
, String
"month" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
month_
, String
"year" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
year_
]
instance AT.FromJSON Date where
parseJSON :: Value -> Parser Date
parseJSON v :: Value
v@(AT.Object Object
obj) = do
String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String
case String
t of
String
"date" -> Value -> Parser Date
parseDate Value
v
String
_ -> Parser Date
forall a. Monoid a => a
mempty
where
parseDate :: A.Value -> AT.Parser Date
parseDate :: Value -> Parser Date
parseDate = String -> (Object -> Parser Date) -> Value -> Parser Date
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Date" ((Object -> Parser Date) -> Value -> Parser Date)
-> (Object -> Parser Date) -> Value -> Parser Date
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
day_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"day"
Maybe Int
month_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"month"
Maybe Int
year_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"year"
Date -> Parser Date
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Date -> Parser Date) -> Date -> Parser Date
forall a b. (a -> b) -> a -> b
$ Date
{ day :: Maybe Int
day = Maybe Int
day_
, month :: Maybe Int
month = Maybe Int
month_
, year :: Maybe Int
year = Maybe Int
year_
}
parseJSON Value
_ = Parser Date
forall a. Monoid a => a
mempty
instance AT.ToJSON Date where
toJSON :: Date -> Value
toJSON Date
{ day :: Date -> Maybe Int
day = Maybe Int
day_
, month :: Date -> Maybe Int
month = Maybe Int
month_
, year :: Date -> Maybe Int
year = Maybe Int
year_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"date"
, Key
"day" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
day_
, Key
"month" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
month_
, Key
"year" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
year_
]
defaultDate :: Date
defaultDate :: Date
defaultDate =
Date
{ day :: Maybe Int
day = Maybe Int
forall a. Maybe a
Nothing
, month :: Maybe Int
month = Maybe Int
forall a. Maybe a
Nothing
, year :: Maybe Int
year = Maybe Int
forall a. Maybe a
Nothing
}