Skip to content

Commit 24d30df

Browse files
committed
Isolate logic
1 parent f7e9268 commit 24d30df

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private static CodeFactory CreateStruct(TypeIdentifier typeIdentifier, Func<ITyp
146146

147147
var structName = TypeName(typeIdentifier.TypeSymbol);
148148
var interfaceName =
149-
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeIdentifier.TypeSymbol.Representation().annotated}>";
149+
$"{Constants.DynamoDBGenerator.Marshaller.AttributeExpressionValueTrackerInterface}<{typeIdentifier.AnnotatedRepresenation}>";
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.OriginalRepresentation} {{ }} {reference}";
26+
var expression = $"{keyReference} is {dataMember.DataMember.TypeIdentifier.OriginalRepresenation} {{ }} {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.OriginalRepresentation}"");"));
34+
$@"throw {ExceptionHelper.KeysInvalidConversionExceptionMethod}(""{dataMember.DataMember.Name}"", ""{keyReference}"", {keyReference}, ""{dataMember.DataMember.TypeIdentifier.OriginalRepresenation}"");"));
3535

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

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ private static CodeFactory CreateCode(TypeIdentifier typeIdentifier, Func<ITypeS
7272
private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
7373
MarshallerOptions options)
7474
{
75-
if (options.TryReadConversion(typeIdentifier.TypeSymbol, Value) is {} conversion)
75+
if (options.TryReadConversion(typeIdentifier, Value) is {} conversion)
7676
{
7777
return typeIdentifier.TypeSymbol switch
7878
{
7979
{ IsValueType: true } => typeIdentifier.TypeSymbol switch
8080
{
81-
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(typeIdentifier.TypeSymbol, options)
81+
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(typeIdentifier, options)
8282
.CreateScope(
8383
$"if ({Value} is null)"
8484
.CreateScope("return null;")
8585
.Append($"return {conversion};")
8686
)
8787
.ToConversion(),
88-
_ => CreateSignature(typeIdentifier.TypeSymbol, options)
88+
_ => CreateSignature(typeIdentifier, options)
8989
.CreateScope(
9090
$"if ({Value} is null)"
9191
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
@@ -102,13 +102,13 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
102102
{ IsReferenceType: true } => typeIdentifier.TypeSymbol switch
103103
{
104104
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(
105-
typeIdentifier.TypeSymbol, options)
105+
typeIdentifier, options)
106106
.CreateScope(
107107
$"if ({Value} is null)"
108108
.CreateScope("return null;")
109109
.Append($"return {conversion};")
110110
).ToConversion(),
111-
_ => CreateSignature(typeIdentifier.TypeSymbol, options)
111+
_ => CreateSignature(typeIdentifier, options)
112112
.CreateScope(
113113
$"if ({Value} is null)"
114114
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
@@ -131,7 +131,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
131131

132132
return typeIdentifier switch
133133
{
134-
SingleGeneric singleGeneric when CreateSignature(singleGeneric.TypeSymbol, options) is var signature => singleGeneric.Type switch
134+
SingleGeneric singleGeneric when CreateSignature(singleGeneric, options) is var signature => singleGeneric.Type switch
135135
{
136136
SingleGeneric.SupportedType.Nullable => signature
137137
.CreateScope(
@@ -180,7 +180,7 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
180180
},
181181
KeyValueGeneric {TKey.SpecialType: not SpecialType.System_String} keyValueGeneric => throw new ArgumentException("Only strings are supported for for TKey",
182182
UncoveredConversionException(keyValueGeneric, nameof(CreateMethod))),
183-
KeyValueGeneric keyValueGeneric when CreateSignature(keyValueGeneric.TypeSymbol, options) is var signature => keyValueGeneric.Type switch
183+
KeyValueGeneric keyValueGeneric when CreateSignature(keyValueGeneric, options) is var signature => keyValueGeneric.Type switch
184184
{
185185
KeyValueGeneric.SupportedType.Dictionary => signature
186186
.CreateScope(
@@ -205,9 +205,9 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
205205

206206
}
207207

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

213213
private static IEnumerable<string> CreateTypeContents(IEnumerable<DynamoDBMarshallerArguments> arguments,

src/DynamoDBGenerator.SourceGenerator/Types/DataMember.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ private DataMember(in ISymbol symbol, in string fieldName, in ITypeSymbol type,
1515
IsAssignable = isAssignable;
1616
NameAsPrivateField = fieldName.ToPrivateFieldFromPascal();
1717
NameAsCamelCase = fieldName.ToCamelCaseFromPascal();
18-
OriginalRepresentation = type.Representation().original;
1918
TypeIdentifier = type.TypeIdentifier();
2019
}
2120

@@ -48,7 +47,6 @@ public static DataMember FromProperty(in IPropertySymbol property)
4847
public string Name { get; }
4948

5049
public TypeIdentifier TypeIdentifier { get; }
51-
public string OriginalRepresentation { get; }
5250
public string NameAsPrivateField { get; }
5351
public string NameAsCamelCase { get; }
5452
public bool IsAssignable { get; }

src/DynamoDBGenerator.SourceGenerator/Types/MarshallerOptions.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,22 @@ int enumStrategy
6767

6868
return null;
6969
}
70-
public string? TryReadConversion(ITypeSymbol typeSymbol, string attributeValueParam)
70+
public string? TryReadConversion(TypeIdentifier typeIdentifier, string attributeValueParam)
7171
{
7272
// Converters comes first so that you your customized converters are always prioritized.
73-
if (Converters.TryGetValue(typeSymbol, out var match))
73+
if (Converters.TryGetValue(typeIdentifier.TypeSymbol, out var match))
7474
return $"{ParamReference}.{ConvertersProperty}.{match.Key}.Read({attributeValueParam})";
7575

76-
if (typeSymbol.TypeKind is TypeKind.Enum)
76+
if (typeIdentifier.TypeSymbol.TypeKind is TypeKind.Enum)
7777
{
78-
var original = typeSymbol.Representation().original;
7978
return _enumStrategy switch
8079
{
81-
ConversionStrategy.Integer => $"(Int32.TryParse({attributeValueParam}.N, out var e) ? ({original}?) e : null)",
82-
ConversionStrategy.Name => $"(Enum.TryParse<{original}>({attributeValueParam}.S, false, out var e) ? ({original}?) e : null)",
80+
ConversionStrategy.Integer => $"(Int32.TryParse({attributeValueParam}.N, out var e) ? ({typeIdentifier.OriginalRepresenation}?) e : null)",
81+
ConversionStrategy.Name => $"(Enum.TryParse<{typeIdentifier.OriginalRepresenation}>({attributeValueParam}.S, false, out var e) ? ({typeIdentifier.OriginalRepresenation}?) e : null)",
8382
ConversionStrategy.NameCI
8483
or ConversionStrategy.LowerCase
8584
or ConversionStrategy.UpperCase
86-
=> $"(Enum.TryParse<{original}>({attributeValueParam}.S, true, out var e) ? ({original}?) e : null)",
85+
=> $"(Enum.TryParse<{typeIdentifier.OriginalRepresenation}>({attributeValueParam}.S, true, out var e) ? ({typeIdentifier.OriginalRepresenation}?) e : null)",
8786
_ => throw new ArgumentException($"Could not resolve enum conversion strategy from value '{_enumStrategy}'.")
8887
};
8988
}

0 commit comments

Comments
 (0)