Skip to content

Commit f72888f

Browse files
authored
Add support for CSharpHelper for List literals (#28212)
* Add support for CSharpHelper for List literals Fixes #19274 Also relates to npgsql/efcore.pg#2402 * Fixes from review comments
1 parent 36b9aa3 commit f72888f

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/EFCore.Design/Design/Internal/CSharpHelper.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,79 @@ public virtual string Literal(object?[,] values)
737737
return builder.ToString();
738738
}
739739

740+
/// <summary>
741+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
742+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
743+
/// any release. You should only use it directly in your code with extreme caution and knowing that
744+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
745+
/// </summary>
746+
public virtual string Literal<T>(List<T> values, bool vertical = false)
747+
=> List(typeof(T), values, vertical);
748+
749+
private string List(Type type, IEnumerable values, bool vertical = false)
750+
{
751+
var builder = new IndentedStringBuilder();
752+
753+
builder.Append("new List<")
754+
.Append(Reference(type))
755+
.Append(">");
756+
757+
var first = true;
758+
foreach (var value in values)
759+
{
760+
if (first)
761+
{
762+
builder.Append(" {");
763+
if (vertical)
764+
{
765+
builder.AppendLine();
766+
builder.IncrementIndent();
767+
}
768+
else
769+
{
770+
builder.Append(" ");
771+
}
772+
first = false;
773+
}
774+
else
775+
{
776+
builder.Append(",");
777+
778+
if (vertical)
779+
{
780+
builder.AppendLine();
781+
}
782+
else
783+
{
784+
builder.Append(" ");
785+
}
786+
}
787+
788+
builder.Append(UnknownLiteral(value));
789+
}
790+
791+
if (first)
792+
{
793+
builder.Append("()");
794+
}
795+
else
796+
{
797+
if (vertical)
798+
{
799+
builder.AppendLine();
800+
builder.DecrementIndent();
801+
}
802+
else
803+
{
804+
builder.Append(" ");
805+
}
806+
807+
builder.Append("}");
808+
}
809+
810+
return builder.ToString();
811+
}
812+
740813
/// <summary>
741814
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
742815
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -844,6 +917,11 @@ public virtual string UnknownLiteral(object? value)
844917
return Array(literalType.GetElementType()!, array);
845918
}
846919

920+
if (value is IList list && value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>))
921+
{
922+
return List(value.GetType().GetGenericArguments()[0], list);
923+
}
924+
847925
var mapping = _typeMappingSource.FindMapping(literalType);
848926
if (mapping != null)
849927
{

test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ public void Literal_works_when_many_ByteArray()
121121
new byte[] { 1, 2 },
122122
"new byte[] { 1, 2 }");
123123

124+
[ConditionalFact]
125+
public void Literal_works_when_empty_list()
126+
=> Literal_works(
127+
new List<string>(),
128+
@"new List<string>()");
129+
130+
[ConditionalFact]
131+
public void Literal_works_when_list_with_single_element()
132+
=> Literal_works(
133+
new List<string> { "one" },
134+
@"new List<string> { ""one"" }");
135+
136+
[ConditionalFact]
137+
public void Literal_works_when_list_of_mixed_objects()
138+
=> Literal_works(
139+
new List<object> { 1, "two" },
140+
@"new List<object> { 1, ""two"" }");
141+
142+
[ConditionalFact]
143+
public void Literal_works_when_list_with_ctor_arguments()
144+
=> Literal_works(
145+
new List<string>(new [] { "one" }) { "two", "three" },
146+
@"new List<string> { ""one"", ""two"", ""three"" }");
147+
124148
[ConditionalFact]
125149
public void Literal_works_when_multiline_string()
126150
=> Literal_works(

0 commit comments

Comments
 (0)