Skip to content

Commit eee92f6

Browse files
committed
Create reusable functions
1 parent 66f9ef3 commit eee92f6

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

src/DynamoDBGenerator.SourceGenerator/DynamoDBDMarshaller.cs

+22-39
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,31 @@ private static IEnumerable<string> CreateFileContent(INamedTypeSymbol type, Comp
5858
using {Constants.DynamoDBGenerator.Namespace.ExceptionsFullName};
5959
using {Constants.DynamoDBGenerator.Namespace.InternalFullName};";
6060

61-
var typeType = type switch
62-
{
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, IsReadOnly: true } => "readonly partial record struct",
68-
{ IsRecord: false, TypeKind: TypeKind.Struct, IsReadOnly: true } => "readonly partial struct",
69-
{ IsRecord: false, TypeKind: TypeKind.Struct, IsReadOnly: false } => "partial struct",
70-
_ => throw new NotImplementedException("Could not determine whether the type is a struct, class or record.")
71-
};
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-
);
7761

7862
var (options, args) = CreateArguments(type, compilation);
79-
var classContent = $"public {(type.IsStatic ? "static " : null)}{typeType} {type.Name}"
80-
.CreateScope(MarshallerFactory.CreateRepository(args, options));
81-
82-
var content = type.ContainingNamespace.IsGlobalNamespace
83-
? classContent
84-
: $"namespace {type.ContainingNamespace.ToDisplayString()}".CreateScope(classContent);
63+
var classContent = type
64+
.TypeDeclaration()
65+
.CreateScope(MarshallerFactory.CreateRepository(args, options));
8566

86-
foreach (var s in content)
67+
foreach (var s in type.NamespaceDeclaration(classContent))
8768
yield return s;
8869

8970
var duration = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - timestamp);
9071

9172
yield return $"// <auto-generated | Duration {duration.ToString()}>";
9273
}
9374

75+
9476
private static (MarshallerOptions, IEnumerable<DynamoDBMarshallerArguments>) CreateArguments(ISymbol type,
9577
Compilation compilation)
9678
{
9779
var attributes = type.GetAttributes();
9880
var marshallerOptionNamedArguments = attributes.Where(x => x.AttributeClass is
99-
{
100-
Name: Constants.DynamoDBGenerator.Attribute.DynamoDbMarshallerOptions,
101-
ContainingNamespace.Name: Constants.DynamoDBGenerator.Namespace.Attributes,
102-
ContainingAssembly.Name: Constants.DynamoDBGenerator.AssemblyName
103-
})
81+
{
82+
Name: Constants.DynamoDBGenerator.Attribute.DynamoDbMarshallerOptions,
83+
ContainingNamespace.Name: Constants.DynamoDBGenerator.Namespace.Attributes,
84+
ContainingAssembly.Name: Constants.DynamoDBGenerator.AssemblyName
85+
})
10486
.SelectMany(x => x.NamedArguments)
10587
.ToArray();
10688

@@ -123,7 +105,8 @@ private static (MarshallerOptions, IEnumerable<DynamoDBMarshallerArguments>) Cre
123105

124106
return (MarshallerOptions.Create(converter, enumStrategy), Arguments(attributes, type));
125107

126-
static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<AttributeData> attributes, ISymbol type)
108+
static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<AttributeData> attributes,
109+
ISymbol type)
127110
{
128111
foreach (var attributeData in attributes)
129112
{
@@ -136,14 +119,14 @@ static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<Attribu
136119
continue;
137120

138121
var entityType = attributeData.NamedArguments
139-
.Where(x => x.Key is Constants.DynamoDBGenerator.Attribute.DynamoDBMarshallerArgument.EntityType)
140-
.Cast<KeyValuePair<string, TypedConstant>?>()
141-
.FirstOrDefault() is { } entityType1
142-
? entityType1.Value is { Value: INamedTypeSymbol et }
143-
? et
144-
: throw new ArgumentException(
145-
$"Could not determine type conversion from argument '{entityType1.Key}'.")
146-
: type;
122+
.Where(x => x.Key is Constants.DynamoDBGenerator.Attribute.DynamoDBMarshallerArgument.EntityType)
123+
.Cast<KeyValuePair<string, TypedConstant>?>()
124+
.FirstOrDefault() is { } entityType1
125+
? entityType1.Value is { Value: INamedTypeSymbol et }
126+
? et
127+
: throw new ArgumentException(
128+
$"Could not determine type conversion from argument '{entityType1.Key}'.")
129+
: type;
147130

148131
if (entityType is not INamedTypeSymbol entityTypeSymbol)
149132
throw new ArgumentException("Could not determine type conversion from attribute constructor.");
@@ -158,7 +141,7 @@ static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<Attribu
158141
.Cast<KeyValuePair<string, TypedConstant>?>()
159142
.FirstOrDefault() is { } argumentType
160143
? argumentType.Value is
161-
{ Value: INamedTypeSymbol namedTypeSymbol }
144+
{ Value: INamedTypeSymbol namedTypeSymbol }
162145
? namedTypeSymbol
163146
: throw new ArgumentException(
164147
$"Could not determine type conversion from argument '{argumentType.Key}'.")
@@ -168,4 +151,4 @@ static IEnumerable<DynamoDBMarshallerArguments> Arguments(ImmutableArray<Attribu
168151
}
169152
}
170153
}
171-
}
154+
}

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

+33-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ namespace DynamoDBGenerator.SourceGenerator.Extensions;
77

88
public static class TypeExtensions
99
{
10+
public static IEnumerable<string> NamespaceDeclaration(this INamedTypeSymbol type, IEnumerable<string> content)
11+
{
12+
return type.ContainingNamespace.IsGlobalNamespace
13+
? content
14+
: $"namespace {type.ContainingNamespace.ToDisplayString()}".CreateScope(content);
15+
}
16+
public static string TypeDeclaration(this INamedTypeSymbol type)
17+
{
18+
if (type.DeclaredAccessibility is not Accessibility.Public)
19+
throw new NotImplementedException(
20+
$"Generate accessibility of '{type.DeclaredAccessibility}' on '{type.ToDisplayParts()}' only '{type.DeclaredAccessibility == Accessibility.Public}' is supported."
21+
);
22+
23+
var typeType = type switch
24+
{
25+
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: true } => $"public {(type.IsStatic ? "static " : null)}sealed partial record {type.Name}",
26+
{ IsRecord: true, TypeKind: TypeKind.Class, IsSealed: false } => $"public {(type.IsStatic ? "static " : null)}partial record {type.Name}",
27+
{ IsRecord: false, TypeKind: TypeKind.Class, IsSealed: true } => $"public {(type.IsStatic ? "static " : null)}sealed partial class {type.Name}",
28+
{ IsRecord: false, TypeKind: TypeKind.Class, IsSealed: false } => $"public {(type.IsStatic ? "static " : null)}partial class {type.Name}",
29+
{ IsRecord: true, TypeKind: TypeKind.Struct, IsReadOnly: true } => $"public {(type.IsStatic ? "static " : null)}readonly partial record struct {type.Name}",
30+
{ IsRecord: false, TypeKind: TypeKind.Struct, IsReadOnly: true } => $"public {(type.IsStatic ? "static " : null)}readonly partial struct {type.Name}",
31+
{ IsRecord: false, TypeKind: TypeKind.Struct, IsReadOnly: false } => $"public {(type.IsStatic ? "static " : null)}partial struct {type.Name}",
32+
_ => throw new NotImplementedException("Could not determine whether the type is a struct, class or record.")
33+
};
34+
return typeType;
35+
}
36+
1037
public static Func<ITypeSymbol, T> CacheFactory<T>(IEqualityComparer<ISymbol> comparer,
1138
Func<ITypeSymbol, T> selector)
1239
{
@@ -18,16 +45,19 @@ public static Func<ITypeSymbol, T> CacheFactory<T>(IEqualityComparer<ISymbol> co
1845

1946
private static readonly Dictionary<ITypeSymbol, TypeIdentifier> TypeIdentifierDictionary =
2047
new(SymbolEqualityComparer.IncludeNullability);
21-
48+
2249
public static bool IsNullable(this ITypeSymbol typeSymbol)
2350
{
2451
return typeSymbol switch
2552
{
26-
{ IsReferenceType: true, NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => true,
53+
{
54+
IsReferenceType: true, NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated
55+
} => true,
2756
{ IsReferenceType: true, NullableAnnotation: NullableAnnotation.NotAnnotated } => false,
2857
{ IsValueType: true, OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => true,
2958
{ IsValueType: true } => false,
30-
_ => throw new ArgumentOutOfRangeException($"Could not determine nullablity of type '{typeSymbol.ToDisplayString()}'.")
59+
_ => throw new ArgumentOutOfRangeException(
60+
$"Could not determine nullablity of type '{typeSymbol.ToDisplayString()}'.")
3161
};
3262
}
3363

0 commit comments

Comments
 (0)