Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/types/src/Flow/Types/Type/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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),
Expand Down
Loading