Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
.CreateScope(
$"if ({Value} is null || {Value}.SS is null)"
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
.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]\")")}));")
.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]\"))")});")
)
.ToConversion(),
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric => signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

public abstract class NoneNullableElementAsserter<TSet, TElement> : SetAsserter<TSet, TElement>
where TSet : IEnumerable<TElement> where TElement : class
public abstract class NoneNullableElementAsserter<TSet, TElement>(
IEnumerable<TElement> seed,
Func<IEnumerable<TElement>, TSet> fn
) : SetAsserter<TSet, TElement>(seed, fn)
where TSet : IEnumerable<TElement>
where TElement : class
{
protected NoneNullableElementAsserter(IEnumerable<TElement> seed, Func<IEnumerable<TElement>, TSet> fn) : base(seed,
fn)
{
}

protected static IEnumerable<string> Strings()
{
yield return "John";
yield return "Tom";
yield return "Michael";
}


[Fact]
public void Marshall_NullElement_ShouldThrow()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

public abstract class NullableElementAsserter<TSet, TElement> : SetAsserter<TSet, TElement?>
where TSet : IEnumerable<TElement?> where TElement : class
{
protected NullableElementAsserter(IEnumerable<TElement?> seed, Func<IEnumerable<TElement?>, TSet> fn) : base(seed,
fn)
{
}

protected static IEnumerable<string> Strings()
{
yield return "John";
yield return "Tom";
yield return "Michael";
}

[Fact]
public void Marshall_NullElement_ShouldNotThrow()
{
Arguments().Should().AllSatisfy(x =>
{
var items = x.element.Element.Append(null).ToList();
var args = CreateArguments(items);
var act = () => MarshallImplementation(args.element);
act.Should().NotThrow();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

public abstract class SetAsserter<TSet, TElement> : MarshalAsserter<Container<TSet>> where TSet : IEnumerable<TElement>
public abstract class SetAsserter<TSet, TElement>(
IEnumerable<TElement> seed,
Func<IEnumerable<TElement>, TSet> fn
) : MarshalAsserter<Container<TSet>>
where TSet : IEnumerable<TElement>
{
private static readonly bool IsStringSet = typeof(TElement) == typeof(string);
private readonly Func<IEnumerable<TElement>, TSet> _fn;
private readonly IEnumerable<TElement> _seed;

protected SetAsserter(IEnumerable<TElement> seed, Func<IEnumerable<TElement>, TSet> fn)
{
_seed = seed;
_fn = fn;
}


protected override IEnumerable<(Container<TSet> element, Dictionary<string, AttributeValue> attributeValues)>
Arguments()
{
yield return CreateArguments(_seed);
yield return CreateArguments(seed);
}

protected (Container<TSet> element, Dictionary<string, AttributeValue> attributeValues) CreateArguments(
IEnumerable<TElement> arg)
{
var res = _fn(arg);
var res = fn(arg);
if (res is not IReadOnlySet<TElement> or not ISet<TElement>)
throw new InvalidOperationException(
$"The type '{nameof(TSet)}' must either one of 'ISet<{nameof(TElement)}>, 'IReadonlySet<{nameof(TElement)}>'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<string>>))]
public partial class HashSetTests : NoneNullableElementAsserter<HashSet<string>, string>
public partial class HashSetTests()
: NoneNullableElementAsserter<HashSet<string>, string>(Strings(), x => new HashSet<string>(x))
{
public HashSetTests() : base(Strings(), x => new HashSet<string>(x))
{
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<HashSet<string>> element)
{
return ContainerMarshaller.Marshall(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<IReadOnlySet<string>>))]
public partial class IReadOnlySetTests : NoneNullableElementAsserter<IReadOnlySet<string>, string>
public partial class IReadOnlySetTests()
: NoneNullableElementAsserter<IReadOnlySet<string>, string>(Strings(), x => new SortedSet<string>(x))
{
public IReadOnlySetTests() : base(Strings(), x => new SortedSet<string>(x))
{
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(
Container<IReadOnlySet<string>> element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<ISet<string>>))]
public partial class ISetTests : NoneNullableElementAsserter<ISet<string>, string>
public partial class ISetTests() : NoneNullableElementAsserter<ISet<string>, string>(Strings(), x => new HashSet<string>(x))
{
public ISetTests() : base(Strings(), x => new HashSet<string>(x))
{
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<ISet<string>> element)
{
return ContainerMarshaller.Marshall(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<int>>))]
public partial class IntHashSetTests : SetAsserter<HashSet<int>, int>
public partial class IntHashSetTests() : SetAsserter<HashSet<int>, int>([2, 3, 4, 5], x => new HashSet<int>(x))
{
public IntHashSetTests() : base(new[] { 2, 3, 4, 5 }, x => new HashSet<int>(x))
{
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<HashSet<int>> element)
{
return ContainerMarshaller.Marshall(element);
Expand All @@ -24,6 +20,22 @@ protected override Container<HashSet<int>> UnmarshallImplementation(
}
}

// TODO Support
//[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<int?>>))]
//public partial class NUllableIntHashSetTests() : SetAsserter<HashSet<int?>, int?>([2, 3, 4, 5], x => new HashSet<int?>(x))
//{
// protected override Container<HashSet<int?>> UnmarshallImplementation(
// Dictionary<string, AttributeValue> attributeValues)
// {
// return ContainerMarshaller.Unmarshall(attributeValues);
// }
//
// protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<HashSet<int?>> element)
// {
// return ContainerMarshaller.Marshall(element);
// }
//}

[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<decimal>>))]
public partial class DecimalHashSetTests : SetAsserter<HashSet<decimal>, decimal>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<HashSet<string?>>))]
public partial class NullableHashSetTests() : NullableElementAsserter<HashSet<string?>, string>(Strings(), x => new HashSet<string?>(x))
{
protected override Container<HashSet<string?>> UnmarshallImplementation(Dictionary<string, AttributeValue> attributeValues)
{
return ContainerMarshaller.Unmarshall(attributeValues);
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<HashSet<string?>> element)
{
return ContainerMarshaller.Marshall(element);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<IReadOnlySet<string?>>))]
public partial class NullableIReadOnlySetTests() : NullableElementAsserter<IReadOnlySet<string?>, string>(Strings(), x => new HashSet<string?>(x))
{
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<IReadOnlySet<string?>> element)
{
return ContainerMarshaller.Marshall(element);
}

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

[Fact]
public void Unmarshall_Implementation_ShouldBeHashset()
{
Arguments().Should().AllSatisfy(x =>
ContainerMarshaller.Unmarshall(x.attributeValues).Element.Should().BeOfType<HashSet<string?>>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<ISet<string?>>))]
public partial class NullableISetTests() : NullableElementAsserter<ISet<string?>, string>(Strings(), x => new HashSet<string?>(x))
{
protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<ISet<string?>> element)
{
return ContainerMarshaller.Marshall(element);
}

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

[Fact]
public void Unmarshall_Implementation_ShouldBeHashset()
{
Arguments().Should().AllSatisfy(x =>
ContainerMarshaller.Unmarshall(x.attributeValues).Element.Should().BeOfType<HashSet<string?>>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets.Asserters;

namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<SortedSet<string?>>))]
public partial class NullableSortedSetTests() : NullableElementAsserter<SortedSet<string?>, string>(Strings(), x => new SortedSet<string?>(x))
{
protected override Container<SortedSet<string?>> UnmarshallImplementation(
Dictionary<string, AttributeValue> attributeValues)
{
return ContainerMarshaller.Unmarshall(attributeValues);
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<SortedSet<string?>> element)
{
return ContainerMarshaller.Marshall(element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Generics.Sets;

[DynamoDBMarshaller(EntityType = typeof(Container<SortedSet<string>>))]
public partial class SortedSetTests : NoneNullableElementAsserter<SortedSet<string>, string>
public partial class SortedSetTests() : NoneNullableElementAsserter<SortedSet<string>, string>(Strings(), x => new SortedSet<string>(x))
{
public SortedSetTests() : base(Strings(), x => new SortedSet<string>(x))
{
}

protected override Dictionary<string, AttributeValue> MarshallImplementation(Container<SortedSet<string>> element)
{
return ContainerMarshaller.Marshall(element);
Expand Down