Skip to content

Commit 73699e9

Browse files
authored
Support primitive arguments (#72)
1 parent 15a7fe8 commit 73699e9

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,25 @@ internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<D
8686
}
8787
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
8888
{
89-
var dataMembers = fn(typeSymbol)
90-
.Select(x =>
91-
{
92-
return (
93-
IsUnknown: !options.IsConvertable(x.DataMember.Type) && x.DataMember.Type.TypeIdentifier() is UnknownType,
94-
DDB: x,
95-
ValueRef: $"_{x.DataMember.Name}ValueRef",
96-
AttributeReference: TypeName(x.DataMember.Type),
97-
AttributeInterfaceName: $"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.Type.Representation().annotated}>"
98-
);
99-
})
100-
.ToArray();
89+
var dataMembers =
90+
options.IsConvertable(typeSymbol)
91+
? Array
92+
.Empty<(bool IsUnknown, DynamoDbDataMember DDB, string ValueRef, string AttributeReference, string
93+
AttributeInterfaceName)>()
94+
: fn(typeSymbol)
95+
.Select(x =>
96+
{
97+
return (
98+
IsUnknown: !options.IsConvertable(x.DataMember.Type) &&
99+
x.DataMember.Type.TypeIdentifier() is UnknownType,
100+
DDB: x,
101+
ValueRef: $"_{x.DataMember.Name}ValueRef",
102+
AttributeReference: TypeName(x.DataMember.Type),
103+
AttributeInterfaceName:
104+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.Type.Representation().annotated}>"
105+
);
106+
})
107+
.ToArray();
101108

102109
var structName = TypeName(typeSymbol);
103110
var interfaceName = $"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeSymbol.Representation().annotated}>";

tests/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/ExpressionAttributeTrackerTests.cs

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests;
88
[DynamoDBMarshaller(typeof(InheritedClass))]
99
public partial class ExpressionAttributeTrackerTests
1010
{
11-
[Fact]
12-
public void Test()
13-
{
14-
}
1511
[Fact]
1612
public void PersonWithTupleArgument_AccessingRootExpressionAttributeName_ShouldThrow()
1713
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Amazon.DynamoDBv2.Model;
2+
using DynamoDBGenerator.Attributes;
3+
using DynamoDBGenerator.Extensions;
4+
5+
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests;
6+
7+
[DynamoDBMarshaller(typeof((Guid, Guid)), PropertyName = "STRING", ArgumentType = typeof(string))]
8+
[DynamoDBMarshaller(typeof((Guid, Guid)), PropertyName = "GUID", ArgumentType = typeof(Guid))]
9+
[DynamoDBMarshaller(typeof((Guid, Guid)), PropertyName = "INT", ArgumentType = typeof(int))]
10+
public partial class PrimitiveArgumentTests
11+
{
12+
[Fact]
13+
public void ExpressionValueTracker_STRING_ShouldBeExpandedCorrectly()
14+
{
15+
IAttributeExpressionValueTracker<string> valueTracker = STRING.AttributeExpressionValueTracker();
16+
valueTracker.ToString().Should().Be(":p1");
17+
18+
valueTracker.AccessedValues("hello").Should().BeEquivalentTo(new Dictionary<string, AttributeValue>
19+
{
20+
{ ":p1", new AttributeValue { S = "hello" } }
21+
});
22+
}
23+
24+
[Fact]
25+
public void ExpressionValueTracker_INT_ShouldBeExpandedCorrectly()
26+
{
27+
IAttributeExpressionValueTracker<int> valueTracker = INT.AttributeExpressionValueTracker();
28+
valueTracker.ToString().Should().Be(":p1");
29+
30+
valueTracker.AccessedValues(2).Should().BeEquivalentTo(new Dictionary<string, AttributeValue>
31+
{
32+
{ ":p1", new AttributeValue { N = "2" } }
33+
});
34+
}
35+
36+
[Fact]
37+
public void ExpressionValueTracker_GUID_ShouldBeExpandedCorrectly()
38+
{
39+
IAttributeExpressionValueTracker<Guid> valueTracker = GUID.AttributeExpressionValueTracker();
40+
valueTracker.ToString().Should().Be(":p1");
41+
42+
var guid = Guid.NewGuid();
43+
valueTracker.AccessedValues(guid).Should().BeEquivalentTo(new Dictionary<string, AttributeValue>
44+
{
45+
{ ":p1", new AttributeValue { S = guid.ToString() } }
46+
});
47+
}
48+
}

0 commit comments

Comments
 (0)