Skip to content

Set timestamp from dynamic table entity if required #2

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

Merged
merged 3 commits into from
Mar 2, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
rowProp.SetValue(o, convertRowKey(entity.RowKey));
properties.Remove(rowProp);
}

SetTimestamp(entity, o, properties);
FillProperties(entity, o, properties);
return o;
}
Expand Down Expand Up @@ -125,15 +127,38 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
return name;
}

private static void SetTimestamp<T>(DynamicTableEntity entity, T o, List<PropertyInfo> properties) where T : new()
{
var timestampProperty = properties
.FirstOrDefault(p => p.Name == nameof(DynamicTableEntity.Timestamp));

if (timestampProperty != null)
{
if(timestampProperty.PropertyType == typeof(DateTimeOffset))
{
timestampProperty.SetValue(o, entity.Timestamp);
}

if (timestampProperty.PropertyType == typeof(DateTime))
{
timestampProperty.SetValue(o, entity.Timestamp.DateTime);
}

if (timestampProperty.PropertyType == typeof(string))
{
timestampProperty.SetValue(o, entity.Timestamp.ToString());
}
}
}

private static void FillProperties<T>(DynamicTableEntity entity, T o, List<PropertyInfo> properties) where T : new()
{
foreach (var propertyInfo in properties)
{
if (entity.Properties.ContainsKey(propertyInfo.Name))
if (entity.Properties.ContainsKey(propertyInfo.Name) && propertyInfo.Name != nameof(DynamicTableEntity.Timestamp))
{
var val = entity.Properties[propertyInfo.Name].PropertyAsObject;


if (val != null && (propertyInfo.PropertyType == typeof(DateTimeOffset) || propertyInfo.PropertyType == typeof(DateTimeOffset?)))
{
val = entity.Properties[propertyInfo.Name].DateTimeOffsetValue;
Expand Down
10 changes: 10 additions & 0 deletions src/TableStorage.Abstractions.UnitTests/Employee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public class Employee
public DateTime? ANullableDateTime { get; set; }
public int? ANullableInt { get; set; }
}

public class EmployeeWithTimestamp : Employee
{
public DateTimeOffset Timestamp { get; set; }
}

public class EmployeeWithTimestampAsString : Employee
{
public string Timestamp { get; set; }
}
}
50 changes: 49 additions & 1 deletion src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ public void convert_from_entity_table()
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
}

[Fact]
public void convert_from_entity_table_with_timestamp()
{
var emp = new EmployeeWithTimestamp
{
Company = "Microsoft",
Name = "John Smith",
Department = new Department
{
Name = "QA",
Id = 1,
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
},
Id = 42,
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
};
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
tableEntity.Timestamp = DateTime.UtcNow;
var employee = tableEntity.FromTableEntity<EmployeeWithTimestamp, string, int>(e => e.Company, e => e.Id);
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
Assert.Equal(tableEntity.Timestamp, employee.Timestamp);
}

[Fact]
public void convert_from_entity_table_with_timestamp_as_string()
{
var emp = new EmployeeWithTimestampAsString
{
Company = "Microsoft",
Name = "John Smith",
Department = new Department
{
Name = "QA",
Id = 1,
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
},
Id = 42,
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
};
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
tableEntity.Timestamp = DateTime.UtcNow;
var employee = tableEntity.FromTableEntity<EmployeeWithTimestampAsString, string, int>(e => e.Company, e => e.Id);
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
Assert.Equal(tableEntity.Timestamp.ToString(), employee.Timestamp);
}

[Fact]
public void convert_from_entity_table_complex_key()
{
Expand Down Expand Up @@ -235,7 +283,7 @@ public void convert_to_entity_table()
};
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);

Assert.Equal(true, tableEntity.Properties.ContainsKey("DepartmentJson"));
Assert.True(tableEntity.Properties.ContainsKey("DepartmentJson"));
}

[Fact]
Expand Down