Skip to content

Commit f4a74ca

Browse files
authored
Merge branch 'main' into upgrade-dotnet-8
2 parents 5551192 + e7c2921 commit f4a74ca

File tree

6 files changed

+148
-89
lines changed

6 files changed

+148
-89
lines changed

src/DynamoDBGenerator.SourceGenerator/Extensions/NotNullEvaluationExtensions.cs

+39-10
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,47 @@ private static string CreateException(in string accessPattern)
3131
return $"throw {Constants.DynamoDBGenerator.ExceptionHelper.NullExceptionMethod}(nameof({accessPattern}));";
3232
}
3333

34-
public static string NotNullIfStatement(this ITypeSymbol typeSymbol, in string accessPattern, in string truthy)
34+
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, string truthy)
35+
{
36+
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
37+
}
38+
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, IEnumerable<string> truthy)
39+
{
40+
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
41+
}
42+
43+
private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, object obj)
3544
{
3645
if (Expression(typeSymbol, accessPattern) is not { } expression)
37-
return truthy;
38-
39-
var ifClause = $"if ({expression}) {{ {truthy} }}";
40-
return typeSymbol.NullableAnnotation switch
4146
{
42-
NullableAnnotation.None or NullableAnnotation.Annotated => ifClause,
43-
NullableAnnotation.NotAnnotated => $"{ifClause} else {{ {CreateException(in accessPattern)} }}",
44-
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
45-
};
47+
if(obj is string single)
48+
yield return single;
49+
else if(obj is IEnumerable<string> truthies)
50+
foreach (var x in truthies)
51+
yield return x;
52+
else
53+
throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'");
54+
}
55+
else
56+
{
57+
58+
var ifClause = obj switch
59+
{
60+
string single => $"if ({expression})".CreateScope(single),
61+
IEnumerable<string> multiple => $"if ({expression})".CreateScope(multiple),
62+
_ => throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'")
63+
};
64+
var enumerable = typeSymbol.NullableAnnotation switch
65+
{
66+
NullableAnnotation.None or NullableAnnotation.Annotated => ifClause,
67+
NullableAnnotation.NotAnnotated => ifClause.Concat("else".CreateScope(CreateException(in accessPattern))),
68+
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
69+
};
70+
71+
foreach (var element in enumerable)
72+
yield return element;
73+
}
74+
4675
}
4776

4877

@@ -72,4 +101,4 @@ public static string NotNullIfStatement(this ITypeSymbol typeSymbol, in string a
72101
}
73102

74103

75-
}
104+
}

src/DynamoDBGenerator.SourceGenerator/Extensions/StringExtensions.cs

+41-22
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,56 @@ namespace DynamoDBGenerator.SourceGenerator.Extensions;
22

33
public static class StringExtensions
44
{
5+
public static string ToCamelCaseFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
6+
{
7+
return ToCamelCaseFromPascal(str.AsSpan(), memberName).ToString();
8+
}
59

6-
private static readonly IDictionary<int, string> IndentCache = new Dictionary<int, string>
10+
public static string ToPrivateFieldFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
711
{
8-
{0, ""},
9-
{1, " "},
10-
{2, " "},
11-
{3, " "},
12-
{4, " "}
13-
};
14-
15-
private static string Indent(int level)
12+
return ToPrivateFieldFromPascal(str.AsSpan(), memberName).ToString();
13+
}
14+
15+
public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
1616
{
17-
if (IndentCache.TryGetValue(level, out var indent)) return indent;
17+
if (span.Length is 0)
18+
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
1819

19-
indent = new string(' ', level * 4);
20-
IndentCache[level] = indent;
20+
var array = new char[span.Length + 1];
2121

22-
return indent;
22+
array[0] = '_';
23+
array[1] = Char.ToLowerInvariant(span[0]);
24+
25+
// Skip first element since we handled it manually.
26+
for (var i = 1; i < span.Length; i++)
27+
array[i + 1] = span[i];
28+
29+
return array;
2330
}
24-
public static IEnumerable<string> CreateScope(this string header, IEnumerable<string> content, int indentLevel)
31+
public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
2532
{
26-
var indent = Indent(indentLevel);
33+
if (span.Length is 0)
34+
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
2735

28-
yield return $"{indent}{header}";
29-
yield return string.Intern($"{indent}{{");
36+
if (char.IsLower(span[0]))
37+
return span;
3038

31-
foreach (var s in content)
32-
yield return $"{Indent(indentLevel + 1)}{s}";
39+
var array = new char[span.Length];
3340

34-
yield return string.Intern($"{indent}}}");
41+
array[0] = Char.ToLowerInvariant(span[0]);
42+
43+
// Skip first element since we handled it manually.
44+
for (var i = 1; i < span.Length; i++)
45+
array[i] = span[i];
46+
47+
return array;
3548
}
49+
50+
public static IEnumerable<string> ScopeTo(this IEnumerable<string> content, string header)
51+
{
52+
return CreateScope(header, content);
53+
}
54+
3655
public static IEnumerable<string> CreateScope(this string header, IEnumerable<string> content)
3756
{
3857
yield return header;
@@ -53,7 +72,7 @@ public static IEnumerable<string> CreateScope(this string header, string content
5372

5473
yield return "}";
5574
}
56-
75+
5776
public static IEnumerable<string> CreateScope(this string header, string content, string second)
5877
{
5978
yield return header;
@@ -81,4 +100,4 @@ public static string ToAlphaNumericMethodName(this string txt)
81100

82101
return new string(arr, 0, index);
83102
}
84-
}
103+
}

src/DynamoDBGenerator.SourceGenerator/Generations/AttributeExpressionName.cs

+14-15
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ internal static IEnumerable<string> CreateClasses(IEnumerable<DynamoDBMarshaller
2424
}
2525
private static IEnumerable<string> TypeContent(
2626
ITypeSymbol typeSymbol,
27-
(bool IsUnknown, DynamoDbDataMember DDB, string IfBranchAlias, string DbRef, string NameRef, string AttributeReference, string AttributeInterfaceName)[] dataMembers,
27+
(bool IsUnknown, DynamoDbDataMember DDB, string DbRef, string AttributeReference, string AttributeInterfaceName)[] dataMembers,
2828
string structName)
2929
{
30-
const string self = "_self";
30+
const string self = "_this";
3131
var constructorFieldAssignments = dataMembers
3232
.Select(x =>
3333
{
3434
var ternaryExpressionName = $"{ConstructorAttributeName} is null ? {@$"""#{x.DDB.AttributeName}"""}: {@$"$""{{{ConstructorAttributeName}}}.#{x.DDB.AttributeName}"""}";
3535
return x.IsUnknown
36-
? $"{x.NameRef} = new (() => new {x.AttributeReference}({ternaryExpressionName}, {ConstructorSetName}));"
37-
: $"{x.NameRef} = new (() => {ternaryExpressionName});";
36+
? $"{x.DDB.DataMember.NameAsPrivateField} = new (() => new {x.AttributeReference}({ternaryExpressionName}, {ConstructorSetName}));"
37+
: $"{x.DDB.DataMember.NameAsPrivateField} = new (() => {ternaryExpressionName});";
3838
})
3939
.Append($"{SetFieldName} = {ConstructorSetName};")
4040
.Append($@"{self} = new(() => {ConstructorAttributeName} ?? throw new NotImplementedException(""Root element AttributeExpressionName reference.""));");
@@ -46,13 +46,13 @@ private static IEnumerable<string> TypeContent(
4646
{
4747
if (fieldDeclaration.IsUnknown)
4848
{
49-
yield return $"private readonly Lazy<{fieldDeclaration.AttributeReference}> {fieldDeclaration.NameRef};";
50-
yield return $"public {fieldDeclaration.AttributeReference} {fieldDeclaration.DDB.DataMember.Name} => {fieldDeclaration.NameRef}.Value;";
49+
yield return $"private readonly Lazy<{fieldDeclaration.AttributeReference}> {fieldDeclaration.DDB.DataMember.NameAsPrivateField};";
50+
yield return $"public {fieldDeclaration.AttributeReference} {fieldDeclaration.DDB.DataMember.Name} => {fieldDeclaration.DDB.DataMember.NameAsPrivateField}.Value;";
5151
}
5252
else
5353
{
54-
yield return $"private readonly Lazy<string> {fieldDeclaration.NameRef};";
55-
yield return $"public string {fieldDeclaration.DDB.DataMember.Name} => {fieldDeclaration.NameRef}.Value;";
54+
yield return $"private readonly Lazy<string> {fieldDeclaration.DDB.DataMember.NameAsPrivateField};";
55+
yield return $"public string {fieldDeclaration.DDB.DataMember.Name} => {fieldDeclaration.DDB.DataMember.NameAsPrivateField}.Value;";
5656

5757
}
5858
}
@@ -69,19 +69,20 @@ private static IEnumerable<string> TypeContent(
6969
yield return $"public override string ToString() => {self}.Value;";
7070
}
7171

72-
private static IEnumerable<string> YieldSelector((bool IsUnknown, DynamoDbDataMember DDB, string IfBranchAlias, string DbRef, string NameRef, string AttributeReference, string AttributeInterfaceName) x)
72+
private static IEnumerable<string> YieldSelector((bool IsUnknown, DynamoDbDataMember DDB, string DbRef, string AttributeReference, string AttributeInterfaceName) x)
7373
{
7474

75+
var camelCase = x.DDB.DataMember.NameAsCamelCase;
7576
if (x.IsUnknown)
7677
{
77-
var scope = $@"if (new KeyValuePair<string, string>(""{x.DbRef}"", ""{x.DDB.AttributeName}"") is var {x.IfBranchAlias} && {SetFieldName}.Add({x.IfBranchAlias}))"
78-
.CreateScope($"yield return {x.IfBranchAlias};")
78+
var scope = $@"if (new KeyValuePair<string, string>(""{x.DbRef}"", ""{x.DDB.AttributeName}"") is var {camelCase} && {SetFieldName}.Add({camelCase}))"
79+
.CreateScope($"yield return {camelCase};")
7980
.Concat($"foreach (var x in ({x.DDB.DataMember.Name} as {x.AttributeInterfaceName}).{AttributeExpressionNameTrackerInterfaceAccessedNames}())".CreateScope("yield return x;"));
80-
return $"if ({x.NameRef}.IsValueCreated)".CreateScope(scope);
81+
return $"if ({x.DDB.DataMember.NameAsPrivateField}.IsValueCreated)".CreateScope(scope);
8182
}
8283
else
8384
{
84-
return $@"if ({x.NameRef}.IsValueCreated && new KeyValuePair<string, string>(""{x.DbRef}"", ""{x.DDB.AttributeName}"") is var {x.IfBranchAlias} && {SetFieldName}.Add({x.IfBranchAlias}))".CreateScope($"yield return {x.IfBranchAlias};");
85+
return $@"if ({x.DDB.DataMember.NameAsPrivateField}.IsValueCreated && new KeyValuePair<string, string>(""{x.DbRef}"", ""{x.DDB.AttributeName}"") is var {camelCase} && {SetFieldName}.Add({camelCase}))".CreateScope($"yield return {camelCase};");
8586
}
8687
}
8788

@@ -91,9 +92,7 @@ private static CodeFactory CreateStruct(ITypeSymbol typeSymbol, Func<ITypeSymbol
9192
.Select(x => (
9293
IsUnknown: !options.IsConvertable(x.DataMember.Type) && x.DataMember.Type.TypeIdentifier() is UnknownType,
9394
DDB: x,
94-
IfBranchAlias: $"__{x.DataMember.Name}__",
9595
DbRef: $"#{x.AttributeName}",
96-
NameRef: $"_{x.DataMember.Name}NameRef",
9796
AttributeReference: TypeName(x.DataMember.Type),
9897
AttributeInterfaceName: AttributeExpressionNameTrackerInterface
9998
))

0 commit comments

Comments
 (0)