From 20915109cdf9194fd1c9c37fc721c029bf5716e0 Mon Sep 17 00:00:00 2001 From: Cussa de Souza Date: Thu, 31 Aug 2023 11:47:47 +0100 Subject: [PATCH] feat(LBE-495): add DateTime converters --- src/DynaMight/Converters/DynamoValueConverter.cs | 8 ++++++++ .../Converters/ClassAttributeValueTests.cs | 2 ++ tests/DynaMight.UnitTests/Converters/ObjectTests.cs | 11 ++++++++--- tests/DynaMight.UnitTests/DynaMightTestClass.cs | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/DynaMight/Converters/DynamoValueConverter.cs b/src/DynaMight/Converters/DynamoValueConverter.cs index 1d1dd98..dfad138 100644 --- a/src/DynaMight/Converters/DynamoValueConverter.cs +++ b/src/DynaMight/Converters/DynamoValueConverter.cs @@ -64,6 +64,14 @@ public static class DynamoValueConverter ToDynamoDbEntry = v => (List)v } }, + { + typeof(DateTime), new CustomConverter + { + ToAttributeValue = v => new AttributeValue { S = ((DateTime)v).ToString("s") }, + ToObject = v => DateTime.Parse(v.S), + ToDynamoDbEntry = v => (DateTime)v + } + }, }; /// diff --git a/tests/DynaMight.UnitTests/Converters/ClassAttributeValueTests.cs b/tests/DynaMight.UnitTests/Converters/ClassAttributeValueTests.cs index f035ff3..3c25041 100644 --- a/tests/DynaMight.UnitTests/Converters/ClassAttributeValueTests.cs +++ b/tests/DynaMight.UnitTests/Converters/ClassAttributeValueTests.cs @@ -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 } }, }); } } \ No newline at end of file diff --git a/tests/DynaMight.UnitTests/Converters/ObjectTests.cs b/tests/DynaMight.UnitTests/Converters/ObjectTests.cs index fd2fe8e..f057233 100644 --- a/tests/DynaMight.UnitTests/Converters/ObjectTests.cs +++ b/tests/DynaMight.UnitTests/Converters/ObjectTests.cs @@ -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(); @@ -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(); @@ -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") }); } diff --git a/tests/DynaMight.UnitTests/DynaMightTestClass.cs b/tests/DynaMight.UnitTests/DynaMightTestClass.cs index 58ed217..2a8483f 100644 --- a/tests/DynaMight.UnitTests/DynaMightTestClass.cs +++ b/tests/DynaMight.UnitTests/DynaMightTestClass.cs @@ -26,6 +26,8 @@ public class DynaMightTestClass public List? 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; }