Skip to content

Commit 03bbc6e

Browse files
committed
Cleaned up some more logic and handled IDictionary/IReadOnlyDictionary.
1 parent 66ca115 commit 03bbc6e

5 files changed

Lines changed: 18 additions & 67 deletions

File tree

GenerateReadOnly Tests/generator/BuiltInTypeTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ internal class BuiltInTypeTests {
1313
[TestCase("System.Collections.Generic.IList<int>",
1414
"System.Collections.Generic.IReadOnlyList<int>",
1515
true)]
16+
[TestCase("System.Collections.Generic.IDictionary<int, int>",
17+
"System.Collections.Generic.IReadOnlyDictionary<int, int>",
18+
true)]
1619
public void TestSupportsEachBuiltInType(string mutable,
1720
string readOnly,
1821
bool needsToCast) {
@@ -51,6 +54,7 @@ public partial interface IReadOnlyWrapper {
5154
[TestCase("System.Span<int>")]
5255
[TestCase("System.Collections.Generic.ICollection<int>")]
5356
[TestCase("System.Collections.Generic.IList<int>")]
57+
[TestCase("System.Collections.Generic.IDictionary<int, int>")]
5458
public void TestDoesNotConvertBuiltInsForMutableProperties(string mutable) {
5559
ReadOnlyGeneratorTestUtil.AssertGenerated(
5660
$$"""
@@ -84,6 +88,7 @@ public partial interface IReadOnlyWrapper {
8488
[TestCase("System.Span<int>")]
8589
[TestCase("System.Collections.Generic.ICollection<int>")]
8690
[TestCase("System.Collections.Generic.IList<int>")]
91+
[TestCase("System.Collections.Generic.IDictionary<int, int>")]
8792
public void TestDoesNotConvertBuiltInsForMutableMethods(string mutable) {
8893
ReadOnlyGeneratorTestUtil.AssertGenerated(
8994
$$"""

GenerateReadOnly/src/generator/ReadOnlyTypeGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ private static bool HasBuiltInReadOnlyType_(
576576
return true;
577577
}
578578

579+
if (symbol.IsType(typeof(IDictionary<,>))) {
580+
readOnlyName = typeof(IReadOnlyDictionary<,>).GetCorrectName();
581+
canImplicitlyConvert = false;
582+
return true;
583+
}
584+
579585
readOnlyName = default;
580586
canImplicitlyConvert = false;
581587
return false;

GenerateReadOnly/src/util/symbols/IsExtensions.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,29 @@ public static bool IsTuple(this ISymbol symbol,
123123
return false;
124124
}
125125

126-
public static bool IsSequence(this ISymbol symbol,
127-
out ITypeSymbol elementType,
128-
out SequenceType sequenceType) {
126+
public static bool IsSequence(this ISymbol symbol, out ITypeSymbol elementType) {
129127
if (symbol.IsArray(out elementType)) {
130-
sequenceType = SequenceType.MUTABLE_ARRAY;
131128
return true;
132129
}
133130

134131
if (symbol.Implements(typeof(ImmutableArray<>),
135132
out var immutableArrayTypeV2)) {
136133
elementType = immutableArrayTypeV2.TypeArguments.ToArray()[0];
137-
sequenceType = SequenceType.IMMUTABLE_ARRAY;
138134
return true;
139135
}
140136

141137
if (symbol.Implements(typeof(List<>), out var listTypeV2)) {
142138
elementType = listTypeV2.TypeArguments.ToArray()[0];
143-
sequenceType = SequenceType.MUTABLE_LIST;
144139
return true;
145140
}
146141

147142
if (symbol.Implements(typeof(IReadOnlyList<>),
148143
out var readonlyListTypeV2)) {
149144
elementType = readonlyListTypeV2.TypeArguments.ToArray()[0];
150-
sequenceType = SequenceType.READ_ONLY_LIST;
151145
return true;
152146
}
153147

154148
elementType = default;
155-
sequenceType = default;
156149
return false;
157150
}
158151
}

GenerateReadOnly/src/util/types/SequenceType.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

GenerateReadOnly/src/util/types/TypeInfo.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ public static ParseStatus ParseMember(ISymbol memberSymbol) {
3232
return ParseStatus.SUCCESS;
3333
}
3434

35-
if (!GetTypeOfMember_(
36-
memberSymbol,
37-
out var memberTypeSymbol,
38-
out var isReadonly)) {
35+
if (!GetTypeOfMember_(memberSymbol, out var memberTypeSymbol)) {
3936
return ParseStatus.NOT_A_FIELD_OR_PROPERTY_OR_METHOD;
4037
}
4138

@@ -45,10 +42,10 @@ public static ParseStatus ParseMember(ISymbol memberSymbol) {
4542
return ParseStatus.NOT_A_FIELD_OR_PROPERTY_OR_METHOD;
4643
}
4744

48-
return ParseTypeSymbol(memberTypeSymbol, isReadonly);
45+
return ParseTypeSymbol(memberTypeSymbol);
4946
}
5047

51-
public static ParseStatus ParseTypeSymbol(ITypeSymbol typeSymbol, bool isReadonly) {
48+
public static ParseStatus ParseTypeSymbol(ITypeSymbol typeSymbol) {
5249
ParseNullable_(ref typeSymbol);
5350

5451
if (typeSymbol.IsPrimitive(out _)) {
@@ -59,10 +56,8 @@ public static ParseStatus ParseTypeSymbol(ITypeSymbol typeSymbol, bool isReadonl
5956
return ParseStatus.SUCCESS;
6057
}
6158

62-
if (typeSymbol.IsSequence(out var elementTypeV2, out var sequenceType)) {
63-
var elementParseStatus = ParseTypeSymbol(
64-
elementTypeV2,
65-
sequenceType.IsReadOnly());
59+
if (typeSymbol.IsSequence(out var elementTypeV2)) {
60+
var elementParseStatus = ParseTypeSymbol(elementTypeV2);
6661
if (elementParseStatus != ParseStatus.SUCCESS) {
6762
return elementParseStatus;
6863
}
@@ -86,21 +81,17 @@ public static ParseStatus ParseTypeSymbol(ITypeSymbol typeSymbol, bool isReadonl
8681

8782
private static bool GetTypeOfMember_(
8883
ISymbol memberSymbol,
89-
out ITypeSymbol memberTypeSymbol,
90-
out bool isMemberReadonly) {
84+
out ITypeSymbol memberTypeSymbol) {
9185
switch (memberSymbol) {
9286
case IPropertySymbol propertySymbol: {
93-
isMemberReadonly = propertySymbol.SetMethod == null;
9487
memberTypeSymbol = propertySymbol.Type;
9588
return true;
9689
}
9790
case IFieldSymbol fieldSymbol: {
98-
isMemberReadonly = fieldSymbol.IsReadOnly;
9991
memberTypeSymbol = fieldSymbol.Type;
10092
return true;
10193
}
10294
default: {
103-
isMemberReadonly = false;
10495
memberTypeSymbol = default;
10596
return false;
10697
}

0 commit comments

Comments
 (0)