Skip to content

Commit 50cc823

Browse files
committed
Use immutable array
1 parent 368d3a8 commit 50cc823

File tree

8 files changed

+124
-89
lines changed

8 files changed

+124
-89
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

+94-63
Large diffs are not rendered by default.

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionName.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using DynamoDBGenerator.SourceGenerator.Extensions;
23
using DynamoDBGenerator.SourceGenerator.Types;
34
using Microsoft.CodeAnalysis;
@@ -10,7 +11,7 @@ public static class AttributeExpressionName
1011
private const string ConstructorAttributeName = "nameRef";
1112

1213
private static readonly Func<ITypeSymbol, string> TypeName = TypeExtensions.SuffixedTypeSymbolNameFactory("Names", SymbolEqualityComparer.Default);
13-
internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
14+
internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
1415
{
1516
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
1617
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
@@ -66,7 +67,7 @@ private static IEnumerable<string> TypeContent(
6667

6768
yield return $"public override string ToString() => {self}.Value;";
6869
}
69-
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
70+
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
7071
{
7172
var dataMembers = fn(typeSymbol)
7273
.Select(x => (

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using DynamoDBGenerator.SourceGenerator.Extensions;
23
using DynamoDBGenerator.SourceGenerator.Types;
34
using Microsoft.CodeAnalysis;
@@ -75,15 +76,15 @@ MarshallerOptions options
7576

7677
yield return $"public override string ToString() => {self}.Value;";
7778
}
78-
internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
79+
internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
7980
{
8081
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
8182
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
8283

8384
return arguments
8485
.SelectMany(x => CodeFactory.Create(x.ArgumentType, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
8586
}
86-
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
87+
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
8788
{
8889
var dataMembers = fn(typeSymbol)
8990
.Select(x =>

src/DynamoDBGenerator.SourceGenerator/Generations/KeyMarshaller.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using DynamoDBGenerator.SourceGenerator.Extensions;
23
using DynamoDBGenerator.SourceGenerator.Types;
34
using Microsoft.CodeAnalysis;
@@ -26,7 +27,7 @@ private static IEnumerable<string> CreateAssignment(string validateReference, st
2627
return $"if({validateReference})".CreateScope(innerContent);
2728

2829
}
29-
private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
30+
private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
3031
{
3132
var keyStructure = DynamoDbDataMember.GetKeyStructure(fn(typeSymbol));
3233
if (keyStructure is null)
@@ -56,7 +57,7 @@ private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<IType
5657
yield return s;
5758

5859
}
59-
internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
60+
internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
6061
{
6162
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
6263

@@ -110,7 +111,7 @@ internal static string AssignmentRoot(ITypeSymbol typeSymbol)
110111
return
111112
$"new {KeyMarshallerImplementationTypeName}((pk, rk, ipk, irk, dm) => {MethodName(typeSymbol)}({MarshallerOptions.FieldReference}, pk, rk, ipk, irk, dm))";
112113
}
113-
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
114+
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
114115
{
115116

116117
var code = $"private static Dictionary<string, AttributeValue> {MethodName(typeSymbol)}({MarshallerOptions.Name} {MarshallerOptions.ParamReference}, object? {PkReference}, object? {RkReference}, bool {EnforcePkReference}, bool {EnforceRkReference}, string? index = null)"

src/DynamoDBGenerator.SourceGenerator/Generations/Marshaller.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using DynamoDBGenerator.SourceGenerator.Extensions;
23
using DynamoDBGenerator.SourceGenerator.Types;
34
using Microsoft.CodeAnalysis;
@@ -12,13 +13,12 @@ public static class Marshaller
1213
private static readonly Func<ITypeSymbol, string> GetSerializationMethodName = TypeExtensions.SuffixedTypeSymbolNameFactory("_M", SymbolEqualityComparer.IncludeNullability);
1314
private const string ParamReference = "entity";
1415

15-
internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
16+
internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
1617
{
1718
return $"private static class {ClassName}".CreateScope(TypeContent(arguments, getDynamoDbProperties, options));
1819
}
19-
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
20+
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
2021
{
21-
2222
var properties = fn(typeSymbol)
2323
.Select(x =>
2424
{
@@ -59,7 +59,7 @@ private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<I
5959

6060
}
6161

62-
private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
62+
private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
6363
{
6464
var hashset = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
6565

@@ -72,7 +72,7 @@ private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArg
7272
.Concat(CodeFactory.Create(x.ArgumentType, y => CreateMethod(y, getDynamoDbProperties, options), hashset))
7373
);
7474
}
75-
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
75+
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
7676
{
7777
if (options.TryWriteConversion(type, ParamReference) is {} conversion)
7878
{

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Collections.Immutable;
23
using DynamoDBGenerator.SourceGenerator.Extensions;
34
using DynamoDBGenerator.SourceGenerator.Types;
45
using Microsoft.CodeAnalysis;
@@ -43,11 +44,11 @@ public static class UnMarshaller
4344

4445
}
4546
internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments,
46-
Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
47+
Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
4748
{
4849
return $"private static class {UnMarshallerClass}".CreateScope(CreateTypeContents(arguments, getDynamoDbProperties, options));
4950
}
50-
private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
51+
private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
5152
{
5253
var assignments = fn(type)
5354
.Select(x => (DDB: x, MethodCall: InvokeUnmarshallMethod(x.DataMember.Type, $"{Dict}.GetValueOrDefault(\"{x.AttributeName}\")", $"\"{x.DataMember.Name}\"", options), x.DataMember.Name))
@@ -70,7 +71,7 @@ private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, IReadO
7071
return new CodeFactory(method, assignments.Select(x => x.DDB.DataMember.Type));
7172

7273
}
73-
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn,
74+
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn,
7475
MarshallerOptions options)
7576
{
7677

@@ -137,7 +138,7 @@ private static string CreateSignature(ITypeSymbol typeSymbol)
137138
return $"public static {typeSymbol.Representation().annotated} {GetDeserializationMethodName(typeSymbol)}(AttributeValue? {Value}, {MarshallerOptions.Name} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
138139
}
139140
private static IEnumerable<string> CreateTypeContents(IEnumerable<DynamoDBMarshallerArguments> arguments,
140-
Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
141+
Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
141142
{
142143
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
143144
return arguments.SelectMany(x =>

src/DynamoDBGenerator.SourceGenerator/Types/DynamoDbDataMember.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ public readonly struct DynamoDbDataMember
99
{
1010
public DynamoDbDataMember(in DataMember dataMember, IReadOnlyList<AttributeData> attributeData)
1111
{
12-
Attributes = attributeData;
13-
AttributeName = Attributes
12+
_attributes = attributeData;
13+
AttributeName = _attributes
1414
.Select(AttributeNameSelector)
1515
.FirstOrDefault(x => string.IsNullOrWhiteSpace(x) is false) ?? dataMember.Name;
1616
DataMember = dataMember;
1717
}
1818

19-
private static string? AttributeNameSelector(AttributeData attributeData1)
19+
private static string? AttributeNameSelector(AttributeData attributeData)
2020
{
21-
return attributeData1 switch
21+
return attributeData switch
2222
{
2323
{AttributeClass: null} => null,
24-
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBHashKey, ConstructorArguments.Length: 1} when FilterString(attributeData1.ConstructorArguments[0]) is { } attributeName => attributeName,
25-
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBRangeKey, ConstructorArguments.Length: 1} when FilterString(attributeData1.ConstructorArguments[0]) is { } attributeName => attributeName,
26-
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBProperty, ConstructorArguments.Length: 1} when FilterString(attributeData1.ConstructorArguments[0]) is { } attributeName => attributeName,
24+
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBHashKey, ConstructorArguments.Length: 1} when FilterString(attributeData.ConstructorArguments[0]) is { } attributeName => attributeName,
25+
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBRangeKey, ConstructorArguments.Length: 1} when FilterString(attributeData.ConstructorArguments[0]) is { } attributeName => attributeName,
26+
{AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBProperty, ConstructorArguments.Length: 1} when FilterString(attributeData.ConstructorArguments[0]) is { } attributeName => attributeName,
2727
{
2828
AttributeClass.Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBHashKey or Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBRangeKey or Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBProperty,
2929
ConstructorArguments.Length: > 0
@@ -44,7 +44,7 @@ public DynamoDbDataMember(in DataMember dataMember, IReadOnlyList<AttributeData>
4444
/// <inheritdoc cref="Types.DataMember" />
4545
public DataMember DataMember { get; }
4646

47-
private IReadOnlyList<AttributeData> Attributes { get; }
47+
private readonly IReadOnlyList<AttributeData> _attributes;
4848

4949

5050
public static AttributeData[] GetDynamoDbAttributes(ISymbol symbol)
@@ -57,13 +57,12 @@ public static AttributeData[] GetDynamoDbAttributes(ISymbol symbol)
5757
public static bool IsIgnored(AttributeData[] attributes)
5858
{
5959
return attributes.Length is not 0 && attributes.Any(x => x.AttributeClass is {Name: Constants.AWSSDK_DynamoDBv2.Attribute.DynamoDBIgnore});
60-
6160
}
6261

6362
public static DynamoDBKeyStructure? GetKeyStructure(IEnumerable<DynamoDbDataMember> members)
6463
{
6564
var items = members
66-
.SelectMany(x => x.Attributes, (x, y) => (DataMember: x, Attribute: y))
65+
.SelectMany(x => x._attributes, (x, y) => (DataMember: x, Attribute: y))
6766
.ToArray();
6867

6968
var partitionKey = items

tests/DynamoDB.SourceGenerator.Benchmarks/Repository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net.Sockets;
12
using DynamoDB.SourceGenerator.Benchmarks.Models;
23
using DynamoDBGenerator.Attributes;
34

0 commit comments

Comments
 (0)