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

Include type restriction on getters in JSON/mapping #5935

Merged
merged 5 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions spec/std/json/mapping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,14 @@ describe "JSON mapping" do
json.bar?.should be_false
end

it "defines query getter with class restriction" do
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure it makes sense to have a spec for this if there is no real reflection. The return type is optional anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It being optional doesn't mean we shouldn't be testing the optional case of supplying it.

{% begin %}
{% methods = JSONWithQueryAttributes.methods %}
{{ methods.find(&.name.==("foo?")).return_type }}.should eq(Bool)
{{ methods.find(&.name.==("bar?")).return_type }}.should eq(Bool)
{% end %}
end

it "defines non-query setter and presence methods" do
json = JSONWithQueryAttributes.from_json(%({"foo": false}))
json.bar_present?.should be_false
Expand Down
2 changes: 1 addition & 1 deletion src/json/mapping.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module JSON
{% end %}

{% if value[:getter] == nil ? true : value[:getter] %}
def {{key.id}}
def {{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}
Copy link
Contributor

Choose a reason for hiding this comment

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

? should come directly after the type (Bool? not Bool ?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is exactly as it is in the setter above. Should both be changed?

Copy link
Member

Choose a reason for hiding this comment

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

It parses with whitespace, but it would be better to remove it.

@{{value[:key_id]}}
end
{% end %}
Expand Down