Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ function float_entry(string $name, float|int|string|null $value, ?Metadata $meta
#[DocumentationDSL(module: Module::CORE, type: DSLType::ENTRY)]
function json_entry(string $name, array|string|null $data, ?Metadata $metadata = null) : Entry
{
if (\is_array($data)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't make much sense, array can still be a regular array but not json object.

{
   "id" : 1,
   "name": "test"
}

In PHP is ['id' => 1, "name" => "test"]

But [{"id": 1, "name": "test"}] in php would be [["id":1, "name": "test"]] however both satisfies php is_array.

If anything I would probably do a json_encode on value when is_array

return JsonEntry::object($name, $data, $metadata);
}

return new JsonEntry($name, $data, $metadata);
}

Expand Down
68 changes: 28 additions & 40 deletions src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace Flow\ETL\Row\Entry;

use function Flow\Types\DSL\{type_equals, type_json, type_optional};
use Flow\ArrayComparison\ArrayComparison;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row\{Entry, Reference};
use Flow\ETL\Schema\{Definition, Metadata};
use Flow\Types\Type;

/**
* @implements Entry<?array<mixed>>
* @implements Entry<?string>
*/
final class JsonEntry implements Entry
{
Expand All @@ -28,58 +27,55 @@ final class JsonEntry implements Entry
private readonly Type $type;

/**
* @var null|array<array-key, mixed>
* @var null|string
*/
private readonly ?array $value;
private readonly ?string $value;

/**
* @param null|array<array-key, mixed>|string $value
* @param null|string $value
*
* @throws InvalidArgumentException
*/
public function __construct(
private readonly string $name,
array|string|null $value,
string|null $value,
?Metadata $metadata = null,
) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
if ($value !== null && !type_json()->isValid($value)) {
throw new InvalidArgumentException("Invalid JSON value given: '{$value}'");
}

if (\is_string($value)) {
if ($value !== null) {
$this->object = \str_starts_with($value, '{') && \str_ends_with($value, '}');

try {
$this->value = (array) \json_decode($value, true, flags: \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new InvalidArgumentException("Invalid value given: '{$value}', reason: " . $e->getMessage(), previous: $e);
}
} else {
$this->value = $value;
}

$this->value = $value;
$this->metadata = $metadata ?: Metadata::empty();
$this->type = type_json();
}

/**
* @param null|array<array-key, mixed> $value
* @param null|array<string, mixed> $value
*
* @throws InvalidArgumentException
* @throws \JsonException
*
* @return Entry<?array<mixed>>
* @return Entry<?string>
*/
public static function object(string $name, ?array $value, ?Metadata $metadata = null) : Entry
{
if (\is_array($value)) {
foreach (\array_keys($value) as $key) {
if (!\is_string($key)) {
throw InvalidArgumentException::because('All keys for JsonEntry object must be strings');
}
if ($value === null) {
return new self($name, null, $metadata);
}

foreach (\array_keys($value) as $key) {
if (!\is_string($key)) {
throw InvalidArgumentException::because('All keys for JsonEntry object must be strings');
}
}

$entry = new self($name, $value, $metadata);
$json = \json_encode($value, \JSON_THROW_ON_ERROR);
$entry = new self($name, $json, $metadata);
$entry->object = true;

return $entry;
Expand Down Expand Up @@ -136,15 +132,15 @@ public function isEqual(Entry $entry) : bool
&& type_equals($this->type, $entry->type);
}

return $this->is($entry->name()) && $entry instanceof self && type_equals($this->type, $entry->type) && (new ArrayComparison())->equals($thisValue, \is_array($entryValue) ? $entryValue : null);
return $this->is($entry->name()) && $entry instanceof self && type_equals($this->type, $entry->type) && $thisValue === $entryValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something tells me this will break a lot of tests 🫢

}

public function map(callable $mapper) : Entry
{
$mappedValue = new self($this->name, $mapper($this->value()));
$mappedValue->object = $this->object;
$entry = new self($this->name, $mapper($this->value()), $this->metadata);
$entry->object = $this->object;

return $mappedValue;
return $entry;
}

public function name() : string
Expand All @@ -154,23 +150,15 @@ public function name() : string

public function rename(string $name) : Entry
{
$entry = new self($name, $this->value);
$entry = new self($name, $this->value, $this->metadata);
$entry->object = $this->object;

return $entry;
}

public function toString() : string
{
if ($this->value === null) {
return '';
}

if (!\count($this->value) && $this->object) {
return '{}';
}

return \json_encode($this->value, \JSON_THROW_ON_ERROR);
return $this->value ?? '';
}

/**
Expand All @@ -183,7 +171,7 @@ public function type() : Type
return $this->type;
}

public function value() : ?array
public function value() : ?string
{
return $this->value;
}
Expand Down
Loading