Skip to content

Commit 7c7ab7d

Browse files
committed
Increase functionality of TypeIdentifier
1 parent 0ae3a50 commit 7c7ab7d

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public static Func<ITypeSymbol, T> CacheFactory<T>(IEqualityComparer<ISymbol> co
5555
private static readonly Dictionary<ITypeSymbol, TypeIdentifier> TypeIdentifierDictionary =
5656
new(SymbolEqualityComparer.IncludeNullability);
5757

58-
public static string ReturnNullOrThrow(this ITypeSymbol typeSymbol, string dataMember) => typeSymbol.IsNullable()
59-
? "return null;"
60-
: $"throw {Constants.DynamoDBGenerator.ExceptionHelper.NullExceptionMethod}({dataMember});";
61-
6258
public static bool IsNullable(this ITypeSymbol typeSymbol) => typeSymbol switch
6359
{
6460
{ IsReferenceType: true, NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => true,

src/DynamoDBGenerator.SourceGenerator/Generations/Marshalling/Marshaller.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,42 +116,42 @@ private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, Dyna
116116
SingleGeneric.SupportedType.Nullable => signature
117117
.CreateScope(
118118
$"if ({ParamReference} is null)"
119-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
119+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
120120
.Append($"return {InvokeMarshallerMethod(singleGeneric.T, $"{ParamReference}.Value", DataMember, options)};")
121121
).ToConversion(singleGeneric.T),
122122
SingleGeneric.SupportedType.Array => signature
123123
.CreateScope(
124124
$"if ({ParamReference} is null)"
125-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
125+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
126126
.Append($"return {AttributeValueUtilityFactory.FromArray}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
127127
).ToConversion(singleGeneric.T),
128128
SingleGeneric.SupportedType.List => signature
129129
.CreateScope(
130130
$"if ({ParamReference} is null)"
131-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
131+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
132132
.Append($"return {AttributeValueUtilityFactory.FromList}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
133133
).ToConversion(singleGeneric.T),
134134
SingleGeneric.SupportedType.IReadOnlyCollection
135135
or SingleGeneric.SupportedType.IEnumerable
136136
or SingleGeneric.SupportedType.ICollection => signature
137137
.CreateScope(
138138
$"if ({ParamReference} is null)"
139-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
139+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
140140
.Append($"return {AttributeValueUtilityFactory.FromEnumerable}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
141141
).ToConversion(singleGeneric.T),
142142
SingleGeneric.SupportedType.Set when singleGeneric.T.SpecialType is SpecialType.System_String
143143
=> signature
144144
.CreateScope(
145145
$"if ({ParamReference} is null)"
146-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
146+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
147147
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ SS = new List<{(singleGeneric.T.IsNullable() ? "string?" : "string")}>({(singleGeneric.T.IsNullable() ? ParamReference : $"{ParamReference}.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\"))")})}};")
148148
)
149149
.ToConversion(singleGeneric.T),
150150
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
151151
=> signature
152152
.CreateScope(
153153
$"if ({ParamReference} is null)"
154-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
154+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
155155
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ NS = new List<string>({ParamReference}.Select(y => y.ToString())) }};")
156156
)
157157
.ToConversion(singleGeneric.T),
@@ -165,14 +165,14 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
165165
KeyValueGeneric.SupportedType.Dictionary => signature
166166
.CreateScope(
167167
$"if ({ParamReference} is null)"
168-
.CreateScope(keyValueGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
168+
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
169169
.Append($"return {AttributeValueUtilityFactory.FromDictionary}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.TValue.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
170170
)
171171
.ToConversion(keyValueGeneric.TValue),
172172
KeyValueGeneric.SupportedType.LookUp => signature
173173
.CreateScope(
174174
$"if ({ParamReference} is null)"
175-
.CreateScope(keyValueGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
175+
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
176176
.Append($"return {AttributeValueUtilityFactory.FromLookup}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.TValue.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
177177
)
178178
.ToConversion(keyValueGeneric.TValue),

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,42 +100,42 @@ private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, Dyna
100100
SingleGeneric.SupportedType.Nullable => signature
101101
.CreateScope(
102102
$"if ({Value} is null || {Value}.NULL is true)"
103-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
103+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
104104
.Append($"return {InvokeUnmarshallMethod(singleGeneric.T, Value, DataMember, options)};" )
105105
)
106106
.ToConversion(singleGeneric.T),
107107
SingleGeneric.SupportedType.List or SingleGeneric.SupportedType.ICollection => signature
108108
.CreateScope(
109109
$"if ({Value} is null || {Value}.L is null)"
110-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
110+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
111111
.Append($"return {AttributeValueUtilityFactory.ToList}({Value}.L, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(singleGeneric.T, "a", "d", options, "o")});")
112112
)
113113
.ToConversion(singleGeneric.T),
114114
SingleGeneric.SupportedType.Array or SingleGeneric.SupportedType.IReadOnlyCollection => signature
115115
.CreateScope(
116116
$"if ({Value} is null || {Value}.L is null)"
117-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
117+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
118118
.Append($"return {AttributeValueUtilityFactory.ToArray}({Value}.L, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(singleGeneric.T, "a", "d", options, "o")});")
119119
)
120120
.ToConversion(singleGeneric.T),
121121
SingleGeneric.SupportedType.IEnumerable => signature
122122
.CreateScope(
123123
$"if ({Value} is null || {Value}.L is null)"
124-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
124+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
125125
.Append($"return {AttributeValueUtilityFactory.ToEnumerable}({Value}.L, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(singleGeneric.T, "a", "d", options, "o")});")
126126
)
127127
.ToConversion(singleGeneric.T),
128128
SingleGeneric.SupportedType.Set when singleGeneric.T.SpecialType is SpecialType.System_String => signature
129129
.CreateScope(
130130
$"if ({Value} is null || {Value}.SS is null)"
131-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
131+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
132132
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{(singleGeneric.T.IsNullable() ? "string?" : "string")}>" : null)}({(singleGeneric.T.IsNullable() ? $"{Value}.SS" : $"{Value}.SS.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\")")}));")
133133
)
134134
.ToConversion(),
135135
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric() => signature
136136
.CreateScope(
137137
$"if ({Value} is null || {Value}.NS is null)"
138-
.CreateScope(singleGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
138+
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
139139
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{singleGeneric.T.Representation().original}>" : null)}({Value}.NS.Select(y => {singleGeneric.T.Representation().original}.Parse(y)));")
140140
)
141141
.ToConversion(singleGeneric.TypeSymbol),
@@ -149,14 +149,14 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric() => signature
149149
KeyValueGeneric.SupportedType.Dictionary => signature
150150
.CreateScope(
151151
$"if ({Value} is null || {Value}.M is null)"
152-
.CreateScope(keyValueGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
152+
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
153153
.Append($"return {AttributeValueUtilityFactory.ToDictionary}({Value}.M, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(keyValueGeneric.TValue, "a", "d", options, "o")});")
154154
)
155155
.ToConversion(keyValueGeneric.TValue),
156156
KeyValueGeneric.SupportedType.LookUp => signature
157157
.CreateScope(
158158
$"if ({Value} is null || {Value}.M is null)"
159-
.CreateScope(keyValueGeneric.TypeSymbol.ReturnNullOrThrow(DataMember))
159+
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
160160
.Append($"return {AttributeValueUtilityFactory.ToLookup}({Value}.M, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeUnmarshallMethod(keyValueGeneric.TValue, "a", "d", options, "o")});")
161161
)
162162
.ToConversion(keyValueGeneric.TValue),

src/DynamoDBGenerator.SourceGenerator/Types/TypeIdentifier.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
using DynamoDBGenerator.SourceGenerator.Extensions;
22
using Microsoft.CodeAnalysis;
3+
34
namespace DynamoDBGenerator.SourceGenerator.Types;
45

56
public abstract record TypeIdentifier(ITypeSymbol TypeSymbol)
67
{
8+
private bool IsNullable { get; } = TypeSymbol.IsNullable();
9+
10+
public string ReturnNullOrThrow(string dataMember)
11+
{
12+
return IsNullable
13+
? "return null;"
14+
: $"throw {Constants.DynamoDBGenerator.ExceptionHelper.NullExceptionMethod}({dataMember});";
15+
}
16+
717
public ITypeSymbol TypeSymbol { get; } = TypeSymbol;
818
}
919

1020
public sealed record UnknownType(ITypeSymbol TypeSymbol) : TypeIdentifier(TypeSymbol);
1121

1222
public sealed record KeyValueGeneric : TypeIdentifier
1323
{
14-
1524
public enum SupportedType
1625
{
1726
LookUp = 1,
1827
Dictionary = 2
1928
}
2029

21-
private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in ITypeSymbol tValue, in SupportedType supportedType) : base(typeSymbol)
30+
private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in ITypeSymbol tValue,
31+
in SupportedType supportedType) : base(typeSymbol)
2232
{
2333
Type = supportedType;
2434
TKey = tKey;
2535
TValue = tValue;
2636
}
37+
2738
public SupportedType Type { get; }
2839

2940
// ReSharper disable once InconsistentNaming
@@ -37,13 +48,13 @@ private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in IType
3748
if (typeSymbol is not INamedTypeSymbol type)
3849
return null;
3950

40-
if (type is not {IsGenericType: true, TypeArguments.Length: 2})
51+
if (type is not { IsGenericType: true, TypeArguments.Length: 2 })
4152
return null;
4253

4354
SupportedType? supported = type switch
4455
{
45-
{Name: "ILookup"} => SupportedType.LookUp,
46-
{Name: "Dictionary" or "IReadOnlyDictionary" or "IDictionary"} => SupportedType.Dictionary,
56+
{ Name: "ILookup" } => SupportedType.LookUp,
57+
{ Name: "Dictionary" or "IReadOnlyDictionary" or "IDictionary" } => SupportedType.Dictionary,
4758
_ => null
4859
};
4960
return supported is null
@@ -54,7 +65,6 @@ private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in IType
5465

5566
public sealed record SingleGeneric : TypeIdentifier
5667
{
57-
5868
public enum SupportedType
5969
{
6070
Nullable = 1,
@@ -72,6 +82,7 @@ private SingleGeneric(ITypeSymbol type, ITypeSymbol innerType, in SupportedType
7282
Type = supportedType;
7383
T = innerType;
7484
}
85+
7586
public SupportedType Type { get; }
7687
public ITypeSymbol T { get; }
7788

@@ -84,25 +95,33 @@ private SingleGeneric(ITypeSymbol type, ITypeSymbol innerType, in SupportedType
8495
if (typeSymbol is not INamedTypeSymbol type)
8596
return null;
8697

87-
if (type is not {IsGenericType: true, TypeArguments.Length: 1})
98+
if (type is not { IsGenericType: true, TypeArguments.Length: 1 })
8899
return null;
89100

90101
SupportedType? supported = type switch
91102
{
92103
_ when type.TryGetNullableValueType() is not null => SupportedType.Nullable,
93-
_ when type.OriginalDefinition.ToDisplayString() == "System.Collections.Generic.List<T>" => SupportedType.List,
94-
{Name: "ISet" } => SupportedType.Set,
95-
_ when type.AllInterfaces.Any(x => x is {Name: "ISet"}) => SupportedType.Set,
96-
{Name: "IReadOnlySet" } => SupportedType.Set,
97-
_ when type.AllInterfaces.Any(x => x is {Name: "IReadOnlySet"}) => SupportedType.Set,
98-
{OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_ICollection_T} => SupportedType.ICollection,
99-
_ when type.AllInterfaces.Any(x => x is {OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_ICollection_T}) => SupportedType.ICollection,
100-
{OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IReadOnlyCollection_T} => SupportedType.IReadOnlyCollection,
101-
_ when type.AllInterfaces.Any(x => x is {OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IReadOnlyCollection_T}) => SupportedType.IReadOnlyCollection,
102-
{OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IEnumerable_T} => SupportedType.IEnumerable,
104+
_ when type.OriginalDefinition.ToDisplayString() == "System.Collections.Generic.List<T>" => SupportedType
105+
.List,
106+
{ Name: "ISet" } => SupportedType.Set,
107+
_ when type.AllInterfaces.Any(x => x is { Name: "ISet" }) => SupportedType.Set,
108+
{ Name: "IReadOnlySet" } => SupportedType.Set,
109+
_ when type.AllInterfaces.Any(x => x is { Name: "IReadOnlySet" }) => SupportedType.Set,
110+
{ OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_ICollection_T } => SupportedType
111+
.ICollection,
112+
_ when type.AllInterfaces.Any(x => x is
113+
{ OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_ICollection_T }) =>
114+
SupportedType.ICollection,
115+
{ OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IReadOnlyCollection_T } =>
116+
SupportedType.IReadOnlyCollection,
117+
_ when type.AllInterfaces.Any(x => x is
118+
{ OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IReadOnlyCollection_T }) =>
119+
SupportedType.IReadOnlyCollection,
120+
{ OriginalDefinition.SpecialType: SpecialType.System_Collections_Generic_IEnumerable_T } => SupportedType
121+
.IEnumerable,
103122
_ => null
104123
};
105124

106125
return supported is null ? null : new SingleGeneric(type, type.TypeArguments[0], supported.Value);
107126
}
108-
}
127+
}

0 commit comments

Comments
 (0)