Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Sep 18, 2023
1 parent dcdf28e commit 3fa17b2
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 130 deletions.
35 changes: 20 additions & 15 deletions DynamoDBGenerator/Attributes/DynamoDBMarshallerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace DynamoDBGenerator.Attributes;

/// <summary>
/// When placed on a class it will generate an implementation of <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}"/> for the specified type.
/// The example below shows an example of this attribute being used in a repository.
/// When placed on a class it will generate an implementation of
/// <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}" /> for the
/// specified type.
/// The example below shows an example of this attribute being used in a repository.
/// </summary>
/// <example>
/// <code>
/// <code>
/// [DynamoDBMarshaller(typeof(OrderEntity), PropertyName = "MyCustomPropertyName")]
/// public class Repository
/// {
Expand All @@ -30,23 +32,26 @@ public class DynamoDBMarshallerAttribute : Attribute
private readonly Type _entityType;

/// <summary>
/// Specifies the name of the property to use when accessing <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}"/>.
/// </summary>
public string? PropertyName { get; set; }

/// <summary>
/// Will set the type <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}"/> will use as its argument type-parameter.
/// </summary>
public Type? ArgumentType { get; set; }

/// <summary>
/// Constructs an <see cref="DynamoDBMarshallerAttribute"/> for source generation purposes.
/// Constructs an <see cref="DynamoDBMarshallerAttribute" /> for source generation purposes.
/// </summary>
/// <param name="entityType">
/// The type to be represented as an DynamoDB entity.
/// The type to be represented as an DynamoDB entity.
/// </param>
public DynamoDBMarshallerAttribute(Type entityType)
{
_entityType = entityType;
}

/// <summary>
/// Specifies the name of the property to use when accessing
/// <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}" />.
/// </summary>
public string? PropertyName { get; set; }

/// <summary>
/// Will set the type
/// <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}" /> will
/// use as its argument type-parameter.
/// </summary>
public Type? ArgumentType { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
namespace DynamoDBGenerator.Attributes;

/// <summary>
/// When placed on a constructor, indicates that the constructor should be used to create instances of the type when <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}.Unmarshall"/> is used.
/// When placed on a constructor, indicates that the constructor should be used to create instances of the type when
/// <see cref="IDynamoDBMarshaller{TEntity,TArg,TEntityAttributeNameTracker,TArgumentAttributeValueTracker}.Unmarshall" />
/// is used.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public class DynamoDBMarshallerConstructorAttribute : Attribute
{
}
}
42 changes: 5 additions & 37 deletions DynamoDBGenerator/DynamoDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,13 @@
using DynamoDBGenerator.Extensions;
namespace DynamoDBGenerator;

public interface IDynamoDBClient<TEntity, TArgument, out TReferences, out TArgumentReferences>
internal class DynamoDBClient<T, TArg, TReferences, TArgumentReferences> : IDynamoDBClient<T, TArg, TReferences, TArgumentReferences>
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArgument>
{
Task Save<T>(
T entity,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
) where T : TEntity, TArgument;
Task Save<T>(
T entity,
CancellationToken cancellationToken = default
) where T : TEntity, TArgument;
Task Update(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
CancellationToken cancellationToken = default
);
Task Update(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
);

Task<TEntity> UpdateReturned(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
);
}

internal class DynamoDBClient<T, TArg, TReferences, TArgumentReferences> : IDynamoDBClient<T, TArg, TReferences, TArgumentReferences> where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
{
private readonly IAmazonDynamoDB _amazonDynamoDB;
private readonly IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> _marshaller;
private readonly string _tableName;
private readonly IAmazonDynamoDB _amazonDynamoDB;

public DynamoDBClient(IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> marshaller, string tableName, IAmazonDynamoDB amazonDynamoDB)
{
Expand All @@ -68,6 +33,7 @@ public Task Save<T1>(T1 entity, CancellationToken cancellationToken = default) w
var putRequest = _marshaller.ToPutItemRequestInternal(entity, entity, null, ReturnValue.NONE, _tableName);
return _amazonDynamoDB.PutItemAsync(putRequest, cancellationToken);
}

public Task Update(
TArg entity,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Expand All @@ -78,6 +44,7 @@ public Task Update(
var updateItemRequest = _marshaller.ToUpdateItemRequestInternal(entity, keySelector, updateExpressionBuilder, null, ReturnValue.NONE, _tableName);
return _amazonDynamoDB.UpdateItemAsync(updateItemRequest, cancellationToken);
}

public Task Update(
TArg entity,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Expand All @@ -89,6 +56,7 @@ public Task Update(
var updateItemRequest = _marshaller.ToUpdateItemRequestInternal(entity, keySelector, updateExpressionBuilder, conditionExpressionBuilder, ReturnValue.NONE, _tableName);
return _amazonDynamoDB.UpdateItemAsync(updateItemRequest, cancellationToken);
}

public async Task<T> UpdateReturned(TArg entity, Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector, Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder, CancellationToken cancellationToken = default)
{
Expand Down
2 changes: 1 addition & 1 deletion DynamoDBGenerator/DynamoDBGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.3" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.3"/>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions DynamoDBGenerator/Exceptions/DynamoDBMarshallingException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace DynamoDBGenerator.Exceptions;

public class DynamoDBMarshallingException : InvalidOperationException
{
public string MemberName { get; }
public DynamoDBMarshallingException(string memberName, string message) : base(message: $"{message} (Data member '{memberName}')")
public DynamoDBMarshallingException(string memberName, string message) : base($"{message} (Data member '{memberName}')")
{
MemberName = memberName;
}
}
public string MemberName { get; }
}
53 changes: 33 additions & 20 deletions DynamoDBGenerator/Extensions/DynamoDBMarshallerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
namespace DynamoDBGenerator.Extensions;
Expand All @@ -13,50 +12,64 @@ public static IDynamoDBClient<T, TArg, TReferences, TArgumentReferences> ToDynam
IAmazonDynamoDB dynamoDB
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg> => new DynamoDBClient<T, TArg, TReferences, TArgumentReferences>(item, tableName, dynamoDB);
public static UpdateItemRequest ToUpdateItemRequest<T, TArg, TReferences, TArgumentReferences>(
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
{
return new DynamoDBClient<T, TArg, TReferences, TArgumentReferences>(item, tableName, dynamoDB);
}

public static PutItemRequest ToPutItemRequest<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
T entity,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg> => item.ToUpdateItemRequestInternal(argument, keySelector, updateExpressionBuilder, null, returnValue, tableName);
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
where T : TArg
{
return item.ToPutItemRequestInternal(entity, entity, null, returnValue, tableName);
}

public static UpdateItemRequest ToUpdateItemRequest<T, TArg, TReferences, TArgumentReferences>(
public static PutItemRequest ToPutItemRequest<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
T entity,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg> => item.ToUpdateItemRequestInternal(argument, keySelector, updateExpressionBuilder, conditionExpressionBuilder, returnValue, tableName);
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
where T : TArg
{
return item.ToPutItemRequestInternal(entity, entity, conditionExpressionBuilder, returnValue, tableName);
}

public static PutItemRequest ToPutItemRequest<T, TArg, TReferences, TArgumentReferences>(
public static UpdateItemRequest ToUpdateItemRequest<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
T entity,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
where T : TArg => item.ToPutItemRequestInternal(entity, entity, null, returnValue, tableName);
{
return item.ToUpdateItemRequestInternal(argument, keySelector, updateExpressionBuilder, null, returnValue, tableName);
}

public static PutItemRequest ToPutItemRequest<T, TArg, TReferences, TArgumentReferences>(
public static UpdateItemRequest ToUpdateItemRequest<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
T entity,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
where T : TArg => item.ToPutItemRequestInternal(entity, entity, conditionExpressionBuilder, returnValue, tableName);


{
return item.ToUpdateItemRequestInternal(argument, keySelector, updateExpressionBuilder, conditionExpressionBuilder, returnValue, tableName);
}
}
58 changes: 29 additions & 29 deletions DynamoDBGenerator/Extensions/RequestFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,7 @@ namespace DynamoDBGenerator.Extensions;

internal static class RequestFactory
{
internal static UpdateItemRequest ToUpdateItemRequestInternal<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string>? conditionExpressionBuilder,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
{

var nameTracker = item.AttributeNameExpressionTracker();
var argumentTracker = item.AttributeExpressionValueTracker();
var updateExpression = updateExpressionBuilder(nameTracker, argumentTracker);
var conditionExpression = conditionExpressionBuilder?.Invoke(nameTracker, argumentTracker);

return new UpdateItemRequest
{
Key = keySelector(item, argument),
TableName = tableName,
ExpressionAttributeNames = nameTracker.AccessedNames().ToDictionary(x => x.Key, x => x.Value),
ExpressionAttributeValues = argumentTracker.AccessedValues(argument).ToDictionary(x => x.Key, x => x.Value),
ConditionExpression = conditionExpression,
UpdateExpression = updateExpression,
ReturnValues = returnValue
};
}

internal static PutItemRequest ToPutItemRequestInternal<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
T entity,
Expand Down Expand Up @@ -72,4 +43,33 @@ string tableName
ReturnValues = returnValue
};
}
internal static UpdateItemRequest ToUpdateItemRequestInternal<T, TArg, TReferences, TArgumentReferences>(
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> item,
TArg argument,
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string>? conditionExpressionBuilder,
ReturnValue returnValue,
string tableName
)
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArg>
{

var nameTracker = item.AttributeNameExpressionTracker();
var argumentTracker = item.AttributeExpressionValueTracker();
var updateExpression = updateExpressionBuilder(nameTracker, argumentTracker);
var conditionExpression = conditionExpressionBuilder?.Invoke(nameTracker, argumentTracker);

return new UpdateItemRequest
{
Key = keySelector(item, argument),
TableName = tableName,
ExpressionAttributeNames = nameTracker.AccessedNames().ToDictionary(x => x.Key, x => x.Value),
ExpressionAttributeValues = argumentTracker.AccessedValues(argument).ToDictionary(x => x.Key, x => x.Value),
ConditionExpression = conditionExpression,
UpdateExpression = updateExpression,
ReturnValues = returnValue
};
}
}
42 changes: 42 additions & 0 deletions DynamoDBGenerator/IDynamoDBClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Amazon.DynamoDBv2.Model;
namespace DynamoDBGenerator;

public interface IDynamoDBClient<TEntity, TArgument, out TReferences, out TArgumentReferences>
where TReferences : IExpressionAttributeNameTracker
where TArgumentReferences : IExpressionAttributeValueTracker<TArgument>
{
Task Save<T>(
T entity,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
) where T : TEntity, TArgument;
Task Save<T>(
T entity,
CancellationToken cancellationToken = default
) where T : TEntity, TArgument;
Task Update(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
CancellationToken cancellationToken = default
);
Task Update(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
);

Task<TEntity> UpdateReturned(
TArgument entity,
Func<IDynamoDBKeyMarshaller, TArgument, Dictionary<string, AttributeValue>> keySelector,
Func<TReferences, TArgumentReferences, string> updateExpressionBuilder,
Func<TReferences, TArgumentReferences, string> conditionExpressionBuilder,
CancellationToken cancellationToken = default
);
}
Loading

0 comments on commit 3fa17b2

Please sign in to comment.