From 6bf6a25053c2f721d67a70b1eaa1a018da5baa87 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 30 Dec 2014 14:18:44 -0500 Subject: [PATCH] Use Generics.to_tuple in TSN.XML.Scores. --- src/TSN/XML/Scores.hs | 59 ++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/TSN/XML/Scores.hs b/src/TSN/XML/Scores.hs index 8bfb196..e4680a5 100644 --- a/src/TSN/XML/Scores.hs +++ b/src/TSN/XML/Scores.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} @@ -39,6 +40,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 ( @@ -56,6 +58,7 @@ import Text.XML.HXT.Core ( xpWrap ) -- Local imports. +import Generics ( Generic(..), to_tuple ) import TSN.Codegen ( tsn_codegen_config ) import TSN.Database ( insert_or_select ) import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) @@ -122,7 +125,13 @@ data Message = xml_season_type :: Maybe String, -- ^ We've seen an empty one xml_game :: ScoreGameXml, xml_time_stamp :: UTCTime } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'Generics.to_tuple'. +-- +instance Generic Message + instance ToDb Message where -- | The database representation of a 'Message' is a 'Score'. @@ -194,7 +203,12 @@ data ScoreGameXml = xml_time_r :: Maybe String, -- ^ Time remaining, the format is uncertain. xml_status :: ScoreGameStatus, xml_notes :: Maybe String } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'Generics.to_tuple'. +-- +instance Generic ScoreGameXml instance ToDb ScoreGameXml where @@ -416,18 +430,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_game_id m, - xml_schedule_id m, - xml_tsnupdate m, - xml_category m, - xml_sport m, - xml_locations m, - xml_season_type m, - xml_game m, - xml_time_stamp m) - @@ -439,18 +441,18 @@ pickle_message = pickle_status :: PU ScoreGameStatus pickle_status = xpElem "status" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpTriple (xpAttr "numeral" $ xpOption xpInt) (xpOption $ xpAttr "type" $ xpOption xpText) xpText where from_tuple (x,y,z) = ScoreGameStatus x (join y) z - to_tuple ScoreGameStatus{..} = + to_tuple' ScoreGameStatus{..} = (db_status_numeral, s, db_status_text) where - s = case db_status_type of - Nothing -> Nothing - Just _ -> Just db_status_type + s = case db_status_type of + Nothing -> Nothing + Just _ -> Just db_status_type -- | Convert a 'ScoreGameXml' to/from \. @@ -468,13 +470,6 @@ pickle_game = (xpOption $ xpElem "notes" xpText) where from_tuple = uncurryN ScoreGameXml - to_tuple ScoreGameXml{..} = (xml_vteam, - xml_hteam, - xml_away_team_score, - xml_home_team_score, - xml_time_r, - xml_status, - xml_notes) -- | Convert a 'VTeamXml' to/from \. The team names @@ -492,15 +487,15 @@ pickle_game = pickle_vteam :: PU VTeamXml pickle_vteam = xpElem "vteam" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpTriple (xpAttr "id" xpText) (xpOption $ xpAttr "pitcher" (xpOption xpText)) (xpOption xpText) -- Team name where from_tuple (x,y,z) = VTeamXml (VTeam (Team x Nothing z)) (join y) - to_tuple (VTeamXml (VTeam t) Nothing) = (team_id t, Nothing, name t) - to_tuple (VTeamXml (VTeam t) jvp) = (team_id t, Just jvp, name t) + to_tuple' (VTeamXml (VTeam t) Nothing) = (team_id t, Nothing, name t) + to_tuple' (VTeamXml (VTeam t) jvp) = (team_id t, Just jvp, name t) -- | Convert a 'HTeamXml' to/from \. Identical to 'pickle_vteam' @@ -519,14 +514,14 @@ pickle_vteam = pickle_hteam :: PU HTeamXml pickle_hteam = xpElem "hteam" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpTriple (xpAttr "id" xpText) (xpOption $ xpAttr "pitcher" (xpOption xpText)) (xpOption xpText) -- Team name where from_tuple (x,y,z)= HTeamXml (HTeam (Team x Nothing z)) (join y) - to_tuple (HTeamXml (HTeam t) Nothing) = (team_id t, Nothing, name t) - to_tuple (HTeamXml (HTeam t) jhp) = (team_id t, Just jhp, name t) + to_tuple' (HTeamXml (HTeam t) Nothing) = (team_id t, Nothing, name t) + to_tuple' (HTeamXml (HTeam t) jhp) = (team_id t, Just jhp, name t) -- 2.43.2