Skip to content

Count the number of UnexpectedEndOfRow errors #50

Description

@Profpatsch

When multiple columns are missing, sv will create an UnexpectedEndOfRow error for each row that is missing, example:

> decode ((,) <$> int <*> int) (Identity mempty)
Failure (DecodeErrors (UnexpectedEndOfRow :| [UnexpectedEndOfRow]))

Here we wanted two int fields, but the given csv had zero (mempty :: Vector s) fields, so there’s two UEOR errors.

When pretty printing, usually the user wants to know how many rows were missing.

Here is a mockup of how they can be counted:

-- Pretties DecdodeErrors, but counts all UnexpectedEndOfRow errors so we can display how many rows were missing.
prettyDecodeErrors errs =
  errs
    & foldMap1
      ( \case
          Dec.UnexpectedEndOfRow -> (Sum 1, singleton Dec.UnexpectedEndOfRow)
          err -> (Sum 0, singleton err)
      )
    & \case
      (Sum 0, errs') -> errs' & fmap (prettyDecodeError 0)
      (Sum n, errs') ->
        errs'
          & NonEmpty.nubBy (\a b -> a == Dec.UnexpectedEndOfRow && b == Dec.UnexpectedEndOfRow)
          & fmap (prettyDecodeError n)

prettyDecodeError missingRows = \case
  Dec.UnexpectedEndOfRow -> [fmt|"Unexpected end of line, requires more {show missingRows} more rows"|]
  Dec.ExpectedEndOfRow fields -> "Extra unknown fields: " <> (fmap bytesToTextUtf8Lenient fields & show & stringToText)
  Dec.MissingColumn c -> "Missing column: " <> bytesToTextUtf8Lenient c
  Dec.BadDecode err -> "Decode error: " <> bytesToTextUtf8Lenient err
  other -> "Unknown decode error: " <> (other & show & stringToText)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions