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

Add to_h and to_json to Granite::ORM::Fields #72

Merged
merged 3 commits into from
Oct 17, 2017

Conversation

migeorge
Copy link
Contributor

This PR should satisfy the agreed upon resolution of #50

It adds a to_h and to_json method to Granite ORM Models.

@migeorge
Copy link
Contributor Author

Let me know if you guys see any areas to improve the to_h method implementation. I would have used DB::Any as the type for the hash values but I had to exclude Slice(UInt8) in order for the to_json call to work.

I also didn't want to cast all the values to String since numeric values and booleans are perfectly valid in JSON.

Copy link
Member

@elorest elorest left a comment

Choose a reason for hiding this comment

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

Good to me. What do you think @drujensen


it "should provide a to_h method" do
t = Todo.new(name: "test todo", priority: 20)
result = { "name" => "test todo", "priority": 20 }
Copy link
Contributor

@veelenga veelenga Oct 17, 2017

Choose a reason for hiding this comment

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

This is interesting:

{ "name" => "test todo", "priority": 20 }.class # => Hash(String, Int32 | String)
{ "priority": 20 }.class # => NamedTuple(priority: Int32)
{ "priority": 20, "name" => "test todo" }.class # space not allowed between named argument name and ':'

I don't want to be pedantic, but the first case looks like a bug in Crystal or at least it is not documented. Personally, I would prefer to use here official way for hashes to avoid problems in future:

{ "name" => "test todo", "priority" => 20 }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@veelenga My ruby background seems to have seeped in here pushing a fix shortly.


if updated_at
fields["updated_at"] = updated_at.not_nil!.to_s("%F %X")
end
Copy link
Contributor

Choose a reason for hiding this comment

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

All fields with nil values will be present in a hash, but created_at and updated_at will not. In this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@veelenga Good point I'll allow the nil values

Copy link
Member

@drujensen drujensen left a comment

Choose a reason for hiding this comment

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

lgtm with @veelenga requested changes. Thanks for explaining why we didn't use DB::ANY.


it "should provide a to_json method" do
t = Todo.new(name: "test todo", priority: 20)
result = "{\"name\":\"test todo\",\"priority\":20}"
Copy link
Contributor

Choose a reason for hiding this comment

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

How about %({"name":"test todo","priority":20})?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this better than escaping all the quotes

@migeorge
Copy link
Contributor Author

I've made the changes on my branch in migeorge/granite-orm but they don't seem to be propagating here. Anyone experienced this before?

{% end %}
{% if SETTINGS[:timestamps] %}
fields["created_at"] = created_at ? created_at.not_nil!.to_s("%F %X") : ""
fields["updated_at"] = updated_at ? updated_at.not_nil!.to_s("%F %X") : ""
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

but why "" instead of nil ?

fields["created_at"] = created_at.try &.to_s("%F %X")
fields["updated_at"] = updated_at.try &.to_s("%F %X")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@veelenga That makes more sense, fixed.

@migeorge
Copy link
Contributor Author

This should be ready to go unless anyone has more feedback

@drujensen drujensen merged commit 4629249 into amberframework:master Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants