Skip to content

Commit 421abfb

Browse files
committed
Stop relying on special syntax
1 parent 7c7ab7d commit 421abfb

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,59 @@ private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, Dyna
7575

7676
if (options.TryReadConversion(type, Value) is {} conversion)
7777
{
78-
if (type.IsNullable())
79-
return CreateSignature(type, options)
80-
.CreateScope(
81-
$"if ({Value} is null)"
82-
.CreateScope("return null;")
83-
.Append($"return {conversion};")
84-
)
85-
.ToConversion();
78+
return type switch
79+
{
80+
{ IsValueType: true } => type switch
81+
{
82+
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(type, options)
83+
.CreateScope(
84+
$"if ({Value} is null)"
85+
.CreateScope("return null;")
86+
.Append($"return {conversion}.Value;")
87+
)
88+
.ToConversion(),
89+
_ => CreateSignature(type, options)
90+
.CreateScope(
91+
$"if ({Value} is null)"
92+
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
93+
.Append($"var result = {conversion};")
94+
.Concat(
95+
"if (result is null)"
96+
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
97+
)
98+
.Append("return result.Value;")
99+
)
100+
.ToConversion()
101+
102+
},
103+
{ IsReferenceType: true } => type switch
104+
{
105+
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(
106+
type, options)
107+
.CreateScope(
108+
$"if ({Value} is null)"
109+
.CreateScope("return null;")
110+
.Append($"return {conversion};")
111+
).ToConversion(),
112+
_ => CreateSignature(type, options)
113+
.CreateScope(
114+
$"if ({Value} is null)"
115+
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
116+
.Append($"var result = {conversion};")
117+
.Concat(
118+
"if (result is null)"
119+
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
120+
)
121+
.Append("return result;")
122+
)
123+
.ToConversion()
124+
86125

87-
return CreateSignature(type, options)
88-
.CreateScope(
89-
$"if ({Value} is null || {conversion} is not {{ }} x)"
90-
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
91-
.Append("return x;")
126+
},
127+
_ => throw new ArgumentException(
128+
$"Neither ValueType or ReferenceType could be resolved for conversion. type '{type.ToDisplayString()}'."
92129
)
93-
.ToConversion();
130+
};
94131
}
95132

96133
return type.TypeIdentifier() switch

0 commit comments

Comments
 (0)