Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeonly converter #69

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Amazon.DynamoDBv2.Model;

namespace DynamoDBGenerator.Converters.Internal;

internal class ISO8601TimeOnlyConverter: IValueTypeConverter<TimeOnly>
{
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")};
}
}
5 changes: 5 additions & 0 deletions src/DynamoDBGenerator/Options/AttributeValueConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public class AttributeValueConverters
/// The <see cref="DateOnly"/> converter.
/// </summary>
public IValueTypeConverter<DateOnly> DateOnlyConverter { get; protected init; } = new ISO8601DateOnlyConverter();

/// <summary>
/// The <see cref="TimeOnly"/> converter.
/// </summary>
public IValueTypeConverter<TimeOnly> TimeOnlyConverter { get; protected init; } = new ISO8601TimeOnlyConverter();

/// <summary>
/// The <see cref="TimeSpan"/> converter.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TimeOnly>))]
public partial class TimeOnlyTests : RecordMarshalAsserter<TimeOnly>
{
protected override Container<TimeOnly> UnmarshallImplementation(Dictionary<string, AttributeValue> attributeValues)
{
return ContainerMarshaller.Unmarshall(attributeValues);
}
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<TimeOnly> 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<TimeOnly?>))]
public partial class NullableTimeOnlyTests : RecordMarshalAsserter<TimeOnly?>
{

protected override Container<TimeOnly?> UnmarshallImplementation(Dictionary<string, AttributeValue> attributeValues)
{
return ContainerMarshaller.Unmarshall(attributeValues);
}
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<TimeOnly?> element)
{
return ContainerMarshaller.Marshall(element);
}

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")})
{
}
}
Loading