-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Replace Virtus with dry-types #1920
Merged
dblock
merged 1 commit into
ruby-grape:master
from
dnesteryuk:chore/bye-virstus-hello-dry-types
Dec 2, 2019
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 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 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 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 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 |
---|---|---|
@@ -1,6 +1,49 @@ | ||
Upgrading Grape | ||
=============== | ||
|
||
### Upgrading to >= 1.3.0 | ||
|
||
#### Ruby | ||
|
||
After adding dry-types, Ruby 2.4 or newer is required. | ||
|
||
#### Coercion | ||
|
||
[Virtus](https://github.com/solnic/virtus) has been replaced by [dry-types](https://dry-rb.org/gems/dry-types/1.2/) for parameter coercion. If your project depends on Virtus, explicitly add it to your `Gemfile`. Also, if Virtus is used for defining custom types | ||
|
||
```ruby | ||
class User | ||
include Virtus.model | ||
|
||
attribute :id, Integer | ||
attribute :name, String | ||
end | ||
|
||
# somewhere in your API | ||
params do | ||
requires :user, type: User | ||
end | ||
``` | ||
|
||
Add a class-level `parse` method to the model: | ||
|
||
```ruby | ||
class User | ||
include Virtus.model | ||
|
||
attribute :id, Integer | ||
attribute :name, String | ||
|
||
def self.parse(attrs) | ||
new(attrs) | ||
end | ||
end | ||
``` | ||
|
||
Custom types which don't depend on Virtus don't require any changes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a "For more information see ..." and link to this PR. |
||
|
||
For more information see [#1920](https://github.com/ruby-grape/grape/pull/1920). | ||
|
||
### Upgrading to >= 1.2.4 | ||
|
||
#### Headers in `error!` call | ||
|
This file contains 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 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 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 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 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 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,54 @@ | ||
require_relative 'dry_type_coercer' | ||
|
||
module Grape | ||
module Validations | ||
module Types | ||
# Coerces elements in an array. It might be an array of strings or integers or | ||
# anything else. | ||
# | ||
# It could've been possible to use an +of+ | ||
# method (https://dry-rb.org/gems/dry-types/1.2/array-with-member/) | ||
# provided by dry-types. Unfortunately, it doesn't work for Grape because of | ||
# behavior of Virtus which was used earlier, a `Grape::Validations::Types::PrimitiveCoercer` | ||
# maintains Virtus behavior in coercing. | ||
class ArrayCoercer < DryTypeCoercer | ||
def initialize(type, strict = false) | ||
super | ||
|
||
@coercer = scope::Array | ||
@elem_coercer = PrimitiveCoercer.new(type.first, strict) | ||
end | ||
|
||
def call(_val) | ||
collection = super | ||
|
||
return collection if collection.is_a?(InvalidValue) | ||
|
||
coerce_elements collection | ||
end | ||
|
||
protected | ||
|
||
def coerce_elements(collection) | ||
collection.each_with_index do |elem, index| | ||
return InvalidValue.new if reject?(elem) | ||
|
||
coerced_elem = @elem_coercer.call(elem) | ||
|
||
return coerced_elem if coerced_elem.is_a?(InvalidValue) | ||
|
||
collection[index] = coerced_elem | ||
end | ||
|
||
collection | ||
end | ||
|
||
# This method maintaine logic which was defined by Virtus for arrays. | ||
# Virtus doesn't allow nil in arrays. | ||
def reject?(val) | ||
val.nil? | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
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 think we want to explain how to rewrite a custom type with dry-types here, first. If I am upgrading I'd rather not use Virtus unless I can't.
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.
Dry-types cannot be used in place of Virtus for custom types, dry-schema might be. So, if users need something like Virtus they need to keep it or migrate to dry-schema. I guess it is up to them to decide, the goal is to avoid breaking changes, so if they use Virtus, they can still use it until they have time to migrate.
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.
It sounds like you're saying that if you want a custom type you have to use Virtus? Or are you saying you can use Virtus? Either way let's make this very clear in the upgrading/readme doc.
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.
No, Virtus isn't required for custom types. As it was before, any class implementing the
parse
method might be used. 🙂 I've added an additional sentence to the upgrading doc.