X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FAutoRacingResults.hs;h=e51fff852aabac0cb6c25ebc6c4e524bd5b0c310;hb=073bbfba4165d4c01d29daaa7523c35c6cc7d114;hp=539c4380dfa68bc9d8fdf763b2e043fd0129a1f8;hpb=4ce681700509beedf38026568ea20102801e6516;p=dead%2Fhtsn-import.git
diff --git a/src/TSN/XML/AutoRacingResults.hs b/src/TSN/XML/AutoRacingResults.hs
index 539c438..e51fff8 100644
--- a/src/TSN/XML/AutoRacingResults.hs
+++ b/src/TSN/XML/AutoRacingResults.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuasiQuotes #-}
@@ -24,21 +25,22 @@ where
-- System imports.
import Control.Monad ( forM_ )
import Data.Data ( Data )
+import Data.Maybe ( fromMaybe )
import Data.Time ( UTCTime(..) )
import Data.Tuple.Curry ( uncurryN )
import Data.Typeable ( Typeable )
+import qualified Data.Vector.HFixed as H ( HVector, cons, convert )
import Database.Groundhog (
countAll,
deleteAll,
- migrate,
- runMigration,
- silentMigrationLogger )
+ migrate )
import Database.Groundhog.Core ( DefaultKey )
-import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Generic ( runDbConn, runMigrationSilent )
import Database.Groundhog.Sqlite ( withSqliteConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
+import qualified GHC.Generics as GHC ( Generic )
import Test.Tasty ( TestTree, testGroup )
import Test.Tasty.HUnit ( (@?=), testCase )
import Text.XML.HXT.Core (
@@ -46,6 +48,7 @@ import Text.XML.HXT.Core (
xp11Tuple,
xp13Tuple,
xpAttr,
+ xpDefault,
xpElem,
xpInt,
xpList,
@@ -126,7 +129,11 @@ data Message =
xml_listings :: [AutoRacingResultsListingXml],
xml_race_information :: AutoRacingResultsRaceInformationXml,
xml_time_stamp :: UTCTime }
- deriving (Eq, Show)
+ deriving (Eq, GHC.Generic, Show)
+
+-- | For 'H.convert'.
+--
+instance H.HVector Message
instance ToDb Message where
@@ -166,46 +173,55 @@ instance XmlImport Message
-- * AutoRacingResultsListing/AutoRacingResultsListingXml
-- | Database representation of a \
contained within a
--- \.
+-- \. The leading underscores prevent unused field
+-- warnings.
--
data AutoRacingResultsListing =
AutoRacingResultsListing {
- db_auto_racing_results_id :: DefaultKey AutoRacingResults,
- db_finish_position :: Int,
- db_starting_position :: Int,
- db_car_number :: Int,
- db_driver_id :: Int,
- db_driver :: String,
- db_car_make :: String,
- db_points :: Int,
- db_laps_completed :: Int,
- db_laps_leading :: Int,
- db_status :: Maybe String,
- db_dnf :: Maybe Bool,
- db_nc :: Maybe Bool,
- db_earnings :: Maybe Int }
-
+ _db_auto_racing_results_id :: DefaultKey AutoRacingResults,
+ _db_finish_position :: Int,
+ _db_starting_position :: Int,
+ _db_car_number :: Int,
+ _db_driver_id :: Int,
+ _db_driver :: String,
+ _db_car_make :: String,
+ _db_points :: Int,
+ _db_laps_completed :: Int,
+ _db_laps_leading :: Int,
+ _db_status :: Maybe String,
+ _db_dnf :: Maybe Bool,
+ _db_nc :: Maybe Bool,
+ _db_earnings :: Maybe Int }
+ deriving ( GHC.Generic )
+
+-- | For 'H.convert' and 'H.cons'.
+--
+instance H.HVector AutoRacingResultsListing
-- | XML representation of a \ contained within a
--- \.
+-- \. The leading underscores prevent unused field
+-- warnings.
--
data AutoRacingResultsListingXml =
AutoRacingResultsListingXml {
- xml_finish_position :: Int,
- xml_starting_position :: Int,
- xml_car_number :: Int,
- xml_driver_id :: Int,
- xml_driver :: String,
- xml_car_make :: String,
- xml_points :: Int,
- xml_laps_completed :: Int,
- xml_laps_leading :: Int,
- xml_status :: Maybe String,
- xml_dnf :: Maybe Bool,
- xml_nc :: Maybe Bool,
- xml_earnings :: Maybe Int }
- deriving (Eq, Show)
-
+ _xml_finish_position :: Int,
+ _xml_starting_position :: Int,
+ _xml_car_number :: Int,
+ _xml_driver_id :: Int,
+ _xml_driver :: String,
+ _xml_car_make :: String,
+ _xml_points :: Int,
+ _xml_laps_completed :: Int,
+ _xml_laps_leading :: Int,
+ _xml_status :: Maybe String,
+ _xml_dnf :: Maybe Bool,
+ _xml_nc :: Maybe Bool,
+ _xml_earnings :: Maybe Int }
+ deriving (Eq, GHC.Generic, Show)
+
+-- | For 'H.convert'.
+--
+instance H.HVector AutoRacingResultsListingXml
instance ToDb AutoRacingResultsListingXml where
-- | The database analogue of an 'AutoRacingResultsListingXml' is
@@ -226,22 +242,7 @@ instance FromXmlFk AutoRacingResultsListingXml where
-- 'AutoRacingResultsListing', we add the foreign key and copy
-- everything else verbatim.
--
- from_xml_fk fk AutoRacingResultsListingXml{..} =
- AutoRacingResultsListing {
- db_auto_racing_results_id = fk,
- db_finish_position = xml_finish_position,
- db_starting_position = xml_starting_position,
- db_car_number = xml_car_number,
- db_driver_id = xml_driver_id,
- db_driver = xml_driver,
- db_car_make = xml_car_make,
- db_points = xml_points,
- db_laps_completed = xml_laps_completed,
- db_laps_leading = xml_laps_leading,
- db_status = xml_status,
- db_dnf = xml_dnf,
- db_nc = xml_nc,
- db_earnings = xml_earnings }
+ from_xml_fk = H.cons
-- | This allows us to insert the XML representation
@@ -259,17 +260,27 @@ instance XmlImportFk AutoRacingResultsListingXml
-- 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
+-- \ 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 \ contained
-- within a \.
--
+-- 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
@@ -306,8 +317,8 @@ data AutoRacingResultsRaceInformationXml =
xml_cautions :: Maybe String,
xml_lead_changes :: Maybe String,
xml_lap_leaders :: Maybe String,
- xml_most_laps_leading :: MostLapsLeading }
- deriving (Eq,Show)
+ xml_most_laps_leading :: Maybe MostLapsLeading }
+ deriving (Eq, Show)
instance ToDb AutoRacingResultsRaceInformationXml where
@@ -329,7 +340,7 @@ instance Child AutoRacingResultsRaceInformationXml where
instance FromXmlFk AutoRacingResultsRaceInformationXml where
-- | To convert an 'AutoRacingResultsRaceInformationXml' to an
-- 'AutoRacingResultsRaceInformartion', we add the foreign key and
- -- copy everything else verbatim.
+ -- massage the 'MostLapsLeading' embedded type,
--
from_xml_fk fk AutoRacingResultsRaceInformationXml{..} =
AutoRacingResultsRaceInformation {
@@ -345,7 +356,14 @@ instance FromXmlFk AutoRacingResultsRaceInformationXml where
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 \, indicate that in
+ -- the database with an (embedded) 'MostLapsLeading' with three
+ -- missing fields.
+ most_laps_leading =
+ fromMaybe (MostLapsLeading Nothing Nothing Nothing)
+ xml_most_laps_leading
-- | This allows us to insert the XML representation
@@ -396,7 +414,7 @@ mkPersist tsn_codegen_config [groundhog|
constructors:
- name: AutoRacingResultsListing
fields:
- - name: db_auto_racing_results_id
+ - name: _db_auto_racing_results_id
reference:
onDelete: cascade
@@ -439,7 +457,7 @@ mkPersist tsn_codegen_config [groundhog|
pickle_listing :: PU AutoRacingResultsListingXml
pickle_listing =
xpElem "Listing" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, H.convert) $
xp13Tuple (xpElem "FinishPosition" xpInt)
(xpElem "StartingPosition" xpInt)
(xpElem "CarNumber" xpInt)
@@ -455,19 +473,6 @@ pickle_listing =
(xpElem "Earnings" xp_earnings)
where
from_tuple = uncurryN AutoRacingResultsListingXml
- to_tuple m = (xml_finish_position m,
- xml_starting_position m,
- xml_car_number m,
- xml_driver_id m,
- xml_driver m,
- xml_car_make m,
- xml_points m,
- xml_laps_completed m,
- xml_laps_leading m,
- xml_status m,
- xml_dnf m,
- xml_nc m,
- xml_earnings m)
-- | Pickler for the top-level 'Message'.
@@ -475,7 +480,7 @@ pickle_listing =
pickle_message :: PU Message
pickle_message =
xpElem "message" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, H.convert) $
xp13Tuple (xpElem "XML_File_ID" xpInt)
(xpElem "heading" xpText)
(xpElem "category" xpText)
@@ -491,44 +496,59 @@ pickle_message =
(xpElem "time_stamp" xp_time_stamp)
where
from_tuple = uncurryN Message
- to_tuple m = (xml_xml_file_id m,
- xml_heading m,
- xml_category m,
- xml_sport m,
- xml_race_id m,
- xml_race_date m,
- xml_title m,
- xml_track_location m,
- xml_laps_remaining m,
- xml_checkered_flag m,
- xml_listings m,
- xml_race_information m,
- xml_time_stamp m)
-- | Pickler for the \ child of a
--- \.
+-- \. This is complicated by the fact that the
+-- three fields we're trying to parse are not actually optional;
+-- only the entire \ 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)
+ xpWrap (from_tuple, to_tuple') $
+ 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 \ child of \.
--
+-- 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
+-- \ is missing.
+--
+-- Clear as mud, I know.
+--
pickle_race_information :: PU AutoRacingResultsRaceInformationXml
pickle_race_information =
xpElem "Race_Information" $
- xpWrap (from_tuple, to_tuple) $
+ xpWrap (from_tuple, to_tuple') $
xp11Tuple (-- I can't think of another way to get both the
-- TrackLength and its KPH attribute. So we shove them
-- both in a 2-tuple. This should probably be an embedded type!
@@ -544,7 +564,7 @@ pickle_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.
@@ -553,17 +573,17 @@ pickle_race_information =
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12
-- And here we have to re-pair the first two.
- to_tuple m = ((xml_track_length m, xml_track_length_kph m),
- xml_laps m,
- xml_average_speed_mph m,
- xml_average_speed_kph m,
- xml_average_speed m,
- xml_time_of_race m,
- xml_margin_of_victory m,
- xml_cautions m,
- xml_lead_changes m,
- xml_lap_leaders m,
- xml_most_laps_leading m)
+ to_tuple' m = ((xml_track_length m, xml_track_length_kph m),
+ xml_laps m,
+ xml_average_speed_mph m,
+ xml_average_speed_kph m,
+ xml_average_speed m,
+ xml_time_of_race m,
+ xml_margin_of_victory m,
+ xml_cautions m,
+ xml_lead_changes m,
+ xml_lap_leaders m,
+ xml_most_laps_leading m)
--
-- * Tasty Tests
@@ -589,7 +609,10 @@ test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
"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
@@ -605,7 +628,10 @@ test_unpickle_succeeds = testGroup "unpickle tests"
"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
@@ -623,7 +649,11 @@ test_on_delete_cascade = testGroup "cascading delete tests"
"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
@@ -632,7 +662,7 @@ test_on_delete_cascade = testGroup "cascading delete tests"
let c = undefined :: AutoRacingResultsRaceInformation
actual <- withSqliteConn ":memory:" $ runDbConn $ do
- runMigration silentMigrationLogger $ do
+ runMigrationSilent $ do
migrate a
migrate b
migrate c