-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Replace hashie mash with hash #1594
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
Changes from 19 commits
a908eb7
1afdc91
162f28c
57f4545
1787cb7
71052c8
790443b
d9e8ed0
bbb88e3
eabe213
2a21427
e54e2d0
2201d15
85e02c7
d7c1daa
2491071
f4aeb9d
dc07f38
6c2d614
abae16c
247869b
6e5964b
2ddaa42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -509,7 +509,7 @@ post 'users/signup' do | |
end | ||
```` | ||
|
||
If we do not specify any params, `declared` will return an empty `Hashie::Mash` instance. | ||
If we do not specify any params, `declared` will return an empty `Hash`. | ||
|
||
**Request** | ||
|
||
|
@@ -562,10 +562,10 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d | |
} | ||
```` | ||
|
||
The returned hash is a `Hashie::Mash` instance, allowing you to access parameters via dot notation: | ||
The returned hash is a `Hash`. | ||
|
||
```ruby | ||
declared(params).user == declared(params)['user'] | ||
declared(params)[:user] == declared(params)['user'] | ||
``` | ||
|
||
|
||
|
@@ -905,11 +905,13 @@ params do | |
requires :avatar, type: File | ||
end | ||
post '/' do | ||
# Parameter will be wrapped using Hashie: | ||
params.avatar.filename # => 'avatar.png' | ||
params.avatar.type # => 'image/png' | ||
params.avatar.tempfile # => #<File> | ||
# Parameter will be a plain Ruby `Hash`: | ||
params[:avatar][:filename] # => 'avatar.png' | ||
params[:avatar]['avatar'] # => 'image/png' | ||
params[:avatar][:tempfile] # => #<File> | ||
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. The old code works here with |
||
end | ||
|
||
`params` hash keys will all be converted to symbols. | ||
``` | ||
|
||
### First-Class `JSON` Types | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Grape | ||
module Extensions | ||
class DeepMergeableHash < ::Hash | ||
def deep_merge!(other_hash) | ||
other_hash.each_pair do |current_key, other_value| | ||
this_value = self[current_key] | ||
|
||
self[current_key] = if this_value.is_a?(::Hash) && other_value.is_a?(::Hash) | ||
this_value.deep_merge(other_value) | ||
else | ||
other_value | ||
end | ||
end | ||
|
||
self | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Grape | ||
module Extensions | ||
module DeepSymbolizeHash | ||
def self.deep_symbolize_keys_in(object) | ||
case object | ||
when ::Hash | ||
object.each_with_object({}) do |(key, value), new_hash| | ||
new_hash[symbolize_key(key)] = deep_symbolize_keys_in(value) | ||
end | ||
when ::Array | ||
object.map { |element| deep_symbolize_keys_in(element) } | ||
else | ||
object | ||
end | ||
end | ||
|
||
def self.symbolize_key(key) | ||
key.respond_to?(:to_sym) ? key.to_sym : key | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require_relative 'deep_mergeable_hash' | ||
require_relative 'deep_symbolize_hash' | ||
module Grape | ||
module Extensions | ||
module Hash | ||
module ParamBuilder | ||
def build_params | ||
params = Grape::Extensions::DeepMergeableHash[rack_params] | ||
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS] | ||
post_process_params(params) | ||
end | ||
|
||
def post_process_params(params) | ||
Grape::Extensions::DeepSymbolizeHash.deep_symbolize_keys_in(params) | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Grape | ||
module Extensions | ||
module HashWithIndifferentAccess | ||
def params_builder | ||
Grape::Extensions::HashWithIndifferentAccess::ParamBuilder | ||
end | ||
|
||
module ParamBuilder | ||
def build_params | ||
params = ActiveSupport::HashWithIndifferentAccess[rack_params] | ||
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS] | ||
params | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'hashie' | ||
module Grape | ||
module Extensions | ||
module Hashie | ||
module Mash | ||
def params_builder | ||
Grape::Extensions::Hashie::Mash::ParamBuilder | ||
end | ||
|
||
module ParamBuilder | ||
def build_params | ||
params = ::Hashie::Mash.new(rack_params) | ||
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS] | ||
params | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,7 +147,7 @@ def infer_type_check(type) | |
# Enforce symbolized keys for complex types | ||
# by wrapping the coercion method such that | ||
# any Hash objects in the immediate heirarchy | ||
# are passed through +Hashie.symbolize_keys!+. | ||
# have their keys recursively symbolized | ||
# This helps common libs such as JSON to work easily. | ||
# | ||
# @param type see #new | ||
|
@@ -162,15 +162,15 @@ def enforce_symbolized_keys(type, method) | |
lambda do |val| | ||
method.call(val).tap do |new_value| | ||
new_value.each do |item| | ||
Hashie.symbolize_keys!(item) if item.is_a? Hash | ||
item.with_indifferent_access if item.is_a? Hash | ||
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. @dblock This slipped through the net. From the description above this method is supposed to wrap the result of the method and transform any Hash to be symbolized, that's fine, I can update. There is no direct test of this method, but reading the code it looks to me like the block above should not be iterating with #each but #map to construct a new Array or Set; method.call(val).tap do |new_value|
new_value.map do |item|
item.is_a?(Hash) ? symbolize_keys(item) : item
end
end Correct? I can easily update this to symbolize the Hash without using the ActiveSupport method, but wanted to check the existing method is actually doing what's expected. 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. It's updating the values in place it looks like. If removing it doesn't cause any specs to fail I would start by writing a test that hits it, then change the behavior. |
||
end | ||
end | ||
end | ||
|
||
# Hash objects are processed directly | ||
elsif type == Hash | ||
lambda do |val| | ||
Hashie.symbolize_keys! method.call(val) | ||
method.call(val).with_indifferent_access | ||
end | ||
|
||
# Simple types are not processed. | ||
|
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.
This should still work, no?
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's a HashWithIndifferentAccess so I wouldn't expect to be able to access :user as a method only with a key.