Skip to content

Commit 8a01b7a

Browse files
committed
Update tests
1 parent 26aaa63 commit 8a01b7a

26 files changed

Lines changed: 339 additions & 18 deletions

src/DynamoDBGenerator.SourceGenerator/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static class Marshaller
8080

8181
public static class AttributeValueUtilityFactory
8282
{
83-
private const string ClassName = "MarshallHelper";
83+
public const string ClassName = "MarshallHelper";
8484
public const string ToAttributeValue = $"{ClassName}.ToAttributeValue";
8585
public const string Null = $"{ClassName}.Null";
8686
public const string ToList = $"{ClassName}.ToList";

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
139139
.CreateScope(
140140
$"if ({Value} is null || {Value}.NS is null)"
141141
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
142-
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{singleGeneric.T.UnannotatedString}>" : null)}({Value}.NS.Select(y => {singleGeneric.T.UnannotatedString}.Parse(y)));")
142+
.Append($"return {AttributeValueUtilityFactory.ClassName}.{(singleGeneric.T.IsSupposedToBeNull ? $"ToNullableNumber{typeIdentifier.TypeSymbol.Name}" : $"ToNumber{typeIdentifier.TypeSymbol.Name}")}<{singleGeneric.T.UnannotatedString}>({Value}.NS, {DataMember});")
143143
)
144144
.ToConversion(singleGeneric),
145145
SingleGeneric.SupportedType.Set => throw new ArgumentException("Only string and integers are supported for sets", UncoveredConversionException(singleGeneric, nameof(CreateMethod))),

src/DynamoDBGenerator.SourceGenerator/Types/TypeIdentifier.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ private static (string annotated, string original) Representation(ITypeSymbol ty
5454
return RepresentationDictionary.GetOrAdd(typeSymbol, x =>
5555
{
5656
var displayString = x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
57-
return RepresentationDictionary[typeSymbol] = (ToString(typeSymbol, displayString), displayString);
57+
58+
var unAnnotated = x.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T
59+
? displayString.Substring(0, displayString.Length - 1)
60+
: displayString;
61+
62+
return RepresentationDictionary[typeSymbol] = (ToString(typeSymbol, displayString), unAnnotated);
5863
});
5964

6065
static string ToString(ITypeSymbol x, string displayString)

src/DynamoDBGenerator/Internal/MarshallHelper.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Buffers;
3+
using System.Collections;
34
using System.Collections.Generic;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
@@ -103,7 +104,7 @@ public static AttributeValue FromStringSet(IEnumerable<string> numbers, string?
103104
ns = [];
104105

105106
// ReSharper disable once LoopCanBeConvertedToQuery
106-
107+
107108
foreach (var element in numbers)
108109
{
109110
ns.Add(element ?? throw ExceptionHelper.NotNull($"{dataMember}[{element}]"));
@@ -133,6 +134,69 @@ public static AttributeValue FromNumberSet<T>(IEnumerable<T> numbers, string? da
133134
return new AttributeValue { NS = ns };
134135
}
135136

137+
public static ISet<TNumber> ToNumberISet<TNumber>(
138+
List<string> ss,
139+
string? dataMember
140+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
141+
{
142+
throw new Exception();
143+
}
144+
145+
public static IReadOnlySet<TNumber> ToNumberIReadOnlySet<TNumber>(
146+
List<string> ss,
147+
string? dataMember
148+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
149+
{
150+
throw new Exception();
151+
}
152+
153+
public static HashSet<TNumber> ToNumberHashSet<TNumber>(
154+
List<string> ss,
155+
string? dataMember
156+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
157+
{
158+
throw new Exception();
159+
}
160+
161+
public static SortedSet<TNumber> ToNumberSortedSet<TNumber>(
162+
List<string> ss,
163+
string? dataMember
164+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
165+
{
166+
throw new Exception();
167+
}
168+
public static ISet<TNumber?> ToNullableNumberISet<TNumber>(
169+
List<string?> ss,
170+
string? dataMember
171+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
172+
{
173+
throw new Exception();
174+
}
175+
176+
public static IReadOnlySet<TNumber?> ToNullableNumberIReadOnlySet<TNumber>(
177+
List<string?> ss,
178+
string? dataMember
179+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
180+
{
181+
throw new Exception();
182+
}
183+
184+
public static HashSet<TNumber?> ToNullableNumberHashSet<TNumber>(
185+
List<string?> ss,
186+
string? dataMember
187+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
188+
{
189+
throw new Exception();
190+
}
191+
192+
public static SortedSet<TNumber?> ToNullableNumberSortedSet<TNumber>(
193+
List<string?> ss,
194+
string? dataMember
195+
) where TNumber : struct, IParsable<TNumber>, INumber<TNumber>
196+
{
197+
throw new Exception();
198+
}
199+
136200
public static ILookup<string, T> ToLookup<T, TArgument>(
137201
Dictionary<string, AttributeValue> dictionary,
138202
TArgument argument,

tests/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Marshaller/Generics/Sets/DecimalHashSetTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
77

88
[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<decimal>>))]
9-
public partial class DecimalHashSetTests : SetAsserter<HashSet<decimal>, decimal>
9+
public partial class DecimalHashSetTests()
10+
: SetAsserter<HashSet<decimal>, decimal>([2032m, 0.323232932m, 0.9329392m], x => new HashSet<decimal>(x))
1011
{
11-
public DecimalHashSetTests() : base([2032m, 0.323232932m, 0.9329392m], x => new HashSet<decimal>(x))
12-
{
13-
}
14-
1512
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<HashSet<decimal>> element)
1613
{
1714
return ContainerMarshaller.Marshall(element);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;
5+
6+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
7+
8+
[DynamoDBMarshaller(EntityType = typeof(Container<IReadOnlySet<decimal>>))]
9+
public partial class DecimalIReadOnlySetTests() : SetAsserter<IReadOnlySet<decimal>, decimal>([2032m, 0.323232932m, 0.9329392m], x => new HashSet<decimal>(x))
10+
{
11+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<IReadOnlySet<decimal>> element)
12+
{
13+
return ContainerMarshaller.Marshall(element);
14+
}
15+
16+
protected override Container<IReadOnlySet<decimal>> UnmarshallImplementation(
17+
Dictionary<string, AttributeValue> attributeValues)
18+
{
19+
return ContainerMarshaller.Unmarshall(attributeValues);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;
5+
6+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
7+
8+
[DynamoDBMarshaller(EntityType = typeof(Container<ISet<decimal>>))]
9+
public partial class DecimalISetTests() : SetAsserter<ISet<decimal>, decimal>([2032m, 0.323232932m, 0.9329392m], x => new HashSet<decimal>(x))
10+
{
11+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<ISet<decimal>> element)
12+
{
13+
return ContainerMarshaller.Marshall(element);
14+
}
15+
16+
protected override Container<ISet<decimal>> UnmarshallImplementation(
17+
Dictionary<string, AttributeValue> attributeValues)
18+
{
19+
return ContainerMarshaller.Unmarshall(attributeValues);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;
5+
6+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
7+
8+
[DynamoDBMarshaller(EntityType = typeof(Container<SortedSet<decimal>>))]
9+
public partial class DecimalSortedSetTests() : SetAsserter<SortedSet<decimal>, decimal>([2032m, 0.323232932m, 0.9329392m], x => new SortedSet<decimal>(x))
10+
{
11+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<SortedSet<decimal>> element)
12+
{
13+
return ContainerMarshaller.Marshall(element);
14+
}
15+
16+
protected override Container<SortedSet<decimal>> UnmarshallImplementation(
17+
Dictionary<string, AttributeValue> attributeValues)
18+
{
19+
return ContainerMarshaller.Unmarshall(attributeValues);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;
5+
6+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
7+
8+
[DynamoDBMarshaller(EntityType = typeof(Container<IReadOnlySet<int>>))]
9+
public partial class IntIReadOnlySetTests() : SetAsserter<IReadOnlySet<int>, int>([2, 3, 4, 5], x => new HashSet<int>(x))
10+
{
11+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<IReadOnlySet<int>> element)
12+
{
13+
return ContainerMarshaller.Marshall(element);
14+
}
15+
16+
protected override Container<IReadOnlySet<int>> UnmarshallImplementation(
17+
Dictionary<string, AttributeValue> attributeValues)
18+
{
19+
return ContainerMarshaller.Unmarshall(attributeValues);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
4+
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;
5+
6+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;
7+
8+
[DynamoDBMarshaller(EntityType = typeof(Container<ISet<int>>))]
9+
public partial class IntISetTests() : SetAsserter<ISet<int>, int>([2, 3, 4, 5], x => new HashSet<int>(x))
10+
{
11+
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<ISet<int>> element)
12+
{
13+
return ContainerMarshaller.Marshall(element);
14+
}
15+
16+
protected override Container<ISet<int>> UnmarshallImplementation(
17+
Dictionary<string, AttributeValue> attributeValues)
18+
{
19+
return ContainerMarshaller.Unmarshall(attributeValues);
20+
}
21+
}

0 commit comments

Comments
 (0)