1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE QuasiQuotes #-}
6 {-# LANGUAGE RecordWildCards #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE StandaloneDeriving #-}
9 {-# LANGUAGE TemplateHaskell #-}
10 {-# LANGUAGE TypeFamilies #-}
12 -- | Parse TSN XML for the DTD "newsxml.dtd". Each document contains a
13 -- root element \<message\> that contains an entire news item.
20 import Data.Data ( Data, constrFields, dataTypeConstrs, dataTypeOf )
21 import Data.List.Utils ( join, split )
22 import Data.Tuple.Curry ( uncurryN )
23 import Data.Typeable ( Typeable )
24 import Database.Groundhog (
25 defaultMigrationLogger,
29 import Database.Groundhog.Core ( DefaultKey )
30 import Database.Groundhog.TH (
33 import Test.Tasty ( TestTree, testGroup )
34 import Test.Tasty.HUnit ( (@?=), testCase )
35 import Text.XML.HXT.Core (
52 tsn_db_field_namer -- Used in a test.
54 import TSN.DbImport ( DbImport(..), ImportResult(..) )
55 import Xml ( ToFromXml(..), pickle_unpickle )
59 -- | The database type for teams as they show up in the news. We need
60 -- this separate from its XML representation because of the
61 -- DefaultKey pointing to a message. We don't know how to create one
62 -- of those unless we've just inserted a message into the database,
63 -- so it screws up pickling.
66 nt_news_id :: DefaultKey Message, -- ^ foreign key.
67 db_team_name :: String }
68 deriving instance Eq NewsTeam -- Standalone instances necessary for
69 deriving instance Show NewsTeam -- Groundhog types with DefaultKeys
71 -- | The XML type for teams as they show up in the news. See
72 -- 'NewsTeam' for why there are two types.
75 xml_team_name :: String }
78 -- | Specify how to convert between the two representations NewsTeam
79 -- (database) and NewsTeamXml (XML).
80 instance ToFromXml NewsTeam where
81 type Xml NewsTeam = NewsTeamXml
82 type Container NewsTeam = Message
83 -- Use a record wildcard here so GHC doesn't complain that we never
84 -- used our named fields.
85 to_xml (NewsTeam {..}) = NewsTeamXml db_team_name
86 -- We can't create a DefaultKey Message...
87 from_xml = error "Called from_xml on a NewsTeam."
88 -- unless we're handed one.
89 from_xml_fk key = (NewsTeam key) . xml_team_name
92 -- | The database type for locations as they show up in the news. We
93 -- need this separate from its XML representation because of the
94 -- DefaultKey pointing to a message. We don't know how to create one
95 -- of those unless we've just inserted a message into the database,
96 -- so it screws up pickling.
99 loc_news_id :: DefaultKey Message, -- ^ foreign key.
102 db_country :: String }
103 deriving instance Eq NewsLocation -- Standalone instances necessary for
104 deriving instance Show NewsLocation -- Groundhog types with DefaultKeys
106 -- | The XML type for locations as they show up in the news. See
107 -- 'NewsLocation' for why there are two types.
108 data NewsLocationXml =
112 xml_country :: String }
116 -- | Specify how to convert between the two representations
117 -- NewsLocation (database) and NewsLocationXml (XML).
118 instance ToFromXml NewsLocation where
119 type Xml NewsLocation = NewsLocationXml
120 type Container NewsLocation = Message
121 -- Use a record wildcard here so GHC doesn't complain that we never
122 -- used our named fields.
123 to_xml (NewsLocation {..}) = NewsLocationXml db_city db_state db_country
124 -- We can't create a DefaultKey Message...
125 from_xml = error "Called from_xml on a NewsLocation."
126 -- unless we're given one.
127 from_xml_fk key (NewsLocationXml x y z) = NewsLocation key x y z
130 -- | The msg_id child of <message> contains an event_id attribute; we
131 -- embed it into the 'Message' type. We (pointlessly) use the "db_"
132 -- prefix here so that the two names collide on "id" when Groundhog
133 -- is creating its fields using our field namer.
137 db_event_id :: Maybe Int }
138 deriving (Data, Eq, Show, Typeable)
143 xml_xml_file_id :: Int,
144 xml_heading :: String,
146 xml_category :: String,
149 xml_teams :: [NewsTeamXml],
150 xml_locations :: [NewsLocationXml],
153 xml_continue :: String,
154 xml_time_stamp :: String }
164 db_continue :: String }
165 deriving (Data, Eq, Show, Typeable)
167 instance ToFromXml Message where
168 type Xml Message = MessageXml
169 type Container Message = ()
171 -- Use a record wildcard here so GHC doesn't complain that we never
172 -- used our named fields.
173 to_xml (Message {..}) =
188 -- We don't need the key argument (from_xml_fk) since the XML type
189 -- contains more information in this case.
190 from_xml (MessageXml _ _ c _ e f _ _ g h i _) =
194 mkPersist tsn_codegen_config [groundhog|
198 - entity: NewsLocation
199 dbName: news_locations
208 - {name: msg_id, dbName: msg_id}
209 - {name: event_id, dbName: event_id}
218 pickle_news_team :: PU NewsTeamXml
221 xpWrap (from_string, to_string) xpText
223 to_string :: NewsTeamXml -> String
224 to_string = xml_team_name
226 from_string :: String -> NewsTeamXml
227 from_string = NewsTeamXml
229 instance XmlPickler NewsTeamXml where
230 xpickle = pickle_news_team
232 pickle_msg_id :: PU MsgId
235 xpWrap (from_tuple, to_tuple) $
236 xpPair xpPrim (xpAttr "EventId" (xpOption xpPrim))
238 from_tuple = uncurryN MsgId
239 to_tuple m = (db_msg_id m, db_event_id m)
241 instance XmlPickler MsgId where
242 xpickle = pickle_msg_id
244 pickle_location :: PU NewsLocationXml
247 xpWrap (from_tuple, to_tuple) $
248 xpTriple (xpElem "city" xpText)
249 (xpElem "state" xpText)
250 (xpElem "country" xpText)
253 uncurryN NewsLocationXml
254 to_tuple l = (xml_city l, xml_state l, xml_country l)
256 instance XmlPickler NewsLocationXml where
257 xpickle = pickle_location
260 pickle_message :: PU MessageXml
263 xpWrap (from_tuple, to_tuple) $
264 xp12Tuple (xpElem "XML_File_ID" xpPrim)
265 (xpElem "heading" xpText)
267 (xpElem "category" xpText)
268 (xpElem "sport" xpText)
269 (xpElem "url" xpText)
270 (xpList $ pickle_news_team)
271 (xpList $ pickle_location)
272 (xpElem "SMS" xpText)
273 (xpElem "text" xpText)
275 (xpElem "time_stamp" xpText)
277 from_tuple = uncurryN MessageXml
278 to_tuple m = (xml_xml_file_id m,
291 pickle_continue :: PU String
293 xpWrap (to_string, from_string) $
295 (xpList $ xpElem "P" xpText)
297 from_string :: String -> [String]
298 from_string = split "\n"
300 to_string :: [String] -> String
301 to_string = join "\n"
303 instance XmlPickler MessageXml where
304 xpickle = pickle_message
308 instance DbImport Message where
310 runMigration defaultMigrationLogger $ do
311 migrate (undefined :: Message)
312 migrate (undefined :: NewsTeam)
313 migrate (undefined :: NewsLocation)
314 let root_element = unpickleDoc xpickle xml :: Maybe MessageXml
317 let errmsg = "Could not unpickle News message in dbimport."
320 news_id <- insert (from_xml message :: Message)
321 let nts :: [NewsTeam] = map (from_xml_fk news_id)
323 let nlocs :: [NewsLocation] = map (from_xml_fk news_id)
324 (xml_locations message)
325 nt_ids <- mapM insert nts
326 loc_ids <- mapM insert nlocs
328 return $ Succ (1 + (length nt_ids) + (length loc_ids))
332 news_tests :: TestTree
336 [ test_news_fields_have_correct_names,
337 test_pickle_of_unpickle_is_identity ]
340 test_pickle_of_unpickle_is_identity :: TestTree
341 test_pickle_of_unpickle_is_identity =
342 testCase "pickle composed with unpickle is the identity" $ do
343 let path = "test/xml/newsxml.xml"
344 (expected :: [MessageXml], actual) <- pickle_unpickle "message" path
348 test_news_fields_have_correct_names :: TestTree
349 test_news_fields_have_correct_names =
350 testCase "news fields get correct database names" $ do
351 mapM_ check (zip actual expected)
353 -- This is cool, it uses the (derived) Data instance of
354 -- News.Message to get its constructor names.
355 field_names :: [String]
357 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: Message)
361 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
364 actual = ["mid", "sport", "url", "sms", "text", "continue"]
366 check (x,y) = (x @?= y)