-
-
Notifications
You must be signed in to change notification settings - Fork 48
#1955 - unify json entry #1959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MrHDOLEK
wants to merge
1
commit into
flow-php:1.x
Choose a base branch
from
MrHDOLEK:#1955-unify-jsonentry
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
#1955 - unify json entry #1959
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| { | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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 ?? ''; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -183,7 +171,7 @@ public function type() : Type | |
| return $this->type; | ||
| } | ||
|
|
||
| public function value() : ?array | ||
| public function value() : ?string | ||
| { | ||
| return $this->value; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
In PHP is
['id' => 1, "name" => "test"]But
[{"id": 1, "name": "test"}]in php would be[["id":1, "name": "test"]]however both satisfies phpis_array.If anything I would probably do a
json_encodeon value whenis_array