Set the table value to null when a property is null (#1905)

* set the table value to null when a property is null

* unit test
This commit is contained in:
Cheick Keita
2022-05-06 14:42:28 -07:00
committed by GitHub
parent ee3908f660
commit 119a40bf0c
2 changed files with 38 additions and 14 deletions

View File

@ -156,16 +156,20 @@ public class EntityConverter {
if (type is null) { if (type is null) {
throw new NullReferenceException(); throw new NullReferenceException();
} }
var tableEntity = new TableEntity();
var entityInfo = GetEntityInfo<T>(); var entityInfo = GetEntityInfo<T>();
foreach (var prop in entityInfo.properties.SelectMany(x => x)) { Dictionary<string, object?> columnValues = entityInfo.properties.SelectMany(x => x).Select(prop => {
//var prop = kvp.First();
var value = entityInfo.type.GetProperty(prop.name)?.GetValue(typedEntity); var value = entityInfo.type.GetProperty(prop.name)?.GetValue(typedEntity);
if (value == null) {
return (prop.columnName, value: (object?)null);
}
if (prop.kind == EntityPropertyKind.PartitionKey || prop.kind == EntityPropertyKind.RowKey) { if (prop.kind == EntityPropertyKind.PartitionKey || prop.kind == EntityPropertyKind.RowKey) {
tableEntity.Add(prop.columnName, value?.ToString()); return (prop.columnName, value?.ToString());
} else if (prop.type == typeof(Guid) || prop.type == typeof(Guid?)) { }
tableEntity.Add(prop.columnName, value?.ToString()); if (prop.type == typeof(Guid) || prop.type == typeof(Guid?)) {
} else if (prop.type == typeof(bool) return (prop.columnName, value?.ToString());
}
if (prop.type == typeof(bool)
|| prop.type == typeof(bool?) || prop.type == typeof(bool?)
|| prop.type == typeof(string) || prop.type == typeof(string)
|| prop.type == typeof(DateTime) || prop.type == typeof(DateTime)
@ -180,19 +184,22 @@ public class EntityConverter {
|| prop.type == typeof(double?) || prop.type == typeof(double?)
) { ) {
tableEntity.Add(prop.columnName, value); return (prop.columnName, value);
} else if (prop.type.IsEnum) { }
if (prop.type.IsEnum) {
var values = var values =
(value?.ToString()?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) (value?.ToString()?.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(CaseConverter.PascalToSnake)).EnsureNotNull($"Unable to read enum data {value}"); .Select(CaseConverter.PascalToSnake)).EnsureNotNull($"Unable to read enum data {value}");
tableEntity.Add(prop.columnName, string.Join(",", values)); return (prop.columnName, string.Join(",", values));
} else {
var serialized = JsonSerializer.Serialize(value, _options);
tableEntity.Add(prop.columnName, serialized.Trim('"'));
} }
} var serialized = JsonSerializer.Serialize(value, _options);
return (prop.columnName, serialized.Trim('"'));
}).ToDictionary(x => x.columnName, x => x.value);
var tableEntity = new TableEntity(columnValues);
if (typedEntity.ETag.HasValue) { if (typedEntity.ETag.HasValue) {
tableEntity.ETag = typedEntity.ETag.Value; tableEntity.ETag = typedEntity.ETag.Value;

View File

@ -330,5 +330,22 @@ namespace Tests {
Assert.Equal(expectedObject, actual); Assert.Equal(expectedObject, actual);
} }
record TestNullField(int? Id, string? Name, TestObject? Obj) : EntityBase();
[Fact]
public void TestNullValue() {
var entityConverter = new EntityConverter();
var tableEntity = entityConverter.ToTableEntity(new TestNullField(null, null, null));
Assert.Null(tableEntity["id"]);
Assert.Null(tableEntity["name"]);
Assert.Null(tableEntity["obj"]);
}
} }
} }