Skip to content

Commit

Permalink
Added helper methods on Schema/Definition to manipulate metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Feb 23, 2025
1 parent 8b8e2c8 commit bc9763c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function normalize(Rows $rows, Schema $schema) : array
$columns[$entry->name()] = match ($entry::class) {
UuidEntry::class => $this->caster->to(type_string())->value($entry->value()),
XMLEntry::class => $this->caster->to(type_string())->value($entry->value()),
default => $this->caster->to($schema->getDefinition($entry->ref())->type())->value($entry->value()),
default => $this->caster->to($schema->get($entry->ref())->type())->value($entry->value()),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/EntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function create(string $entryName, mixed $value, Schema|Definition|null $
}

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

return $this->createAs($definition->entry()->name(), $value, $definition->type(), $definition->metadata());
}
Expand Down
36 changes: 35 additions & 1 deletion src/core/etl/src/Flow/ETL/Row/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public function add(Definition ...$definitions) : self
return $this;
}

/**
* Adds metadata to a given definition.
*
* @throws SchemaDefinitionNotFoundException
*/
public function addMetadata(string $definition, string $name, int|string|bool|float|array $value) : self
{
$this->get($definition)->addMetadata($name, $value);

return $this;
}

public function count() : int
{
return \count($this->definitions);
Expand Down Expand Up @@ -136,11 +148,21 @@ public function findDefinition(string|Reference $ref) : ?Definition
/**
* @throws SchemaDefinitionNotFoundException
*/
public function getDefinition(string|Reference $ref) : Definition
public function get(string|Reference $ref) : Definition
{
return $this->findDefinition($ref) ?: throw new SchemaDefinitionNotFoundException((string) $ref);
}

/**
* @deprecated please use Schema::get() instead
*
* @throw SchemaDefinitionNotFoundException
*/
public function getDefinition(string|Reference $ref) : Definition

Check warning on line 161 in src/core/etl/src/Flow/ETL/Row/Schema.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L161 was not covered by tests
{
return $this->get($ref);

Check warning on line 163 in src/core/etl/src/Flow/ETL/Row/Schema.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L163 was not covered by tests
}

/**
* Gracefully remove entries from schema without throwing an exception if entry does not exist.
*/
Expand Down Expand Up @@ -335,6 +357,18 @@ public function replace(string|Reference $entry, Definition $definition) : self
return $this;
}

/**
* Overwrites metadata for a given definition.
*
* @throws SchemaDefinitionNotFoundException
*/
public function setMetadata(string $definition, Metadata $metadata) : self
{
$this->get($definition)->setMetadata($metadata);

return $this;
}

private function setDefinitions(Definition ...$definitions) : void
{
$uniqueDefinitions = [];
Expand Down
20 changes: 17 additions & 3 deletions src/core/etl/src/Flow/ETL/Row/Schema/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
use Flow\ETL\PHP\Type\{Native\FloatType, Native\IntegerType, Native\StringType, Type, TypeFactory};
use Flow\ETL\Row\{Entry, EntryReference, Reference};

final readonly class Definition
final class Definition
{
private Metadata $metadata;

private Reference $ref;
private readonly Reference $ref;

/**
* @param Type<mixed> $type
*/
public function __construct(
string|Reference $ref,
private Type $type,
private readonly Type $type,
?Metadata $metadata = null,
) {

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

public function addMetadata(string $key, int|string|bool|float|array $value) : self
{
$this->metadata = $this->metadata->add($key, $value);

return $this;
}

public function entry() : Reference
{
return $this->ref;
Expand Down Expand Up @@ -329,6 +336,13 @@ public function rename(string $newName) : self
);
}

public function setMetadata(Metadata $metadata) : self
{
$this->metadata = $metadata;

return $this;
}

/**
* @return Type<mixed>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class SelectiveValidator implements SchemaValidator
public function isValid(Rows $rows, Schema $schema) : bool
{
foreach ($schema->references() as $ref) {
$definition = $schema->getDefinition($ref);
$definition = $schema->get($ref);

foreach ($rows as $row) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ public function test_not_matches_when_type_name_not_match() : void
self::assertFalse($def->matches(int_entry('not-test', 1)));
}

public function test_set_metadata() : void
{
$definition = integer_schema('id', metadata: Metadata::fromArray(['test' => 'test']));

self::assertEquals(
integer_schema('id', false, Metadata::fromArray(['description' => 'some_random_description'])),
$definition->setMetadata(Metadata::fromArray(['description' => 'some_random_description']))
);

}

public function test_structure_definition_metadata() : void
{
$address = struct_entry(
Expand Down
40 changes: 40 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,25 @@
uuid_schema};
use function Flow\ETL\DSL\{integer_schema, string_schema};
use Flow\ETL\Exception\{InvalidArgumentException, SchemaDefinitionNotFoundException, SchemaDefinitionNotUniqueException};
use Flow\ETL\Row\Schema\Metadata;
use Flow\ETL\Row\{EntryReference, Schema};
use Flow\ETL\Tests\FlowTestCase;

final class SchemaTest extends FlowTestCase
{
public function test_add_metadata() : void
{
$schema = schema(
int_schema('id'),
str_schema('name'),
);

self::assertEquals(
int_schema('id', metadata: Metadata::fromArray(['test' => 'test'])),
$schema->addMetadata('id', 'test', 'test')->get('id')
);
}

public function test_adding_duplicated_definitions() : void
{
$this->expectException(SchemaDefinitionNotUniqueException::class);
Expand Down Expand Up @@ -96,6 +110,19 @@ public function test_creating_schema_from_invalid_json_format_at_definition_leve
schema_from_json('[{"ref": "id", "type": "test", "metadata": []}]');
}

public function test_get() : void
{
$schema = schema(
int_schema('id'),
str_schema('name'),
);

self::assertEquals(
int_schema('id'),
$schema->get('id')
);
}

public function test_graceful_remove_non_existing_definition() : void
{

Expand Down Expand Up @@ -385,4 +412,17 @@ public function test_schema_to_from_json() : void
schema_from_json(schema_to_json($schema))
);
}

public function test_set_metadata() : void
{
$schema = schema(
int_schema('id', metadata: Metadata::fromArray(['foo' => 'bar'])),
str_schema('name'),
);

self::assertEquals(
int_schema('id', metadata: Metadata::fromArray(['test' => 'test'])),
$schema->setMetadata('id', Metadata::fromArray(['test' => 'test']))->get('id')
);
}
}

0 comments on commit bc9763c

Please sign in to comment.