Skip to content

Commit 9a5254c

Browse files
committed
fix: using type_array for structure array like assertions
1 parent dd19c1f commit 9a5254c

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/lib/types/src/Flow/Types/Type/Comparator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
OptionalType,
1515
StructureType,
1616
TimeType};
17-
use Flow\Types\Type\Native\{FloatType, IntegerType, NullType, StringType, UnionType};
17+
use Flow\Types\Type\Native\{ArrayType, FloatType, IntegerType, NullType, StringType, UnionType};
1818

1919
final class Comparator
2020
{
@@ -68,6 +68,14 @@ public function comparable(Type $left, Type $right) : bool
6868
return true;
6969
}
7070

71+
if ($left instanceof ArrayType) {
72+
return $right instanceof ArrayType || $right instanceof ListType || $right instanceof MapType || $right instanceof StructureType;
73+
}
74+
75+
if ($right instanceof ArrayType) {
76+
return $left instanceof ListType || $left instanceof MapType || $left instanceof StructureType;
77+
}
78+
7179
return type_equals($left, $right);
7280
}
7381

src/lib/types/tests/Flow/Types/Tests/Unit/Type/ComparatorTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Flow\Types\Tests\Unit\Type;
66

7-
use function Flow\Types\DSL\{type_boolean,
7+
use function Flow\Types\DSL\{type_array,
8+
type_boolean,
89
type_equals,
910
type_float,
1011
type_integer,
@@ -41,6 +42,14 @@ public static function type_comparable_data_provider() : \Generator
4142
yield [type_union(type_integer(), type_null()), type_integer()];
4243
yield [type_union(type_integer(), type_null()), type_float()];
4344
yield [type_integer(), type_string()];
45+
46+
yield [type_array(), type_array()];
47+
yield [type_array(), type_list(type_string())];
48+
yield [type_array(), type_map(type_string(), type_integer())];
49+
yield [type_array(), type_structure(['id' => type_integer()])];
50+
yield [type_list(type_string()), type_array()];
51+
yield [type_map(type_string(), type_integer()), type_array()];
52+
yield [type_structure(['id' => type_integer()]), type_array()];
4453
}
4554

4655
public static function type_comparison_data_provider() : \Generator
@@ -70,6 +79,10 @@ public static function type_not_comparable_data_provider() : \Generator
7079
{
7180
yield [type_integer(), type_union(type_float(), type_integer())];
7281
yield [type_integer(), type_boolean()];
82+
yield [type_array(), type_string()];
83+
yield [type_array(), type_integer()];
84+
yield [type_array(), type_boolean()];
85+
yield [type_string(), type_array()];
7386
}
7487

7588
/**

src/lib/types/tests/Flow/Types/Tests/Unit/Type/Logical/StructureTypeTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Flow\Types\Tests\Unit\Type\Logical;
66

7-
use function Flow\Types\DSL\{type_boolean,
7+
use function Flow\Types\DSL\{type_array,
8+
type_boolean,
89
type_datetime,
910
type_float,
1011
type_from_array,
1112
type_integer,
1213
type_list,
1314
type_map,
15+
type_mixed,
1416
type_optional,
1517
type_string,
1618
type_structure};
@@ -106,6 +108,52 @@ public static function assert_data_provider() : \Generator
106108
'exceptionClass' => null,
107109
];
108110

111+
yield 'valid structure with type_array field containing a list of structures' => [
112+
'value' => [
113+
'id' => 'test-id',
114+
'size' => 123,
115+
'schema' => [
116+
['ref' => 'col1', 'type' => ['key' => 'value'], 'metadata' => [], 'nullable' => true],
117+
['ref' => 'col2', 'type' => ['key2' => 'value2'], 'metadata' => [], 'nullable' => false],
118+
],
119+
'rows_count' => 10,
120+
'processed_rows' => 5,
121+
'synchronization_id' => 'sync-123',
122+
],
123+
'structureType' => type_structure([
124+
'id' => type_string(),
125+
'size' => type_integer(),
126+
'schema' => type_array(),
127+
'rows_count' => type_integer(),
128+
'processed_rows' => type_integer(),
129+
'synchronization_id' => type_string(),
130+
]),
131+
'exceptionClass' => null,
132+
];
133+
134+
yield 'valid structure with type_list(type_mixed()) field containing a list of structures' => [
135+
'value' => [
136+
'id' => 'test-id',
137+
'size' => 123,
138+
'schema' => [
139+
['ref' => 'col1', 'type' => ['key' => 'value'], 'metadata' => [], 'nullable' => true],
140+
['ref' => 'col2', 'type' => ['key2' => 'value2'], 'metadata' => [], 'nullable' => false],
141+
],
142+
'rows_count' => 10,
143+
'processed_rows' => 5,
144+
'synchronization_id' => 'sync-123',
145+
],
146+
'structureType' => type_structure([
147+
'id' => type_string(),
148+
'size' => type_integer(),
149+
'schema' => type_list(type_mixed()),
150+
'rows_count' => type_integer(),
151+
'processed_rows' => type_integer(),
152+
'synchronization_id' => type_string(),
153+
]),
154+
'exceptionClass' => null,
155+
];
156+
109157
yield 'valid structure with multiple extra fields when allow_extra is true' => [
110158
'value' => ['id' => 1, 'name' => 'test', 'active' => false, 'created_at' => '2023-01-01'],
111159
'structureType' => type_structure(['id' => type_integer(), 'name' => type_string()], [], true),

0 commit comments

Comments
 (0)