--
data ScoreGameStatus =
ScoreGameStatus {
- db_status_numeral :: Int,
+ db_status_numeral :: Maybe Int,
db_status_type :: Maybe String, -- ^ These are probably only one-character,
-- long, but they all take the same
-- amount of space in Postgres.
--- | Convert a 'ScoreGameStatus' to/from \<status\>.
+-- | Convert a 'ScoreGameStatus' to/from \<status\>. The \"type\"
+-- attribute can be either missing or empty, so we're really parsing
+-- a double-Maybe here. We use the monad join to collapse it into
+-- one. See also: the hteam/vteam picklers.
--
pickle_status :: PU ScoreGameStatus
pickle_status =
xpElem "status" $
xpWrap (from_tuple, to_tuple) $
- xpTriple (xpAttr "numeral" xpInt)
- (xpOption $ xpAttr "type" xpText)
+ xpTriple (xpAttr "numeral" $ xpOption xpInt)
+ (xpOption $ xpAttr "type" $ xpOption xpText)
xpText
where
- from_tuple = uncurryN ScoreGameStatus
- to_tuple ScoreGameStatus{..} = (db_status_numeral,
- db_status_type,
- db_status_text)
+ from_tuple (x,y,z) = ScoreGameStatus x (join y) z
+ to_tuple ScoreGameStatus{..} =
+ (db_status_numeral, s, db_status_text)
+ where
+ s = case db_status_type of
+ Nothing -> Nothing
+ Just _ -> Just db_status_type
-- | Convert a 'ScoreGameXml' to/from \<game\>.
"test/xml/scoresxml-no-locations.xml",
check "pickle composed with unpickle is the identity (pitcher, no type)"
- "test/xml/scoresxml-pitcher-no-type.xml"]
+ "test/xml/scoresxml-pitcher-no-type.xml",
+
+ check "pickle composed with unpickle is the identity (empty numeral)"
+ "test/xml/scoresxml-empty-numeral.xml",
+
+ check "pickle composed with unpickle is the identity (empty type)"
+ "test/xml/scoresxml-empty-type.xml" ]
where
check desc path = testCase desc $ do
(expected, actual) <- pickle_unpickle pickle_message path
"test/xml/scoresxml-no-locations.xml",
check "unpickling succeeds (pitcher, no type)"
- "test/xml/scoresxml-pitcher-no-type.xml" ]
+ "test/xml/scoresxml-pitcher-no-type.xml",
+
+ check "unpickling succeeds (empty numeral)"
+ "test/xml/scoresxml-empty-numeral.xml",
+
+ check "unpickling succeeds (empty type)"
+ "test/xml/scoresxml-empty-type.xml" ]
where
check desc path = testCase desc $ do
actual <- unpickleable path pickle_message
check "unpickling succeeds (pitcher, no type)"
"test/xml/scoresxml-pitcher-no-type.xml"
- 3 -- 2 teams, 1 location
+ 3, -- 2 teams, 1 location
+
+ check "unpickling succeeds (empty numeral)"
+ "test/xml/scoresxml-empty-numeral.xml"
+ 3, -- 2 teams, 1 location
+
+ check "unpickling succeeds (empty type)"
+ "test/xml/scoresxml-empty-type.xml"
+ 4 -- 2 teams, 2 locations
]
where
check desc path expected = testCase desc $ do