Skip to content

Commit 3d2b742

Browse files
committed
Use TArg for GetRequest
1 parent be538aa commit 3d2b742

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

src/Dynatello/Builders/Extensions.cs

+15-12
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,37 @@ this KeyConditionExpression<T, TArg, TReferences, TArgumentReferences> source
3030
);
3131
}
3232

33-
public static GetItemRequestBuilder<TPartition> ToGetRequestBuilder<T, TArg, TReferences, TArgumentReferences, TPartition>(
33+
public static GetItemRequestBuilder<TArg> ToGetRequestBuilder<T, TArg, TReferences, TArgumentReferences,
34+
TPartition>(
3435
this TableAccess<T, TArg, TReferences, TArgumentReferences> source,
35-
// ReSharper disable once UnusedParameter.Global is used to determined typing
36-
Func<T, TPartition> partitionKeySelector)
36+
Func<TArg, TPartition> partitionKeySelector)
3737
where TReferences : IAttributeExpressionNameTracker
3838
where TArgumentReferences : IAttributeExpressionValueTracker<TArg>
3939
where TPartition : notnull
4040
{
41-
return new GetItemRequestBuilder<TPartition>(source.TableName, source.Item.PrimaryKeyMarshaller,
42-
(x, y) => x.PartitionKey(y));
41+
return new GetItemRequestBuilder<TArg>(
42+
source.TableName,
43+
source.Item.PrimaryKeyMarshaller.ComposeKeys<TArg>(y => partitionKeySelector(y), null)
44+
);
4345
}
4446

45-
public static GetItemRequestBuilder<(TPartition partionKey, TRange rangeKey)> ToGetRequestBuilder<T, TArg,
47+
public static GetItemRequestBuilder<TArg> ToGetRequestBuilder<T, TArg,
4648
TReferences, TArgumentReferences, TPartition, TRange>(
4749
this TableAccess<T, TArg, TReferences, TArgumentReferences> source,
48-
// ReSharper disable once UnusedParameter.Global is used to determined typing
49-
Func<T, TPartition> partitionKeySelector,
50-
// ReSharper disable once UnusedParameter.Global is used to determined typing
51-
Func<T, TRange> rangeKeySelector)
50+
Func<TArg, TPartition> partitionKeySelector,
51+
Func<TArg, TRange> rangeKeySelector)
5252
where TReferences : IAttributeExpressionNameTracker
5353
where TArgumentReferences : IAttributeExpressionValueTracker<TArg>
5454
where TPartition : notnull
5555
where TRange : notnull
5656
{
57-
return new GetItemRequestBuilder<(TPartition partionKey, TRange rangeKey)>(source.TableName,
58-
source.Item.PrimaryKeyMarshaller, (x, y) => x.Keys(y.partionKey, y.rangeKey));
57+
return new GetItemRequestBuilder<TArg>(
58+
source.TableName,
59+
source.Item.PrimaryKeyMarshaller.ComposeKeys<TArg>(y => partitionKeySelector(y), y => rangeKeySelector(y))
60+
);
5961
}
6062

63+
6164
public static UpdateRequestBuilder<TArg> ToUpdateItemRequestBuilder<T, TArg, TReferences, TArgumentReferences>(
6265
this UpdateExpression<T, TArg, TReferences, TArgumentReferences> source,
6366
Func<IDynamoDBKeyMarshaller, TArg, Dictionary<string, AttributeValue>> keySelector

src/Dynatello/Builders/GetItemRequestBuilder.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace Dynatello.Builders;
66

7-
public readonly record struct GetItemRequestBuilder<TKey> where TKey : notnull
7+
public readonly record struct GetItemRequestBuilder<T>
88
{
9-
private readonly IDynamoDBKeyMarshaller _keyMarshaller;
10-
private readonly Func<IDynamoDBKeyMarshaller, TKey, Dictionary<string, AttributeValue>> _keysSelector;
9+
private readonly Func< T, Dictionary<string, AttributeValue>> _keysSelector;
1110

1211
/// <inheritdoc cref="GetItemRequest.TableName"/>
1312
public string TableName { get; init; }
@@ -19,21 +18,21 @@ namespace Dynatello.Builders;
1918
/// <inheritdoc cref="GetItemRequest.ReturnConsumedCapacity"/>
2019
public ReturnConsumedCapacity? ReturnConsumedCapacity { get; init; } = null;
2120

22-
internal GetItemRequestBuilder(string tableName, IDynamoDBKeyMarshaller keyMarshaller,
23-
Func<IDynamoDBKeyMarshaller, TKey, Dictionary<string, AttributeValue>> keysSelector)
21+
internal GetItemRequestBuilder(
22+
string tableName,
23+
Func<T, Dictionary<string, AttributeValue>> keysSelector)
2424
{
25-
_keyMarshaller = keyMarshaller;
2625
_keysSelector = keysSelector;
2726
TableName = tableName;
2827
}
2928

30-
public GetItemRequest Build(TKey key)
29+
public GetItemRequest Build(T arg)
3130
{
3231
var request = new GetItemRequest
3332
{
3433
ReturnConsumedCapacity = ReturnConsumedCapacity,
3534
TableName = TableName,
36-
Key = _keysSelector(_keyMarshaller, key)
35+
Key = _keysSelector(arg)
3736
};
3837

3938
if (ConsistentRead is { } consistentRead)

src/Dynatello/DynamoDBMarshallerExtensions.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Amazon.DynamoDBv2.Model;
12
using DynamoDBGenerator;
23
using Dynatello.Builders.Types;
34
using static DynamoDBGenerator.Extensions.DynamoDBMarshallerExtensions;
@@ -15,6 +16,22 @@ public static TableAccess<T, TArg, TReferences, TArgumentReferences> OnTable
1516
return new TableAccess<T, TArg, TReferences, TArgumentReferences>(in tableName, in item);
1617
}
1718

19+
internal static Func<TArg, Dictionary<string, AttributeValue>> ComposeKeys<TArg>
20+
(
21+
this IDynamoDBKeyMarshaller source,
22+
Func<TArg, object> partitionKeySelector,
23+
Func<TArg, object>? rangeKeySelector
24+
)
25+
{
26+
return (partitionKeySelector, rangeKeySelector) switch
27+
{
28+
(not null, not null) => y => source.Keys(partitionKeySelector(y), rangeKeySelector(y)),
29+
(not null, null) => y => source.PartitionKey(partitionKeySelector(y)),
30+
(null, not null) => y => source.RangeKey(rangeKeySelector(y)),
31+
(null, null) => throw new ArgumentNullException("")
32+
};
33+
}
34+
1835
internal static Func<TArg, IAttributeExpression> ComposeAttributeExpression<T, TArg, TReferences,
1936
TArgumentReferences>(
2037
this IDynamoDBMarshaller<T, TArg, TReferences, TArgumentReferences> source,

tests/Dynatello.Tests/ToGetItemRequestTests.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public class ToGetItemRequestTests
1313

1414
static ToGetItemRequestTests()
1515
{
16-
GetCatByPartitionKey = Cat.QueryWithCuteness.OnTable("TABLE").ToGetRequestBuilder(x => x.Id);
17-
GetCatByCompositeKeys = Cat.QueryWithCuteness.OnTable("TABLE").ToGetRequestBuilder(x => x.Id, x => x.HomeId);
16+
GetCatByPartitionKey = Cat.GetById.OnTable("TABLE").ToGetRequestBuilder(x => x);
17+
GetCatByCompositeKeys = Cat.GetByCompositeKey.OnTable("TABLE").ToGetRequestBuilder(x => x.Id, x => x.HomeId);
1818
}
1919

2020
[Fact]
2121
public void Build_Request_CompositeKeys_InvalidPartition()
2222
{
23-
var act = () => Cat.QueryWithCuteness
23+
var act = () => Cat.GetByCompositeInvalidPartition
2424
.OnTable("TABLE")
25-
.ToGetRequestBuilder(x => x.Name, x => x.HomeId)
25+
.ToGetRequestBuilder(x => x.Item1, x => x.Item2)
2626
.Build(("", Guid.Empty));
2727

2828
act.Should()
@@ -33,9 +33,9 @@ public void Build_Request_CompositeKeys_InvalidPartition()
3333
[Fact]
3434
public void Build_Request_CompositeKeys_InvalidRange()
3535
{
36-
var act = () => Cat.QueryWithCuteness
36+
var act = () => Cat.GetByCompositeInvalidRange
3737
.OnTable("TABLE")
38-
.ToGetRequestBuilder(x => x.Id, x => x.Name)
38+
.ToGetRequestBuilder(x => x.Item1, x => x.Item2)
3939
.Build((Guid.Empty, ""));
4040

4141
act.Should()
@@ -46,9 +46,9 @@ public void Build_Request_CompositeKeys_InvalidRange()
4646
[Fact]
4747
public void Build_Request_CompositeKeys_InvalidPartitionAndRange()
4848
{
49-
var act = () => Cat.QueryWithCuteness
49+
var act = () => Cat.GetByCompositeInvalidPartitionAndRange
5050
.OnTable("TABLE")
51-
.ToGetRequestBuilder(x => x.Cuteness, x => x.Name)
51+
.ToGetRequestBuilder(x => x.Item1, x => x.Item2)
5252
.Build((2.3, ""));
5353

5454
act.Should()
@@ -59,10 +59,11 @@ public void Build_Request_CompositeKeys_InvalidPartitionAndRange()
5959
[Fact]
6060
public void Build_Request_WithInvalidPartitionKey()
6161
{
62-
var act = () => Cat.QueryWithCuteness
62+
var act = () => Cat.GetByInvalidPartition
6363
.OnTable("TABLE")
64-
.ToGetRequestBuilder(x => x.Name)
64+
.ToGetRequestBuilder(x => x)
6565
.Build("TEST");
66+
6667
act.Should()
6768
.Throw<DynamoDBMarshallingException>()
6869
.WithMessage("Value '*' from argument '*' is not convertable*");

tests/Dynatello.Tests/ToQueryRequestTests.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ public void Build_Request_FilterExpression()
7676
}
7777
}
7878

79-
[DynamoDBMarshaller(typeof(Cat), PropertyName = "QueryWithCuteness",
80-
ArgumentType = typeof((Guid Id, double MinimumCuteness)))]
79+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "QueryWithCuteness", ArgumentType = typeof((Guid Id, double MinimumCuteness)))]
80+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetByCompositeKey", ArgumentType = typeof((Guid Id, Guid HomeId)))]
81+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetById", ArgumentType = typeof(Guid))]
82+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetByInvalidPartition", ArgumentType = typeof(string))]
83+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetByCompositeInvalidPartition", ArgumentType = typeof((string, Guid)))]
84+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetByCompositeInvalidRange", ArgumentType = typeof((Guid, string)))]
85+
[DynamoDBMarshaller(typeof(Cat), PropertyName = "GetByCompositeInvalidPartitionAndRange", ArgumentType = typeof((double, string)))]
8186
public readonly partial record struct Cat(
8287
[property: DynamoDBHashKey] Guid Id,
8388
[property: DynamoDBRangeKey] Guid HomeId,

0 commit comments

Comments
 (0)