Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5be78ca
Skip prefixing
inputfalken Apr 17, 2025
47f4ea2
Use constant over hardcoded
inputfalken Apr 17, 2025
29bf5a3
Avoid specifying type when field is known
inputfalken Apr 17, 2025
995ee60
Only default to AttributeValue null when the type is nullable
inputfalken Apr 17, 2025
7816959
Avoid null coaslescing if type system do not expect it
inputfalken Apr 17, 2025
158e645
Use scoped if
inputfalken Apr 17, 2025
5e35895
Use indexer when assigning
inputfalken Apr 17, 2025
77a53da
Remove redundant parantheses
inputfalken Apr 17, 2025
3ee2304
Avoid inline if statements
inputfalken Apr 17, 2025
61a6a25
Rely on scoping instead of ternaries
inputfalken Apr 17, 2025
89b709d
Fix enum conversion
inputfalken Apr 18, 2025
dd57db0
Use consistent naming scheme
inputfalken Apr 18, 2025
8de6c43
Check for null
inputfalken Apr 18, 2025
51f473d
Avoid ternaries
inputfalken Apr 18, 2025
29ea683
Fix build error
inputfalken Apr 18, 2025
6fa43e4
Reuse IsNUll
inputfalken Apr 18, 2025
735969f
Rely on checking for error first when unmarshalling as well
inputfalken Apr 18, 2025
f106960
Reuse IsNullLogic
inputfalken Apr 18, 2025
0ae3a50
Fix build errors
inputfalken Apr 18, 2025
7c7ab7d
Increase functionality of TypeIdentifier
inputfalken Apr 18, 2025
421abfb
Stop relying on special syntax
inputfalken Apr 19, 2025
6d6f7e0
Create variables
inputfalken Apr 19, 2025
29a4d51
Fix bug
inputfalken Apr 19, 2025
d89deeb
Fix special syntax
inputfalken Apr 19, 2025
e87895a
Add additonal ruling
inputfalken Apr 19, 2025
52bbaee
Isolate logic into types
inputfalken Apr 19, 2025
f82c86c
Move representation method to TypeIdentifier
inputfalken Apr 19, 2025
455b3a2
Reformat & CleanUp
inputfalken Apr 19, 2025
4d1497d
Reuse IsUnknown
inputfalken Apr 20, 2025
f3bae89
Fix
inputfalken Apr 20, 2025
bfef66f
Update test dependencies
inputfalken Apr 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DynamoDBGenerator.SourceGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static class Marshaller
public static class AttributeValueUtilityFactory
{
private const string ClassName = "MarshallHelper";
public const string ToAttributeValue = $"{ClassName}.ToAttributeValue";
public const string Null = $"{ClassName}.Null";
public const string ToList = $"{ClassName}.ToList";
public const string ToArray = $"{ClassName}.ToArray";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DynamoDBGenerator.SourceGenerator.Extensions;

// Great source: https://github.com/dotnet/runtime/blob/main/src/tools/illink/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs
public static class CompilationExtensions
{
public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation compilation, ImmutableArray<SyntaxNode> classDeclarations)
public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation compilation,
ImmutableArray<SyntaxNode> classDeclarations)
{
var span = classDeclarations.AsSpan();
var symbols = new INamedTypeSymbol[classDeclarations.Length];
Expand All @@ -19,7 +19,8 @@ public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation com
.GetSemanticModel(classDeclarationSyntax.SyntaxTree)
.GetDeclaredSymbol(classDeclarationSyntax)
is not INamedTypeSymbol typeSymbol)
throw new ArgumentException($"Could not convert the '{classDeclarationSyntax.ToFullString()}' into a '{nameof(ITypeSymbol)}'.");
throw new ArgumentException(
$"Could not convert the '{classDeclarationSyntax.ToFullString()}' into a '{nameof(ITypeSymbol)}'.");

symbols[i] = typeSymbol;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ public static IEnumerable<T> AllButLast<T>(this IEnumerable<T> enumerable, Func<
buffer = item;
}

if (isBuffered)
{
yield return buffer!;
}
if (isBuffered) yield return buffer!;
}
public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> @default, Func<T, TResult> onLast)

public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> @default,
Func<T, TResult> onLast)
{
var buffer = default(T);
var isBuffered = false;
Expand All @@ -35,10 +34,6 @@ public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> en
buffer = item;
}

if (isBuffered)
{
yield return onLast(buffer!);
}
if (isBuffered) yield return onLast(buffer!);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.CodeAnalysis;

namespace DynamoDBGenerator.SourceGenerator.Extensions;

public static class NotNullEvaluationExtensions
{
public static string NotNullTernaryExpression(this ITypeSymbol typeSymbol, in string accessPattern, in string truthy,
public static string NotNullTernaryExpression(this ITypeSymbol typeSymbol, in string accessPattern,
in string truthy,
in string falsy)
{
var result = Expression(in typeSymbol, in accessPattern) is { } expression
Expand All @@ -18,47 +20,51 @@ private static string CreateException(in string accessPattern)
return $"throw {Constants.DynamoDBGenerator.ExceptionHelper.NullExceptionMethod}(nameof({accessPattern}));";
}

public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, string truthy)
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern,
string truthy)
{
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
}
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, IEnumerable<string> truthy)

public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern,
IEnumerable<string> truthy)
{
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
}

private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, object obj)
{
if (Expression(typeSymbol, accessPattern) is not { } expression)
{
if(obj is string single)
yield return single;
else if(obj is IEnumerable<string> truthies)
foreach (var x in truthies)
yield return x;
else
throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'");
if (obj is string single)
yield return single;
else if (obj is IEnumerable<string> truthies)
foreach (var x in truthies)
yield return x;
else
throw new NotImplementedException(
$"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'");
}
else
{

var ifClause = obj switch
var ifClause = obj switch
{
string single => $"if ({expression})".CreateScope(single),
IEnumerable<string> multiple => $"if ({expression})".CreateScope(multiple),
_ => throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'")
string single => $"if ({expression})".CreateScope(single),
IEnumerable<string> multiple => $"if ({expression})".CreateScope(multiple),
_ => throw new NotImplementedException(
$"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'")
};
var enumerable = typeSymbol.NullableAnnotation switch
{
NullableAnnotation.None or NullableAnnotation.Annotated => ifClause,
NullableAnnotation.NotAnnotated => ifClause.Concat("else".CreateScope(CreateException(in accessPattern))),
NullableAnnotation.NotAnnotated => ifClause.Concat(
"else".CreateScope(CreateException(in accessPattern))),
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
};

foreach (var element in enumerable)
yield return element;
}

}


Expand All @@ -73,7 +79,6 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo

static string? OnValueType(in ITypeSymbol typeSymbol, in string accessPattern)
{

if (typeSymbol.TryGetNullableValueType() is not { } namedTypeSymbol)
return null;

Expand All @@ -86,6 +91,4 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo
: $"{accessPattern} is not null && {expression}";
}
}


}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
using System.Runtime.CompilerServices;

namespace DynamoDBGenerator.SourceGenerator.Extensions;

public static class StringExtensions
{
public static string ToCamelCaseFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
public static string ToCamelCaseFromPascal(this string str, [CallerMemberName] string? memberName = null)
{
return ToCamelCaseFromPascal(str.AsSpan(), memberName).ToString();
}

public static string ToPrivateFieldFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
public static string ToPrivateFieldFromPascal(this string str, [CallerMemberName] string? memberName = null)
{
return ToPrivateFieldFromPascal(str.AsSpan(), memberName).ToString();
}

public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span,
[CallerMemberName] string? memberName = null)
{
if (span.Length is 0)
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");

var array = new char[span.Length + 1];

array[0] = '_';
array[1] = Char.ToLowerInvariant(span[0]);
array[1] = char.ToLowerInvariant(span[0]);

// Skip first element since we handled it manually.
for (var i = 1; i < span.Length; i++)
array[i + 1] = span[i];

return array;
}
public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)

public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span,
[CallerMemberName] string? memberName = null)
{
if (span.Length is 0)
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
Expand All @@ -38,7 +43,7 @@ public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> s

var array = new char[span.Length];

array[0] = Char.ToLowerInvariant(span[0]);
array[0] = char.ToLowerInvariant(span[0]);

// Skip first element since we handled it manually.
for (var i = 1; i < span.Length; i++)
Expand Down Expand Up @@ -92,12 +97,9 @@ public static string ToAlphaNumericMethodName(this string txt)
for (var i = 0; i < txt.Length; i++)
{
var c = txt[i];
if (char.IsLetter(c) || index > 0 && char.IsNumber(c))
{
arr[index++] = c;
}
if (char.IsLetter(c) || (index > 0 && char.IsNumber(c))) arr[index++] = c;
}

return new string(arr, 0, index);
}
}
}
Loading