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

Serialize nullable nullable types (#465) #468

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ BenchmarkDotNet.Artifacts/
Parquet.sln.DotSettings.user
launchSettings.json
.DS_Store
*.user
20 changes: 18 additions & 2 deletions src/Parquet.Test/Serialisation/ParquetSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@
public double? ParentMeterValue { get; set; }

public decimal? ParentResolution { get; set; }

public DateTime? ParentTime { get; set; }

public Interval? ParentInterval { get; set; }

public TimeSpan? ParentTimeSpan { get; set; }

}

[Fact]
Expand All @@ -172,8 +179,12 @@
EventName = i % 2 == 0 ? "on" : "off",
MeterValue = i,
ParentId = (i % 4 == 0) ? null : i,
ExternalId = Guid.NewGuid(),
ParentMeterValue = (i % 5 == 0) ? null : (double?)i,
ParentResolution = (i % 6 == 0) ? null : (decimal?)i
ParentResolution = (i % 6 == 0) ? null : (decimal?)i,
ParentTime = (i % 5 == 0) ? null : DateTime.UtcNow.AddSeconds(i),
ParentInterval = (i % 5 == 0) ? null : new Interval(i, i, i),
ParentTimeSpan = (i % 5 == 0) ? null : TimeSpan.FromSeconds(i)
}).ToList();

await Compare(data);
Expand All @@ -186,8 +197,13 @@
["Timestamp"] = DateTime.UtcNow.AddSeconds(i),
["EventName"] = i % 2 == 0 ? "on" : "off",
["MeterValue"] = (double)i,
["ParentId"] = (i % 4 == 0) ? null : i,

Check warning on line 200 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 200 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 200 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 200 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["ExternalId"] = Guid.NewGuid()
["ExternalId"] = Guid.NewGuid(),
["ParentMeterValue"] = (i % 5 == 0) ? null : (double?)i,

Check warning on line 202 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 202 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 202 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 202 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["ParentResolution"] = (i % 6 == 0) ? null : (decimal?)i,

Check warning on line 203 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 203 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 203 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 203 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["ParentTime"] = (i % 5 == 0) ? null : DateTime.UtcNow.AddSeconds(i),

Check warning on line 204 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 204 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 204 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 204 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["ParentInterval"] = (i % 5 == 0) ? null : new Interval(i, i, i),

Check warning on line 205 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 205 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 205 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 205 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["ParentTimeSpan"] = (i % 5 == 0) ? null : TimeSpan.FromSeconds(i)

Check warning on line 206 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 206 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 206 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 206 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
}).ToList();

await DictCompare<NullableRecord>(data);
Expand Down Expand Up @@ -380,7 +396,7 @@
public async Task List_Structs_Serde_Dict() {
var data = Enumerable.Range(0, 1_000).Select(i => new Dictionary<string, object> {
["PersonId"] = i,
["Comments"] = i % 2 == 0 ? "none" : null,

Check warning on line 399 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Possible null reference assignment.

Check warning on line 399 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Possible null reference assignment.

Check warning on line 399 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Possible null reference assignment.

Check warning on line 399 in src/Parquet.Test/Serialisation/ParquetSerializerTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Possible null reference assignment.
["Addresses"] = Enumerable.Range(0, 4).Select(a => new Dictionary<string, object> {
["City"] = "Birmingham",
["Country"] = "United Kingdom"
Expand Down
10 changes: 9 additions & 1 deletion src/Parquet/Serialization/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static Field ConstructDataField(string name, string propertyName, Type t
Field r;
bool? isNullable = member == null
? null
: member.IsRequired ? false : null;
: member.IsRequired ? false : IsNullable(t);

if(t == typeof(DateTime) || t == typeof(DateTime?)) {
ParquetTimestampAttribute? tsa = member?.TimestampAttribute;
Expand Down Expand Up @@ -165,6 +165,14 @@ private static Field ConstructDataField(string name, string propertyName, Type t
return r;
}

static bool IsNullable(Type type) {
if(!type.IsValueType)
return true; // ref-type
if(Nullable.GetUnderlyingType(type) != null)
return true; // Nullable<T>
return false; // value-type
}

private static MapField ConstructMapField(string name, string propertyName,
Type tKey, Type tValue,
bool forWriting) {
Expand Down
Loading