Added support for mapping YAML nodes not specified in properties#4411
Added support for mapping YAML nodes not specified in properties#4411aaronbronow wants to merge 9 commits intocrystal-lang:masterfrom
Conversation
|
To Do:
|
|
Question: If |
I think you should check for this case in the beginning of the macro, and raise a custom error if it happens. {% if extra && properties.keys.includes? extra.id %}
{% raise "The name '#{extra.id}' for extra is already used" %}
{% end %} |
|
No automated test for this? |
|
@dmitryrck it's on his todolist (#4411 (comment)) |
I implemented this suggestion and now it's a macro exception. I don't know how to write a spec for a macro exception so there's no test for that currently. |
src/yaml/mapping.cr
Outdated
| {% end %} | ||
|
|
||
| {% if extra %} | ||
| {{extra.id}} = Hash(String, ::YAML::Any).new |
There was a problem hiding this comment.
Why not use directly @{{extra.id}} (and remove the @{{extra.id}} = {{extra.id}} at the end of the macro) ?
|
Question: why create a temporary variable here instead of work with |
| end | ||
| {% end %} | ||
| {% if extra %} | ||
| _{{extra.id}} = @{{extra.id}} |
There was a problem hiding this comment.
I'd suggest using % fresh variables.
There was a problem hiding this comment.
Ok, but why not just call .to_yaml on the @ instance variable?
There was a problem hiding this comment.
if @{{extra.id}} exists, then it is not nillable so you could call @{{extra.id}}.to_yaml(... directly. For memory footprint I would suggest to let it be lazy initialized / nilable. But for the use case, if the user expect to have some extras, maybe is good enough to eagerly create a and instance for @{{extra.id}}.
There was a problem hiding this comment.
In this implementation extra is nil unless the user has set it and passed it in as an argument to the mapping(...) macro. Because the macro checks for existence of extra before emitting code, @{{extra.id}} won't exist at all if the user is not using extras. If extra does exist then @{{extra.id}} will always get initialized as a Hash(String, ::YAML::Any)
If I'm understanding this correctly, the only problem with trying to call @{{extra.id}}.to_yaml(... would be if @{{extra.id}} is nil at the time of executing this block and that's impossible. I hope I'm understanding correctly 😄
|
@aaronbronow Looks like #5007 will be merged soon. Please review your pull request to meet this breaking change. |
|
Hm, i think this was already merged. Any chance to merge it before next release, at least for json? |
|
This can probably be closed due to |
This change is intended to provide access to YAML nodes not specified in YAML.mapping by setting them in a node named by the user.
It can be used with this code: