Skip to content

Provide the raw string value to ICsvSchemaProvider.GetColumn when there's no headers - #289

Merged
MarkPflug merged 2 commits into
MarkPflug:mainfrom
0xced:GetColumn-raw-string
Mar 25, 2026
Merged

MarkPflug merged 2 commits into
MarkPflug:mainfrom
0xced:GetColumn-raw-string

Conversation

@0xced

@0xced 0xced commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #288

@0xced
0xced force-pushed the GetColumn-raw-string branch from ca3017f to 6c1a126 Compare March 24, 2026 08:23
@0xced
0xced force-pushed the GetColumn-raw-string branch from 6c1a126 to 7993658 Compare March 24, 2026 08:36
Comment thread source/Sylvan.Data.Csv/CsvDataReader.cs Outdated
var columnSchema = schema?.GetColumn(name, i);
var rawString = this.GetStringRaw(i);
var name = hasHeaders ? rawString : null;
var columnSchema = schema?.GetColumn(hasHeaders ? name! : rawString, i);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think this is equivalent:

var name = this.GetStringRaw(i);
var columnSchema = schema?.GetColumn(name, i)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, that's not exactly equivalent. You'll see that two tests are failing when the options are configured with HasHeaders = false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But indeed, the name! was unnecessary. I simplified a little bit in fb40375.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I see now. I was looking too narrow and didn't see the effects on the following code. LGTM now.

@MarkPflug

Copy link
Copy Markdown
Owner

Thanks for catching this issue @0xced.
I'm surprised someone actually hit this. I doubted that anyone used the more esoteric features of my library.
I'll get this merged soon.

@MarkPflug

Copy link
Copy Markdown
Owner

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?

@0xced

0xced commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

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 string? to string.

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., CsvSchemaProvider) noting would happen, everything would still compile just fine.

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 CS8765 warning that, as a consumer of a library, I would like to know about. Is the introduction of a new warning a source breaking change? This is a debatable question.

In any case, this is absolutely not a binary breaking change, so I'd go for reflecting the actual nullability of the name parameter.

@0xced

0xced commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

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 string? name is not updated to string name in the base class I would inevitably get a warning.

Either

  • CS8765: Nullability of type of parameter 'name' doesn't match overridden member (possibly because of nullability attributes) if I force GetColumn(string name, int ordinal) in the overridden method

or

  • CS8604: Possible null reference argument for parameter 'key' in 'bool IReadOnlyDictionary<string, IReadOnlyList>.TryGetValue(string key, out IReadOnlyList value)' if I keep GetColumn(string? name, int ordinal)

@MarkPflug

Copy link
Copy Markdown
Owner

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:

if (name is null) return null;

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.

@0xced

0xced commented Mar 25, 2026

Copy link
Copy Markdown
Contributor Author

Note that CS8765 will not happen in this case, when changing the signature from string? name to string name in the base class.

If someone has warnings as errors enabled, which I hope most people do, then their code won't compile after updating.

I mentioned the warning because I would want my code not to compile, if a nullable annotation was changed from string anything to string? anything in a method signature anywhere else. That would be a NullReferenceException waiting to happen. But this is purely hypothetical; it does not apply in this particular case.

@MarkPflug

Copy link
Copy Markdown
Owner

Thanks for clarifying, I had that backwards in my mind. I think this is good then.

@MarkPflug
MarkPflug merged commit af75b7a into MarkPflug:main Mar 25, 2026
1 check passed
@0xced
0xced deleted the GetColumn-raw-string branch March 25, 2026 20:12
@0xced

0xced commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for merging! Any chance you have a look at #290 before cutting a new release? 😉

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

Successfully merging this pull request may close these issues.

Implementing a CSV schema provider for multi-result data without headers

2 participants