diff --git a/README.md b/README.md index de181f17..b9c15f4a 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ The source generator will look for attributes and implement interfaces that exis | Type | Field | Format | |------------------|-------|----------| | `DateOnly` | `S` | ISO 8601 | +| `TimeOnly` | `S` | ISO 8601 | | `DateTime` | `S` | ISO 8601 | | `DateTimeOffset` | `S` | ISO 8601 | | `TimeSpan` | `S` | ISO 8601 | diff --git a/src/DynamoDBGenerator/Converters/Internal/ISO8601TimeOnlyConverter.cs b/src/DynamoDBGenerator/Converters/Internal/ISO8601TimeOnlyConverter.cs new file mode 100644 index 00000000..51530571 --- /dev/null +++ b/src/DynamoDBGenerator/Converters/Internal/ISO8601TimeOnlyConverter.cs @@ -0,0 +1,17 @@ +using System; +using Amazon.DynamoDBv2.Model; + +namespace DynamoDBGenerator.Converters.Internal; + +internal class ISO8601TimeOnlyConverter: IValueTypeConverter +{ + public TimeOnly? Read(AttributeValue attributeValue) + { + return TimeOnly.TryParse(attributeValue.S, out var timeOnly) ? timeOnly : null; + } + + public AttributeValue Write(TimeOnly element) + { + return new AttributeValue {S = element.ToString("O")}; + } +} diff --git a/src/DynamoDBGenerator/Options/AttributeValueConverters.cs b/src/DynamoDBGenerator/Options/AttributeValueConverters.cs index 027d33c2..22aa4169 100644 --- a/src/DynamoDBGenerator/Options/AttributeValueConverters.cs +++ b/src/DynamoDBGenerator/Options/AttributeValueConverters.cs @@ -100,6 +100,11 @@ public class AttributeValueConverters /// The converter. /// public IValueTypeConverter DateOnlyConverter { get; protected init; } = new ISO8601DateOnlyConverter(); + + /// + /// The converter. + /// + public IValueTypeConverter TimeOnlyConverter { get; protected init; } = new ISO8601TimeOnlyConverter(); /// /// The converter. diff --git a/tests/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Marshaller/Types/TimeOnlyTests.cs b/tests/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Marshaller/Types/TimeOnlyTests.cs new file mode 100644 index 00000000..eceff4c9 --- /dev/null +++ b/tests/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Marshaller/Types/TimeOnlyTests.cs @@ -0,0 +1,40 @@ +using Amazon.DynamoDBv2.Model; +using DynamoDBGenerator.Attributes; +using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters; + +namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Types; + +[DynamoDBMarshaller(typeof(Container))] +public partial class TimeOnlyTests : RecordMarshalAsserter +{ + protected override Container UnmarshallImplementation(Dictionary attributeValues) + { + return ContainerMarshaller.Unmarshall(attributeValues); + } + protected override Dictionary MarshallImplementation(Container element) + { + return ContainerMarshaller.Marshall(element); + } + + public TimeOnlyTests() : base(new[] {new TimeOnly(22, 12, 08), new TimeOnly(21, 12, 09)}, x => new AttributeValue {S = x.ToString("O")}) + { + } +} + +[DynamoDBMarshaller(typeof(Container))] +public partial class NullableTimeOnlyTests : RecordMarshalAsserter +{ + + protected override Container UnmarshallImplementation(Dictionary attributeValues) + { + return ContainerMarshaller.Unmarshall(attributeValues); + } + protected override Dictionary MarshallImplementation(Container element) + { + return ContainerMarshaller.Marshall(element); + } + + public NullableTimeOnlyTests() : base(new[] {new TimeOnly(22, 12, 08), new TimeOnly(21, 12, 09)}.Cast().Append(null), x => x is null ? null : new AttributeValue {S = x.Value.ToString("O")}) + { + } +}