Skip to content

Commit db1e724

Browse files
committed
WIP
1 parent 839b63d commit db1e724

9 files changed

Lines changed: 84 additions & 97 deletions

File tree

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.Collections;
21
using System.Collections.Concurrent;
3-
using System.Collections.Immutable;
4-
using System.Runtime.InteropServices;
52
using DynamoDBGenerator.SourceGenerator.Types;
63
using Microsoft.CodeAnalysis;
74

@@ -292,9 +289,9 @@ public static CodeFactory ToConversion(this IEnumerable<string> enumerable)
292289
return new CodeFactory(enumerable);
293290
}
294291

295-
public static CodeFactory ToConversion(this IEnumerable<string> enumerable, ITypeSymbol typeSymbol)
292+
public static CodeFactory ToConversion(this IEnumerable<string> enumerable, TypeIdentifier typeIdentifier)
296293
{
297-
return new CodeFactory(enumerable, new[] { typeSymbol.TypeIdentifier() });
294+
return new CodeFactory(enumerable, new[] { typeIdentifier });
298295
}
299296

300297
public static INamedTypeSymbol? TryGetNullableValueType(this ITypeSymbol type)
@@ -307,22 +304,6 @@ public static CodeFactory ToConversion(this IEnumerable<string> enumerable, ITyp
307304
: null;
308305
}
309306

310-
public static bool IsNumeric(this ITypeSymbol typeSymbol)
311-
{
312-
return typeSymbol.SpecialType
313-
is SpecialType.System_Int16
314-
or SpecialType.System_Byte
315-
or SpecialType.System_Int32
316-
or SpecialType.System_Int64
317-
or SpecialType.System_SByte
318-
or SpecialType.System_UInt16
319-
or SpecialType.System_UInt32
320-
or SpecialType.System_UInt64
321-
or SpecialType.System_Decimal
322-
or SpecialType.System_Double
323-
or SpecialType.System_Single;
324-
}
325-
326307
public static DynamoDbDataMember[] GetDynamoDbProperties(this ITypeSymbol symbol)
327308
{
328309
// A special rule when it comes to Tuples.

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private static CodeFactory CreateStruct(TypeIdentifier typeIdentifier, Func<ITyp
113113
IsUnknown: options.IsUnknown(x.DataMember.TypeIdentifier),
114114
DDB: x,
115115
DbRef: $"#{x.AttributeName}",
116-
AttributeReference: TypeName(x.DataMember.Type),
116+
AttributeReference: TypeName(x.DataMember.TypeIdentifier.TypeSymbol),
117117
AttributeInterfaceName: AttributeExpressionNameTrackerInterface
118118
))
119119
.ToArray();

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class AttributeExpressionValue
2020
private const string ValueProvider = "valueIdProvider";
2121

2222
private static IEnumerable<string> TypeContents(
23-
ITypeSymbol typeSymbol,
23+
TypeIdentifier typeIdentifier,
2424
(bool IsUnknown, DynamoDbDataMember DDB, string AttributeReference, string AttributeInterfaceName)[]
2525
dataMembers,
2626
string structName,
@@ -61,11 +61,11 @@ MarshallerOptions options
6161

6262
const string param = "entity";
6363

64-
var yields = (typeSymbol switch
64+
var yields = (typeIdentifier switch
6565
{
66-
_ when typeSymbol.IsNullable() => $"if ({param} is null)".CreateScope(
66+
{IsNullable:true} => $"if ({param} is null)".CreateScope(
6767
$"yield return new ({self}.Value, {AttributeValueUtilityFactory.Null});", "yield break;"),
68-
{ IsReferenceType: true } => $"if ({param} is null)".CreateScope(
68+
{ TypeSymbol.IsReferenceType: true } => $"if ({param} is null)".CreateScope(
6969
$"throw {ExceptionHelper.NullExceptionMethod}(\"{structName}\");"),
7070
_ => Enumerable.Empty<string>()
7171
})
@@ -74,12 +74,12 @@ _ when typeSymbol.IsNullable() => $"if ({param} is null)".CreateScope(
7474
.Concat(
7575
$"if ({self}.IsValueCreated)"
7676
.CreateScope(
77-
$"yield return new ({self}.Value, {Marshaller.InvokeMarshallerMethod(typeSymbol, "entity", $"\"{structName}\"", options, MarshallerOptions.FieldReference)}{HandeNullability(typeSymbol)});"
77+
$"yield return new ({self}.Value, {Marshaller.InvokeMarshallerMethod(typeIdentifier, "entity", $"\"{structName}\"", options, MarshallerOptions.FieldReference)}{HandeNullability(typeIdentifier)});"
7878
)
7979
)
8080
)
8181
.ScopeTo(
82-
$"IEnumerable<KeyValuePair<string, AttributeValue>> {interfaceName}.{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerAccessedValues}({typeSymbol.Representation().annotated} entity)");
82+
$"IEnumerable<KeyValuePair<string, AttributeValue>> {interfaceName}.{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerAccessedValues}({typeIdentifier.AnnotatedRepresenation} entity)");
8383

8484

8585
foreach (var yield in yields)
@@ -88,8 +88,8 @@ _ when typeSymbol.IsNullable() => $"if ({param} is null)".CreateScope(
8888
yield return $"public override string ToString() => {self}.Value;";
8989
}
9090

91-
private static string? HandeNullability(ITypeSymbol typeSymbol) =>
92-
typeSymbol.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null;
91+
private static string? HandeNullability(TypeIdentifier typeSymbol) =>
92+
typeSymbol.IsNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null;
9393

9494
private static IEnumerable<string> YieldSelector(
9595
(bool IsUnknown, DynamoDbDataMember DDB, string AttributeReference, string AttributeInterfaceName) x,
@@ -99,7 +99,7 @@ private static IEnumerable<string> YieldSelector(
9999

100100
if (x.IsUnknown)
101101
{
102-
return x.DDB.DataMember.Type.NotNullIfStatement(
102+
return x.DDB.DataMember.TypeIdentifier.TypeSymbol.NotNullIfStatement(
103103
accessPattern,
104104
$"foreach (var x in ({x.DDB.DataMember.Name} as {x.AttributeInterfaceName}).{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerAccessedValues}({accessPattern}))"
105105
.CreateScope("yield return x;")
@@ -109,9 +109,9 @@ private static IEnumerable<string> YieldSelector(
109109

110110

111111
return $"if ({x.DDB.DataMember.NameAsPrivateField}.IsValueCreated)".CreateScope(
112-
x.DDB.DataMember.Type.NotNullIfStatement(
112+
x.DDB.DataMember.TypeIdentifier.TypeSymbol.NotNullIfStatement(
113113
accessPattern,
114-
$"yield return new ({x.DDB.DataMember.NameAsPrivateField}.Value, {Marshaller.InvokeMarshallerMethod(x.DDB.DataMember.Type, $"entity.{x.DDB.DataMember.Name}", $"\"{x.DDB.DataMember.Name}\"", options, MarshallerOptions.FieldReference)}{HandeNullability(x.DDB.DataMember.Type)});"
114+
$"yield return new ({x.DDB.DataMember.NameAsPrivateField}.Value, {Marshaller.InvokeMarshallerMethod(x.DDB.DataMember.TypeIdentifier, $"entity.{x.DDB.DataMember.Name}", $"\"{x.DDB.DataMember.Name}\"", options, MarshallerOptions.FieldReference)}{HandeNullability(x.DDB.DataMember.TypeIdentifier)});"
115115
));
116116
}
117117

@@ -126,30 +126,30 @@ internal static IEnumerable<string> CreateClasses(DynamoDBMarshallerArguments[]
126126
CodeFactory.Create(x.ArgumentType.TypeIdentifier(), y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
127127
}
128128

129-
private static CodeFactory CreateStruct(TypeIdentifier typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
129+
private static CodeFactory CreateStruct(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
130130
MarshallerOptions options)
131131
{
132132
var dataMembers =
133-
options.IsConvertable(typeSymbol.TypeSymbol)
133+
options.IsConvertable(typeIdentifier.TypeSymbol)
134134
? Array
135135
.Empty<(bool IsUnknown, DynamoDbDataMember DDB, string AttributeReference, string
136136
AttributeInterfaceName)>()
137-
: fn(typeSymbol.TypeSymbol)
137+
: fn(typeIdentifier.TypeSymbol)
138138
.Select(x => (
139139
IsUnknown: options.IsUnknown(x.DataMember.TypeIdentifier),
140140
DDB: x,
141-
AttributeReference: TypeName(x.DataMember.Type),
141+
AttributeReference: TypeName(x.DataMember.TypeIdentifier.TypeSymbol),
142142
AttributeInterfaceName:
143-
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.Type.Representation().annotated}>"
143+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.TypeIdentifier.AnnotatedRepresenation}>"
144144
))
145145
.ToArray();
146146

147-
var structName = TypeName(typeSymbol.TypeSymbol);
147+
var structName = TypeName(typeIdentifier.TypeSymbol);
148148
var interfaceName =
149-
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeSymbol.TypeSymbol.Representation().annotated}>";
149+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeIdentifier.TypeSymbol.Representation().annotated}>";
150150

151151
var @struct =
152-
$"public readonly struct {structName} : {interfaceName}".CreateScope(TypeContents(typeSymbol.TypeSymbol, dataMembers,
152+
$"public readonly struct {structName} : {interfaceName}".CreateScope(TypeContents(typeIdentifier, dataMembers,
153153
structName, interfaceName, options));
154154

155155
return new CodeFactory(@struct, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.TypeIdentifier));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static IEnumerable<string> CreateAssignment(string validateReference, st
2727

2828
var innerContent = $"if ({expression}) "
2929
.CreateScope(
30-
$@"{DictionaryName}.Add(""{dataMember.AttributeName}"", {InvokeMarshallerMethod(dataMember.DataMember.Type, reference, $"nameof({keyReference})", options)});")
30+
$@"{DictionaryName}.Add(""{dataMember.AttributeName}"", {InvokeMarshallerMethod(dataMember.DataMember.TypeIdentifier, reference, $"nameof({keyReference})", options)});")
3131
.Concat($"else if ({keyReference} is null) ".CreateScope(
3232
$@"throw {ExceptionHelper.KeysArgumentNullExceptionMethod}(""{dataMember.DataMember.Name}"", ""{keyReference}"");"))
3333
.Concat("else".CreateScope(

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static CodeFactory CreateDictionaryMethod(TypeIdentifier typeIdentifier,
2323
{
2424
var accessPattern = $"{ParamReference}.{x.DataMember.Name}";
2525

26-
var marshallerInvocation = InvokeMarshallerMethod(x.DataMember.Type, accessPattern, $"\"{x.DataMember.Name}\"", options);
26+
var marshallerInvocation = InvokeMarshallerMethod(x.DataMember.TypeIdentifier, accessPattern, $"\"{x.DataMember.Name}\"", options);
2727

2828
var assignment = x.DataMember.IsNullable
2929
? $"if ({x.DataMember.NameAsCamelCase} is not null)"
@@ -33,7 +33,7 @@ private static CodeFactory CreateDictionaryMethod(TypeIdentifier typeIdentifier,
3333

3434
return (
3535
dictionaryAssignment: assignment,
36-
capacityTernary: x.DataMember.IsNullable ? x.DataMember.Type.NotNullTernaryExpression(in accessPattern, "1", "0") : "1",
36+
capacityTernary: x.DataMember.IsNullable ? x.DataMember.TypeIdentifier.TypeSymbol.NotNullTernaryExpression(in accessPattern, "1", "0") : "1",
3737
x.DataMember
3838
);
3939
})
@@ -122,31 +122,31 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
122122
.CreateScope(
123123
$"if ({ParamReference} is null)"
124124
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
125-
.Append($"return {AttributeValueUtilityFactory.FromArray}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
125+
.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)});")
126126
).ToConversion(singleGeneric.T),
127127
SingleGeneric.SupportedType.List => signature
128128
.CreateScope(
129129
$"if ({ParamReference} is null)"
130130
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
131-
.Append($"return {AttributeValueUtilityFactory.FromList}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
131+
.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)});")
132132
).ToConversion(singleGeneric.T),
133133
SingleGeneric.SupportedType.IReadOnlyCollection
134134
or SingleGeneric.SupportedType.IEnumerable
135135
or SingleGeneric.SupportedType.ICollection => signature
136136
.CreateScope(
137137
$"if ({ParamReference} is null)"
138138
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
139-
.Append($"return {AttributeValueUtilityFactory.FromEnumerable}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
139+
.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)});")
140140
).ToConversion(singleGeneric.T),
141-
SingleGeneric.SupportedType.Set when singleGeneric.T.SpecialType is SpecialType.System_String
141+
SingleGeneric.SupportedType.Set when singleGeneric.T.TypeSymbol.SpecialType is SpecialType.System_String
142142
=> signature
143143
.CreateScope(
144144
$"if ({ParamReference} is null)"
145145
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
146-
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ SS = new List<{(singleGeneric.IsTNullable ? "string?" : "string")}>({(singleGeneric.IsTNullable ? ParamReference : $"{ParamReference}.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\"))")})}};")
146+
.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]\"))")})}};")
147147
)
148148
.ToConversion(singleGeneric.T),
149-
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
149+
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric
150150
=> signature
151151
.CreateScope(
152152
$"if ({ParamReference} is null)"
@@ -165,14 +165,14 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
165165
.CreateScope(
166166
$"if ({ParamReference} is null)"
167167
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
168-
.Append($"return {AttributeValueUtilityFactory.FromDictionary}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.IsTValueNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
168+
.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)});")
169169
)
170170
.ToConversion(keyValueGeneric.TValue),
171171
KeyValueGeneric.SupportedType.LookUp => signature
172172
.CreateScope(
173173
$"if ({ParamReference} is null)"
174174
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
175-
.Append($"return {AttributeValueUtilityFactory.FromLookup}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.IsTValueNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
175+
.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)});")
176176
)
177177
.ToConversion(keyValueGeneric.TValue),
178178
_ => throw UncoveredConversionException(keyValueGeneric, nameof(CreateMethod))
@@ -205,17 +205,16 @@ private static IEnumerable<string> InitializeDictionary(IEnumerable<string> capa
205205
}
206206
}
207207

208-
internal static string InvokeMarshallerMethod(ITypeSymbol typeSymbol, string parameterReference, string dataMember, MarshallerOptions options, string optionParam = MarshallerOptions.ParamReference)
208+
internal static string InvokeMarshallerMethod(TypeIdentifier typeIdentifier, string parameterReference, string dataMember, MarshallerOptions options, string optionParam = MarshallerOptions.ParamReference)
209209
{
210-
var invocation = $"{ClassName}.{GetSerializationMethodName(typeSymbol)}({parameterReference}, {optionParam}, {dataMember})";
210+
var invocation = $"{ClassName}.{GetSerializationMethodName(typeIdentifier.TypeSymbol)}({parameterReference}, {optionParam}, {dataMember})";
211211

212-
if (options.IsConvertable(typeSymbol))
212+
if (options.IsConvertable(typeIdentifier.TypeSymbol))
213213
return invocation;
214214

215-
if (typeSymbol.TypeIdentifier() is UnknownType)
216-
return $"{AttributeValueUtilityFactory.ToAttributeValue}({invocation})";
217-
218-
return invocation;
215+
return typeIdentifier is UnknownType
216+
? $"{AttributeValueUtilityFactory.ToAttributeValue}({invocation})"
217+
: invocation;
219218
}
220219

221220
internal static IEnumerable<string> RootSignature(ITypeSymbol typeSymbol, string rootTypeName)

0 commit comments

Comments
 (0)