You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.0 KiB
32 lines
1.0 KiB
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
module Worldcup.Events (Events, Event(..), parseEvents, lookupEvent) where
|
|
|
|
import Data.Aeson
|
|
import Data.Aeson.Types as AET
|
|
import Data.HashMap.Strict as HM
|
|
import Data.Maybe as M
|
|
import Prelude as P
|
|
|
|
type Events = HashMap Int Event
|
|
|
|
data Event = Event {
|
|
eventId :: Int,
|
|
eventName :: String } deriving (Show)
|
|
|
|
instance FromJSON Event where
|
|
parseJSON = withObject "event" $ \o -> do
|
|
eventId <- o .: "id"
|
|
eventName <- o .: "key"
|
|
return Event{..}
|
|
|
|
lookupEvent :: Int -> Events -> Event
|
|
lookupEvent _id es = M.fromMaybe (Event 999 "DNE") (HM.lookup _id es)
|
|
|
|
parseEvents :: Either String [Value] -> Events
|
|
parseEvents (Left x) = error x
|
|
parseEvents (Right xs) = P.foldl reduce HM.empty xs where
|
|
reduce acc x = case (parseEither parseJSON x :: Either String Event) of
|
|
(Left s) -> error s
|
|
(Right v) -> HM.insert (eventId v) v acc
|
|
|