diff --git a/src/lib/types/src/Flow/Types/Type/Comparator.php b/src/lib/types/src/Flow/Types/Type/Comparator.php index d50ff4bcf..3edbd66a6 100644 --- a/src/lib/types/src/Flow/Types/Type/Comparator.php +++ b/src/lib/types/src/Flow/Types/Type/Comparator.php @@ -14,7 +14,7 @@ OptionalType, StructureType, TimeType}; -use Flow\Types\Type\Native\{FloatType, IntegerType, NullType, StringType, UnionType}; +use Flow\Types\Type\Native\{ArrayType, FloatType, IntegerType, NullType, StringType, UnionType}; final class Comparator { @@ -68,6 +68,14 @@ public function comparable(Type $left, Type $right) : bool return true; } + if ($left instanceof ArrayType) { + return $right instanceof ArrayType || $right instanceof ListType || $right instanceof MapType || $right instanceof StructureType; + } + + if ($right instanceof ArrayType) { + return $left instanceof ListType || $left instanceof MapType || $left instanceof StructureType; + } + return type_equals($left, $right); } diff --git a/src/lib/types/tests/Flow/Types/Tests/Unit/Type/ComparatorTest.php b/src/lib/types/tests/Flow/Types/Tests/Unit/Type/ComparatorTest.php index 5a530c513..c1de13a11 100644 --- a/src/lib/types/tests/Flow/Types/Tests/Unit/Type/ComparatorTest.php +++ b/src/lib/types/tests/Flow/Types/Tests/Unit/Type/ComparatorTest.php @@ -4,7 +4,8 @@ namespace Flow\Types\Tests\Unit\Type; -use function Flow\Types\DSL\{type_boolean, +use function Flow\Types\DSL\{type_array, + type_boolean, type_equals, type_float, type_integer, @@ -41,6 +42,14 @@ public static function type_comparable_data_provider() : \Generator yield [type_union(type_integer(), type_null()), type_integer()]; yield [type_union(type_integer(), type_null()), type_float()]; yield [type_integer(), type_string()]; + + yield [type_array(), type_array()]; + yield [type_array(), type_list(type_string())]; + yield [type_array(), type_map(type_string(), type_integer())]; + yield [type_array(), type_structure(['id' => type_integer()])]; + yield [type_list(type_string()), type_array()]; + yield [type_map(type_string(), type_integer()), type_array()]; + yield [type_structure(['id' => type_integer()]), type_array()]; } public static function type_comparison_data_provider() : \Generator @@ -70,6 +79,10 @@ public static function type_not_comparable_data_provider() : \Generator { yield [type_integer(), type_union(type_float(), type_integer())]; yield [type_integer(), type_boolean()]; + yield [type_array(), type_string()]; + yield [type_array(), type_integer()]; + yield [type_array(), type_boolean()]; + yield [type_string(), type_array()]; } /** diff --git a/src/lib/types/tests/Flow/Types/Tests/Unit/Type/Logical/StructureTypeTest.php b/src/lib/types/tests/Flow/Types/Tests/Unit/Type/Logical/StructureTypeTest.php index 75fb7998a..c29ca41fb 100644 --- a/src/lib/types/tests/Flow/Types/Tests/Unit/Type/Logical/StructureTypeTest.php +++ b/src/lib/types/tests/Flow/Types/Tests/Unit/Type/Logical/StructureTypeTest.php @@ -4,13 +4,15 @@ namespace Flow\Types\Tests\Unit\Type\Logical; -use function Flow\Types\DSL\{type_boolean, +use function Flow\Types\DSL\{type_array, + type_boolean, type_datetime, type_float, type_from_array, type_integer, type_list, type_map, + type_mixed, type_optional, type_string, type_structure}; @@ -106,6 +108,52 @@ public static function assert_data_provider() : \Generator 'exceptionClass' => null, ]; + yield 'valid structure with type_array field containing a list of structures' => [ + 'value' => [ + 'id' => 'test-id', + 'size' => 123, + 'schema' => [ + ['ref' => 'col1', 'type' => ['key' => 'value'], 'metadata' => [], 'nullable' => true], + ['ref' => 'col2', 'type' => ['key2' => 'value2'], 'metadata' => [], 'nullable' => false], + ], + 'rows_count' => 10, + 'processed_rows' => 5, + 'synchronization_id' => 'sync-123', + ], + 'structureType' => type_structure([ + 'id' => type_string(), + 'size' => type_integer(), + 'schema' => type_array(), + 'rows_count' => type_integer(), + 'processed_rows' => type_integer(), + 'synchronization_id' => type_string(), + ]), + 'exceptionClass' => null, + ]; + + yield 'valid structure with type_list(type_mixed()) field containing a list of structures' => [ + 'value' => [ + 'id' => 'test-id', + 'size' => 123, + 'schema' => [ + ['ref' => 'col1', 'type' => ['key' => 'value'], 'metadata' => [], 'nullable' => true], + ['ref' => 'col2', 'type' => ['key2' => 'value2'], 'metadata' => [], 'nullable' => false], + ], + 'rows_count' => 10, + 'processed_rows' => 5, + 'synchronization_id' => 'sync-123', + ], + 'structureType' => type_structure([ + 'id' => type_string(), + 'size' => type_integer(), + 'schema' => type_list(type_mixed()), + 'rows_count' => type_integer(), + 'processed_rows' => type_integer(), + 'synchronization_id' => type_string(), + ]), + 'exceptionClass' => null, + ]; + yield 'valid structure with multiple extra fields when allow_extra is true' => [ 'value' => ['id' => 1, 'name' => 'test', 'active' => false, 'created_at' => '2023-01-01'], 'structureType' => type_structure(['id' => type_integer(), 'name' => type_string()], [], true),