Skip to content

Commit 15a7fe8

Browse files
committed
Support source generation ontop of structs
1 parent 96b619d commit 15a7fe8

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/DynamoDBGenerator.SourceGenerator/DynamoDBDMarshallerEntry.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ private static void Execute(SourceProductionContext context,
3636

3737
foreach (var typeSymbol in compilation.GetTypeSymbols(classDeclarationSyntax))
3838
{
39-
var typeNamespace = typeSymbol.ContainingNamespace.IsGlobalNamespace
40-
? null
41-
: $"{typeSymbol.ContainingNamespace}.";
42-
4339
context.AddSource(
44-
$"{typeNamespace}{typeSymbol.Name}.g",
40+
$"{typeSymbol.ToDisplayString()}.g",
4541
string.Join(Constants.NewLine, CreateFileContent(typeSymbol, compilation))
4642
);
4743
}
@@ -64,15 +60,26 @@ private static IEnumerable<string> CreateFileContent(INamedTypeSymbol type, Comp
6460

6561
var typeType = type switch
6662
{
67-
{IsRecord: true} => "sealed partial record",
68-
{TypeKind: TypeKind.Class} => "sealed partial class",
69-
{TypeKind: TypeKind.Struct or TypeKind.Structure} => throw new NotImplementedException("Structs are not implemented yet."),
63+
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: true } => "sealed partial record",
64+
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: false } => "partial record",
65+
{ IsRecord: false, TypeKind: TypeKind.Class, IsSealed: true } => "sealed partial class",
66+
{ IsRecord: false, TypeKind: TypeKind.Class, IsSealed: false } => "partial class",
67+
{ IsRecord: true, TypeKind: TypeKind.Struct or TypeKind.Structure, IsReadOnly: true } => "readonly partial record struct",
68+
{ IsRecord: false, TypeKind: TypeKind.Struct or TypeKind.Structure, IsReadOnly: true } => "readonly partial struct",
69+
{ IsRecord: false, TypeKind: TypeKind.Struct or TypeKind.Structure, IsReadOnly: false } => "partial struct",
7070
_ => throw new NotImplementedException("Could not determine whether the type is a struct, class or record.")
7171
};
72+
73+
if (type.DeclaredAccessibility is not Accessibility.Public)
74+
throw new NotImplementedException(
75+
$"Generate accessibility of '{type.DeclaredAccessibility}' on '{type.ToDisplayParts()}' only '{type.DeclaredAccessibility == Accessibility.Public}' is supported."
76+
);
77+
var @static = type.IsStatic ? "static " : null;
7278

73-
var (options, args) = CreateArguments(type, compilation);
79+
80+
var (options, args) = CreateArguments(type, compilation);
7481
var classContent =
75-
$"public {typeType} {type.Name}".CreateScope(DynamoDbMarshaller.CreateRepository(args, options));
82+
$"public {@static}{typeType} {type.Name}".CreateScope(DynamoDbMarshaller.CreateRepository(args, options));
7683
var content = type.ContainingNamespace.IsGlobalNamespace
7784
? classContent
7885
: $"namespace {type.ContainingNamespace.ToDisplayString()}".CreateScope(classContent);

tests/DynamoDB.SourceGenerator.Benchmarks/Repository.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace DynamoDB.SourceGenerator.Benchmarks;
66

7+
78
[DynamoDBMarshaller(typeof(PersonEntity))]
8-
public partial class Repository
9+
public static partial class Repository
910
{
1011
}

0 commit comments

Comments
 (0)