-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added support for mapping YAML nodes not specified in properties #4411
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
def initialize(%pull : ::YAML::PullParser) | ||
{% for key, value in properties %} | ||
%var{key.id} = nil | ||
%found{key.id} = false | ||
{% end %} | ||
|
||
{% if extra %} | ||
{{extra.id}} = Hash(String, ::YAML::Any).new |
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.
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
|
@@ -181,6 +181,14 @@ module YAML | |||
{% end %} | |||
end | |||
{% end %} | |||
{% if extra %} | |||
_{{extra.id}} = @{{extra.id}} |
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.
I'd suggest using %
fresh variables.
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.
Ok, but why not just call .to_yaml on the @ instance variable?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: