xp11Tuple,
xp13Tuple,
xpAttr,
+ xpDefault,
xpElem,
xpInt,
xpList,
-- the \"db_\" prefix since our field namer is going to strip of
-- everything before the first underscore.
--
+-- We make the three fields optional because the entire
+-- \<Most_Laps_Leading\> is apparently optional (although it is
+-- usually present). A 'Nothing' in the XML should get turned into
+-- three 'Nothing's in the DB.
+--
data MostLapsLeading =
MostLapsLeading {
- db_most_laps_leading_driver_id :: Int,
- db_most_laps_leading_driver :: String,
- db_most_laps_leading_number_of_laps :: Int }
+ db_most_laps_leading_driver_id :: Maybe Int,
+ db_most_laps_leading_driver :: Maybe String,
+ db_most_laps_leading_number_of_laps :: Maybe Int }
deriving (Data, Eq, Show, Typeable)
-- | Database representation of a \<Race_Information\> contained
-- within a \<message\>.
--
+-- The 'db_most_laps_leading' field is not optional because when we
+-- convert from our XML representation, a missing 'MostLapsLeading'
+-- will be replaced with a 'MostLapsLeading' with three missing
+-- fields.
+--
data AutoRacingResultsRaceInformation =
AutoRacingResultsRaceInformation {
-- Note the apostrophe to disambiguate it from the
xml_cautions :: Maybe String,
xml_lead_changes :: Maybe String,
xml_lap_leaders :: Maybe String,
- xml_most_laps_leading :: MostLapsLeading }
+ xml_most_laps_leading :: Maybe MostLapsLeading }
deriving (Eq,Show)
db_cautions = xml_cautions,
db_lead_changes = xml_lead_changes,
db_lap_leaders = xml_lap_leaders,
- db_most_laps_leading = xml_most_laps_leading }
-
+ db_most_laps_leading = most_laps_leading }
+ where
+ -- If we didn't get a \<Most_Laps_Leading\>, indicate that in
+ -- the database with an (embedded) 'MostLapsLeading' with three
+ -- missing fields.
+ most_laps_leading =
+ case xml_most_laps_leading of
+ Just mll -> mll
+ Nothing -> MostLapsLeading Nothing Nothing Nothing
-- | This allows us to insert the XML representation
-- 'AutoRacingResultsRaceInformationXml' directly.
-- | Pickler for the \<Most_Laps_Leading\> child of a
--- \<Race_Information\>.
+-- \<Race_Information\>. This is complicated by the fact that the
+-- three fields we're trying to parse are not actually optional;
+-- only the entire \<Most_Laps_Leading\> is. So we always wrap what
+-- we parse in a 'Just', and when converting from the DB to XML,
+-- we'll drop the entire element if any of its fields are missing
+-- (which they never should be).
--
-pickle_most_laps_leading :: PU MostLapsLeading
+pickle_most_laps_leading :: PU (Maybe MostLapsLeading)
pickle_most_laps_leading =
xpElem "Most_Laps_Leading" $
xpWrap (from_tuple, to_tuple) $
- xpTriple (xpElem "DriverID" xpInt)
- (xpElem "Driver" xpText)
- (xpElem "NumberOfLaps" xpInt)
+ xpTriple (xpOption $ xpElem "DriverID" xpInt)
+ (xpOption $ xpElem "Driver" xpText)
+ (xpOption $ xpElem "NumberOfLaps" xpInt)
where
- from_tuple = uncurryN MostLapsLeading
- to_tuple m = (db_most_laps_leading_driver_id m,
- db_most_laps_leading_driver m,
- db_most_laps_leading_number_of_laps m)
+ from_tuple :: (Maybe Int, Maybe String, Maybe Int) -> Maybe MostLapsLeading
+ from_tuple (Just x, Just y, Just z) =
+ Just $ MostLapsLeading (Just x) (Just y) (Just z)
+ from_tuple _ = Nothing
+
+ -- Sure had to go out of my way to avoid the warnings about unused
+ -- db_most_laps_foo fields here.
+ to_tuple :: Maybe MostLapsLeading -> (Maybe Int, Maybe String, Maybe Int)
+ to_tuple Nothing = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading Nothing _ _)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading _ Nothing _)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading _ _ Nothing)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just m) = (db_most_laps_leading_driver_id m,
+ db_most_laps_leading_driver m,
+ db_most_laps_leading_number_of_laps m)
-- | Pickler for the \<Race_Information\> child of \<message\>.
--
+-- There's so much voodoo going on here. We have a double-layered
+-- Maybe on top of the MostLapsLeading. When unpickling, we return a
+-- Nothing (i.e. a Maybe MostLapsLeading) if any of its fields are
+-- missing. But if the entire element is missing, unpickling
+-- fails. 'xpOption' doesn't fix this because it would give us a
+-- Maybe (Maybe MostLapsLeading). But we can use 'xpDefault' with a
+-- default of (Nothing :: Maybe MostLapsLeading) to stick one in
+-- there if unpicking a (Maybe MostLapsLeading) fails because
+-- \<Most_Laps_Leading\> is missing.
+--
+-- Clear as mud, I know.
+--
pickle_race_information :: PU AutoRacingResultsRaceInformationXml
pickle_race_information =
xpElem "Race_Information" $
(xpOption $ xpElem "Cautions" xpText)
(xpOption $ xpElem "LeadChanges" xpText)
(xpOption $ xpElem "LapLeaders" xpText)
- pickle_most_laps_leading
+ (xpDefault Nothing pickle_most_laps_leading)
where
-- Derp. Since the first two are paired, we have to
-- manually unpack the bazillion arguments.
"test/xml/AutoRacingResultsXML.xml",
check "pickle composed with unpickle is the identity (fractional KPH)"
- "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+ check "pickle composed with unpickle is the identity (No Most_Laps_Leading)"
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml"]
where
check desc path = testCase desc $ do
(expected, actual) <- pickle_unpickle pickle_message path
"test/xml/AutoRacingResultsXML.xml",
check "unpickling succeeds (fractional KPH)"
- "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+ check "unpickling succeeds (no Most_Laps_Leading)"
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ]
where
check desc path = testCase desc $ do
actual <- unpickleable path pickle_message
"test/xml/AutoRacingResultsXML.xml",
check "deleting auto_racing_results deletes its children (fractional KPH)"
- "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+ check ("deleting auto_racing_results deletes its children " ++
+ "(No Most_Laps_Leading)")
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ]
where
check desc path = testCase desc $ do
results <- unsafe_unpickle path pickle_message