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.
40 lines
1.1 KiB
40 lines
1.1 KiB
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
module Worldcup.Games (Game(..), parseGames) where
|
|
|
|
import Data.Aeson
|
|
import Data.Aeson.Types
|
|
|
|
data Game = Game {
|
|
roundId :: Int,
|
|
playAt :: String,
|
|
team1Id :: Int,
|
|
team2Id :: Int,
|
|
score1 :: Value,
|
|
score2 :: Value,
|
|
score1et :: Value,
|
|
score2et :: Value,
|
|
score1p :: Value,
|
|
score2p :: Value } deriving (Show)
|
|
|
|
instance FromJSON Game where
|
|
parseJSON = withObject "game" $ \o -> do
|
|
roundId <- o .: "round_id"
|
|
team1Id <- o .: "team1_id"
|
|
team2Id <- o .: "team2_id"
|
|
playAt <- o .: "play_at"
|
|
score1 <- o .: "score1"
|
|
score2 <- o .: "score2"
|
|
score1et <- o .: "score1et"
|
|
score2et <- o .: "score2et"
|
|
score1p <- o .: "score1p"
|
|
score2p <- o .: "score2p"
|
|
return Game{..}
|
|
|
|
parseGames :: Either String [Value] -> [Game]
|
|
parseGames (Left x) = error x
|
|
parseGames (Right xs) = foldl reduce [] xs where
|
|
reduce acc x = case (parseEither parseJSON x :: Either String Game) of
|
|
(Left s) -> error s
|
|
(Right v) -> v : acc
|
|
|