Skip to content

Commit 3e9a2eb

Browse files
committed
Move source generatio into marshaller
1 parent 357ca1c commit 3e9a2eb

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

src/DynamoDBGenerator.SourceGenerator/DynamoDBDMarshaller.cs

+2-24
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,9 @@ private static void Execute(SourceProductionContext context,
4545

4646
private static IEnumerable<string> CreateFileContent(INamedTypeSymbol type, Compilation compilation)
4747
{
48-
var timestamp = Stopwatch.GetTimestamp();
49-
yield return $@"// <auto-generated | TimeStamp: {DateTime.Now:yyyy-MM-dd HH:mm:ss}>
50-
#nullable enable
51-
using System;
52-
using System.Linq;
53-
using System.Collections.Generic;
54-
using System.Runtime.CompilerServices;
55-
using {Constants.AWSSDK_DynamoDBv2.Namespace.ModelFullName};
56-
using {Constants.DynamoDBGenerator.Namespace.Root};
57-
using {Constants.DynamoDBGenerator.Namespace.AttributesFullName};
58-
using {Constants.DynamoDBGenerator.Namespace.ExceptionsFullName};
59-
using {Constants.DynamoDBGenerator.Namespace.InternalFullName};";
60-
61-
6248
var (options, args) = CreateArguments(type, compilation);
63-
var classContent = type
64-
.TypeDeclaration()
65-
.CreateScope(MarshallerFactory.CreateRepository(args, options));
66-
67-
foreach (var s in type.NamespaceDeclaration(classContent))
49+
foreach (var s in MarshallerFactory.Create(type, args.ToArray(), options))
6850
yield return s;
69-
70-
var duration = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - timestamp);
71-
72-
yield return $"// <auto-generated | Duration {duration.ToString()}>";
7351
}
7452

7553

@@ -103,7 +81,7 @@ private static (MarshallerOptions, IEnumerable<DynamoDBMarshallerArguments>) Cre
10381
if (converter is null)
10482
throw new ArgumentException("Could not find converter implementation");
10583

106-
return (MarshallerOptions.Create(type ,converter, enumStrategy), Arguments(attributes, type));
84+
return (MarshallerOptions.Create(type, converter, enumStrategy), Arguments(attributes, type));
10785

10886
static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<AttributeData> attributes,
10987
ISymbol type)

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ public static IEnumerable<string> NamespaceDeclaration(this INamedTypeSymbol typ
1313
? content
1414
: $"namespace {type.ContainingNamespace.ToDisplayString()}".CreateScope(content);
1515
}
16+
1617
public static string TypeDeclaration(this INamedTypeSymbol type)
1718
{
1819
if (type.DeclaredAccessibility is not Accessibility.Public)
1920
throw new NotImplementedException(
2021
$"Generate accessibility of '{type.DeclaredAccessibility}' on '{type.ToDisplayParts()}' only '{type.DeclaredAccessibility == Accessibility.Public}' is supported."
2122
);
2223

23-
var typeType = type switch
24+
return type switch
2425
{
2526
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: true } => $"public {(type.IsStatic ? "static " : null)}sealed partial record {type.Name}",
2627
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: false } => $"public {(type.IsStatic ? "static " : null)}partial record {type.Name}",
@@ -31,7 +32,6 @@ public static string TypeDeclaration(this INamedTypeSymbol type)
3132
{ IsRecord: false, TypeKind: TypeKind.Struct, IsReadOnly: false } => $"public {(type.IsStatic ? "static " : null)}partial struct {type.Name}",
3233
_ => throw new NotImplementedException("Could not determine whether the type is a struct, class or record.")
3334
};
34-
return typeType;
3535
}
3636

3737
public static Func<ITypeSymbol, T> CacheFactory<T>(IEqualityComparer<ISymbol> comparer,
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System.Diagnostics;
12
using DynamoDBGenerator.SourceGenerator.Extensions;
23
using DynamoDBGenerator.SourceGenerator.Generations;
34
using DynamoDBGenerator.SourceGenerator.Generations.Marshalling;
45
using DynamoDBGenerator.SourceGenerator.Types;
56
using Microsoft.CodeAnalysis;
67
using static DynamoDBGenerator.SourceGenerator.Constants.DynamoDBGenerator.Marshaller;
8+
79
namespace DynamoDBGenerator.SourceGenerator;
810

911
public static class MarshallerFactory
@@ -16,14 +18,18 @@ private static IEnumerable<string> CreateImplementations(IEnumerable<DynamoDBMar
1618

1719
foreach (var argument in arguments)
1820
{
19-
var (expressionValueMethod, valueTrackerTypeName) = AttributeExpressionValue.RootSignature(argument.ArgumentType);
20-
var (expressionMethodName, nameTrackerTypeName) = AttributeExpressionName.RootSignature(argument.EntityTypeSymbol);
21+
var (expressionValueMethod, valueTrackerTypeName) =
22+
AttributeExpressionValue.RootSignature(argument.ArgumentType);
23+
var (expressionMethodName, nameTrackerTypeName) =
24+
AttributeExpressionName.RootSignature(argument.EntityTypeSymbol);
2125

2226
var entityTypeName = argument.AnnotatedEntityType;
2327
var argumentTypeName = argument.AnnotatedArgumentType;
2428

25-
var constructor = $"public {argument.ImplementationName}({options.FullName} {MarshallerOptions.ParamReference})"
26-
.CreateScope($"{MarshallerOptions.FieldReference} = {MarshallerOptions.ParamReference};", $"{Marshaller.KeyMarshaller.PrimaryKeyMarshallerReference} = {Marshaller.KeyMarshaller.AssignmentRoot(argument.EntityTypeSymbol)};");
29+
var constructor =
30+
$"public {argument.ImplementationName}({options.FullName} {MarshallerOptions.ParamReference})"
31+
.CreateScope($"{MarshallerOptions.FieldReference} = {MarshallerOptions.ParamReference};",
32+
$"{Marshaller.KeyMarshaller.PrimaryKeyMarshallerReference} = {Marshaller.KeyMarshaller.AssignmentRoot(argument.EntityTypeSymbol)};");
2733
var interfaceImplementation = constructor
2834
.Concat(Marshaller.RootSignature(argument.EntityTypeSymbol, entityTypeName))
2935
.Concat(UnMarshaller.RootSignature(argument.EntityTypeSymbol, entityTypeName))
@@ -33,14 +39,16 @@ private static IEnumerable<string> CreateImplementations(IEnumerable<DynamoDBMar
3339
.Append(Marshaller.KeyMarshaller.PrimaryKeyMarshallerDeclaration)
3440
.Prepend(options.FieldDeclaration);
3541

36-
var classImplementation = $"private sealed class {argument.ImplementationName}: {Interface}<{entityTypeName}, {argumentTypeName}, {nameTrackerTypeName}, {valueTrackerTypeName}>"
37-
.CreateScope(interfaceImplementation);
42+
var classImplementation =
43+
$"private sealed class {argument.ImplementationName}: {Interface}<{entityTypeName}, {argumentTypeName}, {nameTrackerTypeName}, {valueTrackerTypeName}>"
44+
.CreateScope(interfaceImplementation);
3845

3946
yield return options.TryInstantiate() switch
4047
{
41-
{} arg =>
48+
{ } arg =>
4249
$"public static {Interface}<{entityTypeName}, {argumentTypeName}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName} {{ get; }} = new {argument.ImplementationName}({arg});",
43-
null => $"public static {Interface}<{entityTypeName}, {argumentTypeName}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName}({options.FullName} options) => new {argument.ImplementationName}(options);"
50+
null =>
51+
$"public static {Interface}<{entityTypeName}, {argumentTypeName}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName}({options.FullName} options) => new {argument.ImplementationName}(options);"
4452
};
4553

4654
foreach (var s in classImplementation)
@@ -49,16 +57,39 @@ private static IEnumerable<string> CreateImplementations(IEnumerable<DynamoDBMar
4957
}
5058

5159

52-
public static IEnumerable<string> CreateRepository(IEnumerable<DynamoDBMarshallerArguments> arguments, MarshallerOptions options)
60+
public static IEnumerable<string> Create(
61+
INamedTypeSymbol originatingType,
62+
DynamoDBMarshallerArguments[] arguments,
63+
MarshallerOptions options
64+
)
5365
{
54-
var loadedArguments = arguments.ToArray();
55-
var getDynamoDbProperties = TypeExtensions.CacheFactory(SymbolEqualityComparer.IncludeNullability, TypeExtensions.GetDynamoDbProperties);
56-
var code = CreateImplementations(loadedArguments, options)
57-
.Concat(Marshaller.CreateClass(loadedArguments, getDynamoDbProperties, options))
58-
.Concat(UnMarshaller.CreateClass(loadedArguments, getDynamoDbProperties, options))
59-
.Concat(AttributeExpressionName.CreateClasses(loadedArguments, getDynamoDbProperties, options))
60-
.Concat(AttributeExpressionValue.CreateExpressionAttributeValue(loadedArguments, getDynamoDbProperties, options));
66+
var timestamp = Stopwatch.GetTimestamp();
67+
yield return $@"// <auto-generated | TimeStamp: {DateTime.Now:yyyy-MM-dd HH:mm:ss}>
68+
#nullable enable
69+
using System;
70+
using System.Linq;
71+
using System.Collections.Generic;
72+
using System.Runtime.CompilerServices;
73+
using {Constants.AWSSDK_DynamoDBv2.Namespace.ModelFullName};
74+
using {Constants.DynamoDBGenerator.Namespace.Root};
75+
using {Constants.DynamoDBGenerator.Namespace.AttributesFullName};
76+
using {Constants.DynamoDBGenerator.Namespace.ExceptionsFullName};
77+
using {Constants.DynamoDBGenerator.Namespace.InternalFullName};";
78+
79+
var dynamoDbProperties = TypeExtensions.CacheFactory(
80+
SymbolEqualityComparer.IncludeNullability,
81+
TypeExtensions.GetDynamoDbProperties
82+
);
83+
84+
var code = CreateImplementations(arguments, options)
85+
.Concat(Marshaller.CreateClass(arguments, dynamoDbProperties, options))
86+
.Concat(UnMarshaller.CreateClass(arguments, dynamoDbProperties, options))
87+
.Concat(AttributeExpressionName.CreateClasses(arguments, dynamoDbProperties, options))
88+
.Concat(AttributeExpressionValue.CreateExpressionAttributeValue(arguments, dynamoDbProperties, options));
89+
90+
foreach (var x in originatingType.NamespaceDeclaration(originatingType.TypeDeclaration().CreateScope(code)))
91+
yield return x;
6192

62-
return code;
93+
yield return $"// <auto-generated | Duration {TimeSpan.FromTicks(Stopwatch.GetTimestamp() - timestamp)}>";
6394
}
64-
}
95+
}

0 commit comments

Comments
 (0)