From 7b33eb87f59d46ec97d619f7521c067d8a6b9308 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 30 Mar 2015 15:16:05 -0400 Subject: [PATCH] Allow "TBA" laps in TSN.XML.AutoRacingSchedule. --- doc/TODO | 2 ++ src/TSN/Picklers.hs | 41 +++++++++++++++++++++++++++++++ src/TSN/XML/AutoRacingSchedule.hs | 8 +++--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/doc/TODO b/doc/TODO index 6748d67..9ae15d2 100644 --- a/doc/TODO +++ b/doc/TODO @@ -49,3 +49,5 @@ two copies of newsxml just because one has an empty attribute that we want to test: we could just delete an attribute from the first file. + +6. Update the Auto_Racing_Schedule_XML schema diagram. diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 3135d6a..4f0020b 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -14,6 +14,7 @@ module TSN.Picklers ( xp_earnings, xp_fracpart_only_double, xp_gamedate, + xp_tba_int, xp_tba_time, xp_time, xp_time_dots, @@ -451,6 +452,46 @@ xp_tba_time = from_time (Just t) = formatTime defaultTimeLocale time_format t +-- | (Un)pickle a 'Int', allowing for a value of \"TBA\" (which gets +-- translated to 'Nothing'). +-- +-- /Examples/: +-- +-- A failed parse will return 'Nothing': +-- +-- >>> let tn = text_node "YO" +-- >>> unpickleDoc xp_tba_int tn +-- Just Nothing +-- +-- And so will parsing a \"TBA\": +-- +-- >>> let tn = text_node "TBA" +-- >>> unpickleDoc xp_tba_int tn +-- Just Nothing +-- +-- But re-pickling 'Nothing' gives only \"TBA\": +-- +-- >>> pickleDoc xp_tba_int Nothing +-- NTree (XTag "/" []) [NTree (XText "TBA") []] +-- +-- A normal integer is also parsed successfully, of course: +-- +-- >>> let tn = text_node "110" +-- >>> unpickleDoc xp_tba_int tn +-- Just (Just 110) +-- +xp_tba_int :: PU (Maybe Int) +xp_tba_int = + (to_int, from_int) `xpWrap` xpText + where + to_int :: String -> Maybe Int + to_int = readMaybe + + from_int :: Maybe Int -> String + from_int Nothing = "TBA" + from_int (Just t) = show t + + -- | (Un)pickle the \ element format to/from a 'UTCTime'. -- The time_stamp elements look something like, diff --git a/src/TSN/XML/AutoRacingSchedule.hs b/src/TSN/XML/AutoRacingSchedule.hs index 64132bc..edfcaac 100644 --- a/src/TSN/XML/AutoRacingSchedule.hs +++ b/src/TSN/XML/AutoRacingSchedule.hs @@ -58,7 +58,7 @@ import Text.XML.HXT.Core ( import TSN.Codegen ( tsn_codegen_config ) import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) -import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp ) +import TSN.Picklers ( xp_date_padded, xp_tba_int, xp_tba_time, xp_time_stamp ) import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) ) import Xml ( Child(..), @@ -160,7 +160,7 @@ data AutoRacingScheduleListing = db_track_name :: String, db_location :: String, db_tv_listing :: Maybe String, - db_laps :: Int, + db_laps :: Maybe Int, db_track_length :: String -- ^ Sometimes the word "miles" shows up. } @@ -177,7 +177,7 @@ data AutoRacingScheduleListingXml = xml_track_name :: String, xml_location :: String, xml_tv_listing :: Maybe String, - xml_laps :: Int, + xml_laps :: Maybe Int, xml_track_length :: String, -- ^ Sometimes the word \"miles\" shows up, -- so we can't do the right thing and use -- a 'Double'. @@ -436,7 +436,7 @@ pickle_listing = (xpElem "TrackName" xpText) (xpElem "Location" xpText) (xpElem "TV_Listing" $ xpOption xpText) - (xpElem "Laps" xpInt) + (xpElem "Laps" xp_tba_int) (xpElem "TrackLength" xpText) (xpList pickle_race_results) where -- 2.43.2