Repository navigation
Provide the raw string value to ICsvSchemaProvider.GetColumn when there's no headers - #289
Conversation
ca3017f to
6c1a126
Compare
…re's no headers Fixes MarkPflug#288
6c1a126 to
7993658
Compare
| var columnSchema = schema?.GetColumn(name, i); | ||
| var rawString = this.GetStringRaw(i); | ||
| var name = hasHeaders ? rawString : null; | ||
| var columnSchema = schema?.GetColumn(hasHeaders ? name! : rawString, i); |
There was a problem hiding this comment.
I think this is equivalent:
var name = this.GetStringRaw(i);
var columnSchema = schema?.GetColumn(name, i)
There was a problem hiding this comment.
Actually, that's not exactly equivalent. You'll see that two tests are failing when the options are configured with HasHeaders = false.
There was a problem hiding this comment.
But indeed, the name! was unnecessary. I simplified a little bit in fb40375.
There was a problem hiding this comment.
I see now. I was looking too narrow and didn't see the effects on the following code. LGTM now.
|
Thanks for catching this issue @0xced. |
|
The only other thing I'm worried about is the nullability annotation change on the interface parameters. This feels like a breaking change, since anyone who implemented this interface will have to modify their code to remove the nullability. I think I'd rather leave it nullable even though the arguments never will be. What are your thoughts? |
|
Unfortunately, the Change rules for compatibility documentation do not mention changing nullability annotations of reference types. It would be a breaking change for value types for sure but for reference types I think it's not an issue at all when changing from Here's why. public class Base
{
protected virtual void NotNullable(string text) {}
protected virtual void Nullable(string? text) {}
}
public class Derived : Base
{
// 👇 This is absolutely fine, no warning whatsoever
protected override void NotNullable(string? text) {}
// 👇 This produces warning CS8765: Nullability of type of parameter 'text' doesn't match overridden member (possibly because of nullability attributes).
protected override void Nullable(string text) {}
}An existing implementation of (I)CsvSchemaProvider would be defined like this: public override DbColumn? GetColumn(string? name, int ordinal)After changing the nullability annotation of the base class (i.e., In my opinion, even if changing the other way around (i.e., from non nullable to nullable) it should be done to reflect the actual nullability of the parameter. This would cause a new In any case, this is absolutely not a binary breaking change, so I'd go for reflecting the actual nullability of the |
|
Using this branch from source really makes it clear that the nullability should be updated to not nullable. Here's my actual implementation: internal class MultiResultSchemaProvider(IReadOnlyDictionary<string, IReadOnlyList<DbColumn>> schemas) : CsvSchemaProvider
{
private IReadOnlyList<DbColumn>? _columns;
public override DbColumn? GetColumn(string name, int ordinal)
{
if (ordinal == 0)
{
schemas.TryGetValue(name, out _columns);
}
return _columns != null && ordinal < _columns.Count ? _columns[ordinal] : null;
}
}If Either
or
|
|
The warning CS8765 is exactly what I'm worried about. If someone has warnings as errors enabled, which I hope most people do, then their code won't compile after updating. Yes, the fix is easy, but I also try to be sensitive to such breaking changes. For your implementation, you'd just add a line at the beginning of the method:
Which should fix the dictionary lookup, since name is known to be non-null by that point in the flow. That line should never do anything, but it would have the same effect as looking up a column name that doesn't exist in the dictionary. |
|
Note that
I mentioned the warning because I would want my code not to compile, if a nullable annotation was changed from |
|
Thanks for clarifying, I had that backwards in my mind. I think this is good then. |
|
Thanks for merging! Any chance you have a look at #290 before cutting a new release? 😉 |
Fixes #288