Skip to content

Commit 839b63d

Browse files
committed
WIP enrich types
1 parent 60d9f11 commit 839b63d

10 files changed

Lines changed: 122 additions & 82 deletions

File tree

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public static CodeFactory ToConversion(this IEnumerable<string> enumerable)
294294

295295
public static CodeFactory ToConversion(this IEnumerable<string> enumerable, ITypeSymbol typeSymbol)
296296
{
297-
return new CodeFactory(enumerable, new[] { typeSymbol });
297+
return new CodeFactory(enumerable, new[] { typeSymbol.TypeIdentifier() });
298298
}
299299

300300
public static INamedTypeSymbol? TryGetNullableValueType(this ITypeSymbol type)

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionName.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ internal static IEnumerable<string> CreateClasses(DynamoDBMarshallerArguments[]
2121
Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
2222
{
2323
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
24-
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
24+
var hashSet = new HashSet<TypeIdentifier>(TypeIdentifier.Default);
2525

2626
return arguments
2727
.SelectMany(x =>
28-
CodeFactory.Create(x.EntityTypeSymbol, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
28+
CodeFactory.Create(x.EntityTypeSymbol.TypeIdentifier(), y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
2929
}
3030

3131
private static IEnumerable<string> TypeContent(
@@ -105,25 +105,25 @@ private static IEnumerable<string> YieldSelector(
105105
.CreateScope($"yield return {camelCase};");
106106
}
107107

108-
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
108+
private static CodeFactory CreateStruct(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
109109
MarshallerOptions options)
110110
{
111-
var dataMembers = fn(typeSymbol)
111+
var dataMembers = fn(typeIdentifier.TypeSymbol)
112112
.Select(x => (
113-
IsUnknown: options.IsUnknown(x.DataMember.Type),
113+
IsUnknown: options.IsUnknown(x.DataMember.TypeIdentifier),
114114
DDB: x,
115115
DbRef: $"#{x.AttributeName}",
116116
AttributeReference: TypeName(x.DataMember.Type),
117117
AttributeInterfaceName: AttributeExpressionNameTrackerInterface
118118
))
119119
.ToArray();
120120

121-
var structName = TypeName(typeSymbol);
121+
var structName = TypeName(typeIdentifier.TypeSymbol);
122122

123123
var @class =
124124
$"public readonly struct {structName} : {AttributeExpressionNameTrackerInterface}".CreateScope(
125-
TypeContent(typeSymbol, dataMembers, structName));
126-
return new CodeFactory(@class, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
125+
TypeContent(typeIdentifier.TypeSymbol, dataMembers, structName));
126+
return new CodeFactory(@class, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.TypeIdentifier));
127127
}
128128

129129
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,40 @@ internal static IEnumerable<string> CreateClasses(DynamoDBMarshallerArguments[]
119119
Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
120120
{
121121
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
122-
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
122+
var hashSet = new HashSet<TypeIdentifier>(TypeIdentifier.Default);
123123

124124
return arguments
125125
.SelectMany(x =>
126-
CodeFactory.Create(x.ArgumentType, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
126+
CodeFactory.Create(x.ArgumentType.TypeIdentifier(), y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
127127
}
128128

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

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

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

155-
return new CodeFactory(@struct, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.Type));
155+
return new CodeFactory(@struct, dataMembers.Where(x => x.IsUnknown).Select(x => x.DDB.DataMember.TypeIdentifier));
156156
}
157157

158158
[MethodImpl(MethodImplOptions.AggressiveInlining)]

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol,
7575
internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArguments> arguments,
7676
Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
7777
{
78-
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
78+
var hashSet = new HashSet<TypeIdentifier>(TypeIdentifier.Nullable);
7979

8080
return arguments
81-
.SelectMany(x => CodeFactory.Create(x.EntityTypeSymbol,
81+
.SelectMany(x => CodeFactory.Create(x.EntityTypeSymbol.TypeIdentifier(),
8282
y => StaticAttributeValueDictionaryKeys(y, getDynamoDbProperties, options), hashSet));
8383
}
8484

@@ -147,12 +147,12 @@ internal static string AssignmentRoot(ITypeSymbol typeSymbol)
147147
$"new {KeyMarshallerImplementationTypeName}((pk, rk, ipk, irk, dm) => {ClassName}.{MethodName(typeSymbol)}({MarshallerOptions.FieldReference}, pk, rk, ipk, irk, dm))";
148148
}
149149

150-
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol,
150+
private static CodeFactory StaticAttributeValueDictionaryKeys(TypeIdentifier typeIdentifier,
151151
Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
152152
{
153153
var code =
154-
$"public static Dictionary<string, AttributeValue> {MethodName(typeSymbol)}({options.FullName} {MarshallerOptions.ParamReference}, object? {PkReference}, object? {RkReference}, bool {EnforcePkReference}, bool {EnforceRkReference}, string? index = null)"
155-
.CreateScope(MethodBody(typeSymbol, fn, options));
154+
$"public static Dictionary<string, AttributeValue> {MethodName(typeIdentifier.TypeSymbol)}({options.FullName} {MarshallerOptions.ParamReference}, object? {PkReference}, object? {RkReference}, bool {EnforcePkReference}, bool {EnforceRkReference}, string? index = null)"
155+
.CreateScope(MethodBody(typeIdentifier.TypeSymbol, fn, options));
156156

157157
return new CodeFactory(code);
158158
}

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ internal static IEnumerable<string> CreateClass(DynamoDBMarshallerArguments[] ar
1616
{
1717
return $"file static class {ClassName}".CreateScope(TypeContent(arguments, getDynamoDbProperties, options));
1818
}
19-
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
19+
private static CodeFactory CreateDictionaryMethod(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
2020
{
21-
var properties = fn(typeSymbol)
21+
var properties = fn(typeIdentifier.TypeSymbol)
2222
.Select(x =>
2323
{
2424
var accessPattern = $"{ParamReference}.{x.DataMember.Name}";
@@ -34,16 +34,15 @@ private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<I
3434
return (
3535
dictionaryAssignment: assignment,
3636
capacityTernary: x.DataMember.IsNullable ? x.DataMember.Type.NotNullTernaryExpression(in accessPattern, "1", "0") : "1",
37-
x.DataMember.Type
37+
x.DataMember
3838
);
3939
})
4040
.ToArray();
4141

42-
var isNullable = typeSymbol.IsNullable();
4342
var enumerable = Enumerable.Empty<string>();
44-
if (isNullable)
43+
if (typeIdentifier.IsNullable)
4544
enumerable = $"if ({ParamReference} is null)".CreateScope("return null;");
46-
else if (typeSymbol.IsReferenceType)
45+
else if (typeIdentifier.TypeSymbol.IsReferenceType)
4746
enumerable = $"if ({ParamReference} is null)".CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});");
4847

4948
var body =
@@ -52,67 +51,66 @@ private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<I
5251
.Append($"return {DictionaryReference};"));
5352

5453
var code =
55-
$"public static Dictionary<string, AttributeValue>{(isNullable ? '?' : null)} {GetSerializationMethodName(typeSymbol)}({typeSymbol.Representation().annotated} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
54+
$"public static Dictionary<string, AttributeValue>{(typeIdentifier.IsNullable ? '?' : null)} {GetSerializationMethodName(typeIdentifier.TypeSymbol)}({typeIdentifier.AnnotatedRepresenation} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
5655
.CreateScope(body);
5756

58-
return new CodeFactory(code, properties.Select(y => y.Type));
57+
return new CodeFactory(code, properties.Select(y => y.DataMember.TypeIdentifier));
5958

6059
}
6160

6261
private static IEnumerable<string> TypeContent(DynamoDBMarshallerArguments[] arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
6362
{
64-
var hashset = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
63+
var hashset = new HashSet<TypeIdentifier>(TypeIdentifier.Nullable);
6564

6665
return arguments.SelectMany(x => CodeFactory
6766
.Create(
68-
x.EntityTypeSymbol,
67+
x.EntityTypeSymbol.TypeIdentifier(),
6968
y => CreateMethod(y, getDynamoDbProperties, options),
7069
hashset
7170
)
72-
.Concat(CodeFactory.Create(x.ArgumentType, y => CreateMethod(y, getDynamoDbProperties, options),
71+
.Concat(CodeFactory.Create(x.ArgumentType.TypeIdentifier(), y => CreateMethod(y, getDynamoDbProperties, options),
7372
hashset))
7473
)
7574
.Concat(KeyMarshaller.CreateKeys(arguments, getDynamoDbProperties, options));
7675
}
77-
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
76+
private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
7877
{
79-
if (options.TryWriteConversion(type, ParamReference) is {} conversion)
78+
if (options.TryWriteConversion(typeIdentifier.TypeSymbol, ParamReference) is {} conversion)
8079
{
81-
return type switch
80+
return typeIdentifier.TypeSymbol switch
8281
{
83-
{ IsValueType: true } => type switch
82+
{ IsValueType: true } => typeIdentifier.TypeSymbol switch
8483
{
85-
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(type, options)
84+
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(typeIdentifier, options)
8685
.CreateScope($"if ({ParamReference} is null)"
8786
.CreateScope("return null;")
8887
.Append($"return {conversion};")
8988
)
9089
.ToConversion(),
91-
_ => CreateSignature(type, options)
90+
_ => CreateSignature(typeIdentifier, options)
9291
.CreateScope($"return {conversion};")
9392
.ToConversion()
9493
},
95-
{ IsReferenceType: true } => type switch
94+
{ IsReferenceType: true } => typeIdentifier.TypeSymbol switch
9695
{
97-
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(
98-
type, options)
96+
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(typeIdentifier, options)
9997
.CreateScope($"if ({ParamReference} is null)".CreateScope("return null;")
10098
.Append($"return {conversion};"))
10199
.ToConversion(),
102-
_ => CreateSignature(type, options)
100+
_ => CreateSignature(typeIdentifier, options)
103101
.CreateScope(
104102
$"if ({ParamReference} is null)".CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});").Append($"return {conversion};")
105103
)
106104
.ToConversion()
107105
},
108106
_ => throw new ArgumentException(
109-
$"Neither ValueType or ReferenceType could be resolved for conversion. type '{type.ToDisplayString()}'.")
107+
$"Neither ValueType or ReferenceType could be resolved for conversion. type '{typeIdentifier.TypeSymbol.ToDisplayString()}'.")
110108
};
111109
}
112110

113-
return type.TypeIdentifier() switch
111+
return typeIdentifier switch
114112
{
115-
SingleGeneric singleGeneric when CreateSignature(singleGeneric.TypeSymbol, options) is var signature => singleGeneric.Type switch
113+
SingleGeneric singleGeneric when CreateSignature(singleGeneric, options) is var signature => singleGeneric.Type switch
116114
{
117115
SingleGeneric.SupportedType.Nullable => signature
118116
.CreateScope(
@@ -161,7 +159,7 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
161159
},
162160
KeyValueGeneric {TKey.SpecialType: not SpecialType.System_String} keyValueGeneric => throw new ArgumentException("Only strings are supported for for TKey",
163161
UncoveredConversionException(keyValueGeneric, nameof(CreateMethod))),
164-
KeyValueGeneric keyValueGeneric when CreateSignature(keyValueGeneric.TypeSymbol, options) is var signature => keyValueGeneric.Type switch
162+
KeyValueGeneric keyValueGeneric when CreateSignature(keyValueGeneric, options) is var signature => keyValueGeneric.Type switch
165163
{
166164
KeyValueGeneric.SupportedType.Dictionary => signature
167165
.CreateScope(
@@ -179,17 +177,18 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
179177
.ToConversion(keyValueGeneric.TValue),
180178
_ => throw UncoveredConversionException(keyValueGeneric, nameof(CreateMethod))
181179
},
182-
UnknownType unknownType => CreateDictionaryMethod(unknownType.TypeSymbol, fn, options),
183-
var typeIdentifier => throw UncoveredConversionException(typeIdentifier, nameof(CreateMethod))
180+
UnknownType unknownType => CreateDictionaryMethod(unknownType, fn, options),
181+
_ => throw UncoveredConversionException(typeIdentifier, nameof(CreateMethod))
184182

185183
};
186184

187185
}
188-
private static string CreateSignature(ITypeSymbol typeSymbol, MarshallerOptions options)
186+
private static string CreateSignature(TypeIdentifier typeIdentifier, MarshallerOptions options)
189187
{
190-
return typeSymbol.IsNullable()
191-
? $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue}? {GetSerializationMethodName(typeSymbol)}({typeSymbol.Representation().annotated} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)"
192-
: $"public static {Constants.AWSSDK_DynamoDBv2.AttributeValue} {GetSerializationMethodName(typeSymbol)}({typeSymbol.Representation().annotated} {ParamReference}, {options.FullName} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
188+
var typeSymbol = typeIdentifier.TypeSymbol;
189+
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)";
193192
}
194193

195194
private static IEnumerable<string> InitializeDictionary(IEnumerable<string> capacityCalculations)

0 commit comments

Comments
 (0)