Skip to content

Commit

Permalink
feat(LBE-495): add DateTime converters
Browse files Browse the repository at this point in the history
  • Loading branch information
Cussa committed Aug 31, 2023
1 parent e3a7369 commit 2091510
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/DynaMight/Converters/DynamoValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public static class DynamoValueConverter
ToDynamoDbEntry = v => (List<string>)v
}
},
{
typeof(DateTime), new CustomConverter
{
ToAttributeValue = v => new AttributeValue { S = ((DateTime)v).ToString("s") },
ToObject = v => DateTime.Parse(v.S),
ToDynamoDbEntry = v => (DateTime)v
}
},
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void FromClass()
}
},
{ "InternalClassNullableValue", new AttributeValue { NULL = true } },
{ "DateTimeValue", new AttributeValue { S = "2023-08-29T15:44:27" } },
{ "DateTimeNullableValue", new AttributeValue { NULL = true } },
});
}
}
11 changes: 8 additions & 3 deletions tests/DynaMight.UnitTests/Converters/ObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void ConvertToClassWithNulls()
{ "InternalString", new AttributeValue { S = "internal string value" } }
}
}
}
},
{ "DateTimeValue", new AttributeValue { S = "2023-08-29T15:44:27" } }
});

result.Should().NotBeNull();
Expand Down Expand Up @@ -87,7 +88,9 @@ public void ConvertToClassWithFilledNulls()
{ "InternalString", new AttributeValue { S = "another string value" } }
}
}
}
},
{ "DateTimeValue", new AttributeValue { S = "2023-08-29T15:44:27" } },
{ "DateTimeNullableValue", new AttributeValue { S = "2023-08-29T15:44:27" } },
});

result.Should().NotBeNull();
Expand All @@ -103,7 +106,9 @@ public void ConvertToClassWithFilledNulls()
TestAttributeEnumNullableValue = DynaMightTestClass.TestAttributeEnum.ValueWithAttribute1,
InternalClassValue = new DynaMightTestClass.InternalClass { InternalString = "internal string value" },
InternalClassNullableValue = new DynaMightTestClass.InternalClass
{ InternalString = "another string value" }
{ InternalString = "another string value" },
DateTimeValue = DateTime.Parse("2023-08-29T15:44:27"),
DateTimeNullableValue = DateTime.Parse("2023-08-29T15:44:27")
});
}

Expand Down
2 changes: 2 additions & 0 deletions tests/DynaMight.UnitTests/DynaMightTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class DynaMightTestClass
public List<string>? StringListNullableValue { get; set; }
public InternalClass InternalClassValue { get; set; } = default!;
public InternalClass? InternalClassNullableValue { get; set; }
public DateTime DateTimeValue { get; set; } = DateTime.Parse("2023-08-29T15:44:27");
public DateTime? DateTimeNullableValue { get; set; }

[DynamoDBIgnore] public InternalClassNotSerializable? IgnoreClass { get; set; }

Expand Down

0 comments on commit 2091510

Please sign in to comment.