Skip to content

Commit 2249cbd

Browse files
committed
Rework RenameReplaceEntryStrategy to allow multiple strings
1 parent 54532d8 commit 2249cbd

10 files changed

Lines changed: 47 additions & 16 deletions

File tree

examples/topics/data_frame/cache/code.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use function Flow\ETL\DSL\{config_builder, data_frame, filesystem_cache, from_cache, ref, to_stream};
5+
use function Flow\ETL\DSL\{config_builder, data_frame, filesystem_cache, from_cache, ref, rename_replace, to_stream};
66
use Flow\ETL\Adapter\Http\DynamicExtractor\NextRequestFactory;
77
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
88
use Http\Client\Curl\Client;
@@ -41,7 +41,7 @@ public function create(?ResponseInterface $previousResponse = null) : ?RequestIn
4141
->withEntry('unpacked', ref('response_body')->jsonDecode())
4242
->select('unpacked')
4343
->withEntry('unpacked', ref('unpacked')->unpack())
44-
->renameAll('unpacked.', '')
44+
->renameEach(rename_replace('unpacked.', ''))
4545
->drop('unpacked')
4646
->select('name', 'html_url', 'blog', 'login', 'public_repos', 'followers', 'created_at')
4747
->write(to_stream(__DIR__ . '/output.txt', truncate: false))

examples/topics/data_frame/rename_columns/code.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use function Flow\ETL\DSL\{data_frame, from_array, to_stream};
5+
use function Flow\ETL\DSL\{data_frame, from_array, rename_replace, to_stream};
66

77
require __DIR__ . '/vendor/autoload.php';
88

@@ -13,7 +13,7 @@
1313
['id' => 3, 'name' => 'Jane', 'joined_id' => 3, 'joined_status' => 'active'],
1414
]))
1515
->rename('id', 'user_id')
16-
->renameAll('joined_', '')
16+
->renameEach(rename_replace('joined.', ''))
1717
->collect()
1818
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
1919
->run();

examples/topics/data_reading/http_dynamic/code.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use function Flow\ETL\DSL\{data_frame, ref, to_stream};
5+
use function Flow\ETL\DSL\{data_frame, ref, rename_replace, to_stream};
66
use Flow\ETL\Adapter\Http\DynamicExtractor\NextRequestFactory;
77
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
88
use Http\Client\Curl\Client;
@@ -34,7 +34,7 @@ public function create(?Message\ResponseInterface $previousResponse = null) : ?M
3434
->read($from_github_api)
3535
->withEntry('unpacked', ref('response_body')->jsonDecode())
3636
->withEntry('unpacked', ref('unpacked')->unpack())
37-
->renameAll('unpacked.', '')
37+
->renameEach(rename_replace('unpacked.', ''))
3838
->drop('unpacked')
3939
->select('name', 'html_url', 'blog', 'login', 'public_repos', 'followers', 'created_at')
4040
->write(to_stream(__DIR__ . '/output.txt', truncate: false))

src/core/etl/src/Flow/ETL/DSL/functions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,12 @@ function rename_style(Style $style) : Transformer\Rename\RenameCaseEntryStrategy
354354
return new Transformer\Rename\RenameCaseEntryStrategy($style);
355355
}
356356

357+
/**
358+
* @param array<string>|string $search
359+
* @param array<string>|string $replace
360+
*/
357361
#[DocumentationDSL(module: Module::CORE, type: DSLType::TRANSFORMER)]
358-
function rename_replace(string $search, string $replace) : Transformer\Rename\RenameReplaceEntryStrategy
362+
function rename_replace(string|array $search, string|array $replace) : Transformer\Rename\RenameReplaceEntryStrategy
359363
{
360364
return new Transformer\Rename\RenameReplaceEntryStrategy($search, $replace);
361365
}

src/core/etl/src/Flow/ETL/Row/Formatter/ASCIISchemaFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Row\Formatter;
66

7-
use function Flow\ETL\DSL\{df, from_array, ref, to_output};
7+
use function Flow\ETL\DSL\{df, from_array, ref, rename_replace, to_output};
88
use Flow\ETL\Exception\RuntimeException;
99
use Flow\ETL\PHP\Type\Logical\StructureType;
1010
use Flow\ETL\PHP\Type\Type;
@@ -24,7 +24,7 @@ public function format(Schema $schema) : string
2424
df()
2525
->read(from_array($schema->normalize()))
2626
->withEntry('type', ref('type')->unpack())
27-
->renameAll('type.', '')
27+
->renameEach(rename_replace('type.', ''))
2828
->rename('ref', 'name')
2929
->collect()
3030
->select('name', 'type', 'nullable', 'metadata')

src/core/etl/src/Flow/ETL/Transformer/Rename/RenameReplaceEntryStrategy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
final readonly class RenameReplaceEntryStrategy implements RenameEntryStrategy
1010
{
11+
/**
12+
* @param array<string>|string $search
13+
* @param array<string>|string $replace
14+
*/
1115
public function __construct(
12-
private string $search,
13-
private string $replace,
16+
private string|array $search,
17+
private string|array $replace,
1418
) {
1519
}
1620

src/core/etl/tests/Flow/ETL/Tests/Integration/DataFrame/RenameTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ public function test_rename_all_lower_case_i18n() : void
8383
);
8484
}
8585

86+
public function test_rename_all_multiple() : void
87+
{
88+
$rows = rows(
89+
row(json_entry('array', ['id' => 1, 'name' => 'name', 'isActive' => true])),
90+
row(json_entry('array', ['id' => 2, 'name' => 'name', 'isActive' => false])),
91+
);
92+
93+
$ds = df()
94+
->read(from_rows($rows))
95+
->withEntry('row', ref('array')->unpack())
96+
->renameEach(rename_replace(['row.', 'isActive'], ['', 'active']))
97+
->drop('array')
98+
->getEachAsArray();
99+
100+
self::assertEquals(
101+
[
102+
['id' => 1, 'name' => 'name', 'active' => true],
103+
['id' => 2, 'name' => 'name', 'active' => false],
104+
],
105+
\iterator_to_array($ds)
106+
);
107+
}
108+
86109
public function test_rename_all_to_ascii() : void
87110
{
88111
$rows = rows(row(int_entry('ÓSMY', 8)), row(int_entry('DZIEWIĄTY', 9)));

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/ArrayUnpackTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Flow\ETL\Tests\Integration\Function;
66

77
use function Flow\ETL\DSL\data_frame;
8-
use function Flow\ETL\DSL\{from_array, ref, to_memory};
8+
use function Flow\ETL\DSL\{from_array, ref, rename_replace, to_memory};
99
use Flow\ETL\Memory\ArrayMemory;
1010
use Flow\ETL\Tests\FlowTestCase;
1111

@@ -23,7 +23,7 @@ public function test_array_unpack() : void
2323
)
2424
)
2525
->withEntry('array', ref('array')->unpack())
26-
->renameAll('array.', '')
26+
->renameEach(rename_replace('array.', ''))
2727
->drop('array')
2828
->write(to_memory($memory = new ArrayMemory()))
2929
->run();

web/landing/resources/dsl.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

web/landing/src/Flow/Website/Service/Github.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\Website\Service;
66

7-
use function Flow\ETL\DSL\{config_builder, df, from_cache, lit, not, ref, to_memory};
7+
use function Flow\ETL\DSL\{config_builder, df, from_cache, lit, not, ref, rename_replace, to_memory};
88
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
99
use Flow\ETL\Cache\Implementation\PSRSimpleCache;
1010
use Flow\ETL\Memory\ArrayMemory;
@@ -55,7 +55,7 @@ public function contributors() : array
5555
->select('unpacked')
5656
->withEntry('data', ref('unpacked')->expand())
5757
->withEntry('data', ref('data')->unpack())
58-
->renameAll('data.', '')
58+
->renameEach(rename_replace('data.', ''))
5959
->drop('unpacked', 'data')
6060
->filter(not(ref('login')->endsWith(lit('[bot]'))))
6161
->filter(not(ref('login')->equals(lit('aeon-automation'))))

0 commit comments

Comments
 (0)