Skip to content

Commit 2f0f6ac

Browse files
committed
Add support for VisualBasicHelper for List literals
1 parent 6dc728a commit 2f0f6ac

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

EFCore.VisualBasic/Design/Internal/VisualBasicHelper.vb

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ Namespace Design.Internal
2626
_typeMappingSource = typeMappingSource
2727
End Sub
2828

29-
Private Shared ReadOnly _literalFuncs As IReadOnlyDictionary(Of Type, Func(Of VisualBasicHelper, Object, String)) =
30-
New Dictionary(Of Type, Func(Of VisualBasicHelper, Object, String)) From
29+
Private Shared ReadOnly _literalFuncs As New Dictionary(Of Type, Func(Of VisualBasicHelper, Object, String)) From
3130
{
3231
{GetType(Boolean), Function(c, v) c.Literal(CBool(v))},
3332
{GetType(Byte), Function(c, v) c.Literal(CByte(v))},
@@ -578,6 +577,60 @@ Namespace Design.Internal
578577
Return builder.ToString()
579578
End Function
580579

580+
''' <summary>
581+
''' This API supports the Entity Framework Core infrastructure And Is Not intended to be used
582+
''' directly from your code. This API may change Or be removed in future releases.
583+
''' </summary>
584+
Public Overridable Function Literal(Of T)(values As List(Of T), Optional vertical As Boolean = False) As String
585+
Return ListLitetal(GetType(T), values, vertical)
586+
End Function
587+
588+
Private Function ListLitetal(type As Type, values As IEnumerable, Optional vertical As Boolean = False) As String
589+
590+
Dim builder As New IndentedStringBuilder()
591+
592+
builder.
593+
Append("New List(Of ").
594+
Append(Reference(type)).
595+
Append(")")
596+
597+
Dim hasData = False
598+
Dim first = True
599+
600+
For Each value In values
601+
hasData = True
602+
If first Then
603+
builder.Append(" From {")
604+
If vertical Then
605+
builder.AppendLine()
606+
builder.IncrementIndent()
607+
End If
608+
first = False
609+
Else
610+
builder.Append(","c)
611+
612+
If vertical Then
613+
builder.AppendLine()
614+
Else
615+
builder.Append(" "c)
616+
End If
617+
End If
618+
619+
builder.Append(UnknownLiteral(value))
620+
Next
621+
622+
If hasData Then
623+
If vertical Then
624+
builder.AppendLine()
625+
builder.DecrementIndent()
626+
End If
627+
628+
builder.Append("}"c)
629+
End If
630+
631+
Return builder.ToString()
632+
End Function
633+
581634
''' <summary>
582635
''' This API supports the Entity Framework Core infrastructure And Is Not intended to be used
583636
''' directly from your code. This API may change Or be removed in future releases.
@@ -682,6 +735,13 @@ Namespace Design.Internal
682735
Return ArrayLitetal(LiteralType.GetElementType(), DirectCast(value, Array))
683736
End If
684737

738+
If TypeOf value Is IList AndAlso
739+
value.GetType().IsGenericType AndAlso
740+
value.GetType().GetGenericTypeDefinition() Is GetType(List(Of)) Then
741+
742+
Return ListLitetal(value.GetType().GetGenericArguments()(0), DirectCast(value, IList))
743+
End If
744+
685745
Dim mapping = _typeMappingSource.FindMapping(LiteralType)
686746
If mapping IsNot Nothing Then
687747
Dim builder As New StringBuilder

Test/EFCore.VisualBasic.Test/Design/Internal/VisualBasicHelperTests.vb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ Namespace Design.Internal
123123
"New Byte() {1, 2}")
124124
End Sub
125125

126+
<ConditionalFact>
127+
Public Sub Literal_works_when_empty_list()
128+
Literal_works(
129+
New List(Of String),
130+
"New List(Of String)")
131+
End Sub
132+
133+
<ConditionalFact>
134+
Public Sub Literal_works_when_list_with_single_element()
135+
Literal_works(
136+
New List(Of String) From {"one"},
137+
"New List(Of String) From {""one""}")
138+
End Sub
139+
140+
<ConditionalFact>
141+
Public Sub Literal_works_when_list_of_mixed_objects()
142+
Literal_works(
143+
New List(Of Object) From {1, "two"},
144+
"New List(Of Object) From {1, ""two""}")
145+
End Sub
146+
147+
<ConditionalFact>
148+
Public Sub Literal_works_when_list_with_ctor_arguments()
149+
Literal_works(
150+
New List(Of String)({"one"}) From {"two", "three"},
151+
"New List(Of String) From {""one"", ""two"", ""three""}")
152+
End Sub
153+
126154
<ConditionalFact>
127155
Public Sub Literal_works_when_multiline_string()
128156
Literal_works(
@@ -249,7 +277,24 @@ string with """,
249277
{""A"", 1},
250278
{""B"", 2}
251279
}",
252-
result,
280+
result,
281+
ignoreLineEndingDifferences:=True)
282+
End Sub
283+
284+
<ConditionalFact>
285+
Public Sub Literal_works_when_list_in_vertical()
286+
287+
Dim listToTest As New List(Of Object) From {New List(Of Integer)({1}), "two", 3}
288+
289+
Dim result = New VisualBasicHelper(TypeMappingSource).Literal(listToTest, True)
290+
291+
Assert.Equal(
292+
"New List(Of Object) From {
293+
New List(Of Integer) From {1},
294+
""two"",
295+
3
296+
}",
297+
result,
253298
ignoreLineEndingDifferences:=True)
254299
End Sub
255300

0 commit comments

Comments
 (0)