-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Config] Declarative Syntax for Config Tree Builder #35127
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 about:
But keeping the |
The use of array in "Declarative Syntax" is a tempting but very dangerous way. IDE cannot help you and a larger configuration tree is difficult to maintain. The use of the configure method with lambda parameter is much better approach if proper typehints are provided. IDE can assist you, it is easy to split the definition into multiple methods to reduce nesting levels in the source code, and PHP checks the syntax on compilation. |
Some inspiration from @nette https://github.com/nette/schema#defining-schema Used in neon (~= YAML) like this: https://github.com/phpstan/phpstan-src/blob/35047b54c1d2435fe2e16488bf5a5164f313e123/conf/config.neon#L106-L192 |
I'm pretty sure this can be done independently from the component, by implementing |
Another potential inspiration is JSON Schema. Just like XML Schema, it is a JSON document that defines allowed structure of JSON documents. |
@TomasVotruba The static builder seems like a good idea. However, I wonder what are the benefits in comparison with an instantiated builder, i.e., like |
It's rather about idea, not static/normal. |
Sorry, something went wrong.
@nicolas-grekas I definitely agree this can be a standalone package. I'm not fully sure where the LoaderInterface comes into play because this is for configuring a TreeBuilder in the Configuration. But your input is very appreciated.
This still feels fairly verbose when compared to the declarative syntax. But removing the end for sure would help with usability, but I believe it could make the implementation much more difficult. @TomasVotruba Ah, that nette package looks very neat, and definitely the same vibes that I'm going for. My gut tells me that if this has any chance of ever making it into the core one day, then I'd likely need to ensure that i'm not using any external packages. @jkufner There still is decent IDE support with the declarative syntax. It's not like Laravel validations where everything is string based. The only piece that is string based are the dictionary keys, but those are currently also defined as strings in the current tree builder as well. Also, when it comes to static method calls vs global methods vs a builder object, I'm not sure any of those have any real benefits over the others, so whichever is least verbose and maximized discovery is what I think we should choose. If you think about it, this isn't much different than symfony's validation api. You compose validations that are apart of the Validations namespace and then validate data against the one validation. The only real difference is that I'm creating global helpers to make the code a bit more terse. |
Agreed. It's just for inspiration, aka copy-paste, not to use it as a dependency |
Oomph, this took a bit for me to get around to this, but just built out this library to address the above: https://github.com/krakphp/schema Would appreciate some feedback, it's in a very early state. |
@ragboyjr note that in all your examples, your usage of |
hmm, it’s interesting though because php arrays really can only be a list or dict. I’m not sure known keys is the only difference of strict vs dict. I feel like fixed keys is more the deciding difference. Im not validating objects with fixed keys but rather arrays that act as dicts (or maps). But either way, I feel dicts and structs are also fairly similar in their nature of key value pairs, so using dict or struct would have been equally understandable from my pov. Honest question: what initially were you thinking dict was doing before you made the connection that it was like struct? |
for the processing of the config, that makes a big difference. That's why the config component can configure an array node either with |
ah, ok, this makes more sense where you're coming from. I took children vs prototype to be dict vs list, but the reality of it is that it's really struct vs dict because the config module doesn't care if you use int based keys or string based keys. Very helpful, will update, thanks for the feedback. I'll probably just introduce the struct keyword, but leave dict where struct = heterogenous values +fixed keys, dict = homogenous values, + unfixed keys, list = homogenous values, index based keys. |
|
And the Config component cares about lists vs maps when merging config from several files (forgetting |
Thank you for this suggestion. |
Friendly ping? Should this still be open? I will close if I don't hear anything. |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
Uh oh!
There was an error while loading. Please reload this page.
Description
Defining configuration trees can be a bit cumbersome and syntactically verbose, and in some non-intuitive for some combinations of configuration.
Let's look a seemingly simple configuration file:
To define this with the current tree builder syntax, it would look like:
Current Syntax
Syntax aside, I still find myself struggling to write the proper code for validation (even with an IDE) because it's very easy to miss an
end()
call which then messes up downstream definitions, and this can cascade if you have large config trees.I think this is a great example where using a more declarative composition-friendly interface would make declaring configuration schemas easier to maintain, learn, and understand.
Declarative Syntax
IMHO, this syntax is much easier to read and understand the structure of what we are configuring and is less error prone to missing an
end
statement.It also is pretty easy to allow access to the actual node definitions via the
configure
method like:Rough Implementation
The following code is a working implementation for this declarative syntax, the formatting is rough, but if this RFC is received well, I could clean this up into a pull request.
Related Merge Requests
The text was updated successfully, but these errors were encountered: