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

Coerce collections of parseable types #1711

Merged
merged 1 commit into from
Nov 26, 2017

Conversation

dslh
Copy link
Contributor

@dslh dslh commented Nov 20, 2017

Hi @dblock and @javmorin!

This implements support for "type: Array[type that responds to .parse]", as discussed in #1700. CustomTypeCoercer is leveraged, so the type may also implement coerced? or parsed? to implement custom type-checking (or also call, which I admit seems a bit weird in this instance).

Supporting type: Array[Virtus::Attribute subclass] should also be possible but I think it needs some different plumbing to make it work. From the discussion it wasn't clear to me that there's anyone who is specifically looking for it, or if this PR would make it redundant. Let me know if there's any interest and I can take a look at it.

Supports Set declarations as well as Array. Hash didn't make much sense to me in this context but if anyone has a usage example in mind I could take a look.

A couple more tests could be appropriate, I'll also get the readme and changelog filled in when I can, but now seemed like a good time to get get some feedback. Is there anything that could be changed or added?

# @return [Array,Set] the coerced result. May be an +Array+ or a
# +Set+ depending on the setting given to the constructor
def call(value)
coerced = value.map { |item| super(item) }
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'm aware this will fail if value is a string but I wasn't sure of the best thing to do about it. Maybe Array.wrap?

Copy link
Contributor

Choose a reason for hiding this comment

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

To allow for a single item to be passed in as such (without the array wrapping/declaration) is a convenience, but could be improper. I suppose it depends on how strict Grape wants to be.

Copy link
Member

Choose a reason for hiding this comment

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

I would deal with that scenario later with a proper spec.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Excellent work @dslh! A CHANGELOG entry / a README update and maybe squash and this is good to go, please.

# @return [Array,Set] the coerced result. May be an +Array+ or a
# +Set+ depending on the setting given to the constructor
def call(value)
coerced = value.map { |item| super(item) }
Copy link
Member

Choose a reason for hiding this comment

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

I would deal with that scenario later with a proper spec.

@dslh dslh force-pushed the custom-type-array-coercion branch from 0d8be56 to cae9274 Compare November 24, 2017 15:43
@dslh dslh force-pushed the custom-type-array-coercion branch from cae9274 to 3ed8bbe Compare November 24, 2017 16:17
@dslh
Copy link
Contributor Author

dslh commented Nov 24, 2017

Readme and changelog are updated. It didn't seem like there was much to be said about the functionality added in this PR; once you have custom types, wrapping them in an array or a set is something that should "just work". But I did notice that the existence of the parsed? method was undocumented so I added a note about that and also a quick test.

@dblock
Copy link
Member

dblock commented Nov 26, 2017

Perfect.

@dblock dblock merged commit 1541bd7 into ruby-grape:master Nov 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants