-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Yaml] Add custom tags support #21185
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
Comments
What would be the resulting PHP structure ? |
Nothing's finished but I began this.
|
Would it be possible to have a much smaller diff, to only add code for supporting first level tags, |
The idea looks interesting and might help implementing #21071. I am just not sure how we should best implement it to allow some flexibility which will probably be needed. |
Note that #21071 is ready and shouldn't be waiting for this one. But of course, it'd be great to leverage tags and use them instead of special keys when it's ready. |
@nicolas-grekas and hardcode in the parser the defaults ?
I don't think that's worth it, having it as arguments is enough imo.
@xabbuh indeed, you can still take a look at other libraries and see if their implementation inspires you ( Note that my current implementation doesn't support dynamic tags name, I don't think this is useful, so |
@nicolas-grekas I agree that it shouldn't wait. But having sort for custom tags in 3.3 would allow to update your implementation before the final release. |
Of course not, "foo" === "whatever" !== "hardcoded".
What does that mean? Looks like we're talking foreign languages :) So, back to my first question, which still needs an answer: "What would be the resulting PHP structure ?", where "resulting" means "output from the parser" (and not "internal structure"). Let's take an example, in the context of the DI component: My personal pov is that it could be well enough to just make it be: so that eg the DI Yaml loader could then iterate over this structure and do at some point: if ($value instanceof TaggedValue) {
if ('iterator' === $value->getTag()) {
$iteratedValues = $value->getValue();
// etc. (like validation)
$resultingValue = new IteratorArgument(...);
}
} there is another possibility which could be to make Yaml able to hydrate an IteratorArgument directly, so that $output could directly be |
@nicolas-grekas did you mean having this kind of diff?
I was talking about having an interface for tags (and manage them in the parser): interface TagInterface
{
/**
* @param string $value a plain scalar
*
* @return mixed
*/
public function construct($value);
} But it seems you were talking about having a special class for the output 😛
Yaml::parse('- !iterator [...]') === array( new TaggedValue('iterator', [...]) ); It seems weird to me; core tags (eg I think it is much more powerful to pass this responsibility to the parser, you tell it to convert |
About the diff, yes!
custom vs core, that's enough a difference to me, so yes
sure, but people are already iterating over the resulting structure to just read what's inside of it, so that doesn't shock me.
responsibility = complexity. Given that we don't want to build a full hydrating system here, we can only support simple types here (similar to doctrine annotations). Which means "more powerful": maybe, but powerful enough: not sure. Thus, my personal preference is simplicity here. But that doesn't prevent you from trying your way :) |
This PR was squashed before being merged into the 3.3-dev branch (closes #21194). Discussion ---------- [Yaml] Add tags support | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | symfony/symfony#21185 | License | MIT | Doc PR | This PR adds custom tags support to the Yaml component. Symfony tags (`!!binary`, `!str`, etc.) are still managed in the parser to have a lighter diff but we'll be able to convert them later if we want to. The primary addition of this PR is the `TagInterface`: ```php interface TagInterface { public function construct(mixed $value): mixed; } ``` It can be used to register custom tags. An example that could be used to convert [the syntax `=iterator`](symfony/symfony#20907 (comment)) to a tag: ```php final class IteratorTag implements TagInterface { public function construct(mixed $value): mixed { return new IteratorArgument($value); } } $parser = new Parser(['iterator' => new IteratorTag()]); ``` If you think this is too complex, @nicolas-grekas [proposed an alternative](symfony/symfony#21185 (comment)) to my proposal externalizing this support by introducing a new class `TaggedValue`. Commits ------- 4744107 [Yaml] Add tags support
This PR was squashed before being merged into the 3.3-dev branch (closes #21194). Discussion ---------- [Yaml] Add tags support | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21185 | License | MIT | Doc PR | This PR adds custom tags support to the Yaml component. Symfony tags (`!!binary`, `!str`, etc.) are still managed in the parser to have a lighter diff but we'll be able to convert them later if we want to. The primary addition of this PR is the `TagInterface`: ```php interface TagInterface { public function construct(mixed $value): mixed; } ``` It can be used to register custom tags. An example that could be used to convert [the syntax `=iterator`](#20907 (comment)) to a tag: ```php final class IteratorTag implements TagInterface { public function construct(mixed $value): mixed { return new IteratorArgument($value); } } $parser = new Parser(['iterator' => new IteratorTag()]); ``` If you think this is too complex, @nicolas-grekas [proposed an alternative](#21185 (comment)) to my proposal externalizing this support by introducing a new class `TaggedValue`. Commits ------- 4744107 [Yaml] Add tags support
Custom tags support is a very important feature imo that currently lacks to the yaml component.
It is basically allowing things like:
By having a dedicated system for tags, we could centralize their management and avoid duplication (here and here).
If wanted, it could also be used in the core to remove reserved keys in config files.
For example,
WDYT?
The text was updated successfully, but these errors were encountered: