Skip to content

Commit f82c86c

Browse files
committed
Move representation method to TypeIdentifier
1 parent 52bbaee commit f82c86c

File tree

10 files changed

+150
-148
lines changed

10 files changed

+150
-148
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -71,75 +71,7 @@ static TypeIdentifier Create(ITypeSymbol typeSymbol)
7171
}
7272
}
7373

74-
private static readonly ConcurrentDictionary<ITypeSymbol, (string, string)> RepresentationDictionary =
75-
new(SymbolEqualityComparer.IncludeNullability);
76-
77-
public static (string annotated, string original) Representation(this ITypeSymbol typeSymbol)
78-
{
79-
return RepresentationDictionary.GetOrAdd(typeSymbol, x =>
80-
{
81-
var displayString = x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
82-
return RepresentationDictionary[typeSymbol] = (ToString(typeSymbol, displayString), displayString);
83-
});
84-
85-
static string ToString(ITypeSymbol x, string displayString)
86-
{
87-
if (x is IArrayTypeSymbol arrayTypeSymbol)
88-
{
89-
var result = ToString(arrayTypeSymbol.ElementType,
90-
arrayTypeSymbol.ElementType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
9174

92-
return x.NullableAnnotation switch
93-
{
94-
NullableAnnotation.Annotated or NullableAnnotation.None => $"{result}[]?",
95-
NullableAnnotation.NotAnnotated => $"{result}[]",
96-
_ => throw new ArgumentException(ExceptionMessage(x))
97-
};
98-
}
99-
100-
if (x is not INamedTypeSymbol namedTypeSymbol || namedTypeSymbol.TypeArguments.Length is 0)
101-
{
102-
return x.NullableAnnotation switch
103-
{
104-
// Having `Annotated` and `None` produce append '?' is fine as long as `SuffixedTypeSymbolNameFactory` is giving them different names. Otherwise we could create broken signatures due to duplication.
105-
NullableAnnotation.Annotated or NullableAnnotation.None => $"{displayString}?",
106-
NullableAnnotation.NotAnnotated => displayString,
107-
_ => throw new ArgumentException(ExceptionMessage(x))
108-
};
109-
}
110-
111-
if (namedTypeSymbol.OriginalDefinition.SpecialType is SpecialType.System_Nullable_T)
112-
return displayString;
113-
114-
if (namedTypeSymbol.IsTupleType)
115-
{
116-
var tupleElements = namedTypeSymbol.TupleElements
117-
.Select(y =>
118-
$"{ToString(y.Type, $"{y.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}")} {y.Name}");
119-
return $"({string.Join(", ", tupleElements)})";
120-
}
121-
122-
var index = displayString.AsSpan().IndexOf('<');
123-
if (index == -1)
124-
return displayString;
125-
126-
var typeWithoutGenericParameters = displayString.Substring(0, index);
127-
var typeParameters = string.Join(", ",
128-
namedTypeSymbol.TypeArguments.Select(y =>
129-
ToString(y, y.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))));
130-
return namedTypeSymbol.NullableAnnotation switch
131-
{
132-
// Having `Annotated` and `None` produce append '?' is fine as long as `SuffixedTypeSymbolNameFactory` is giving them different names. Otherwise we could create broken signatures due to duplication.
133-
NullableAnnotation.Annotated or NullableAnnotation.None =>
134-
$"{typeWithoutGenericParameters}<{typeParameters}>?",
135-
NullableAnnotation.NotAnnotated => $"{typeWithoutGenericParameters}<{typeParameters}>",
136-
_ => throw new ArgumentException(ExceptionMessage(namedTypeSymbol))
137-
};
138-
139-
static string ExceptionMessage(ISymbol typeSymbol) =>
140-
$"Could nullable annotation on type: {typeSymbol.ToDisplayString()}";
141-
}
142-
}
14375

14476
//Source: https://referencesource.microsoft.com/#mscorlib/system/tuple.cs,49b112811bc359fd,references
14577
private class TupleComparer : IEqualityComparer<(ITypeSymbol, ITypeSymbol)>

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ MarshallerOptions options
7979
)
8080
)
8181
.ScopeTo(
82-
$"IEnumerable<KeyValuePair<string, AttributeValue>> {interfaceName}.{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerAccessedValues}({typeIdentifier.AnnotatedRepresenation} entity)");
82+
$"IEnumerable<KeyValuePair<string, AttributeValue>> {interfaceName}.{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerAccessedValues}({typeIdentifier.AnnotatedString} entity)");
8383

8484

8585
foreach (var yield in yields)
@@ -140,13 +140,13 @@ private static CodeFactory CreateStruct(TypeIdentifier typeIdentifier, Func<ITyp
140140
DDB: x,
141141
AttributeReference: TypeName(x.DataMember.TypeIdentifier.TypeSymbol),
142142
AttributeInterfaceName:
143-
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.TypeIdentifier.AnnotatedRepresenation}>"
143+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{x.DataMember.TypeIdentifier.AnnotatedString}>"
144144
))
145145
.ToArray();
146146

147147
var structName = TypeName(typeIdentifier.TypeSymbol);
148148
var interfaceName =
149-
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeIdentifier.AnnotatedRepresenation}>";
149+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeIdentifier.AnnotatedString}>";
150150

151151
var @struct =
152152
$"public readonly struct {structName} : {interfaceName}".CreateScope(TypeContents(typeIdentifier, dataMembers,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ private static IEnumerable<string> CreateAssignment(string validateReference, st
2323
DynamoDbDataMember dataMember, MarshallerOptions options)
2424
{
2525
const string reference = "value";
26-
var expression = $"{keyReference} is {dataMember.DataMember.TypeIdentifier.OriginalRepresenation} {{ }} {reference}";
26+
var expression = $"{keyReference} is {dataMember.DataMember.TypeIdentifier.UnannotatedString} {{ }} {reference}";
2727

2828
var innerContent = $"if ({expression}) "
2929
.CreateScope(
3030
$@"{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(
34-
$@"throw {ExceptionHelper.KeysInvalidConversionExceptionMethod}(""{dataMember.DataMember.Name}"", ""{keyReference}"", {keyReference}, ""{dataMember.DataMember.TypeIdentifier.OriginalRepresenation}"");"));
34+
$@"throw {ExceptionHelper.KeysInvalidConversionExceptionMethod}(""{dataMember.DataMember.Name}"", ""{keyReference}"", {keyReference}, ""{dataMember.DataMember.TypeIdentifier.UnannotatedString}"");"));
3535

3636
return $"if ({validateReference})".CreateScope(innerContent);
3737
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static CodeFactory CreateDictionaryMethod(TypeIdentifier typeIdentifier,
5151
.Append($"return {DictionaryReference};"));
5252

5353
var code =
54-
$"public static Dictionary<string, AttributeValue>{(typeIdentifier.IsNullable ? '?' : null)} {GetSerializationMethodName(typeIdentifier.TypeSymbol)}({typeIdentifier.AnnotatedRepresenation} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
54+
$"public static Dictionary<string, AttributeValue>{(typeIdentifier.IsNullable ? '?' : null)} {GetSerializationMethodName(typeIdentifier.TypeSymbol)}({typeIdentifier.AnnotatedString} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
5555
.CreateScope(body);
5656

5757
return new CodeFactory(code, properties.Select(y => y.DataMember.TypeIdentifier));
@@ -187,8 +187,8 @@ private static string CreateSignature(TypeIdentifier typeIdentifier, MarshallerO
187187
{
188188
var typeSymbol = typeIdentifier.TypeSymbol;
189189
return typeIdentifier.IsNullable
190-
? $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue}? {GetSerializationMethodName(typeSymbol)}({typeIdentifier.AnnotatedRepresenation} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
191-
: $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue} {GetSerializationMethodName(typeSymbol)}({typeIdentifier.AnnotatedRepresenation} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
190+
? $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue}? {GetSerializationMethodName(typeSymbol)}({typeIdentifier.AnnotatedString} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
191+
: $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue} {GetSerializationMethodName(typeSymbol)}({typeIdentifier.AnnotatedString} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
192192
}
193193

194194
private static IEnumerable<string> InitializeDictionary(IEnumerable<string> capacityCalculations)
@@ -217,13 +217,13 @@ internal static string InvokeMarshallerMethod(TypeIdentifier typeIdentifier, str
217217
: invocation;
218218
}
219219

220-
internal static IEnumerable<string> RootSignature(ITypeSymbol typeSymbol, string rootTypeName)
220+
internal static IEnumerable<string> RootSignature(TypeIdentifier typeIdentifier)
221221
{
222222

223-
return $"public Dictionary<{nameof(String)}, {Constants.AWSSDK_DynamoDBv2.AttributeValue}> {Constants.DynamoDBGenerator.Marshaller.MarshallMethodName}({rootTypeName} {ParamReference})"
223+
return $"public Dictionary<{nameof(String)}, {Constants.AWSSDK_DynamoDBv2.AttributeValue}> {Constants.DynamoDBGenerator.Marshaller.MarshallMethodName}({typeIdentifier.AnnotatedString} {ParamReference})"
224224
.CreateScope(
225225
$"ArgumentNullException.ThrowIfNull({ParamReference});",
226-
$"return {ClassName}.{GetSerializationMethodName(typeSymbol)}({ParamReference}, {MarshallerOptions.FieldReference});"
226+
$"return {ClassName}.{GetSerializationMethodName(typeIdentifier.TypeSymbol)}({ParamReference}, {MarshallerOptions.FieldReference});"
227227
);
228228
}
229229

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ private static CodeFactory CreateCode(TypeIdentifier typeIdentifier, Func<ITypeS
6161
.SelectMany(x => x)
6262
.DefaultIfEmpty("();")
6363
// Is needed in order to not perform new entity? where '?' is not allowed in the end of the string.
64-
.Prepend(typeIdentifier.TypeSymbol.IsTupleType ? "return" : $"return new {typeIdentifier.AnnotatedRepresenation.TrimEnd('?')}")
64+
.Prepend(typeIdentifier.TypeSymbol.IsTupleType ? "return" : $"return new {typeIdentifier.AnnotatedString.TrimEnd('?')}")
6565
);
6666

67-
var method = $"public static {typeIdentifier.AnnotatedRepresenation} {GetDeserializationMethodName(typeIdentifier.TypeSymbol)}(Dictionary<string, AttributeValue>? {Dict}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)".CreateScope(blockBody);
67+
var method = $"public static {typeIdentifier.AnnotatedString} {GetDeserializationMethodName(typeIdentifier.TypeSymbol)}(Dictionary<string, AttributeValue>? {Dict}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)".CreateScope(blockBody);
6868

6969
return new CodeFactory(method, assignments.Select(x => x.DDB.DataMember.TypeIdentifier));
7070

@@ -172,7 +172,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
172172
.CreateScope(
173173
$"if ({Value} is null || {Value}.NS is null)"
174174
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
175-
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{singleGeneric.T.OriginalRepresenation}>" : null)}({Value}.NS.Select(y => {singleGeneric.T.OriginalRepresenation}.Parse(y)));")
175+
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{singleGeneric.T.UnannotatedString}>" : null)}({Value}.NS.Select(y => {singleGeneric.T.UnannotatedString}.Parse(y)));")
176176
)
177177
.ToConversion(singleGeneric),
178178
SingleGeneric.SupportedType.Set => throw new ArgumentException("Only string and integers are supported for sets", UncoveredConversionException(singleGeneric, nameof(CreateMethod))),
@@ -207,7 +207,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
207207

208208
private static string CreateSignature(TypeIdentifier typeIdentifier, MarshallerOptions options)
209209
{
210-
return $"public static {typeIdentifier.AnnotatedRepresenation} {GetDeserializationMethodName(typeIdentifier.TypeSymbol)}(AttributeValue? {Value}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
210+
return $"public static {typeIdentifier.AnnotatedString} {GetDeserializationMethodName(typeIdentifier.TypeSymbol)}(AttributeValue? {Value}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
211211
}
212212

213213
private static IEnumerable<string> CreateTypeContents(IEnumerable<DynamoDBMarshallerArguments> arguments,
@@ -264,11 +264,11 @@ private static IEnumerable<string> ObjectAssignmentBlock(bool useParentheses, IE
264264
}
265265

266266

267-
internal static IEnumerable<string> RootSignature(ITypeSymbol typeSymbol, string rootTypeName)
267+
internal static IEnumerable<string> RootSignature(TypeIdentifier typeIdentifier)
268268
{
269-
return $"public {rootTypeName} {Marshaller.UnmarshalMethodName}(Dictionary<{nameof(String)}, {Constants.AWSSDK_DynamoDBv2.AttributeValue}> entity)".CreateScope(
269+
return $"public {typeIdentifier.AnnotatedString} {Marshaller.UnmarshalMethodName}(Dictionary<{nameof(String)}, {Constants.AWSSDK_DynamoDBv2.AttributeValue}> entity)".CreateScope(
270270
"ArgumentNullException.ThrowIfNull(entity);",
271-
$"return {UnMarshallerClass}.{GetDeserializationMethodName(typeSymbol)}(entity, {MarshallerOptions.FieldReference});");
271+
$"return {UnMarshallerClass}.{GetDeserializationMethodName(typeIdentifier.TypeSymbol)}(entity, {MarshallerOptions.FieldReference});");
272272
}
273273
private static IEnumerable<(string DataMember, string ParameterName)> TryGetMatchedConstructorArguments(ITypeSymbol typeSymbol)
274274
{

src/DynamoDBGenerator.SourceGenerator/MarshallerFactory.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ private static IEnumerable<string> CreateImplementations(
1717
{
1818
foreach (var argument in arguments)
1919
{
20-
var (expressionValueMethod, valueTrackerTypeName) = AttributeExpressionValue.RootSignature(parentType, argument.ArgumentType);
21-
var (expressionMethodName, nameTrackerTypeName) = AttributeExpressionName.RootSignature(parentType, argument.EntityTypeSymbol);
20+
var (expressionValueMethod, valueTrackerTypeName) = AttributeExpressionValue.RootSignature(parentType, argument.ArgumentType.TypeSymbol);
21+
var (expressionMethodName, nameTrackerTypeName) = AttributeExpressionName.RootSignature(parentType, argument.EntityTypeSymbol.TypeSymbol);
2222
var constructor =
2323
$"public {argument.ImplementationName}({options.FullName} {MarshallerOptions.ParamReference})"
2424
.CreateScope($"{MarshallerOptions.FieldReference} = {MarshallerOptions.ParamReference};",
25-
$"{KeyMarshaller.PrimaryKeyMarshallerReference} = {KeyMarshaller.AssignmentRoot(argument.EntityTypeSymbol)};");
25+
$"{KeyMarshaller.PrimaryKeyMarshallerReference} = {KeyMarshaller.AssignmentRoot(argument.EntityTypeSymbol.TypeSymbol)};");
2626

2727
var implementation = constructor
28-
.Concat(RootSignature(argument.EntityTypeSymbol, argument.AnnotatedEntityType))
29-
.Concat(UnMarshaller.RootSignature(argument.EntityTypeSymbol, argument.AnnotatedEntityType))
30-
.Concat(KeyMarshaller.IndexKeyMarshallerRootSignature(argument.EntityTypeSymbol))
28+
.Concat(RootSignature(argument.EntityTypeSymbol))
29+
.Concat(UnMarshaller.RootSignature(argument.EntityTypeSymbol))
30+
.Concat(KeyMarshaller.IndexKeyMarshallerRootSignature(argument.EntityTypeSymbol.TypeSymbol))
3131
.Concat(expressionValueMethod)
3232
.Append(expressionMethodName)
3333
.Append(KeyMarshaller.PrimaryKeyMarshallerDeclaration)
3434
.Prepend(options.FieldDeclaration)
35-
.ScopeTo($"file sealed class {argument.ImplementationName}: {Interface}<{argument.AnnotatedEntityType}, {argument.AnnotatedArgumentType}, {nameTrackerTypeName}, {valueTrackerTypeName}>");
35+
.ScopeTo($"file sealed class {argument.ImplementationName}: {Interface}<{argument.EntityTypeSymbol.AnnotatedString}, {argument.ArgumentType.AnnotatedString}, {nameTrackerTypeName}, {valueTrackerTypeName}>");
3636

3737
foreach (var row in implementation)
3838
yield return row;
@@ -44,12 +44,12 @@ private static IEnumerable<string> PublicProperties(ITypeSymbol parentType, Dyna
4444
{
4545
foreach (var argument in arguments)
4646
{
47-
var valueTrackerTypeName = AttributeExpressionValue.GloballyAccessibleName(parentType, argument.ArgumentType);
48-
var nameTrackerTypeName = AttributeExpressionName.GloballyAccessibleName(parentType, argument.EntityTypeSymbol);
47+
var valueTrackerTypeName = AttributeExpressionValue.GloballyAccessibleName(parentType, argument.ArgumentType.TypeSymbol);
48+
var nameTrackerTypeName = AttributeExpressionName.GloballyAccessibleName(parentType, argument.EntityTypeSymbol.TypeSymbol);
4949
yield return options.TryInstantiate() switch
5050
{
51-
{ } arg => $"public static {Interface}<{argument.AnnotatedEntityType}, {argument.AnnotatedArgumentType}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName} {{ get; }} = new {argument.ImplementationName}({arg});",
52-
null => $"public static {Interface}<{argument.AnnotatedEntityType}, {argument.AnnotatedArgumentType}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName}({options.FullName} options) => new {argument.ImplementationName}(options);"
51+
{ } arg => $"public static {Interface}<{argument.EntityTypeSymbol.AnnotatedString}, {argument.ArgumentType.AnnotatedString}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName} {{ get; }} = new {argument.ImplementationName}({arg});",
52+
null => $"public static {Interface}<{argument.EntityTypeSymbol.AnnotatedString}, {argument.ArgumentType.AnnotatedString}, {nameTrackerTypeName}, {valueTrackerTypeName}> {argument.AccessName}({options.FullName} options) => new {argument.ImplementationName}(options);"
5353
};
5454
}
5555
}

0 commit comments

Comments
 (0)