X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FAutoRacingDriverList.hs;h=453986ea9fc21d26467bc1c75e7a54965521f4ec;hb=99cb78224987bd773f5c008df81302e07ff31e25;hp=327c358bcc1ed305d1b016cc5b462825c3162825;hpb=c864af7aa8a195cf441904479c416202590e9375;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/AutoRacingDriverList.hs b/src/TSN/XML/AutoRacingDriverList.hs index 327c358..453986e 100644 --- a/src/TSN/XML/AutoRacingDriverList.hs +++ b/src/TSN/XML/AutoRacingDriverList.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE QuasiQuotes #-} @@ -24,6 +25,7 @@ where import Control.Monad ( forM_ ) import Data.Time ( UTCTime(..) ) import Data.Tuple.Curry ( uncurryN ) +import qualified Data.Vector.HFixed as H ( HVector, cons, convert ) import Database.Groundhog ( countAll, deleteAll, @@ -36,6 +38,7 @@ 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 ( @@ -101,7 +104,11 @@ data Message = xml_title :: String, xml_listings :: [AutoRacingDriverListListingXml], xml_time_stamp :: UTCTime } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + +-- | For 'H.convert'. +-- +instance H.HVector Message instance ToDb Message where @@ -136,37 +143,47 @@ instance XmlImport Message -- AutoRacingDriverListListing / AutoRacingDriverListListingXml -- | Database representation of a \ contained within a --- \. +-- \. The leading underscores prevent unused field +-- warnings. -- data AutoRacingDriverListListing = AutoRacingDriverListListing { - db_auto_racing_driver_lists_id :: DefaultKey AutoRacingDriverList, - db_driver_id :: Int, - db_driver :: String, - db_height :: Maybe String, - db_weight :: Int, - db_date_of_birth :: UTCTime, - db_hometown :: String, - db_nationality :: Maybe String, - db_car_number :: Int, - db_car :: String } + _db_auto_racing_driver_lists_id :: DefaultKey AutoRacingDriverList, + _db_driver_id :: Int, + _db_driver :: String, + _db_height :: Maybe String, + _db_weight :: Int, + _db_date_of_birth :: UTCTime, + _db_hometown :: String, + _db_nationality :: Maybe String, + _db_car_number :: Int, + _db_car :: String } + deriving ( GHC.Generic ) + +-- | For 'H.convert'. +-- +instance H.HVector AutoRacingDriverListListing + -- | XML representation of a \ contained within a --- \. +-- \. The underscores prevent unused field warnings. -- data AutoRacingDriverListListingXml = AutoRacingDriverListListingXml { - xml_driver_id :: Int, - xml_driver :: String, - xml_height :: Maybe String, - xml_weight :: Int, - xml_date_of_birth :: UTCTime, - xml_hometown :: String, - xml_nationality :: Maybe String, - xml_car_number :: Int, - xml_car :: String } - deriving (Eq, Show) - + _xml_driver_id :: Int, + _xml_driver :: String, + _xml_height :: Maybe String, + _xml_weight :: Int, + _xml_date_of_birth :: UTCTime, + _xml_hometown :: String, + _xml_nationality :: Maybe String, + _xml_car_number :: Int, + _xml_car :: String } + deriving (Eq, GHC.Generic, Show) + +-- | For 'H.convert' and 'H.cons'. +-- +instance H.HVector AutoRacingDriverListListingXml instance ToDb AutoRacingDriverListListingXml where -- | The database analogue of an 'AutoRacingDriverListListingXml' is @@ -187,18 +204,8 @@ instance FromXmlFk AutoRacingDriverListListingXml where -- 'AutoRacingDriverListListing', we add the foreign key and copy -- everything else verbatim. -- - from_xml_fk fk AutoRacingDriverListListingXml{..} = - AutoRacingDriverListListing { - db_auto_racing_driver_lists_id = fk, - db_driver_id = xml_driver_id, - db_driver = xml_driver, - db_height = xml_height, - db_weight = xml_weight, - db_date_of_birth = xml_date_of_birth, - db_hometown = xml_hometown, - db_nationality = xml_nationality, - db_car_number = xml_car_number, - db_car = xml_car } + from_xml_fk = H.cons + -- | This allows us to insert the XML representation @@ -244,7 +251,7 @@ mkPersist tsn_codegen_config [groundhog| constructors: - name: AutoRacingDriverListListing fields: - - name: db_auto_racing_driver_lists_id + - name: _db_auto_racing_driver_lists_id reference: onDelete: cascade @@ -260,7 +267,7 @@ mkPersist tsn_codegen_config [groundhog| pickle_listing :: PU AutoRacingDriverListListingXml pickle_listing = xpElem "Listing" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, H.convert) $ xp9Tuple (xpElem "DriverID" xpInt) (xpElem "Driver" xpText) (xpElem "Height" $ xpOption xpText) @@ -272,22 +279,13 @@ pickle_listing = (xpElem "Car" xpText) where from_tuple = uncurryN AutoRacingDriverListListingXml - to_tuple m = (xml_driver_id m, - xml_driver m, - xml_height m, - xml_weight m, - xml_date_of_birth m, - xml_hometown m, - xml_nationality m, - xml_car_number m, - xml_car m) -- | Pickler for the top-level 'Message'. -- pickle_message :: PU Message pickle_message = xpElem "message" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, H.convert) $ xp7Tuple (xpElem "XML_File_ID" xpInt) (xpElem "heading" xpText) (xpElem "category" xpText) @@ -297,13 +295,6 @@ 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_title m, - xml_listings m, - xml_time_stamp m)