-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Problem With getCurrentJsonPointer()
#105
Comments
Digging further, I understand why you probably wouldn't want the iterable to yield full paths as keys: each structure, including the top-most one, should probably have original keys, such that when converted to an array with e.g. So, currently the only problem with this is that apparently, when not specifying pointers, there's never any current path/pointer to retrieve. To my mind, the relationship between pointers and the path should not exist at all: the path should be available regardless of absence or presence of any pointers, because each value in a JSON document has a path. |
By the way, here's what I mean by wrapping // For each item, yield its full path instead of just the key
return call_user_func_array(function (Items $data): Generator {
foreach ($data as $key => $value) {
$path = $data->getCurrentJsonPointer();
// Path may be empty if all-matching pointer '' was specified
// https://github.com/halaxa/json-machine/issues/105
$newKey = !empty($path)
// Remove root prefix to make working with key easier, especially if top-level
? ltrim($path, '/')
: $key;
yield $newKey => $value;
}
}, [$data]); |
Tests of |
Is this still relevant, @XedinUnknown? |
Heya! It's been a while, and I don't remember what the problem was or how/whether I went around it. Reading this thread, though, gave me a thought: just because tests are passing, doesn't mean there are tests that cover what I'm talking about. Hope that helps 🙏 |
The "tests work" was a reaction to this. But if one doesn't specify a JSON pointer, the main level is iterated and thus a pointer to the current collection is the same - an empty string. The relationship is natural. |
Hi!
The Problem
Decoder::decode(StreamInterface $stream): iterable
layer is configured with JSON pointers for use with JSON Machine. In the subject case, it is configured with an all-matching empty string pointer, but could be configured with e.g. JSON pointers/items
and/total
.SelectResult implements Traversable
layer, which wraps aniterable
Items
collection, and yields certain (perhaps decorated) items from an/items
path, while also remembering the scalar value of/total
path.SelectResult
only knows that the decoded collection isiterable
at the moment; it has no awareness of theItems
type. I'd like to keep it that way. For the same reason,SelectResult
also should not care what pointers theDecoder
was configured with; only that the keys it needs are present in theiterable
.Items
in aGenerator
that will yield the value ofItems::getCurrentJsonPointer()
instead of the original key. Since the actual current (not matched) path is unique for any value in the document, this will not yield different values for the same key.SelectResult
can work on the values based on their key now, without knowing of theItems
interface. In this example, theSelectResult
can yield values from the keys that match e.g./items/(\d*)
, while memoizing the value of the/total
key. Also forward-compatible with your announced future plans to provide content as a stream without having already consumed it.getCurrentJsonPointer()
seems to always be empty. I have also used xDebug to inspect the values at runtime, and it seems that the value ofParser#currentPath
is never set.Questions
Thank you!
The text was updated successfully, but these errors were encountered: