Skip to content

Commit b375cb1

Browse files
authored
check array rank (#29)
* check array rank * disable unit test of array rank * update versions
1 parent 6cc842f commit b375cb1

File tree

13 files changed

+36
-13
lines changed

13 files changed

+36
-13
lines changed

Assets/Samples/ReadOnlyArrayValues.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ public class ReadOnlyArrayValues : MonoBehaviour
77
{
88
[AnySerialize] public int[] IntArray { get; }
99
[AnySerialize] public int[][] IntArray2 { get; }
10+
// [AnySerialize] public int[,,] IntArrayRank3 { get; }
1011
[AnySerialize] public List<long>[] LongListArray { get; }
1112
[AnySerialize] public Dictionary<string, int>[] DictionaryArray { get; }
13+
// [AnySerialize] public string[,] StringArrayRank2 { get; }
1214
[AnySerialize] public string[][][] StringArray3 { get; }
1315
[AnySerialize] public IReadOnlyList<string> StringReadOnlyList { get; }
1416
[AnySerialize] public PlainClass[][] PlainClasses2 { get; }
1517

18+
[ContextMenu("log")]
1619
private void Awake()
1720
{
1821
this.JsonLog();

Assets/Samples/ReadOnlyArrayValues.prefab

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ MonoBehaviour:
9797
_value:
9898
- _value:
9999
- _value:
100-
Int: 0
101-
Float: 0
102-
DoubleArray: []
100+
Int: 321
101+
Float: 432
102+
DoubleArray:
103+
- 33
104+
- 44

Assets/Tests/AnySerialize/TestDefaultTypeSearcher.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public void should_find_replace_type_for_3D_array_type()
8787
AssertTypeEqual<ReadOnlyAnyArray<int[][], ReadOnlyAnyArray<int[], AnyArray_Int32>>>(SearchReadOnly<int[][][]>());
8888
}
8989

90+
[Test, Ignore("not support yet")]
91+
public void should_find_replace_type_for_array_type_with_rank()
92+
{
93+
SearchReadOnly<int[,]>();
94+
}
95+
9096
[Test]
9197
public void should_find_replace_type_for_dictionary_type()
9298
{

Packages/com.quabug.any-processor/CodeGen/Extension/ReflectionExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public static Type ToReflectionType(this TypeReference cecilType)
4242
.ToArray()
4343
),
4444
GenericParameter _ => throw new NotSupportedException(),
45-
ArrayType arrayType => arrayType.ElementType.ToReflectionType().MakeArrayType(/*arrayType.Rank*/), // array with rank become [*] which is not the same as array type in reflection.
45+
ArrayType { Rank: > 1 } arrayType => arrayType.ElementType.ToReflectionType().MakeArrayType(arrayType.Rank),
46+
ArrayType arrayType => arrayType.ElementType.ToReflectionType().MakeArrayType(), // array with rank become [*] which is not the same as array type in reflection.
4647
ByReferenceType byReferenceType => byReferenceType.ElementType.ToReflectionType().MakeByRefType(),
4748
PointerType pointerType => pointerType.ElementType.ToReflectionType().MakePointerType(),
4849
TypeSpecification _ => throw new NotSupportedException(),

Packages/com.quabug.any-processor/CodeGen/Extension/TypeEqualsExtension.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public static bool TypeEquals(this GenericParameterConstraint? lhs, GenericParam
6868
return TypeEquals(lhs.ConstraintType, rhs.ConstraintType);
6969
}
7070

71+
[Pure]
72+
public static bool TypeEquals(this ArrayType? lhs, ArrayType? rhs)
73+
{
74+
if (lhs == null) throw new ArgumentNullException(nameof(lhs));
75+
if (rhs == null) throw new ArgumentNullException(nameof(rhs));
76+
return lhs.Rank == rhs.Rank && TypeEquals(lhs.ElementType, rhs.ElementType);
77+
}
78+
7179
[Pure]
7280
public static bool TypeEquals(this TypeSpecification? lhs, TypeSpecification? rhs)
7381
{

Packages/com.quabug.any-processor/CodeGen/Extension/TypeGenericExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static bool IsDerivedFrom(this TypeReference derived, TypeReference @base
115115
if (derived.Resolve() == null) throw new ArgumentException("cannot resolve type", nameof(derived));
116116
if (@base.Resolve() == null) throw new ArgumentException("cannot resolve type", nameof(@base));
117117
if (derived is ArrayType derivedArray && @base is ArrayType baseArray)
118-
return derivedArray.ElementType.IsDerivedFrom(baseArray.ElementType);
118+
return derivedArray.Rank == baseArray.Rank && derivedArray.ElementType.IsDerivedFrom(baseArray.ElementType);
119119
if (derived.IsArray || @base.IsArray) return false;
120120
if (!derived.Resolve()!.IsDerivedFrom(@base.Resolve()!)) return false;
121121
if (derived.GetGenericParametersOrArgumentsCount() != @base.GetGenericParametersOrArgumentsCount()) return false;
@@ -156,7 +156,7 @@ static bool IsMatch(TypeReference selfArgument, TypeReference genericArgument)
156156
{
157157
if (selfArgument.IsGenericParameter || genericArgument.IsGenericParameter) return true;
158158
if (selfArgument is ArrayType selfArray && genericArgument is ArrayType genericArray)
159-
return IsMatch(selfArray.ElementType!, genericArray.ElementType!);
159+
return selfArray.Rank == genericArray.Rank && IsMatch(selfArray.ElementType!, genericArray.ElementType!);
160160
if (selfArgument.IsArray || genericArgument.IsArray) return false;
161161
if (!selfArgument.IsGenericType() && !genericArgument.IsGenericType()) return selfArgument.TypeEquals(genericArgument);
162162
if (!(selfArgument.IsGenericType() && genericArgument.IsGenericType())) return false;

Packages/com.quabug.any-processor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.quabug.any-processor",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"unity": "2021.3",
55
"displayName": "AnyProcessor",
66
"type": "library",

Packages/com.quabug.any-serialize/CodeGen/FieldTypeSearcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public FieldTypeSearcher(
3636
).ToArray();
3737
if (searcherFieldCount != fields.Length || fieldIndex < 0) return;
3838

39-
4039
var field = fields[fieldIndex];
4140
var property = field.GetBackingFieldProperty();
4241
var fieldType = property == null ? field.FieldType! : property.PropertyType!;

Packages/com.quabug.any-serialize/CodeGen/SerializeTypeSearcher.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public SerializeTypeSearcher(
4040
throw new ArgumentException($"{nameof(_targetType)} must be a concrete generic instance with one and only one arguments.", nameof(_targetType));
4141

4242
var propertyType = _targetType.GetGenericParametersOrArguments().First();
43+
if (propertyType is ArrayType propertyArrayType && propertyArrayType.Rank > 1)
44+
throw new ArgumentException($"Invalid property type ({propertyType}): array type with rank is not supported yet.", nameof(_targetType));
45+
4346
var isAnySerializable = propertyType.Resolve().GetAttributesOf<AnySerializableAttribute>().Any();
4447
var anyClassInterface = _module.ImportReference(typeof(IReadOnlyAnyClass<>)).Resolve();
4548

@@ -74,7 +77,7 @@ bool IsCloserImplementation(TypeReference? previous, TypeReference? current, Typ
7477
if (current == null || current.IsGenericParameter) return false;
7578
if (previous.TypeEquals(current)) return true;
7679
if (previous is ArrayType previousArray && current is ArrayType currentArray && target is ArrayType targetArray)
77-
return IsCloserImplementation(previousArray.ElementType, currentArray.ElementType, targetArray.ElementType!);
80+
return currentArray.Rank == targetArray.Rank && IsCloserImplementation(previousArray.ElementType, currentArray.ElementType, targetArray.ElementType!);
7881
if (current.IsArray && target.IsArray) return true;
7982

8083
var previousDefinition = previous.Resolve()!;

Packages/com.quabug.any-serialize/Runtime/AnyNullableClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AnySerialize
55
{
6-
[Serializable, AnySerializePriority(AnySerializePriorityAttribute.AnyValuePriority + 10000)]
6+
[Serializable, AnySerializePriority(AnySerializePriorityAttribute.AnyCustomOnly)]
77
public class AnyNullableClass<T> : IReadOnlyAny<T?>, IAny<T?> where T : class
88
{
99
[field: SerializeReference] public T? Value { get; set; } = null;

0 commit comments

Comments
 (0)