Skip to content

Commit

Permalink
Include member name for better exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Sep 16, 2023
1 parent 403d78d commit e55220e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.Exceptions;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Deserialize;

[DynamoDBMarshaller(typeof(RequiredReferenceTypeValueMissingClass))]
Expand All @@ -11,14 +12,14 @@ public partial class MissingValueTests
public void Deserialize_RequiredReferenceTypeValueMissingClass_NoKeyValueProvidedShouldThrow()
{
var act = () => RequiredReferenceTypeValueMissingClassMarshaller.Unmarshall(new Dictionary<string, AttributeValue>());
act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>();
}

[Fact]
public void Deserialize_RequiredReferenceTypeValueMissingClass_KeyWithoutValueProvidedShouldThrow()
{
var act = () => RequiredReferenceTypeValueMissingClassMarshaller.Unmarshall(new Dictionary<string, AttributeValue> {{"RequiredProperty", new AttributeValue {S = null}}});
act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>();
}

[Fact]
Expand All @@ -39,14 +40,14 @@ public void Deserialize_OptionalReferenceTypeValueMissingClass_NoKeyValueProvide
public void Deserialize_RequiredValueTypeValueMissingClass_KeyWithoutValueProvidedShouldThrow()
{
var act = () => RequiredValueTypeValueMissingClassMarshaller.Unmarshall(new Dictionary<string, AttributeValue> {{"RequiredProperty", new AttributeValue {N = null}}});
act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>();
}

[Fact]
public void Deserialize_RequiredValueTypeValueMissingClass_NoKeyValueProvidedShouldThrow()
{
var act = () => RequiredValueTypeValueMissingClassMarshaller.Unmarshall(new Dictionary<string, AttributeValue>());
act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>().WithMessage("The data member is not supposed to be null, to allow this; make the data member nullable. (Data member 'RequiredProperty')");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.Exceptions;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Deserialize.Types;

[DynamoDBMarshaller(typeof(WeekDayClass))]
Expand Down Expand Up @@ -53,7 +54,7 @@ public void Deserialize_WeekDayClass_NoKeyValue()
.Unmarshall(new Dictionary<string, AttributeValue>())
.Should();

act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>();

}

Expand All @@ -64,7 +65,7 @@ public void Deserialize_WeekDayClass_NoValueProvided()
.Unmarshall(new Dictionary<string, AttributeValue> {{nameof(WeekDayClass.DayOfWeek), new AttributeValue {N = null}}})
.Should();

act.Should().Throw<ArgumentNullException>();
act.Should().Throw<DynamoDBMarshallingException>();

}
[Fact]
Expand Down
1 change: 0 additions & 1 deletion DynamoDBGenerator.SourceGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static class Errors
public const int MaxMethodNameLenght = 511;

public const string NotNullErrorMessage = "The data member is not supposed to be null, to allow this; make the data member nullable.";

}

public static class ConstantExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.SourceGenerator.Types;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -96,9 +99,9 @@ private Assignment BuildList(in ITypeSymbol elementType, in string accessPattern
return new Assignment(in outerAssignment, in elementType, innerAssignment.HasExternalDependency);
}

private Assignment BuildPocoList(in SingleGeneric singleGeneric, in string? operation, in string accessPattern, in string defaultCause)
private Assignment BuildPocoList(in SingleGeneric singleGeneric, in string? operation, in string accessPattern, in string defaultCause, in string memberName)
{
var innerAssignment = DataMemberAssignment(singleGeneric.T, "y");
var innerAssignment = DataMemberAssignment(singleGeneric.T, "y", in memberName);
var outerAssignment = $"{accessPattern} switch {{ {{ L: {{ }} x }} => x.Select(y => {innerAssignment.Value}){operation}, {defaultCause} }}";

return new Assignment(in outerAssignment, singleGeneric.T, innerAssignment.HasExternalDependency);
Expand Down Expand Up @@ -217,12 +220,13 @@ public string CreateDynamoDbDocumentProperty(Accessibility accessibility)
{@class}";
}

private Assignment DataMemberAssignment(in ITypeSymbol type, in string pattern)
private Assignment DataMemberAssignment(in ITypeSymbol type, in string pattern, in string memberName)
{
var defaultCase = type.IsNullable() ? "_ => null" : @$"_ => throw new ArgumentNullException(""{Constants.NotNullErrorMessage}"")";
return Execution(in type, in pattern, defaultCase);

var defaultCase = type.IsNullable() ? "_ => null" : @$"_ => throw new DynamoDBMarshallingException(""{memberName}"", ""{Constants.NotNullErrorMessage}"")";
return Execution(in type, in pattern, defaultCase, in memberName);

Assignment Execution(in ITypeSymbol typeSymbol, in string accessPattern, string @default)
Assignment Execution(in ITypeSymbol typeSymbol, in string accessPattern, string @default, in string memberName)
{
if (typeSymbol.GetKnownType() is not { } knownType) return ExternalAssignment(in typeSymbol, in accessPattern);

Expand Down Expand Up @@ -252,15 +256,15 @@ Assignment Execution(in ITypeSymbol typeSymbol, in string accessPattern, string
},
SingleGeneric singleGeneric => singleGeneric.Type switch
{
SingleGeneric.SupportedType.Nullable => Execution(singleGeneric.T, in accessPattern, @default),
SingleGeneric.SupportedType.Nullable => Execution(singleGeneric.T, in accessPattern, @default, in memberName),
SingleGeneric.SupportedType.Set => BuildPocoSet(singleGeneric.T, in accessPattern, in @default),
SingleGeneric.SupportedType.Array => BuildPocoList(in singleGeneric, ".ToArray()", in accessPattern, in @default),
SingleGeneric.SupportedType.ICollection => BuildPocoList(in singleGeneric, ".ToList()", in accessPattern, in @default),
SingleGeneric.SupportedType.IReadOnlyCollection => BuildPocoList(in singleGeneric, ".ToArray()", in accessPattern, in @default),
SingleGeneric.SupportedType.IEnumerable => BuildPocoList(in singleGeneric, null, in accessPattern, in @default),
SingleGeneric.SupportedType.Array => BuildPocoList(in singleGeneric, ".ToArray()", in accessPattern, in @default, in memberName),
SingleGeneric.SupportedType.ICollection => BuildPocoList(in singleGeneric, ".ToList()", in accessPattern, in @default, in memberName),
SingleGeneric.SupportedType.IReadOnlyCollection => BuildPocoList(in singleGeneric, ".ToArray()", in accessPattern, in @default, in memberName),
SingleGeneric.SupportedType.IEnumerable => BuildPocoList(in singleGeneric, null, in accessPattern, in @default, in memberName),
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
},
KeyValueGeneric keyValueGeneric => StringKeyedPocoGeneric(in keyValueGeneric, in accessPattern, @default),
KeyValueGeneric keyValueGeneric => StringKeyedPocoGeneric(in keyValueGeneric, in accessPattern, in @default, in memberName),
_ => null
};

Expand Down Expand Up @@ -564,7 +568,7 @@ private Conversion StaticPocoFactory(ITypeSymbol type)
{
var assignments = typeSymbol
.GetDynamoDbProperties()
.Select(x => (DDB: x, Assignment: DataMemberAssignment(x.DataMember.Type, @$"{paramReference}.GetValueOrDefault(""{x.AttributeName}"")")))
.Select(x => (DDB: x, Assignment: DataMemberAssignment(x.DataMember.Type, @$"{paramReference}.GetValueOrDefault(""{x.AttributeName}"")", x.DataMember.Name)))
.ToArray();
if (typeSymbol.IsTupleType)
Expand Down Expand Up @@ -653,21 +657,21 @@ _ when namedTypeSymbol.InstanceConstructors
}
private Assignment? StringKeyedPocoGeneric(in KeyValueGeneric keyValueGeneric, in string accessPattern, string defaultCase)
private Assignment? StringKeyedPocoGeneric(in KeyValueGeneric keyValueGeneric, in string accessPattern, in string defaultCase, in string memberName)
{
switch (keyValueGeneric)
{
case {TKey: not {SpecialType: SpecialType.System_String}}:
return null;
case {Type: KeyValueGeneric.SupportedType.LookUp}:
var lookupValueAssignment = DataMemberAssignment(keyValueGeneric.TValue, "y.z");
var lookupValueAssignment = DataMemberAssignment(keyValueGeneric.TValue, "y.z", in memberName);
return new Assignment(
$"{accessPattern} switch {{ {{ M: {{ }} x }} => x.SelectMany(y => y.Value.L, (y, z) => (y.Key, z)).ToLookup(y => y.Key, y => {lookupValueAssignment.Value}), {defaultCase} }}",
keyValueGeneric.TValue,
lookupValueAssignment.HasExternalDependency
);
case {Type: KeyValueGeneric.SupportedType.Dictionary}:
var dictionaryValueAssignment = DataMemberAssignment(keyValueGeneric.TValue, "y.Value");
var dictionaryValueAssignment = DataMemberAssignment(keyValueGeneric.TValue, "y.Value", in memberName);
return new Assignment(
$@"{accessPattern} switch {{ {{ M: {{ }} x }} => x.ToDictionary(y => y.Key, y => {dictionaryValueAssignment.Value}), {defaultCase} }}",
keyValueGeneric.TValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public DynamoDBMarshallingException(string memberName, string message) : base(me
{
MemberName = memberName;
}
}
}
5 changes: 0 additions & 5 deletions SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
using Amazon.DynamoDBv2.Model;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using DynamoDBGenerator;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.Extensions;
using static SampleApp.Repository.PersonEntityMarshallerImplementation;
using PutItemRequest = Amazon.DynamoDBv2.Model.PutItemRequest;

namespace SampleApp;

internal static class Program
{
private static IDynamoDBClient<PersonEntity, PersonEntity, PersonEntityName, PersonEntityValue> _toDynamoDBClient;
public static void Main()
{
_toDynamoDBClient = new Repository().PersonEntityMarshaller.ToDynamoDBClient("MyTable", new AmazonDynamoDBClient());
BenchmarkRunner.Run<Marshalling>();
}

Expand Down

0 comments on commit e55220e

Please sign in to comment.