Skip to content

Commit 258b94a

Browse files
committed
Use converter for string set
1 parent a057e75 commit 258b94a

File tree

6 files changed

+130
-41
lines changed

6 files changed

+130
-41
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+
}
Lines changed: 48 additions & 25 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

@@ -19,27 +21,29 @@ public class AttributeValueConverters
1921
/// <summary>
2022
/// The <see cref="DateTime"/> converter.
2123
/// </summary>
22-
public IValueTypeConverter<DateTime> DateTimeConverter { get; protected init;} = new ISO8601DateTimeConverter();
24+
public IValueTypeConverter<DateTime> DateTimeConverter { get; protected init; } = new ISO8601DateTimeConverter();
2325

2426
/// <summary>
2527
/// The <see cref="DateTimeOffsetConverter"/> converter.
2628
/// </summary>
27-
public IValueTypeConverter<DateTimeOffset> DateTimeOffsetConverter { get; protected init;} = new ISO8601DateTimeOffsetConverter();
29+
public IValueTypeConverter<DateTimeOffset> DateTimeOffsetConverter { get; protected init; } =
30+
new ISO8601DateTimeOffsetConverter();
2831

2932
/// <summary>
3033
/// The <see cref="bool"/> converter.
3134
/// </summary>
32-
public IValueTypeConverter<bool> BoolConverter { get; protected init;} = new BoolConverter();
35+
public IValueTypeConverter<bool> BoolConverter { get; protected init; } = new BoolConverter();
3336

3437
/// <summary>
3538
/// The <see cref="char"/> converter.
3639
/// </summary>
37-
public IValueTypeConverter<char> CharConverter { get; protected init;} = new CharConverter();
40+
public IValueTypeConverter<char> CharConverter { get; protected init; } = new CharConverter();
3841

3942
/// <summary>
4043
/// The <see cref="MemoryStream"/> converter.
4144
/// </summary>
42-
public IReferenceTypeConverter<MemoryStream> MemoryStreamConverter { get; protected init;} = new MemoryStreamConverter();
45+
public IReferenceTypeConverter<MemoryStream> MemoryStreamConverter { get; protected init; } =
46+
new MemoryStreamConverter();
4347

4448
/// <summary>
4549
/// The <see cref="int"/> converter.
@@ -49,58 +53,58 @@ public class AttributeValueConverters
4953
/// <summary>
5054
/// The <see cref="decimal"/> converter.
5155
/// </summary>
52-
public IValueTypeConverter<decimal> DecimalConverter { get; protected init;} = new DecimalConverter();
56+
public IValueTypeConverter<decimal> DecimalConverter { get; protected init; } = new DecimalConverter();
5357

5458
/// <summary>
5559
/// The <see cref="double"/> converter.
5660
/// </summary>
57-
public IValueTypeConverter<double> DoubleConverter { get; protected init;} = new DoubleConverter();
61+
public IValueTypeConverter<double> DoubleConverter { get; protected init; } = new DoubleConverter();
5862

5963
/// <summary>
6064
/// The <see cref="float"/> converter.
6165
/// </summary>
62-
public IValueTypeConverter<float> FloatConverter { get; protected init;} = new FloatConverter();
66+
public IValueTypeConverter<float> FloatConverter { get; protected init; } = new FloatConverter();
6367

6468
/// <summary>
6569
/// The <see cref="long"/> converter.
6670
/// </summary>
67-
public IValueTypeConverter<long> LongConverter { get; protected init;} = new LongConverter();
71+
public IValueTypeConverter<long> LongConverter { get; protected init; } = new LongConverter();
6872

6973
/// <summary>
7074
/// The <see cref="ulong"/> converter.
7175
/// </summary>
72-
public IValueTypeConverter<ulong> ULongConverter { get; protected init;} = new ULongConverter();
73-
76+
public IValueTypeConverter<ulong> ULongConverter { get; protected init; } = new ULongConverter();
77+
7478
/// <summary>
7579
/// The <see cref="uint"/> converter.
7680
/// </summary>
77-
public IValueTypeConverter<uint> UIntConverter { get; protected init;} = new UIntConverter();
78-
81+
public IValueTypeConverter<uint> UIntConverter { get; protected init; } = new UIntConverter();
82+
7983
/// <summary>
8084
/// The <see cref="sbyte"/> converter.
8185
/// </summary>
82-
public IValueTypeConverter<sbyte> SbyteConverter { get; protected init;} = new SByteConverter();
83-
86+
public IValueTypeConverter<sbyte> SbyteConverter { get; protected init; } = new SByteConverter();
87+
8488
/// <summary>
8589
/// The <see cref="short"/> converter.
8690
/// </summary>
87-
public IValueTypeConverter<short> ShortConverter { get; protected init;} = new ShortConverter();
88-
91+
public IValueTypeConverter<short> ShortConverter { get; protected init; } = new ShortConverter();
92+
8993
/// <summary>
9094
/// The <see cref="byte"/> converter.
9195
/// </summary>
92-
public IValueTypeConverter<byte> ByteConverter { get; protected init;} = new ByteConverter();
93-
96+
public IValueTypeConverter<byte> ByteConverter { get; protected init; } = new ByteConverter();
97+
9498
/// <summary>
9599
/// The <see cref="ushort"/> converter.
96100
/// </summary>
97-
public IValueTypeConverter<ushort> UShortConverter { get; protected init;} = new UShortConverter();
98-
101+
public IValueTypeConverter<ushort> UShortConverter { get; protected init; } = new UShortConverter();
102+
99103
/// <summary>
100104
/// The <see cref="DateOnly"/> converter.
101105
/// </summary>
102106
public IValueTypeConverter<DateOnly> DateOnlyConverter { get; protected init; } = new ISO8601DateOnlyConverter();
103-
107+
104108
/// <summary>
105109
/// The <see cref="TimeOnly"/> converter.
106110
/// </summary>
@@ -109,11 +113,30 @@ public class AttributeValueConverters
109113
/// <summary>
110114
/// The <see cref="TimeSpan"/> converter.
111115
/// </summary>
112-
public IValueTypeConverter<TimeSpan> TimeSpanConverter { get; protected init;} = new ISO8601TimeSpanConveter();
113-
116+
public IValueTypeConverter<TimeSpan> TimeSpanConverter { get; protected init; } = new ISO8601TimeSpanConveter();
117+
114118
/// <summary>
115119
/// The <see cref="Guid"/> converter.
116120
/// </summary>
117-
public IValueTypeConverter<Guid> GuidConverter { get; protected init;} = new GuidConverter();
121+
public IValueTypeConverter<Guid> GuidConverter { get; protected init; } = new GuidConverter();
118122

123+
/// <summary>
124+
/// The <see cref="ISet{T}"/> converter for <see cref="string"/> sets.
125+
/// </summary>
126+
public IReferenceTypeConverter<ISet<string>> ISetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
127+
128+
/// <summary>
129+
/// The <see cref="IReadOnlySet{T}"/> converter for <see cref="string"/> sets.
130+
/// </summary>
131+
public IReferenceTypeConverter<IReadOnlySet<string>> IReadOnlySetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
132+
133+
/// <summary>
134+
/// The <see cref="HashSet{T}"/> converter for <see cref="string"/> sets.
135+
/// </summary>
136+
public IReferenceTypeConverter<HashSet<string>> HashSetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
137+
138+
/// <summary>
139+
/// The <see cref="SortedSet{T}"/> converter for <see cref="string"/> sets.
140+
/// </summary>
141+
public IReferenceTypeConverter<SortedSet<string>> SortedSetConverter { get; protected init; } = Singleton.Static<StringSetConverter>();
119142
}

0 commit comments

Comments
 (0)