Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework expressions to return null instead of throwing exception on failure #556

Merged
merged 1 commit into from
Oct 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Flow\ETL\Row\Reference\Expression;

use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\ExpandResults;
use Flow\ETL\Row\Reference\Expression;
Expand All @@ -20,7 +19,7 @@ public function eval(Row $row) : mixed
$array = $this->ref->eval($row);

if (!\is_array($array)) {
throw new RuntimeException(\get_class($this->ref) . ' is not an array, got: ' . \gettype($array));
return null;
}

if ($this->expand === ArrayExpand\ArrayExpand::KEYS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use function Flow\ArrayDot\array_dot_get;
use Flow\ArrayDot\Exception\InvalidPathException;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression;

Expand Down Expand Up @@ -45,11 +44,11 @@ public function eval(Row $row) : mixed

$extractedValues = array_dot_get($array, $path);
} catch (InvalidPathException) {
throw new RuntimeException(\get_class($this->ref) . ' must be an array of array (collection of arrays) but it seems to be a regular array.');
return null;
}

if (!\is_array($extractedValues)) {
throw new RuntimeException("Extracted value from path \"{$path}\" is not array but " . \gettype($extractedValues));
return null;
}

return $extractedValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function eval(Row $row) : mixed
$array = $this->ref->eval($row);

if (!\is_array($array)) {
throw new RuntimeException(\get_class($this->ref) . ' is not an array, got: ' . \gettype($array));
return null;
}

$converter = (new StyleConverter\ArrayKeyConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Flow\ETL\Row\Reference\Expression;

use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression;

Expand All @@ -23,11 +22,9 @@ public function eval(Row $row) : mixed
return null;
}

foreach ($value as $index => $element) {
foreach ($value as $element) {
if (!\is_array($element)) {
$type = \gettype($element);

throw new RuntimeException(\get_class($this->ref) . " must be an array of arrays, instead element at position \"{$index}\" is {$type}");
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Flow\ETL\Row\Reference\Expression;

use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression;

Expand All @@ -22,7 +21,7 @@ public function eval(Row $row) : mixed
$array = $this->ref->eval($row);

if (!\is_array($array)) {
throw new RuntimeException(\get_class($this->ref) . ' is not an array, got: ' . \gettype($array));
return null;
}

$values = [];
Expand Down
9 changes: 4 additions & 5 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ public function eval(Row $row) : mixed
'bool', 'boolean' => (bool) $value,
'array' => $this->toArray($value),
'object' => (object) $value,
'null' => null,
'json' => \json_encode($value, JSON_THROW_ON_ERROR),
'json_pretty' => \json_encode($value, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT),
'xml' => $this->toXML($value),
default => throw new InvalidArgumentException("Unknown cast type '{$this->type}'")
default => null
};
}

Expand All @@ -69,13 +68,13 @@ private function toArray(mixed $data) : array
return (array) $data;
}

private function toXML(mixed $value) : \DOMDocument
private function toXML(mixed $value) : null|\DOMDocument
{
if (\is_string($value)) {
$doc = new \DOMDocument();

if (!@$doc->load($value)) {
throw new InvalidArgumentException('Invalid XML string given: ' . $value);
return null;
}

return $doc;
Expand All @@ -85,6 +84,6 @@ private function toXML(mixed $value) : \DOMDocument
return $value;
}

throw new InvalidArgumentException(\sprintf('Cannot cast %s to XML', \gettype($value)));
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function eval(Row $row) : mixed
$value = $this->ref->eval($row);

if (!$value instanceof \DateTimeInterface) {
throw new \InvalidArgumentException('Entry ' . \gettype($value) . ' is not a DateTimeEntry');
return null;
}

return $value->format($this->format);
Expand Down
19 changes: 8 additions & 11 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/ToDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,23 @@ public function eval(Row $row) : mixed
$value = $this->ref->eval($row);

if (\is_object($value)) {
return match (\get_class($value)) {
\DateTimeImmutable::class, \DateTime::class => $value->setTimezone($this->timeZone)->setTime(0, 0, 0, 0),
default => throw new \InvalidArgumentException('Entry ' . \get_class($value) . ' is not a DateTimeImmutable|DateTime')
};
if (\is_a($value, \DateTimeImmutable::class) || \is_a($value, \DateTime::class)) {
return $value->setTimezone($this->timeZone)->setTime(0, 0, 0, 0);
}

return null;
}

if (\is_int($value)) {
/**
* @phpstan-ignore-next-line
*/
/** @phpstan-ignore-next-line */
return \DateTimeImmutable::createFromFormat('U', (string) $value, $this->timeZone)->setTime(0, 0, 0, 0);
}

if (\is_string($value)) {
/**
* @phpstan-ignore-next-line
*/
/** @phpstan-ignore-next-line */
return \DateTimeImmutable::createFromFormat($this->format, $value, $this->timeZone)->setTime(0, 0, 0, 0);
}

throw new \InvalidArgumentException('Value ' . \gettype($value) . ' is not a DateTimeImmutable|DateTime|string|int');
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public function eval(Row $row) : mixed
$value = $this->ref->eval($row);

if (\is_object($value)) {
return match (\get_class($value)) {
\DateTimeImmutable::class, \DateTime::class => $value->setTimezone($this->timeZone),
default => throw new \InvalidArgumentException('Entry ' . \get_class($value) . ' is not a DateTimeImmutable|DateTime')
};
if (\is_a($value, \DateTimeImmutable::class) || \is_a($value, \DateTime::class)) {
return $value->setTimezone($this->timeZone)->setTime(0, 0, 0, 0);
}

return null;
}

if (\is_int($value)) {
Expand All @@ -40,6 +41,6 @@ public function eval(Row $row) : mixed
return \DateTimeImmutable::createFromFormat($this->format, $value, $this->timeZone);
}

throw new \InvalidArgumentException('Value ' . \gettype($value) . ' is not a DateTimeImmutable|DateTime|string|int');
return null;
}
}
3 changes: 1 addition & 2 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/Trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Flow\ETL\Row\Reference\Expression;

use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression;

Expand Down Expand Up @@ -34,6 +33,6 @@ public function eval(Row $row) : mixed
}
}

throw new InvalidArgumentException("Unsupported trim method: {$value}");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use function Flow\ETL\DSL\array_expand;
use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression\ArrayExpand\ArrayExpand;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -56,9 +55,8 @@ public function test_expand_values() : void

public function test_for_not_array_entry() : void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Flow\ETL\Row\EntryReference is not an array, got: integer');

array_expand(ref('integer_entry'))->eval(Row::create(Entry::int('integer_entry', 1)));
$this->assertNull(
array_expand(ref('integer_entry'))->eval(Row::create(Entry::int('integer_entry', 1)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use function Flow\ETL\DSL\array_get_collection_first;
use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -37,10 +36,7 @@ public function test_getting_keys_from_simple_array() : void
),
);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Flow\ETL\Row\EntryReference must be an array of array (collection of arrays) but it seems to be a regular array.');

array_get_collection(ref('array_entry'), 'id', 'status')->eval($row);
$this->assertNull(array_get_collection(ref('array_entry'), 'id', 'status')->eval($row));
}

public function test_getting_specific_keys_from_collection_of_array() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use PHPUnit\Framework\TestCase;

Expand All @@ -28,14 +27,11 @@ public function test_for_invalid_style() : void

public function test_for_not_array_entry() : void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Flow\ETL\Row\EntryReference is not an array, got: integer');

$row = Row::create(
Entry::integer('invalid_entry', 1),
);

array_keys_style_convert(ref('invalid_entry'), 'snake')->eval($row);
$this->assertNull(array_keys_style_convert(ref('invalid_entry'), 'snake')->eval($row));
}

public function test_transforms_case_style_for_all_keys_in_array_entry() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use function Flow\ETL\DSL\array_merge_collection;
use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use PHPUnit\Framework\TestCase;

Expand All @@ -25,10 +24,7 @@ public function test_attempt_of_merging_collection_where_not_every_element_is_ar
),
);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Flow\ETL\Row\EntryReference must be an array of arrays, instead element at position "1" is integer');

array_merge_collection(ref('array_entry'))->eval($row);
$this->assertNull(array_merge_collection(ref('array_entry'))->eval($row));
}

public function test_for_not_array_entry() : void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use function Flow\ETL\DSL\cast;
use function Flow\ETL\DSL\ref;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row;
use Flow\ETL\Row\Factory\NativeEntryFactory;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -57,17 +56,15 @@ public function test_cast(mixed $from, string $to, mixed $expected) : void

public function test_casting_integer_to_xml() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot cast integer to XML');

ref('value')->cast('xml')->eval(Row::create((new NativeEntryFactory())->create('value', 1)));
$this->assertNull(
ref('value')->cast('xml')->eval(Row::create((new NativeEntryFactory())->create('value', 1)))
);
}

public function test_casting_non_xml_string_to_xml() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid XML string given: foo');

ref('value')->cast('xml')->eval(Row::create((new NativeEntryFactory())->create('value', 'foo')));
$this->assertNull(
ref('value')->cast('xml')->eval(Row::create((new NativeEntryFactory())->create('value', 'foo')))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,37 @@ public function test_array_combine() : void
{
$this->assertSame(
['a' => 1, 'b' => 2, 'c' => 3],
combine(lit(['a', 'b', 'c']), lit([1, 2, 3]), )->eval(Row::create()),
combine(lit(['a', 'b', 'c']), lit([1, 2, 3]))->eval(Row::create()),
);
}

public function test_array_combine_when_arrays_are_empty() : void
{
$this->assertSame(
[],
combine(lit([]), lit([]), )->eval(Row::create()),
combine(lit([]), lit([]))->eval(Row::create()),
);
}

public function test_array_combine_when_keys_are_not_array() : void
{
$this->assertSame(
null,
combine(lit('a'), lit([1, 2, 3]), )->eval(Row::create()),
$this->assertNull(
combine(lit('a'), lit([1, 2, 3]))->eval(Row::create()),
);
}

public function test_array_combine_when_keys_are_not_unique() : void
{
$this->assertSame(
['a' => 4, 'b' => 2, 'c' => 3],
combine(lit(['a', 'b', 'c', 'a']), lit([1, 2, 3, 4]), )->eval(Row::create()),
combine(lit(['a', 'b', 'c', 'a']), lit([1, 2, 3, 4]))->eval(Row::create()),
);
}

public function test_array_combine_when_one_of_arrays_is_empty() : void
{
$this->assertSame(
null,
combine(lit(['a', 'b', 'c']), lit([]), )->eval(Row::create()),
$this->assertNull(
combine(lit(['a', 'b', 'c']), lit([]))->eval(Row::create()),
);
}
}
Loading