Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count the number of UnexpectedEndOfRow errors #50

Open
Profpatsch opened this issue Aug 3, 2021 · 1 comment
Open

Count the number of UnexpectedEndOfRow errors #50

Profpatsch opened this issue Aug 3, 2021 · 1 comment

Comments

@Profpatsch
Copy link

Profpatsch commented Aug 3, 2021

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)
@Profpatsch
Copy link
Author

Note: I confused rows and columns again, of course it’s missing columns, not rows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant