Skip to content

Commit f09e152

Browse files
committed
Use converter for string set
1 parent eb0d1e7 commit f09e152

File tree

6 files changed

+103
-16
lines changed

6 files changed

+103
-16
lines changed

src/DynamoDBGenerator.SourceGenerator/Generations/Marshalling/Marshaller.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ or SingleGeneric.SupportedType.IEnumerable
120120
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
121121
.Append($"return {AttributeValueUtilityFactory.FromEnumerable}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsSupposedToBeNull ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
122122
).ToConversion(singleGeneric.T),
123-
SingleGeneric.SupportedType.Set when singleGeneric.T.TypeSymbol.SpecialType is SpecialType.System_String
124-
=> signature
125-
.CreateScope(
126-
$"if ({ParamReference} is null)"
127-
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
128-
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ SS = new List<{(singleGeneric.T.IsSupposedToBeNull ? "string?" : "string")}>({(singleGeneric.T.IsSupposedToBeNull ? ParamReference : $"{ParamReference}.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\"))")})}};")
129-
)
130-
.ToConversion(singleGeneric.T),
131123
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric
132124
=> signature
133125
.CreateScope(

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,6 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
128128
.Append($"return {AttributeValueUtilityFactory.ToEnumerable}({Value}.L, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(singleGeneric.T, "a", "d", options, "o")});")
129129
)
130130
.ToConversion(singleGeneric.T),
131-
SingleGeneric.SupportedType.Set when singleGeneric.T.TypeSymbol.SpecialType is SpecialType.System_String => signature
132-
.CreateScope(
133-
$"if ({Value} is null || {Value}.SS is null)"
134-
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
135-
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{(singleGeneric.T.IsSupposedToBeNull ? "string?" : "string")}>" : null)}({(singleGeneric.T.IsSupposedToBeNull ? $"{Value}.SS" : $"{Value}.SS.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\")")}));")
136-
)
137-
.ToConversion(),
138131
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric => signature
139132
.CreateScope(
140133
$"if ({Value} is null || {Value}.NS is null)"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
3+
using Amazon.DynamoDBv2.Model;
4+
using DynamoDBGenerator.Internal;
5+
6+
namespace DynamoDBGenerator.Converters.Internal;
7+
8+
internal sealed class StringSetConverter :
9+
IReferenceTypeConverter<IReadOnlySet<string>>,
10+
IReferenceTypeConverter<HashSet<string>>,
11+
IReferenceTypeConverter<ISet<string>>,
12+
IReferenceTypeConverter<SortedSet<string>>,
13+
IStaticSingleton<StringSetConverter>
14+
{
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
private static HashSet<string>? ToHashSet(AttributeValue attributeValue)
17+
{
18+
return attributeValue.IsSSSet
19+
? new HashSet<string>(attributeValue.SS)
20+
: null;
21+
}
22+
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
private static AttributeValue ToAttributeValue(IEnumerable<string> enumerable)
25+
{
26+
return new AttributeValue { SS = new List<string>(enumerable) };
27+
}
28+
29+
IReadOnlySet<string>? IReferenceTypeConverter<IReadOnlySet<string>>.Read(AttributeValue attributeValue)
30+
{
31+
return ToHashSet(attributeValue);
32+
}
33+
34+
ISet<string>? IReferenceTypeConverter<ISet<string>>.Read(AttributeValue attributeValue)
35+
{
36+
return ToHashSet(attributeValue);
37+
}
38+
39+
HashSet<string>? IReferenceTypeConverter<HashSet<string>>.Read(AttributeValue attributeValue)
40+
{
41+
return ToHashSet(attributeValue);
42+
}
43+
44+
SortedSet<string>? IReferenceTypeConverter<SortedSet<string>>.Read(AttributeValue attributeValue)
45+
{
46+
return attributeValue.IsSSSet
47+
? new SortedSet<string>(attributeValue.SS)
48+
: null;
49+
}
50+
51+
AttributeValue IReferenceTypeConverter<SortedSet<string>>.Write(SortedSet<string> element)
52+
{
53+
return ToAttributeValue(element);
54+
}
55+
56+
AttributeValue IReferenceTypeConverter<ISet<string>>.Write(ISet<string> element)
57+
{
58+
return ToAttributeValue(element);
59+
}
60+
61+
AttributeValue IReferenceTypeConverter<HashSet<string>>.Write(HashSet<string> element)
62+
{
63+
return ToAttributeValue(element);
64+
}
65+
66+
AttributeValue IReferenceTypeConverter<IReadOnlySet<string>>.Write(IReadOnlySet<string> element)
67+
{
68+
return ToAttributeValue(element);
69+
}
70+
}

src/DynamoDBGenerator/DynamoDBGenerator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<IsPackable>true</IsPackable>
7-
<LangVersion>10.0</LangVersion>
87
<PackageVersion>0.0.0</PackageVersion>
98
<Title>DynamoDBGenerator</Title>
109
<Authors>Robert Andersson</Authors>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace DynamoDBGenerator.Internal;
2+
3+
internal interface IStaticSingleton<out T> where T : new()
4+
{
5+
private static T? _backingField;
6+
internal static virtual T Singleton => _backingField ??= new T();
7+
}
8+
9+
internal static class Singleton
10+
{
11+
public static T Static<T>() where T : IStaticSingleton<T>, new() => T.Singleton;
12+
}

src/DynamoDBGenerator/Options/AttributeValueConverters.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using DynamoDBGenerator.Converters;
45
using DynamoDBGenerator.Converters.Internal;
6+
using DynamoDBGenerator.Internal;
57

68
namespace DynamoDBGenerator.Options;
79

@@ -115,5 +117,24 @@ public class AttributeValueConverters
115117
/// The <see cref="Guid"/> converter.
116118
/// </summary>
117119
public IValueTypeConverter<Guid> GuidConverter { get; protected init;} = new GuidConverter();
120+
121+
/// <summary>
122+
/// The <see cref="ISet{T}"/> converter for <see cref="string"/> sets.
123+
/// </summary>
124+
public IReferenceTypeConverter<ISet<string>> ISetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
125+
126+
/// <summary>
127+
/// The <see cref="IReadOnlySet{T}"/> converter for <see cref="string"/> sets.
128+
/// </summary>
129+
public IReferenceTypeConverter<IReadOnlySet<string>> IReadOnlySetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
118130

131+
/// <summary>
132+
/// The <see cref="HashSet{T}"/> converter for <see cref="string"/> sets.
133+
/// </summary>
134+
public IReferenceTypeConverter<HashSet<string>> HashSetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
135+
136+
/// <summary>
137+
/// The <see cref="SortedSet{T}"/> converter for <see cref="string"/> sets.
138+
/// </summary>
139+
public IReferenceTypeConverter<SortedSet<string>> SortedSetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
119140
}

0 commit comments

Comments
 (0)