-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Coerce collections of parseable types #1711
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
lib/grape/validations/types/custom_type_collection_coercer.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Grape | ||
module Validations | ||
module Types | ||
# Instances of this class may be passed to | ||
# +Virtus::Attribute.build+ as the +:coercer+ | ||
# option, to handle collections of types that | ||
# provide their own parsing (and optionally, | ||
# type-checking) functionality. | ||
# | ||
# See {CustomTypeCoercer} for details on types | ||
# that will be supported by this by this coercer. | ||
# This coercer works in the same way as +CustomTypeCoercer+ | ||
# except that it expects to receive an array of strings to | ||
# coerce and will return an array (or optionally, a set) | ||
# of coerced values. | ||
# | ||
# +CustomTypeCoercer+ is already capable of providing type | ||
# checking for arrays where an independent coercion method | ||
# is supplied. As such, +CustomTypeCollectionCoercer+ does | ||
# not allow for such a method to be supplied independently | ||
# of the type. | ||
class CustomTypeCollectionCoercer < CustomTypeCoercer | ||
# A new coercer for collections of the given type. | ||
# | ||
# @param type [Class,#parse] | ||
# type to which items in the array should be coerced. | ||
# Must implement a +parse+ method which accepts a string, | ||
# and for the purposes of type-checking it may either be | ||
# a class, or it may implement a +coerced?+, +parsed?+ or | ||
# +call+ method (in that order of precedence) which | ||
# accepts a single argument and returns true if the given | ||
# array item has been coerced correctly. | ||
# @param set [Boolean] | ||
# when true, a +Set+ will be returned by {#call} instead | ||
# of an +Array+ and duplicate items will be discarded. | ||
def initialize(type, set = false) | ||
super(type) | ||
@set = set | ||
end | ||
|
||
# This method is called from somewhere within | ||
# +Virtus::Attribute::coerce+ in order to coerce | ||
# the given value. | ||
# | ||
# @param value [Array<String>] an array of values to be coerced | ||
# @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) } | ||
|
||
@set ? Set.new(coerced) : coerced | ||
end | ||
|
||
# This method is called from somewhere within | ||
# +Virtus::Attribute::value_coerced?+ in order to assert | ||
# that the all of the values in the array have been coerced | ||
# successfully. | ||
# | ||
# @param primitive [Axiom::Types::Type] primitive type for | ||
# the coercion as deteced by axiom-types' inference system. | ||
# @param value [Enumerable] a coerced result returned from {#call} | ||
# @return [true,false] whether or not all of the coerced values in | ||
# the collection satisfy type requirements. | ||
def success?(primitive, value) | ||
value.is_a?(@set ? Set : Array) && | ||
value.all? { |item| super(primitive, item) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm aware this will fail if
value
is a string but I wasn't sure of the best thing to do about it. MaybeArray.wrap
?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.
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.
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 would deal with that scenario later with a proper spec.