Skip to content

Commit ad781c5

Browse files
authored
Add timeonly converter (#69)
1 parent 653389e commit ad781c5

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The source generator will look for attributes and implement interfaces that exis
7171
| Type | Field | Format |
7272
|------------------|-------|----------|
7373
| `DateOnly` | `S` | ISO 8601 |
74+
| `TimeOnly` | `S` | ISO 8601 |
7475
| `DateTime` | `S` | ISO 8601 |
7576
| `DateTimeOffset` | `S` | ISO 8601 |
7677
| `TimeSpan` | `S` | ISO 8601 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Amazon.DynamoDBv2.Model;
3+
4+
namespace DynamoDBGenerator.Converters.Internal;
5+
6+
internal class ISO8601TimeOnlyConverter: IValueTypeConverter<TimeOnly>
7+
{
8+
public TimeOnly? Read(AttributeValue attributeValue)
9+
{
10+
return TimeOnly.TryParse(attributeValue.S, out var timeOnly) ? timeOnly : null;
11+
}
12+
13+
public AttributeValue Write(TimeOnly element)
14+
{
15+
return new AttributeValue {S = element.ToString("O")};
16+
}
17+
}

src/DynamoDBGenerator/Options/AttributeValueConverters.cs

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public class AttributeValueConverters
100100
/// The <see cref="DateOnly"/> converter.
101101
/// </summary>
102102
public IValueTypeConverter<DateOnly> DateOnlyConverter { get; protected init; } = new ISO8601DateOnlyConverter();
103+
104+
/// <summary>
105+
/// The <see cref="TimeOnly"/> converter.
106+
/// </summary>
107+
public IValueTypeConverter<TimeOnly> TimeOnlyConverter { get; protected init; } = new ISO8601TimeOnlyConverter();
103108

104109
/// <summary>
105110
/// The <see cref="TimeSpan"/> converter.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
5+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Types;
6+
7+
[DynamoDBMarshaller(typeof(Container<TimeOnly>))]
8+
public partial class TimeOnlyTests : RecordMarshalAsserter<TimeOnly>
9+
{
10+
protected override Container<TimeOnly> UnmarshallImplementation(Dictionary<string, AttributeValue> attributeValues)
11+
{
12+
return ContainerMarshaller.Unmarshall(attributeValues);
13+
}
14+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<TimeOnly> element)
15+
{
16+
return ContainerMarshaller.Marshall(element);
17+
}
18+
19+
public TimeOnlyTests() : base(new[] {new TimeOnly(22, 12, 08), new TimeOnly(21, 12, 09)}, x => new AttributeValue {S = x.ToString("O")})
20+
{
21+
}
22+
}
23+
24+
[DynamoDBMarshaller(typeof(Container<TimeOnly?>))]
25+
public partial class NullableTimeOnlyTests : RecordMarshalAsserter<TimeOnly?>
26+
{
27+
28+
protected override Container<TimeOnly?> UnmarshallImplementation(Dictionary<string, AttributeValue> attributeValues)
29+
{
30+
return ContainerMarshaller.Unmarshall(attributeValues);
31+
}
32+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<TimeOnly?> element)
33+
{
34+
return ContainerMarshaller.Marshall(element);
35+
}
36+
37+
public NullableTimeOnlyTests() : base(new[] {new TimeOnly(22, 12, 08), new TimeOnly(21, 12, 09)}.Cast<TimeOnly?>().Append(null), x => x is null ? null : new AttributeValue {S = x.Value.ToString("O")})
38+
{
39+
}
40+
}

0 commit comments

Comments
 (0)