Skip to content

Commit 0f8a12f

Browse files
committed
Move logic to types
1 parent ea1fcf7 commit 0f8a12f

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,28 @@ private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, Dyna
124124
.CreateScope(
125125
$"if ({ParamReference} is null)"
126126
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
127-
.Append($"return {AttributeValueUtilityFactory.FromArray}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
127+
.Append($"return {AttributeValueUtilityFactory.FromArray}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
128128
).ToConversion(singleGeneric.T),
129129
SingleGeneric.SupportedType.List => signature
130130
.CreateScope(
131131
$"if ({ParamReference} is null)"
132132
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
133-
.Append($"return {AttributeValueUtilityFactory.FromList}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
133+
.Append($"return {AttributeValueUtilityFactory.FromList}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
134134
).ToConversion(singleGeneric.T),
135135
SingleGeneric.SupportedType.IReadOnlyCollection
136136
or SingleGeneric.SupportedType.IEnumerable
137137
or SingleGeneric.SupportedType.ICollection => signature
138138
.CreateScope(
139139
$"if ({ParamReference} is null)"
140140
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
141-
.Append($"return {AttributeValueUtilityFactory.FromEnumerable}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.T.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
141+
.Append($"return {AttributeValueUtilityFactory.FromEnumerable}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(singleGeneric.T, "a", "d", options, "o")}{(singleGeneric.IsTNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
142142
).ToConversion(singleGeneric.T),
143143
SingleGeneric.SupportedType.Set when singleGeneric.T.SpecialType is SpecialType.System_String
144144
=> signature
145145
.CreateScope(
146146
$"if ({ParamReference} is null)"
147147
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
148-
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ SS = new List<{(singleGeneric.T.IsNullable() ? "string?" : "string")}>({(singleGeneric.T.IsNullable() ? ParamReference : $"{ParamReference}.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\"))")})}};")
148+
.Append($"return new {Constants.AWSSDK_DynamoDBv2.AttributeValue} {{ SS = new List<{(singleGeneric.IsTNullable ? "string?" : "string")}>({(singleGeneric.T.IsNullable() ? ParamReference : $"{ParamReference}.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\"))")})}};")
149149
)
150150
.ToConversion(singleGeneric.T),
151151
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
@@ -167,14 +167,14 @@ SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric()
167167
.CreateScope(
168168
$"if ({ParamReference} is null)"
169169
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
170-
.Append($"return {AttributeValueUtilityFactory.FromDictionary}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.TValue.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
170+
.Append($"return {AttributeValueUtilityFactory.FromDictionary}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.IsTValueNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
171171
)
172172
.ToConversion(keyValueGeneric.TValue),
173173
KeyValueGeneric.SupportedType.LookUp => signature
174174
.CreateScope(
175175
$"if ({ParamReference} is null)"
176176
.CreateScope(keyValueGeneric.ReturnNullOrThrow(DataMember))
177-
.Append($"return {AttributeValueUtilityFactory.FromLookup}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.TValue.IsNullable() ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
177+
.Append($"return {AttributeValueUtilityFactory.FromLookup}({ParamReference}, {MarshallerOptions.ParamReference}, {DataMember}, static (a, o, d) => {InvokeMarshallerMethod(keyValueGeneric.TValue, "a", "d", options, "o")}{(keyValueGeneric.IsTValueNullable ? $" ?? {AttributeValueUtilityFactory.Null}" : null)});")
178178
)
179179
.ToConversion(keyValueGeneric.TValue),
180180
_ => throw UncoveredConversionException(keyValueGeneric, nameof(CreateMethod))

src/DynamoDBGenerator.SourceGenerator/Generations/UnMarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, Dyna
166166
.CreateScope(
167167
$"if ({Value} is null || {Value}.SS is null)"
168168
.CreateScope(singleGeneric.ReturnNullOrThrow(DataMember))
169-
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{(singleGeneric.T.IsNullable() ? "string?" : "string")}>" : null)}({(singleGeneric.T.IsNullable() ? $"{Value}.SS" : $"{Value}.SS.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\")")}));")
169+
.Append($"return new {(singleGeneric.TypeSymbol.TypeKind is TypeKind.Interface ? $"HashSet<{(singleGeneric.IsTNullable ? "string?" : "string")}>" : null)}({(singleGeneric.T.IsNullable() ? $"{Value}.SS" : $"{Value}.SS.Select((y,i) => y ?? throw {ExceptionHelper.NullExceptionMethod}($\"{{{DataMember}}}[UNKNOWN]\")")}));")
170170
)
171171
.ToConversion(),
172172
SingleGeneric.SupportedType.Set when singleGeneric.T.IsNumeric() => signature

src/DynamoDBGenerator.SourceGenerator/Types/TypeIdentifier.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in IType
3333
Type = supportedType;
3434
TKey = tKey;
3535
TValue = tValue;
36+
IsTValueNullable = TValue.IsNullable();
3637
}
3738

3839
public SupportedType Type { get; }
3940

41+
public bool IsTValueNullable { get; }
4042
// ReSharper disable once InconsistentNaming
4143
public ITypeSymbol TKey { get; }
4244

@@ -81,10 +83,12 @@ private SingleGeneric(ITypeSymbol type, ITypeSymbol innerType, in SupportedType
8183
{
8284
Type = supportedType;
8385
T = innerType;
86+
IsTNullable = T.IsNullable();
8487
}
8588

8689
public SupportedType Type { get; }
8790
public ITypeSymbol T { get; }
91+
public bool IsTNullable { get; }
8892

8993

9094
public static SingleGeneric? CreateInstance(in ITypeSymbol typeSymbol)

0 commit comments

Comments
 (0)