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

Emit an error if row and header size mismatch #622

Merged
merged 1 commit into from
Jul 28, 2024

Conversation

satabin
Copy link
Member

@satabin satabin commented Jul 27, 2024

Fixes #621

A new method is created and the old one is deprecated to keep binary compatibility.

@satabin satabin added bug Something isn't working csv labels Jul 27, 2024
@satabin satabin added this to the 1.11.1 milestone Jul 27, 2024
@satabin satabin requested a review from a team as a code owner July 27, 2024 13:28
@satabin satabin changed the base branch from main to 1.11.x July 27, 2024 13:28
@satabin satabin force-pushed the fix/emit-row-header-size-mismatch branch from cb7f54e to 812c16d Compare July 27, 2024 13:29
@@ -217,6 +221,27 @@ package object csv {
}
}

/** Encode a specified type into a CSV prepending the given headers. */
def encodeWithGivenHeaders[T]: PartiallyAppliedEncodeWithGivenHeaders[T] =
Copy link
Collaborator

@ybasket ybasket Jul 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about renaming to encodeWithHeaders? We already have encodeWithoutHeaders, so the naming feels a bit more consistent then the WithGiven.

No strong opinion on this though, just noticed it, decide as you see fit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep consistent naming between the high and low level naming. Since the low level naming cannot be writeWithHeaders(this is the deprecated one) and high-level cannot be encodeGivenHeaders (this is the deprecated one), I settled for merging both.

Comment on lines +25 to +31
val input = List(
Row(NonEmptyList.of("a", "b", "c"), Some(1)),
Row(NonEmptyList.of("d", "e"), Some(2)),
Row(NonEmptyList.of("f", "g", "h", "i"), Some(3)),
Row(NonEmptyList.of("j", "k", "l"), Some(4))
)
val headers = NonEmptyList.of("first", "second", "third")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val input = List(
Row(NonEmptyList.of("a", "b", "c"), Some(1)),
Row(NonEmptyList.of("d", "e"), Some(2)),
Row(NonEmptyList.of("f", "g", "h", "i"), Some(3)),
Row(NonEmptyList.of("j", "k", "l"), Some(4))
)
val headers = NonEmptyList.of("first", "second", "third")
val headers = NonEmptyList.of("first", "second", "third")
val input = List(
Row(NonEmptyList.of("a", "b", "c"), Some(1)),
Row(NonEmptyList.of("d", "e"), Some(2)),
Row(NonEmptyList.of("f", "g", "h", "i"), Some(3)),
Row(NonEmptyList.of("j", "k", "l"), Some(4))
)

I find this test case slightly annoying to read, but except for this reordering (so it matches output order) and maybe adding some unapplySeq in cats itself, I found no better.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thought on this snippet (not to be done in this PR though): Maybe a row"…, …, …" string interpolator could be nice, especially for our tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried adding unapplySeq to cats, unfortunately it doesn't work because NonEmptyList is a case class and hence has a generated unapply that takes precedence. Would need a dedicated extractor object.

@satabin satabin merged commit e3e6561 into 1.11.x Jul 28, 2024
27 of 28 checks passed
@satabin satabin deleted the fix/emit-row-header-size-mismatch branch July 28, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working csv
Projects
None yet
Development

Successfully merging this pull request may close these issues.

encodeGivenHeaders allows different number of headers than the number of columns with actual data
2 participants