Skip to content

Commit 33d24d7

Browse files
committed
Simplify conditions
1 parent cc3ba80 commit 33d24d7

4 files changed

Lines changed: 38 additions & 74 deletions

File tree

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

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,17 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
7777
{
7878
if (options.TryWriteConversion(typeIdentifier.TypeSymbol, ParamReference) is {} conversion)
7979
{
80-
return typeIdentifier.TypeSymbol switch
81-
{
82-
{ IsValueType: true } => typeIdentifier.TypeSymbol switch
83-
{
84-
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(typeIdentifier, options)
85-
.CreateScope($"if ({ParamReference} is null)"
86-
.CreateScope("return null;")
87-
.Append($"return {conversion};")
80+
var signature = CreateSignature(typeIdentifier, options);
81+
return typeIdentifier.CanBeNull is false
82+
? signature.CreateScope($"return {conversion};").ToConversion()
83+
: signature.CreateScope($"if ({ParamReference} is null)"
84+
.CreateScope(typeIdentifier.IsNullable
85+
? "return null;"
86+
: $"throw {ExceptionHelper.NullExceptionMethod}({DataMember});"
8887
)
89-
.ToConversion(),
90-
_ => CreateSignature(typeIdentifier, options)
91-
.CreateScope($"return {conversion};")
92-
.ToConversion()
93-
},
94-
{ IsReferenceType: true } => typeIdentifier.TypeSymbol switch
95-
{
96-
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(typeIdentifier, options)
97-
.CreateScope($"if ({ParamReference} is null)".CreateScope("return null;")
98-
.Append($"return {conversion};"))
99-
.ToConversion(),
100-
_ => CreateSignature(typeIdentifier, options)
101-
.CreateScope(
102-
$"if ({ParamReference} is null)".CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});").Append($"return {conversion};")
103-
)
104-
.ToConversion()
105-
},
106-
_ => throw new ArgumentException(
107-
$"Neither ValueType or ReferenceType could be resolved for conversion. type '{typeIdentifier.TypeSymbol.ToDisplayString()}'.")
108-
};
88+
.Append($"return {conversion};")
89+
)
90+
.ToConversion();
10991
}
11092

11193
return typeIdentifier switch

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,49 +74,23 @@ private static CodeFactory CreateMethod(TypeIdentifier typeIdentifier, Func<ITyp
7474
{
7575
if (options.TryReadConversion(typeIdentifier, Value) is {} conversion)
7676
{
77-
return typeIdentifier.TypeSymbol switch
78-
{
79-
{ IsValueType: true } => typeIdentifier.TypeSymbol switch
80-
{
81-
{ OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => CreateSignature(typeIdentifier, options)
82-
.CreateScope(
83-
$"if ({Value} is null)"
84-
.CreateScope("return null;")
85-
.Append($"return {conversion};")
86-
)
87-
.ToConversion(),
88-
_ => CreateSignature(typeIdentifier, options)
89-
.CreateScope(
90-
$"if ({Value} is null)"
91-
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
92-
.Append($"return {conversion} ?? throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
93-
)
94-
.ToConversion()
95-
96-
},
97-
{ IsReferenceType: true } => typeIdentifier.TypeSymbol switch
98-
{
99-
{ NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => CreateSignature(
100-
typeIdentifier, options)
101-
.CreateScope(
102-
$"if ({Value} is null)"
103-
.CreateScope("return null;")
104-
.Append($"return {conversion};")
105-
).ToConversion(),
106-
_ => CreateSignature(typeIdentifier, options)
107-
.CreateScope(
108-
$"if ({Value} is null)"
109-
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
110-
.Append($"return {conversion} ?? throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
111-
)
112-
.ToConversion()
113-
114-
115-
},
116-
_ => throw new ArgumentException(
117-
$"Neither ValueType or ReferenceType could be resolved for conversion. type '{typeIdentifier.TypeSymbol.ToDisplayString()}'."
118-
)
119-
};
77+
var signature = CreateSignature(typeIdentifier, options);
78+
return typeIdentifier.CanBeNull || typeIdentifier.IsNullable is false
79+
? signature
80+
.CreateScope(
81+
$"if ({Value} is null)"
82+
.CreateScope($"throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
83+
.Append(
84+
$"return {conversion} ?? throw {ExceptionHelper.NullExceptionMethod}({DataMember});")
85+
)
86+
.ToConversion()
87+
: signature
88+
.CreateScope(
89+
$"if ({Value} is null)"
90+
.CreateScope("return null;")
91+
.Append($"return {conversion};")
92+
)
93+
.ToConversion();
12094
}
12195

12296
return typeIdentifier switch

src/DynamoDBGenerator.SourceGenerator/Types/TypeIdentifier.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ protected TypeIdentifier(ITypeSymbol typeSymbol)
1919
{
2020
IsNullable = typeSymbol switch
2121
{
22-
{
23-
IsReferenceType: true, NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated
24-
} => true,
22+
{ IsReferenceType: true, NullableAnnotation: NullableAnnotation.None or NullableAnnotation.Annotated } => true,
2523
{ IsReferenceType: true, NullableAnnotation: NullableAnnotation.NotAnnotated } => false,
2624
{ IsValueType: true, OriginalDefinition.SpecialType: SpecialType.System_Nullable_T } => true,
2725
{ IsValueType: true } => false,
@@ -34,8 +32,16 @@ protected TypeIdentifier(ITypeSymbol typeSymbol)
3432
UnannotatedString = original;
3533
AnnotatedString = annotated;
3634
IsNumeric = IsNumericMethod(typeSymbol);
35+
CanBeNull = typeSymbol is { IsReferenceType: true } or { IsValueType: true, OriginalDefinition.SpecialType: SpecialType.System_Nullable_T };
3736
}
3837

38+
/// <summary>
39+
/// Represents whether it can even be null.
40+
/// </summary>
41+
public bool CanBeNull { get; }
42+
/// <summary>
43+
/// Represents whether the type is intended to be nullable
44+
/// </summary>
3945
public bool IsNullable { get; }
4046
public bool IsNumeric { get; }
4147
public string AnnotatedString { get; }

src/DynamoDBGenerator/Internal/MarshallHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers;
23
using System.Collections.Generic;
34
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
@@ -140,6 +141,7 @@ public static AttributeValue FromEnumerable<T, TArgument>(
140141
foreach (var (element, i) in enumerable.Select((x, y) => (x, y)))
141142
attributeValues.Add(resultSelector(element, argument, $"{dataMember}[{i}]"));
142143

144+
143145
return new AttributeValue { L = attributeValues };
144146
}
145147

0 commit comments

Comments
 (0)