{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} -- | Parse TSN XML for the DTD "Injuries_Detail_XML.dtd". Each -- document contains a root element \ that in turn -- contains zero or more \s (note: capitalization). The -- \s contain \s then contain the real -- meat; everything contained in the parent \ can also be -- found within the \s. -- -- The player listings will be mapped to a database table called -- "injuries_detail" automatically. The root "message" and "listing" -- are not retained. -- module TSN.XML.InjuriesDetail ( Message, injuries_detail_tests ) where import Data.Time ( UTCTime ) import Data.Tuple.Curry ( uncurryN ) import Database.Groundhog ( migrate ) import Database.Groundhog.TH ( defaultCodegenConfig, groundhog, mkPersist ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( PU, XmlPickler(..), xpTriple, xp6Tuple, xp10Tuple, xpElem, xpInt, xpList, xpPrim, xpText, xpText0, xpWrap ) import TSN.Picklers( xp_date, xp_team_id ) import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) import TSN.XmlImport ( XmlImport(..) ) import Xml ( FromXml(..), pickle_unpickle, unpickleable ) data PlayerListing = PlayerListing { team_id :: Int, player_id :: Int, date :: UTCTime, pos :: String, name :: String, injury :: String, status :: String, fantasy :: String, -- ^ Nobody knows what this is. injured :: Bool, injury_type :: String -- ^ "type" is a reserved keyword so we can't use it } deriving (Eq, Show) instance FromXml PlayerListing where type Db PlayerListing = PlayerListing from_xml = id instance XmlImport PlayerListing data Listing = Listing { listing_team_id :: Int -- ^ Avoid conflict with PlayerListing's team_id , full_name :: String, -- ^ Team full name player_listings :: [PlayerListing] } deriving (Eq, Show) data Message = Message { xml_file_id :: Int, heading :: String, category :: String, sport :: String, listings :: [Listing], time_stamp :: String } deriving (Eq, Show) instance DbImport Message where dbimport msg = do mapM_ insert_xml (concatMap player_listings $ listings msg) return ImportSucceeded dbmigrate _ = run_dbmigrate $ migrate (undefined :: PlayerListing) mkPersist defaultCodegenConfig [groundhog| - entity: PlayerListing dbName: injuries_detail_player_listings |] pickle_player_listing :: PU PlayerListing pickle_player_listing = xpElem "PlayerListing" $ xpWrap (from_tuple, to_tuple) $ xp10Tuple (xpElem "TeamID" xp_team_id) (xpElem "PlayerID" xpInt) (xpElem "Date" xp_date) (xpElem "Pos" xpText) (xpElem "Name" xpText) (xpElem "Injury" xpText) (xpElem "Status" xpText) (xpElem "Fantasy" xpText0) (xpElem "Injured" xpPrim) (xpElem "Type" xpText) where from_tuple = uncurryN PlayerListing to_tuple pl = (team_id pl, player_id pl, date pl, pos pl, name pl, injury pl, status pl, fantasy pl, injured pl, injury_type pl) instance XmlPickler PlayerListing where xpickle = pickle_player_listing pickle_listing :: PU Listing pickle_listing = xpElem "Listing" $ xpWrap (from_tuple, to_tuple) $ xpTriple (xpElem "TeamID" xp_team_id) (xpElem "FullName" xpText) (xpList pickle_player_listing) where from_tuple = uncurryN Listing to_tuple l = (listing_team_id l, full_name l, player_listings l) instance XmlPickler Listing where xpickle = pickle_listing pickle_message :: PU Message pickle_message = xpElem "message" $ xpWrap (from_tuple, to_tuple) $ xp6Tuple (xpElem "XML_File_ID" xpInt) (xpElem "heading" xpText) (xpElem "category" xpText) (xpElem "sport" xpText) (xpList pickle_listing) (xpElem "time_stamp" xpText) where from_tuple = uncurryN Message to_tuple m = (xml_file_id m, heading m, category m, sport m, listings m, time_stamp m) instance XmlPickler Message where xpickle = pickle_message -- * Tasty Tests injuries_detail_tests :: TestTree injuries_detail_tests = testGroup "InjuriesDetail tests" [ test_pickle_of_unpickle_is_identity, test_unpickle_succeeds ] -- | Warning, succeess of this test does not mean that unpickling -- succeeded. test_pickle_of_unpickle_is_identity :: TestTree test_pickle_of_unpickle_is_identity = testCase "pickle composed with unpickle is the identity" $ do let path = "test/xml/Injuries_Detail_XML.xml" (expected, actual) <- pickle_unpickle pickle_message path actual @?= expected test_unpickle_succeeds :: TestTree test_unpickle_succeeds = testCase "unpickling succeeds" $ do let path = "test/xml/Injuries_Detail_XML.xml" actual <- unpickleable path pickle_message let expected = True actual @?= expected