1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | Parse TSN XML for the DTD "Injuries_Detail_XML.dtd". Each
9 -- document contains a root element \<message\> that in turn
10 -- contains zero or more \<Listing\>s (note: capitalization). The
11 -- \<Listing\>s contain \<PlayerListing\>s then contain the real
12 -- meat; everything contained in the parent \<Listing\> can also be
13 -- found within the \<PlayerListing\>s.
15 -- The player listings will be mapped to a database table called
16 -- "injuries_detail" automatically. The root "message" and "listing"
19 module TSN.XML.InjuriesDetail (
21 injuries_detail_tests )
24 import Data.Time ( UTCTime )
25 import Data.Tuple.Curry ( uncurryN )
26 import Database.Groundhog (
28 import Database.Groundhog.TH (
32 import Test.Tasty ( TestTree, testGroup )
33 import Test.Tasty.HUnit ( (@?=), testCase )
34 import Text.XML.HXT.Core (
48 import TSN.Picklers( xp_date, xp_team_id )
49 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
50 import TSN.XmlImport ( XmlImport(..) )
51 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
63 fantasy :: String, -- ^ Nobody knows what this is.
65 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
69 instance FromXml PlayerListing where
70 type Db PlayerListing = PlayerListing
73 instance XmlImport PlayerListing
77 listing_team_id :: Int -- ^ Avoid conflict with PlayerListing's team_id
78 , full_name :: String, -- ^ Team full name
79 player_listings :: [PlayerListing] }
89 listings :: [Listing],
90 time_stamp :: String }
93 instance DbImport Message where
95 mapM_ insert_xml (concatMap player_listings $ listings msg)
96 return ImportSucceeded
98 dbmigrate _ = run_dbmigrate $ migrate (undefined :: PlayerListing)
100 mkPersist defaultCodegenConfig [groundhog|
101 - entity: PlayerListing
102 dbName: injuries_detail_player_listings
106 pickle_player_listing :: PU PlayerListing
107 pickle_player_listing =
108 xpElem "PlayerListing" $
109 xpWrap (from_tuple, to_tuple) $
110 xp10Tuple (xpElem "TeamID" xp_team_id)
111 (xpElem "PlayerID" xpInt)
112 (xpElem "Date" xp_date)
113 (xpElem "Pos" xpText)
114 (xpElem "Name" xpText)
115 (xpElem "Injury" xpText)
116 (xpElem "Status" xpText)
117 (xpElem "Fantasy" xpText0)
118 (xpElem "Injured" xpPrim)
119 (xpElem "Type" xpText)
121 from_tuple = uncurryN PlayerListing
122 to_tuple pl = (team_id pl,
133 instance XmlPickler PlayerListing where
134 xpickle = pickle_player_listing
136 pickle_listing :: PU Listing
139 xpWrap (from_tuple, to_tuple) $
140 xpTriple (xpElem "TeamID" xp_team_id)
141 (xpElem "FullName" xpText)
142 (xpList pickle_player_listing)
144 from_tuple = uncurryN Listing
145 to_tuple l = (listing_team_id l, full_name l, player_listings l)
147 instance XmlPickler Listing where
148 xpickle = pickle_listing
151 pickle_message :: PU Message
154 xpWrap (from_tuple, to_tuple) $
155 xp6Tuple (xpElem "XML_File_ID" xpInt)
156 (xpElem "heading" xpText)
157 (xpElem "category" xpText)
158 (xpElem "sport" xpText)
159 (xpList pickle_listing)
160 (xpElem "time_stamp" xpText)
162 from_tuple = uncurryN Message
163 to_tuple m = (xml_file_id m,
170 instance XmlPickler Message where
171 xpickle = pickle_message
175 injuries_detail_tests :: TestTree
176 injuries_detail_tests =
178 "InjuriesDetail tests"
179 [ test_pickle_of_unpickle_is_identity,
180 test_unpickle_succeeds ]
183 -- | Warning, succeess of this test does not mean that unpickling
185 test_pickle_of_unpickle_is_identity :: TestTree
186 test_pickle_of_unpickle_is_identity =
187 testCase "pickle composed with unpickle is the identity" $ do
188 let path = "test/xml/Injuries_Detail_XML.xml"
189 (expected, actual) <- pickle_unpickle pickle_message path
193 test_unpickle_succeeds :: TestTree
194 test_unpickle_succeeds =
195 testCase "unpickling succeeds" $ do
196 let path = "test/xml/Injuries_Detail_XML.xml"
197 actual <- unpickleable path pickle_message