Skip to content

Commit

Permalink
Fixed Cast expression to handle null as first (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 4, 2023
1 parent bbc1329 commit 9dd0330
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function eval(Row $row) : mixed
/** @psalm-suppress MixedAssignment */
$value = $this->ref->eval($row);

if (null === $value) {
return null;
}

return match (\mb_strtolower($this->type)) {
'datetime' => match (\gettype($value)) {
'string' => new \DateTimeImmutable($value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function cast_provider() : array
$xml->loadXML($xmlString = '<root><foo baz="buz">bar</foo></root>');

return [
'invalid' => [null, 'int', null],
'int' => ['1', 'int', 1],
'integer' => ['1', 'integer', 1],
'float' => ['1', 'float', 1.0],
Expand All @@ -42,14 +43,16 @@ public static function cast_provider() : array
*/
public function test_cast(mixed $from, string $to, mixed $expected) : void
{
$this->assertEquals(
$expected,
ref('value')->cast($to)->eval(Row::create((new NativeEntryFactory())->create('value', $from)))
);
$this->assertEquals(
$expected,
cast(ref('value'), $to)->eval(Row::create((new NativeEntryFactory())->create('value', $from)))
);
$resultRefCast = ref('value')->cast($to)->eval(Row::create((new NativeEntryFactory())->create('value', $from)));
$resultCastRef = cast(ref('value'), $to)->eval(Row::create((new NativeEntryFactory())->create('value', $from)));

if (\is_object($expected)) {
$this->assertEquals($expected, $resultRefCast);
$this->assertEquals($expected, $resultCastRef);
} else {
$this->assertSame($expected, $resultRefCast);
$this->assertSame($expected, $resultCastRef);
}
}

public function test_casting_integer_to_xml() : void
Expand Down

0 comments on commit 9dd0330

Please sign in to comment.