@@ -772,8 +772,9 @@ class FromField a where
772772genericParseField
773773 :: forall a rep meta . (Generic a , GFromField rep , Rep a ~ D1 meta rep , Datatype meta )
774774 => Options -> Field -> Parser a
775- genericParseField opts field = fmap (to . M1 ) (gParseField opts field)
776- <|> fail (" Can't parseField for " <> datatypeName (Proxy :: Proxy meta d f ))
775+ genericParseField opts field = fmap (to . M1 ) (gParseField opts onFail field)
776+ where
777+ onFail _ = fail $ " Can't parseField of type " <> datatypeName (Proxy :: Proxy meta d f )
777778{-# INLINE genericParseField #-}
778779
779780-- | A type that can be converted to a single CSV field.
@@ -1395,12 +1396,12 @@ instance (ToField a, Selector s) => GToRecord (M1 S s (K1 i a)) (B.ByteString, B
13951396 name = T. encodeUtf8 (T. pack (fieldLabelModifier opts (selName m)))
13961397
13971398class GFromField f where
1398- gParseField :: Options -> Field -> Parser (f p )
1399+ gParseField :: Options -> ( Field -> Parser ( f p )) -> Field -> Parser (f p )
13991400
14001401-- Type with single nullary constructor
14011402instance (Constructor c ) => GFromField (C1 c U1 ) where
1402- gParseField opts field = do
1403- if field == expected then pure val else mempty
1403+ gParseField opts onFail field = do
1404+ if field == expected then pure val else onFail field
14041405 where
14051406 expected = encodeConstructor opts val
14061407 val :: C1 c U1 p
@@ -1409,13 +1410,18 @@ instance (Constructor c) => GFromField (C1 c U1) where
14091410
14101411-- Type with single unary constructor
14111412instance (FromField a ) => GFromField (C1 c (S1 meta (K1 i a ))) where
1412- gParseField _ = fmap (M1 . M1 . K1 ) . parseField
1413+ gParseField _ onFail field =
1414+ fmap (M1 . M1 . K1 ) (parseField field) <|> onFail field
14131415 {-# INLINE gParseField #-}
14141416
14151417-- Sum type
14161418instance (GFromField c1 , GFromField c2 ) => GFromField (c1 :+: c2 ) where
1417- gParseField opts field =
1418- fmap L1 (gParseField opts field) <|> fmap R1 (gParseField opts field)
1419+ gParseField opts onFail field =
1420+ case runParser $ gParseField opts mempty field of
1421+ Left _ -> case runParser $ gParseField opts mempty field of
1422+ Left _ -> onFail field
1423+ Right res -> pure $ R1 res
1424+ Right res -> pure $ L1 res
14191425 {-# INLINE gParseField #-}
14201426
14211427class GToField f where
0 commit comments