Skip to content

Commit 8b7633b

Browse files
committed
Rename Conversion
1 parent 78c3d88 commit 8b7633b

File tree

8 files changed

+76
-76
lines changed

8 files changed

+76
-76
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ when string.Join("_", namedTypeSymbol.TypeArguments.Select(NullableAnnotation))
164164
static string ExceptionMessage(ITypeSymbol typeSymbol) => $"Could not apply naming suffix on type: {typeSymbol.ToDisplayString()}";
165165
}
166166

167-
public static Conversion ToConversion(this IEnumerable<string> enumerable)
167+
public static CodeFactory ToConversion(this IEnumerable<string> enumerable)
168168
{
169-
return new Conversion(enumerable);
169+
return new CodeFactory(enumerable);
170170
}
171-
public static Conversion ToConversion(this IEnumerable<string> enumerable, ITypeSymbol typeSymbol)
171+
public static CodeFactory ToConversion(this IEnumerable<string> enumerable, ITypeSymbol typeSymbol)
172172
{
173-
return new Conversion(enumerable, new[] {typeSymbol});
173+
return new CodeFactory(enumerable, new[] {typeSymbol});
174174
}
175175

176176
public static INamedTypeSymbol? TryGetNullableValueType(this ITypeSymbol type)

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionName.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshaller
1616
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
1717

1818
return arguments
19-
.SelectMany(x => Conversion.ConversionMethods(x.EntityTypeSymbol, y => CreateStruct(y, getDynamoDbProperties, options), hashSet)).SelectMany(x => x.Code);
19+
.SelectMany(x => CodeFactory.Create(x.EntityTypeSymbol, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
2020

2121
}
2222
private static IEnumerable<string> CreateCode(
@@ -66,7 +66,7 @@ private static IEnumerable<string> CreateCode(
6666

6767
yield return $"public override string ToString() => {self}.Value;";
6868
}
69-
private static Conversion CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
69+
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
7070
{
7171
var dataMembers = fn(typeSymbol)
7272
.Select(x => (
@@ -81,7 +81,7 @@ private static Conversion CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol,
8181
var structName = TypeName(typeSymbol);
8282

8383
var @class = $"public readonly struct {structName} : {AttributeExpressionNameTrackerInterface}".CreateScope(CreateCode(typeSymbol, dataMembers, structName));
84-
return new Conversion(@class, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
84+
return new CodeFactory(@class, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
8585

8686
}
8787

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<D
8181
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
8282

8383
return arguments
84-
.SelectMany(x => Conversion.ConversionMethods(x.ArgumentType, y => CreateStruct(y, getDynamoDbProperties, options), hashSet)).SelectMany(x => x.Code);
84+
.SelectMany(x => CodeFactory.Create(x.ArgumentType, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
8585
}
86-
private static Conversion CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
86+
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
8787
{
8888
var dataMembers = fn(typeSymbol)
8989
.Select(x =>
@@ -103,7 +103,7 @@ private static Conversion CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol,
103103

104104
var @struct = $"public readonly struct {structName} : {interfaceName}".CreateScope(CreateCode(typeSymbol, dataMembers, structName, interfaceName, options));
105105

106-
return new Conversion(@struct, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
106+
return new CodeFactory(@struct, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
107107

108108
}
109109

src/DynamoDBGenerator.SourceGenerator/Generations/KeyMarshaller.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArg
6161
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
6262

6363
return arguments
64-
.SelectMany(x => Conversion.ConversionMethods(x.EntityTypeSymbol, y => StaticAttributeValueDictionaryKeys(y, getDynamoDbProperties, options), hashSet)).SelectMany(x => x.Code);
64+
.SelectMany(x => CodeFactory.Create(x.EntityTypeSymbol, y => StaticAttributeValueDictionaryKeys(y, getDynamoDbProperties, options), hashSet));
6565
}
6666
private static IEnumerable<(string? IndexName, IEnumerable<string> assignments)> GetAssignments(DynamoDBKeyStructure keyStructure, MarshallerOptions options)
6767
{
@@ -110,13 +110,13 @@ internal static string AssignmentRoot(ITypeSymbol typeSymbol)
110110
return
111111
$"new {KeyMarshallerImplementationTypeName}((pk, rk, ipk, irk, dm) => {MethodName(typeSymbol)}({MarshallerOptions.FieldReference}, pk, rk, ipk, irk, dm))";
112112
}
113-
private static Conversion StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
113+
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
114114
{
115115

116116
var code = $"private static Dictionary<string, AttributeValue> {MethodName(typeSymbol)}({MarshallerOptions.Name} {MarshallerOptions.ParamReference}, object? {PkReference}, object? {RkReference}, bool {EnforcePkReference}, bool {EnforceRkReference}, string? index = null)"
117117
.CreateScope(CreateBody(typeSymbol, fn, options));
118118

119-
return new Conversion(code);
119+
return new CodeFactory(code);
120120

121121
}
122122
}

src/DynamoDBGenerator.SourceGenerator/Generations/Marshaller.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerAr
1616
{
1717
return $"private static class {ClassName}".CreateScope(CreateMarshaller(arguments, getDynamoDbProperties, options));
1818
}
19-
private static Conversion CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
19+
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
2020
{
2121

2222
var properties = fn(typeSymbol)
@@ -55,25 +55,24 @@ private static Conversion CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<IT
5555
$"public static Dictionary<string, AttributeValue>{(isNullable ? '?' : null)} {GetSerializationMethodName(typeSymbol)}({typeSymbol.Representation().annotated} {ParamReference}, {MarshallerOptions.Name} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
5656
.CreateScope(body);
5757

58-
return new Conversion(code, properties.Select(y => y.Type));
58+
return new CodeFactory(code, properties.Select(y => y.Type));
5959

6060
}
6161

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

66-
return arguments.SelectMany(x => Conversion
67-
.ConversionMethods(
66+
return arguments.SelectMany(x => CodeFactory
67+
.Create(
6868
x.EntityTypeSymbol,
6969
y => CreateMethod(y, getDynamoDbProperties, options),
7070
hashset
7171
)
72-
.Concat(Conversion.ConversionMethods(x.ArgumentType, y => CreateMethod(y, getDynamoDbProperties, options), hashset))
73-
)
74-
.SelectMany(x => x.Code);
72+
.Concat(CodeFactory.Create(x.ArgumentType, y => CreateMethod(y, getDynamoDbProperties, options), hashset))
73+
);
7574
}
76-
private static Conversion CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
75+
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
7776
{
7877
if (options.TryWriteConversion(type, ParamReference) is {} conversion)
7978
{

src/DynamoDBGenerator.SourceGenerator/Generations/Unmarshaller.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerAr
4747
{
4848
return $"private static class {UnMarshallerClass}".CreateScope(CreateUnMarshaller(arguments, getDynamoDbProperties, options));
4949
}
50-
private static Conversion CreateCode(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
50+
private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn, MarshallerOptions options)
5151
{
5252
var assignments = fn(type)
5353
.Select(x => (DDB: x, MethodCall: InvokeUnmarshallMethod(x.DataMember.Type, $"{Dict}.GetValueOrDefault(\"{x.AttributeName}\")", $"\"{x.DataMember.Name}\"", options), x.DataMember.Name))
@@ -67,10 +67,10 @@ private static Conversion CreateCode(ITypeSymbol type, Func<ITypeSymbol, IReadOn
6767

6868
var method = $"public static {typeName.annotated} {GetDeserializationMethodName(type)}(Dictionary<string, AttributeValue>? {Dict}, {MarshallerOptions.Name} {MarshallerOptions.ParamReference}, string? {DataMember} = null)".CreateScope(blockBody);
6969

70-
return new Conversion(method, assignments.Select(x => x.DDB.DataMember.Type));
70+
return new CodeFactory(method, assignments.Select(x => x.DDB.DataMember.Type));
7171

7272
}
73-
private static Conversion CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn,
73+
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, IReadOnlyList<DynamoDbDataMember>> fn,
7474
MarshallerOptions options)
7575
{
7676

@@ -141,13 +141,12 @@ private static IEnumerable<string> CreateUnMarshaller(IEnumerable<DynamoDBMarsha
141141
{
142142
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
143143
return arguments.SelectMany(x =>
144-
Conversion.ConversionMethods(
145-
x.EntityTypeSymbol,
146-
y => CreateMethod(y, getDynamoDbProperties, options),
147-
hashSet
148-
)
144+
CodeFactory.Create(
145+
x.EntityTypeSymbol,
146+
y => CreateMethod(y, getDynamoDbProperties, options),
147+
hashSet
149148
)
150-
.SelectMany(x => x.Code);
149+
);
151150
}
152151
private static string Else(ITypeSymbol typeSymbol)
153152
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace DynamoDBGenerator.SourceGenerator.Types;
4+
5+
public readonly struct CodeFactory
6+
{
7+
public CodeFactory(IEnumerable<string> code, IEnumerable<ITypeSymbol> typeIdentifiers)
8+
{
9+
_code = code;
10+
TypeIdentifiers = typeIdentifiers;
11+
}
12+
13+
public CodeFactory(IEnumerable<string> code)
14+
{
15+
_code = code;
16+
TypeIdentifiers = Enumerable.Empty<ITypeSymbol>();
17+
}
18+
19+
/// <summary>
20+
/// The code surrounding all assignments.
21+
/// </summary>
22+
private readonly IEnumerable<string> _code;
23+
24+
/// <summary>
25+
/// The assignments that occur within the method.
26+
/// </summary>
27+
private IEnumerable<ITypeSymbol> TypeIdentifiers { get; }
28+
29+
public static IEnumerable<string> Create(
30+
ITypeSymbol typeSymbol,
31+
Func<ITypeSymbol, CodeFactory> codeSelector,
32+
ISet<ITypeSymbol> handledTypes
33+
)
34+
{
35+
// We already support the type.
36+
if (handledTypes.Add(typeSymbol) is false)
37+
yield break;
38+
39+
var code = codeSelector(typeSymbol);
40+
41+
foreach (var s in code._code)
42+
yield return s;
43+
44+
foreach (var x in code.TypeIdentifiers)
45+
foreach (var assignment in Create(x, codeSelector, handledTypes))
46+
yield return assignment;
47+
}
48+
}

src/DynamoDBGenerator.SourceGenerator/Types/Conversion.cs

-46
This file was deleted.

0 commit comments

Comments
 (0)