Skip to content

Commit

Permalink
Use Array
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Dec 8, 2024
1 parent c734446 commit 8c52c10
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ or SpecialType.System_Double
or SpecialType.System_Single;
}

public static ImmutableArray<DynamoDbDataMember> GetDynamoDbProperties(this ITypeSymbol symbol)
public static DynamoDbDataMember[] GetDynamoDbProperties(this ITypeSymbol symbol)
{
// A special rule when it comes to Tuples.
// If we remove this we will get duplicated DataMembers when tuples are being used.
Expand All @@ -238,9 +238,7 @@ public static ImmutableArray<DynamoDbDataMember> GetDynamoDbProperties(this ITyp
.Where(x => x is not null)
.Select(x => x!.Value);

var builder = ImmutableArray.CreateBuilder<DynamoDbDataMember>();
builder.AddRange(items);
return builder.ToImmutableArray();
return items.ToArray();

static DynamoDbDataMember? Create(DataMember dataMember)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class AttributeExpressionName
private const string SetFieldName = "___set___";

private static readonly Func<ITypeSymbol, string> TypeName = TypeExtensions.SuffixedTypeSymbolNameFactory("Names", SymbolEqualityComparer.Default);
internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
Expand Down Expand Up @@ -86,7 +86,7 @@ private static IEnumerable<string> YieldSelector((bool IsUnknown, DynamoDbDataMe
}
}

private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var dataMembers = fn(typeSymbol)
.Select(x => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ private static IEnumerable<string> YieldSelector((bool IsUnknown, DynamoDbDataMe
return $"if ({x.DDB.DataMember.NameAsPrivateField}.IsValueCreated)".CreateScope(x.DDB.DataMember.Type.NotNullIfStatement(accessPattern, $"yield return new ({x.DDB.DataMember.NameAsPrivateField}.Value, {Marshaller.InvokeMarshallerMethod(x.DDB.DataMember.Type, $"entity.{x.DDB.DataMember.Name}", $"\"{x.DDB.DataMember.Name}\"", options, MarshallerOptions.FieldReference)} ?? {AttributeValueUtilityFactory.Null});"));

}
internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
internal static IEnumerable<string> CreateExpressionAttributeValue(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
// Using _comparer can double classes when there's a None nullable property mixed with a nullable property
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);

return arguments
.SelectMany(x => CodeFactory.Create(x.ArgumentType, y => CreateStruct(y, getDynamoDbProperties, options), hashSet));
}
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var dataMembers =
options.IsConvertable(typeSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static IEnumerable<string> CreateAssignment(string validateReference, st
return $"if ({validateReference})".CreateScope(innerContent);

}
private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var keyStructure = DynamoDbDataMember.GetKeyStructure(fn(typeSymbol));
if (keyStructure is null)
Expand Down Expand Up @@ -57,7 +57,7 @@ private static IEnumerable<string> MethodBody(ITypeSymbol typeSymbol, Func<IType
yield return s;

}
internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
internal static IEnumerable<string> CreateKeys(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);

Expand Down Expand Up @@ -111,7 +111,7 @@ internal static string AssignmentRoot(ITypeSymbol typeSymbol)
return
$"new {KeyMarshallerImplementationTypeName}((pk, rk, ipk, irk, dm) => {MethodName(typeSymbol)}({MarshallerOptions.FieldReference}, pk, rk, ipk, irk, dm))";
}
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory StaticAttributeValueDictionaryKeys(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{

var code = $"private static Dictionary<string, AttributeValue> {MethodName(typeSymbol)}({MarshallerOptions.Name} {MarshallerOptions.ParamReference}, object? {PkReference}, object? {RkReference}, bool {EnforcePkReference}, bool {EnforceRkReference}, string? index = null)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public static class Marshaller
private static readonly Func<ITypeSymbol, string> GetSerializationMethodName = TypeExtensions.SuffixedTypeSymbolNameFactory("_M", SymbolEqualityComparer.IncludeNullability);
private const string ParamReference = "entity";

internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
return $"private static class {ClassName}".CreateScope(TypeContent(arguments, getDynamoDbProperties, options));
}
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var properties = fn(typeSymbol)
.Select(x =>
Expand Down Expand Up @@ -59,7 +59,7 @@ private static CodeFactory CreateDictionaryMethod(ITypeSymbol typeSymbol, Func<I

}

private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArguments> arguments, Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
var hashset = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);

Expand All @@ -72,7 +72,7 @@ private static IEnumerable<string> TypeContent(IEnumerable<DynamoDBMarshallerArg
.Concat(CodeFactory.Create(x.ArgumentType, y => CreateMethod(y, getDynamoDbProperties, options), hashset))
);
}
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
if (options.TryWriteConversion(type, ParamReference) is {} conversion)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public static class UnMarshaller

}
internal static IEnumerable<string> CreateClass(IEnumerable<DynamoDBMarshallerArguments> arguments,
Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
return $"private static class {UnMarshallerClass}".CreateScope(CreateTypeContents(arguments, getDynamoDbProperties, options));
}
private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn, MarshallerOptions options)
private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var assignments = fn(type)
.Select(x => (DDB: x, MethodCall: InvokeUnmarshallMethod(x.DataMember.Type, $"{Dict}.GetValueOrDefault(\"{x.AttributeName}\")", $"\"{x.DataMember.Name}\"", options), x.DataMember.Name))
Expand All @@ -71,7 +71,7 @@ private static CodeFactory CreateCode(ITypeSymbol type, Func<ITypeSymbol, Immuta
return new CodeFactory(method, assignments.Select(x => x.DDB.DataMember.Type));

}
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> fn,
private static CodeFactory CreateMethod(ITypeSymbol type, Func<ITypeSymbol, DynamoDbDataMember[]> fn,
MarshallerOptions options)
{

Expand Down Expand Up @@ -138,7 +138,7 @@ private static string CreateSignature(ITypeSymbol typeSymbol)
return $"public static {typeSymbol.Representation().annotated} {GetDeserializationMethodName(typeSymbol)}(AttributeValue? {Value}, {MarshallerOptions.Name} {MarshallerOptions.ParamReference}, string? {DataMember} = null)";
}
private static IEnumerable<string> CreateTypeContents(IEnumerable<DynamoDBMarshallerArguments> arguments,
Func<ITypeSymbol, ImmutableArray<DynamoDbDataMember>> getDynamoDbProperties, MarshallerOptions options)
Func<ITypeSymbol, DynamoDbDataMember[]> getDynamoDbProperties, MarshallerOptions options)
{
var hashSet = new HashSet<ITypeSymbol>(SymbolEqualityComparer.IncludeNullability);
return arguments.SelectMany(x =>
Expand Down

0 comments on commit 8c52c10

Please sign in to comment.