Skip to content

Commit

Permalink
Merge branch '1.x' into integration/symfony-indexof
Browse files Browse the repository at this point in the history
  • Loading branch information
f-lapinski authored Feb 12, 2025
2 parents 42e388d + dfde1cc commit c369340
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased] - 2025-02-11
## [Unreleased] - 2025-02-12

### Added
- [#1470](https://github.com/flow-php/flow/pull/1470) - **ByteString IsUTF8 function with Test** - [@f-lapinski](https://github.com/f-lapinski)
- [#1465](https://github.com/flow-php/flow/pull/1465) - **jsonl writing example** - [@norberttech](https://github.com/norberttech)
- [#1465](https://github.com/flow-php/flow/pull/1465) - **csv writing example** - [@norberttech](https://github.com/norberttech)
- [#1465](https://github.com/flow-php/flow/pull/1465) - **xml writing example** - [@norberttech](https://github.com/norberttech)
Expand Down Expand Up @@ -155,6 +156,8 @@
- [#1240](https://github.com/flow-php/flow/pull/1240) - **Update Homebrew TAP formula: flow-php to version: 0.10.0** - [@norberttech](https://github.com/norberttech)

### Fixed
- [#1470](https://github.com/flow-php/flow/pull/1470) - **Add Missing Test for return type of function** - [@f-lapinski](https://github.com/f-lapinski)
- [#1469](https://github.com/flow-php/flow/pull/1469) - **JSONLines Loader would occasionally write a newline to the start of the file.** - [@jmortlock](https://github.com/jmortlock)
- [#1457](https://github.com/flow-php/flow/pull/1457) - **missing entry types to JSON/CSV entry normalizers** - [@norberttech](https://github.com/norberttech)
- [d82381](https://github.com/flow-php/flow/commit/d823813af9be12d5cc21d4b8e2a01171ce0e0c90) - **failing example test** - [@norberttech](https://github.com/norberttech)
- [#1448](https://github.com/flow-php/flow/pull/1448) - **ParquetOutput didn't implement Output interface** - [@radozato](https://github.com/radozato)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ final class JsonLinesLoader implements Closure, Loader, Loader\FileLoader

private int $flags = JSON_THROW_ON_ERROR;

/**
* @var array<string, int>
*/
private array $writes = [];

public function __construct(private readonly Path $path)
{
}
Expand Down Expand Up @@ -65,16 +60,7 @@ public function write(Rows $nextRows, array $partitions, FlowContext $context) :
$streams = $context->streams();
$normalizer = new RowsNormalizer(new EntryNormalizer($this->dateTimeFormat));

if (!$streams->isOpen($this->path, $partitions)) {
$stream = $streams->writeTo($this->path, $partitions);

if (!\array_key_exists($stream->path()->path(), $this->writes)) {
$this->writes[$stream->path()->path()] = 0;
}

} else {
$stream = $streams->writeTo($this->path, $partitions);
}
$stream = $streams->writeTo($this->path, $partitions);

$this->writeJSON($nextRows, $stream, $normalizer);
}
Expand Down Expand Up @@ -103,11 +89,7 @@ private function writeJSON(Rows $rows, DestinationStream $stream, RowsNormalizer
throw new RuntimeException('Failed to encode JSON: ' . $e->getMessage(), 0, $e);
}

$json = ($this->writes[$stream->path()->path()] > 0) ? ("\n" . $json) : $json;

$stream->append($json);

$this->writes[$stream->path()->path()]++;
$stream->append($json . "\n");
}
}
}
34 changes: 34 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/IsUtf8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\{type_boolean};
use function Symfony\Component\String\{b};
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class IsUtf8 extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(private readonly ScalarFunction|string $string)
{
}

public function eval(Row $row) : mixed
{
$string = (new Parameter($this->string))->asString($row);

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

return b($string)->isUtf8();
}

public function returns() : Type
{
return type_boolean();
}
}
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public function isType(string|Type ...$types) : self
return new IsType($this, ...$types);
}

public function isUtf8() : IsUtf8
{
return new IsUtf8($this);
}

public function jsonDecode(ScalarFunction|int $flags = JSON_THROW_ON_ERROR) : self
{
return new JsonDecode($this, $flags);
Expand Down
39 changes: 39 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/IsUtf8Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Function;

use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\{ref, str_entry, type_boolean};
use Flow\ETL\Function\IsUtf8;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Tests\FlowTestCase;

final class IsUtf8Test extends FlowTestCase
{
public function test_is_utf_8() : void
{
self::assertTrue(
ref('str')->isUtf8()->eval(
row(str_entry('str', 'Lorem Ipsum'))
)
);

self::assertFalse(
ref('str')->isUtf8()->eval(
row(str_entry('str', "\xc3\x28"))
)
);
}

public function test_returns_method_returns_string_boolean() : void
{
$isUtf8Function = new IsUtf8('Lorem Ipsum');
$returnType = $isUtf8Function->returns();

self::assertInstanceOf(Type::class, $returnType);

self::assertTrue($returnType->isEqual(type_boolean()));
}
}

0 comments on commit c369340

Please sign in to comment.