Skip to content

Commit bc9763c

Browse files
committed
Added helper methods on Schema/Definition to manipulate metadata
1 parent 8b8e2c8 commit bc9763c

File tree

7 files changed

+106
-7
lines changed

7 files changed

+106
-7
lines changed

src/adapter/etl-adapter-parquet/src/Flow/ETL/Adapter/Parquet/RowsNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function normalize(Rows $rows, Schema $schema) : array
3030
$columns[$entry->name()] = match ($entry::class) {
3131
UuidEntry::class => $this->caster->to(type_string())->value($entry->value()),
3232
XMLEntry::class => $this->caster->to(type_string())->value($entry->value()),
33-
default => $this->caster->to($schema->getDefinition($entry->ref())->type())->value($entry->value()),
33+
default => $this->caster->to($schema->get($entry->ref())->type())->value($entry->value()),
3434
};
3535
}
3636

src/core/etl/src/Flow/ETL/Row/EntryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function create(string $entryName, mixed $value, Schema|Definition|null $
7373
}
7474

7575
if ($schema instanceof Schema) {
76-
$definition = $schema->getDefinition($entryName);
76+
$definition = $schema->get($entryName);
7777

7878
return $this->createAs($definition->entry()->name(), $value, $definition->type(), $definition->metadata());
7979
}

src/core/etl/src/Flow/ETL/Row/Schema.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ public function add(Definition ...$definitions) : self
9393
return $this;
9494
}
9595

96+
/**
97+
* Adds metadata to a given definition.
98+
*
99+
* @throws SchemaDefinitionNotFoundException
100+
*/
101+
public function addMetadata(string $definition, string $name, int|string|bool|float|array $value) : self
102+
{
103+
$this->get($definition)->addMetadata($name, $value);
104+
105+
return $this;
106+
}
107+
96108
public function count() : int
97109
{
98110
return \count($this->definitions);
@@ -136,11 +148,21 @@ public function findDefinition(string|Reference $ref) : ?Definition
136148
/**
137149
* @throws SchemaDefinitionNotFoundException
138150
*/
139-
public function getDefinition(string|Reference $ref) : Definition
151+
public function get(string|Reference $ref) : Definition
140152
{
141153
return $this->findDefinition($ref) ?: throw new SchemaDefinitionNotFoundException((string) $ref);
142154
}
143155

156+
/**
157+
* @deprecated please use Schema::get() instead
158+
*
159+
* @throw SchemaDefinitionNotFoundException
160+
*/
161+
public function getDefinition(string|Reference $ref) : Definition
162+
{
163+
return $this->get($ref);
164+
}
165+
144166
/**
145167
* Gracefully remove entries from schema without throwing an exception if entry does not exist.
146168
*/
@@ -335,6 +357,18 @@ public function replace(string|Reference $entry, Definition $definition) : self
335357
return $this;
336358
}
337359

360+
/**
361+
* Overwrites metadata for a given definition.
362+
*
363+
* @throws SchemaDefinitionNotFoundException
364+
*/
365+
public function setMetadata(string $definition, Metadata $metadata) : self
366+
{
367+
$this->get($definition)->setMetadata($metadata);
368+
369+
return $this;
370+
}
371+
338372
private function setDefinitions(Definition ...$definitions) : void
339373
{
340374
$uniqueDefinitions = [];

src/core/etl/src/Flow/ETL/Row/Schema/Definition.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
use Flow\ETL\PHP\Type\{Native\FloatType, Native\IntegerType, Native\StringType, Type, TypeFactory};
2323
use Flow\ETL\Row\{Entry, EntryReference, Reference};
2424

25-
final readonly class Definition
25+
final class Definition
2626
{
2727
private Metadata $metadata;
2828

29-
private Reference $ref;
29+
private readonly Reference $ref;
3030

3131
/**
3232
* @param Type<mixed> $type
3333
*/
3434
public function __construct(
3535
string|Reference $ref,
36-
private Type $type,
36+
private readonly Type $type,
3737
?Metadata $metadata = null,
3838
) {
3939

@@ -160,6 +160,13 @@ public static function xml_element(string|Reference $entry, bool $nullable = fal
160160
return new self($entry, type_xml_element($nullable), $metadata);
161161
}
162162

163+
public function addMetadata(string $key, int|string|bool|float|array $value) : self
164+
{
165+
$this->metadata = $this->metadata->add($key, $value);
166+
167+
return $this;
168+
}
169+
163170
public function entry() : Reference
164171
{
165172
return $this->ref;
@@ -329,6 +336,13 @@ public function rename(string $newName) : self
329336
);
330337
}
331338

339+
public function setMetadata(Metadata $metadata) : self
340+
{
341+
$this->metadata = $metadata;
342+
343+
return $this;
344+
}
345+
332346
/**
333347
* @return Type<mixed>
334348
*/

src/core/etl/src/Flow/ETL/Row/Schema/SelectiveValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class SelectiveValidator implements SchemaValidator
1616
public function isValid(Rows $rows, Schema $schema) : bool
1717
{
1818
foreach ($schema->references() as $ref) {
19-
$definition = $schema->getDefinition($ref);
19+
$definition = $schema->get($ref);
2020

2121
foreach ($rows as $row) {
2222
try {

src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Schema/DefinitionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ public function test_not_matches_when_type_name_not_match() : void
263263
self::assertFalse($def->matches(int_entry('not-test', 1)));
264264
}
265265

266+
public function test_set_metadata() : void
267+
{
268+
$definition = integer_schema('id', metadata: Metadata::fromArray(['test' => 'test']));
269+
270+
self::assertEquals(
271+
integer_schema('id', false, Metadata::fromArray(['description' => 'some_random_description'])),
272+
$definition->setMetadata(Metadata::fromArray(['description' => 'some_random_description']))
273+
);
274+
275+
}
276+
266277
public function test_structure_definition_metadata() : void
267278
{
268279
$address = struct_entry(

src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@
2525
uuid_schema};
2626
use function Flow\ETL\DSL\{integer_schema, string_schema};
2727
use Flow\ETL\Exception\{InvalidArgumentException, SchemaDefinitionNotFoundException, SchemaDefinitionNotUniqueException};
28+
use Flow\ETL\Row\Schema\Metadata;
2829
use Flow\ETL\Row\{EntryReference, Schema};
2930
use Flow\ETL\Tests\FlowTestCase;
3031

3132
final class SchemaTest extends FlowTestCase
3233
{
34+
public function test_add_metadata() : void
35+
{
36+
$schema = schema(
37+
int_schema('id'),
38+
str_schema('name'),
39+
);
40+
41+
self::assertEquals(
42+
int_schema('id', metadata: Metadata::fromArray(['test' => 'test'])),
43+
$schema->addMetadata('id', 'test', 'test')->get('id')
44+
);
45+
}
46+
3347
public function test_adding_duplicated_definitions() : void
3448
{
3549
$this->expectException(SchemaDefinitionNotUniqueException::class);
@@ -96,6 +110,19 @@ public function test_creating_schema_from_invalid_json_format_at_definition_leve
96110
schema_from_json('[{"ref": "id", "type": "test", "metadata": []}]');
97111
}
98112

113+
public function test_get() : void
114+
{
115+
$schema = schema(
116+
int_schema('id'),
117+
str_schema('name'),
118+
);
119+
120+
self::assertEquals(
121+
int_schema('id'),
122+
$schema->get('id')
123+
);
124+
}
125+
99126
public function test_graceful_remove_non_existing_definition() : void
100127
{
101128

@@ -385,4 +412,17 @@ public function test_schema_to_from_json() : void
385412
schema_from_json(schema_to_json($schema))
386413
);
387414
}
415+
416+
public function test_set_metadata() : void
417+
{
418+
$schema = schema(
419+
int_schema('id', metadata: Metadata::fromArray(['foo' => 'bar'])),
420+
str_schema('name'),
421+
);
422+
423+
self::assertEquals(
424+
int_schema('id', metadata: Metadata::fromArray(['test' => 'test'])),
425+
$schema->setMetadata('id', Metadata::fromArray(['test' => 'test']))->get('id')
426+
);
427+
}
388428
}

0 commit comments

Comments
 (0)