Skip to content
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

JSON: Change properties key to _properties_ to reduce chance of conflict #5180

Merged
merged 1 commit into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spec/std/json/mapping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ private class JSONWithNilableTimeEmittingNull
end
end

private class JSONWithPropertiesKey
JSON.mapping(
properties: Hash(String, String),
)
end

private class JSONWithSimpleMapping
JSON.mapping({name: String, age: Int32})
end
Expand Down Expand Up @@ -252,6 +258,14 @@ describe "JSON mapping" do
json.to_json.should eq(%({"value":null}))
end

it "outputs JSON with properties key" do
input = {
properties: {"foo" => "bar"},
}.to_json
json = JSONWithPropertiesKey.from_json(input)
json.to_json.should eq(input)
end

it "parses json with keywords" do
json = JSONWithKeywordsMapping.from_json(%({"end": 1, "abstract": 2}))
json.end.should eq(1)
Expand Down
24 changes: 12 additions & 12 deletions src/json/mapping.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ module JSON
# If *strict* is `true`, unknown properties in the JSON
# document will raise a parse exception. The default is `false`, so unknown properties
# are silently ignored.
macro mapping(properties, strict = false)
{% for key, value in properties %}
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
macro mapping(_properties_, strict = false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does %properties work for macro arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, syntax error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wouldn't make sense, because the macro argument properties is a macro-level variable, whereas %-prefixed variables are local variables in the generated code. This are two different levels.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it obviously doesn't work, but it is unfortunate that it is impossible to avoid collisions.

{% for key, value in _properties_ %}
{% _properties_[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
@{{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}

{% if value[:setter] == nil ? true : value[:setter] %}
Expand All @@ -90,7 +90,7 @@ module JSON
{% end %}

def initialize(%pull : ::JSON::PullParser)
{% for key, value in properties %}
{% for key, value in _properties_ %}
%var{key.id} = nil
%found{key.id} = false
{% end %}
Expand All @@ -101,7 +101,7 @@ module JSON
%key_location = %pull.location
key = %pull.read_object_key
case key
{% for key, value in properties %}
{% for key, value in _properties_ %}
when {{value[:key] || key.id.stringify}}
%found{key.id} = true

Expand Down Expand Up @@ -137,15 +137,15 @@ module JSON
end
%pull.read_next

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key.id}.nil? && !%found{key.id} && !::Union({{value[:type]}}).nilable?
raise ::JSON::ParseException.new("Missing json attribute: {{(value[:key] || key).id}}", *%location)
end
{% end %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% if value[:nilable] %}
{% if value[:default] != nil %}
@{{key.id}} = %found{key.id} ? %var{key.id} : {{value[:default]}}
Expand All @@ -159,7 +159,7 @@ module JSON
{% end %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% if value[:presence] %}
@{{key.id}}_present = %found{key.id}
{% end %}
Expand All @@ -168,7 +168,7 @@ module JSON

def to_json(json : ::JSON::Builder)
json.object do
{% for key, value in properties %}
{% for key, value in _properties_ %}
_{{key.id}} = @{{key.id}}

{% unless value[:emit_null] %}
Expand Down Expand Up @@ -216,7 +216,7 @@ module JSON

# This is a convenience method to allow invoking `JSON.mapping`
# with named arguments instead of with a hash/named-tuple literal.
macro mapping(**properties)
::JSON.mapping({{properties}})
macro mapping(**_properties_)
::JSON.mapping({{_properties_}})
end
end