Skip to content

Commit 1c0fd79

Browse files
VS-98: Work on Expanding Collections Supported by TypesProcessor (#76)
1 parent 0ee9f0e commit 1c0fd79

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed

src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,29 @@ TypeKind.Enum or
217217
_ => false
218218
};
219219

220-
public static bool IsSupportedCollection(this ITypeSymbol typeSymbol) =>
221-
typeSymbol is INamedTypeSymbol namedTypeSymbol &&
222-
s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString());
220+
public static bool IsSupportedCollection(this ITypeSymbol typeSymbol)
221+
{
222+
if (typeSymbol is not INamedTypeSymbol namedTypeSymbol)
223+
{
224+
return false;
225+
}
226+
227+
while (namedTypeSymbol != null)
228+
{
229+
if (s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString()))
230+
{
231+
return true;
232+
}
233+
234+
if (namedTypeSymbol.Interfaces.Any(i => s_supportedCollections.Contains(i.ConstructedFrom?.ToDisplayString()))){
235+
return true;
236+
}
237+
238+
namedTypeSymbol = namedTypeSymbol.BaseType;
239+
}
240+
241+
return false;
242+
}
223243

224244
public static bool IsSupportedIMongoCollection(this ITypeSymbol typeSymbol) =>
225245
typeSymbol.IsIMongoCollection() &&

tests/MongoDB.Analyzer.Tests.Common.TestCases/Builders/BuildersCollections.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ namespace MongoDB.Analyzer.Tests.Common.TestCases.Builders
2020
{
2121
public sealed class BuildersCollections : TestCasesBase
2222
{
23+
[BuildersMQL("{ \"$or\" : [{ \"Enumerable1.0\" : 2 }, { \"Enumerable2.1.Enumerable1.0\" : 2 }] }")]
24+
public void CustomEnumerables()
25+
{
26+
_ = Builders<CustomEnumerableHolder>.Filter.Eq(t => t.Enumerable1.ElementAt(0), 2) |
27+
Builders<CustomEnumerableHolder>.Filter.Eq(t => t.Enumerable2.ElementAt(1).Enumerable1.ElementAt(0), 2);
28+
}
29+
30+
[BuildersMQL("{ \"$or\" : [{ \"IntList.2\" : 1 }, { \"StringList.3\" : \"Value\" }, { \"PesonsList.4.Name\" : \"Bob\" }, { \"NestedListsHolderList.5.IntList.1\" : 1 }, { \"IntIList.4\" : 3 }, { \"NestedListsHolderIList.15.IntList.3\" : 3 }] }")]
31+
public void CustomLists()
32+
{
33+
_ = Builders<CustomListsHolder>.Filter.Eq(t => t.IntList[2], 1) |
34+
Builders<CustomListsHolder>.Filter.Eq(t => t.StringList[3], "Value") |
35+
Builders<CustomListsHolder>.Filter.Eq(t => t.PesonsList[4].Name, "Bob") |
36+
Builders<CustomListsHolder>.Filter.Eq(t => t.NestedListsHolderList[5].IntList[1], 1) |
37+
Builders<CustomListsHolder>.Filter.Eq(t => t.IntIList[4], 3) |
38+
Builders<CustomListsHolder>.Filter.Eq(t => t.NestedListsHolderIList[15].IntList[3], 3);
39+
}
40+
2341
[BuildersMQL("{ \"$or\" : [{ \"Enumerable1.0\" : 2 }, { \"Enumerable2.1.Enumerable1.0\" : 2 }] }")]
2442
public void Enumerables()
2543
{

tests/MongoDB.Analyzer.Tests.Common.TestCases/Linq/LinqCollections.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ namespace MongoDB.Analyzer.Tests.Common.TestCases.Linq
2020
{
2121
public sealed class LinqCollections : TestCasesBase
2222
{
23+
[MQL("aggregate([{ \"$match\" : { \"Enumerable1\" : { \"$size\" : 121 }, \"Enumerable1.12\" : 1, \"Enumerable2\" : { \"$size\" : 22 }, \"Enumerable2.12.Enumerable2.21.Enumerable1.1\" : 2 } }])")]
24+
public void CustomEnumerables()
25+
{
26+
_ = GetMongoQueryable<CustomEnumerableHolder>().Where(t =>
27+
t.Enumerable1.Count() == 121 &&
28+
t.Enumerable1.ElementAt(12) == 1 &&
29+
t.Enumerable2.Count() == 22 &&
30+
t.Enumerable2.ElementAt(12).Enumerable2.ElementAt(21).Enumerable1.ElementAt(1) == 2);
31+
}
32+
33+
[MQL("aggregate([{ \"$match\" : { \"IntList.0\" : 2 } }, { \"$match\" : { \"StringList\" : { \"$size\" : 12 } } }, { \"$match\" : { \"PesonsList.2.Address.City\" : \"Hamburg\" } }, { \"$match\" : { \"NestedListsHolderList.2.StringList.4\" : \"Nested\" } }, { \"$match\" : { \"IntIList.1\" : 12 } }, { \"$match\" : { \"NestedListsHolderIList.12.IntIList.12\" : 2 } }])")]
34+
public void CustomLists()
35+
{
36+
_ = GetMongoQueryable<CustomListsHolder>()
37+
.Where(t => t.IntList[0] == 2)
38+
.Where(t => t.StringList.Count == 12)
39+
.Where(t => t.PesonsList[2].Address.City == "Hamburg")
40+
.Where(t => t.NestedListsHolderList[2].StringList[4] == "Nested")
41+
.Where(t => t.IntIList[1] == 12)
42+
.Where(t => t.NestedListsHolderIList[12].IntIList[12] == 2);
43+
}
44+
2345
[MQL("aggregate([{ \"$match\" : { \"Enumerable1\" : { \"$size\" : 121 }, \"Enumerable1.12\" : 1, \"Enumerable2\" : { \"$size\" : 22 }, \"Enumerable2.12.Enumerable2.21.Enumerable1.1\" : 2 } }])")]
2446
public void Enumerables()
2547
{
@@ -42,6 +64,9 @@ public void Lists()
4264
.Where(t => t.NestedListsHolderIList[12].IntIList[12] == 2);
4365
}
4466

67+
68+
[MQL("aggregate([{ \"$match\" : { \"IntList.0\" : 2 } }, { \"$match\" : { \"StringList\" : { \"$size\" : 12 } } }, { \"$match\" : { \"PesonsList.2.Address.City\" : \"Hamburg\" } }, { \"$match\" : { \"NestedListsHolderList.2.StringList.4\" : \"Nested\" } }, { \"$match\" : { \"IntIList.1\" : 12 } }, { \"$match\" : { \"NestedListsHolderIList.12.IntIList.12\" : 2 } }])")]
69+
[MQL("aggregate([{ \"$match\" : { \"Enumerable1\" : { \"$size\" : 121 }, \"Enumerable1.12\" : 1, \"Enumerable2\" : { \"$size\" : 22 }, \"Enumerable2.12.Enumerable2.21.Enumerable1.1\" : 2 } }])")]
4570
[MQL("aggregate([{ \"$match\" : { \"IntList.0\" : 2 } }, { \"$match\" : { \"StringList\" : { \"$size\" : 12 } } }, { \"$match\" : { \"PesonsList.2.Address.City\" : \"Hamburg\" } }, { \"$match\" : { \"NestedListsHolderList.2.StringList.4\" : \"Nested\" } }, { \"$match\" : { \"IntIList.1\" : 12 } }, { \"$match\" : { \"NestedListsHolderIList.12.IntIList.12\" : 2 } }])")]
4671
[MQL("aggregate([{ \"$match\" : { \"Enumerable1\" : { \"$size\" : 121 }, \"Enumerable1.12\" : 1, \"Enumerable2\" : { \"$size\" : 22 }, \"Enumerable2.12.Enumerable2.21.Enumerable1.1\" : 2 } }])")]
4772
public void Query_syntax()
@@ -61,6 +86,22 @@ where enumerableHolder.Enumerable1.Count() == 121 &&
6186
enumerableHolder.Enumerable2.Count() == 22 &&
6287
enumerableHolder.Enumerable2.ElementAt(12).Enumerable2.ElementAt(21).Enumerable1.ElementAt(1) == 2
6388
select enumerableHolder;
89+
90+
_ = from customListsHolder in GetMongoQueryable<CustomListsHolder>()
91+
where customListsHolder.IntList[0] == 2
92+
where customListsHolder.StringList.Count == 12
93+
where customListsHolder.PesonsList[2].Address.City == "Hamburg"
94+
where customListsHolder.NestedListsHolderList[2].StringList[4] == "Nested"
95+
where customListsHolder.IntIList[1] == 12
96+
where customListsHolder.NestedListsHolderIList[12].IntIList[12] == 2
97+
select customListsHolder;
98+
99+
_ = from customEnumerableHolder in GetMongoQueryable<CustomEnumerableHolder>()
100+
where customEnumerableHolder.Enumerable1.Count() == 121 &&
101+
customEnumerableHolder.Enumerable1.ElementAt(12) == 1 &&
102+
customEnumerableHolder.Enumerable2.Count() == 22 &&
103+
customEnumerableHolder.Enumerable2.ElementAt(12).Enumerable2.ElementAt(21).Enumerable1.ElementAt(1) == 2
104+
select customEnumerableHolder;
64105
}
65106
}
66107
}

tests/MongoDB.Analyzer.Tests.Common/DataModel/Collections.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,59 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System.Collections;
1516
using System.Collections.Generic;
1617

1718
namespace MongoDB.Analyzer.Tests.Common.DataModel
1819
{
20+
public class CustomEnumerableHolder
21+
{
22+
public CustomIEnumerable<int> Enumerable1 { get; set; }
23+
public CustomIEnumerable<EnumerableHolder> Enumerable2 { get; set; }
24+
}
25+
26+
public class CustomIEnumerable<T> : IEnumerable<T>
27+
{
28+
public IEnumerator<T> GetEnumerator() => throw new System.NotImplementedException();
29+
IEnumerator IEnumerable.GetEnumerator() => throw new System.NotImplementedException();
30+
}
31+
32+
public class CustomIList<T> : IList<T>
33+
{
34+
public T this[int index] { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
35+
public int Count => throw new System.NotImplementedException();
36+
public bool IsReadOnly => throw new System.NotImplementedException();
37+
public void Add(T item) => throw new System.NotImplementedException();
38+
public void Clear() => throw new System.NotImplementedException();
39+
public bool Contains(T item) => throw new System.NotImplementedException();
40+
public void CopyTo(T[] array, int arrayIndex) => throw new System.NotImplementedException();
41+
public IEnumerator<T> GetEnumerator() => throw new System.NotImplementedException();
42+
public int IndexOf(T item) => throw new System.NotImplementedException();
43+
public void Insert(int index, T item) => throw new System.NotImplementedException();
44+
public bool Remove(T item) => throw new System.NotImplementedException();
45+
public void RemoveAt(int index) => throw new System.NotImplementedException();
46+
IEnumerator IEnumerable.GetEnumerator() => throw new System.NotImplementedException();
47+
}
48+
49+
public class CustomList<T> : List<T> { }
50+
51+
public class CustomListsHolder
52+
{
53+
public CustomList<int> IntList { get; set; }
54+
public CustomList<Person> PesonsList { get; set; }
55+
public CustomList<string> StringList { get; set; }
56+
public CustomList<CustomListsHolder> NestedListsHolderList { get; set; }
57+
58+
public CustomIList<int> IntIList { get; set; }
59+
public CustomIList<CustomListsHolder> NestedListsHolderIList { get; set; }
60+
}
61+
62+
public class EnumerableHolder
63+
{
64+
public IEnumerable<int> Enumerable1 { get; set; }
65+
public System.Collections.Generic.IEnumerable<EnumerableHolder> Enumerable2 { get; set; }
66+
}
67+
1968
public class ListsHolder
2069
{
2170
public List<int> IntList { get; set; }
@@ -26,10 +75,4 @@ public class ListsHolder
2675
public IList<int> IntIList { get; set; }
2776
public System.Collections.Generic.IList<ListsHolder> NestedListsHolderIList { get; set; }
2877
}
29-
30-
public class EnumerableHolder
31-
{
32-
public IEnumerable<int> Enumerable1 { get; set; }
33-
public System.Collections.Generic.IEnumerable<EnumerableHolder> Enumerable2 { get; set; }
34-
}
3578
}

0 commit comments

Comments
 (0)