Skip to content

Commit adec9b6

Browse files
authored
Set timestamp from dynamic table entity if required (#2)
* Set timestamp if present as property * Fixed issue when using POCO for both serialization and deserialization purposes * Changed nameof to keep consistency
1 parent 50eec14 commit adec9b6

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

src/TableStorage.Abstractions.TableEntityConverters/EntityConvert.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
9797
rowProp.SetValue(o, convertRowKey(entity.RowKey));
9898
properties.Remove(rowProp);
9999
}
100+
101+
SetTimestamp(entity, o, properties);
100102
FillProperties(entity, o, properties);
101103
return o;
102104
}
@@ -125,15 +127,38 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
125127
return name;
126128
}
127129

130+
private static void SetTimestamp<T>(DynamicTableEntity entity, T o, List<PropertyInfo> properties) where T : new()
131+
{
132+
var timestampProperty = properties
133+
.FirstOrDefault(p => p.Name == nameof(DynamicTableEntity.Timestamp));
134+
135+
if (timestampProperty != null)
136+
{
137+
if(timestampProperty.PropertyType == typeof(DateTimeOffset))
138+
{
139+
timestampProperty.SetValue(o, entity.Timestamp);
140+
}
141+
142+
if (timestampProperty.PropertyType == typeof(DateTime))
143+
{
144+
timestampProperty.SetValue(o, entity.Timestamp.DateTime);
145+
}
146+
147+
if (timestampProperty.PropertyType == typeof(string))
148+
{
149+
timestampProperty.SetValue(o, entity.Timestamp.ToString());
150+
}
151+
}
152+
}
153+
128154
private static void FillProperties<T>(DynamicTableEntity entity, T o, List<PropertyInfo> properties) where T : new()
129155
{
130156
foreach (var propertyInfo in properties)
131157
{
132-
if (entity.Properties.ContainsKey(propertyInfo.Name))
158+
if (entity.Properties.ContainsKey(propertyInfo.Name) && propertyInfo.Name != nameof(DynamicTableEntity.Timestamp))
133159
{
134160
var val = entity.Properties[propertyInfo.Name].PropertyAsObject;
135161

136-
137162
if (val != null && (propertyInfo.PropertyType == typeof(DateTimeOffset) || propertyInfo.PropertyType == typeof(DateTimeOffset?)))
138163
{
139164
val = entity.Properties[propertyInfo.Name].DateTimeOffsetValue;

src/TableStorage.Abstractions.UnitTests/Employee.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ public class Employee
2424
public DateTime? ANullableDateTime { get; set; }
2525
public int? ANullableInt { get; set; }
2626
}
27+
28+
public class EmployeeWithTimestamp : Employee
29+
{
30+
public DateTimeOffset Timestamp { get; set; }
31+
}
32+
33+
public class EmployeeWithTimestampAsString : Employee
34+
{
35+
public string Timestamp { get; set; }
36+
}
2737
}

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,54 @@ public void convert_from_entity_table()
3535
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
3636
}
3737

38+
[Fact]
39+
public void convert_from_entity_table_with_timestamp()
40+
{
41+
var emp = new EmployeeWithTimestamp
42+
{
43+
Company = "Microsoft",
44+
Name = "John Smith",
45+
Department = new Department
46+
{
47+
Name = "QA",
48+
Id = 1,
49+
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
50+
},
51+
Id = 42,
52+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
53+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
54+
};
55+
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
56+
tableEntity.Timestamp = DateTime.UtcNow;
57+
var employee = tableEntity.FromTableEntity<EmployeeWithTimestamp, string, int>(e => e.Company, e => e.Id);
58+
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
59+
Assert.Equal(tableEntity.Timestamp, employee.Timestamp);
60+
}
61+
62+
[Fact]
63+
public void convert_from_entity_table_with_timestamp_as_string()
64+
{
65+
var emp = new EmployeeWithTimestampAsString
66+
{
67+
Company = "Microsoft",
68+
Name = "John Smith",
69+
Department = new Department
70+
{
71+
Name = "QA",
72+
Id = 1,
73+
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
74+
},
75+
Id = 42,
76+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
77+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
78+
};
79+
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
80+
tableEntity.Timestamp = DateTime.UtcNow;
81+
var employee = tableEntity.FromTableEntity<EmployeeWithTimestampAsString, string, int>(e => e.Company, e => e.Id);
82+
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
83+
Assert.Equal(tableEntity.Timestamp.ToString(), employee.Timestamp);
84+
}
85+
3886
[Fact]
3987
public void convert_from_entity_table_complex_key()
4088
{
@@ -235,7 +283,7 @@ public void convert_to_entity_table()
235283
};
236284
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
237285

238-
Assert.Equal(true, tableEntity.Properties.ContainsKey("DepartmentJson"));
286+
Assert.True(tableEntity.Properties.ContainsKey("DepartmentJson"));
239287
}
240288

241289
[Fact]

0 commit comments

Comments
 (0)