Skip to content

Commit

Permalink
Allow Scalar Functions to defined return Type (#1400)
Browse files Browse the repository at this point in the history
* Allow Scalar Functions to defined return Type

* Updated dependencies

* updated dsl definitions

* Locked tailwind to 3.4.17 version

* Updated dependencies and fixed static analysis
  • Loading branch information
norberttech authored Jan 23, 2025
1 parent 9d4b3dc commit 643e514
Show file tree
Hide file tree
Showing 51 changed files with 458 additions and 210 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ private function client() : \Elasticsearch\Client|\Elastic\Elasticsearch\Client
}
}

/**
* @phpstan-ignore-next-line
*/
return $this->client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ private function client() : \Elasticsearch\Client|\Elastic\Elasticsearch\Client
}
}

/**
* @phpstan-ignore-next-line
*/
return $this->client;
}
}
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,10 @@ function hash(mixed $value, Algorithm $algorithm = new NativePHPHash()) : Hash
}

/**
* @param ScalarFunction|string|Type<mixed> $type
* @param string|Type<mixed> $type
*/
#[DocumentationDSL(module: Module::CORE, type: DSLType::SCALAR_FUNCTION)]
function cast(mixed $value, ScalarFunction|string|Type $type) : Cast
function cast(mixed $value, string|Type $type) : Cast
{
return new Cast($value, $type);
}
Expand Down
7 changes: 1 addition & 6 deletions src/core/etl/src/Flow/ETL/Function/ArrayExpand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(private readonly ScalarFunction $ref, private readon
{
}

public function eval(Row $row) : mixed
public function eval(Row $row) : ?array
{
$array = (new Parameter($this->ref))->asArray($row);

Expand All @@ -31,9 +31,4 @@ public function eval(Row $row) : mixed

return $array;
}

public function expandResults() : bool
{
return true;
}
}
7 changes: 1 addition & 6 deletions src/core/etl/src/Flow/ETL/Function/ArrayUnpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
) {
}

public function eval(Row $row) : mixed
public function eval(Row $row) : ?array
{
$array = (new Parameter($this->array))->asArray($row);
$skipKeys = (new Parameter($this->skipKeys))->asArray($row);
Expand Down Expand Up @@ -47,9 +47,4 @@ public function eval(Row $row) : mixed

return $values;
}

public function unpackResults() : bool
{
return true;
}
}
10 changes: 9 additions & 1 deletion src/core/etl/src/Flow/ETL/Function/Ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\type_string;
use function Symfony\Component\String\u;
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class Ascii extends ScalarFunctionChain
final class Ascii extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(private readonly ScalarFunction|string $string)
{
Expand All @@ -23,4 +26,9 @@ public function eval(Row $row) : mixed

return u($string)->ascii()->toString();
}

public function returns() : Type
{
return type_string();
}
}
10 changes: 9 additions & 1 deletion src/core/etl/src/Flow/ETL/Function/Capitalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\type_string;
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class Capitalize extends ScalarFunctionChain
final class Capitalize extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(private readonly ScalarFunction|string $string)
{
Expand All @@ -26,4 +29,9 @@ public function eval(Row $row) : mixed

return \ucwords((string) $string);
}

public function returns() : Type
{
return type_string();
}
}
49 changes: 41 additions & 8 deletions src/core/etl/src/Flow/ETL/Function/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\{type_array, type_boolean, type_datetime, type_float, type_integer, type_json, type_object, type_string, type_xml};
use function Flow\ETL\DSL\{type_array,
type_boolean,
type_date,
type_datetime,
type_float,
type_integer,
type_json,
type_object,
type_string,
type_xml};
use Flow\ETL\Exception\{CastingException, InvalidArgumentException};
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\{Caster, Type};
use Flow\ETL\Row;

final class Cast extends ScalarFunctionChain
final class Cast extends ScalarFunctionChain implements TypedScalarFunction
{
/**
* @param mixed $value
* @param ScalarFunction|string|Type<mixed> $type
* @param string|Type<mixed> $type
*/
public function __construct(
private readonly mixed $value,
private readonly ScalarFunction|Type|string $type,
private readonly Type|string $type,
) {
}

Expand All @@ -28,16 +38,14 @@ public function __construct(
public function eval(Row $row) : mixed
{
$value = (new Parameter($this->value))->eval($row);
$type = $this->type instanceof ScalarFunction ? (new Parameter($this->type))->asString($row) : $this->type;
$type = $this->type;

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

$caster = Caster::default();

$type = $this->type;

if ($type instanceof Type) {
return $caster->to($type)->value($value);
}
Expand Down Expand Up @@ -70,4 +78,29 @@ public function eval(Row $row) : mixed
return null;
}
}

/**
* @returns Type<mixed>
*/
public function returns() : Type
{
if ($this->type instanceof Type) {
return $this->type;
}

return match (\mb_strtolower($this->type)) {
'datetime' => type_datetime(),
'date' => type_date(),
'int', 'integer' => type_integer(),
'float', 'double', 'real' => type_float(),
'string' => type_string(),
'bool', 'boolean' => type_boolean(),
'array' => type_array(),
'object' => type_object(\stdClass::class),
'json' => type_json(),
'json_pretty' => type_json(),
'xml' => type_xml(),
default => type_string(),
};
}
}
10 changes: 8 additions & 2 deletions src/core/etl/src/Flow/ETL/Function/Concat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Flow\ETL\Function;

use function Flow\ETL\DSL\type_string;
use Flow\ETL\PHP\Type\Caster;
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\{Caster, Type};
use Flow\ETL\Row;

final class Concat extends ScalarFunctionChain
final class Concat extends ScalarFunctionChain implements TypedScalarFunction
{
/**
* @var array<ScalarFunction|string>
Expand Down Expand Up @@ -36,4 +37,9 @@ public function eval(Row $row) : mixed

return \implode('', $concatValues);
}

public function returns() : Type
{
return type_string();
}
}
11 changes: 9 additions & 2 deletions src/core/etl/src/Flow/ETL/Function/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\{type_array, type_string};
use function Flow\ETL\DSL\{type_array, type_boolean, type_string};
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class Contains extends ScalarFunctionChain
final class Contains extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(
private readonly ScalarFunction|string $haystack,
Expand All @@ -34,4 +36,9 @@ public function eval(Row $row) : bool

return false;
}

public function returns() : Type
{
return type_boolean();
}
}
10 changes: 9 additions & 1 deletion src/core/etl/src/Flow/ETL/Function/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\type_boolean;
use Flow\ETL\Function\Comparison\Comparable;
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class Equals extends ScalarFunctionChain
final class Equals extends ScalarFunctionChain implements TypedScalarFunction
{
use Comparable;

Expand All @@ -26,4 +29,9 @@ public function eval(Row $row) : bool

return $left == $right;
}

public function returns() : Type
{
return type_boolean();
}
}
Loading

0 comments on commit 643e514

Please sign in to comment.