Skip to content

Commit 1c425e6

Browse files
committed
Revert "Autoload Types (ruby-grape#2209)"
This reverts commit 224402f.
1 parent 05d4410 commit 1c425e6

File tree

8 files changed

+125
-120
lines changed

8 files changed

+125
-120
lines changed

lib/grape.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ module Grape
4545
autoload :Env, 'grape/util/env'
4646
autoload :Json, 'grape/util/json'
4747
autoload :Xml, 'grape/util/xml'
48-
autoload :DryTypes
4948
end
5049

5150
module Http
@@ -223,24 +222,6 @@ module ServeStream
223222
module Validations
224223
extend ::ActiveSupport::Autoload
225224

226-
module Types
227-
extend ::ActiveSupport::Autoload
228-
229-
eager_autoload do
230-
autoload :InvalidValue
231-
autoload :File
232-
autoload :Json
233-
autoload :DryTypeCoercer
234-
autoload :ArrayCoercer
235-
autoload :SetCoercer
236-
autoload :PrimitiveCoercer
237-
autoload :CustomTypeCoercer
238-
autoload :CustomTypeCollectionCoercer
239-
autoload :MultipleTypeCoercer
240-
autoload :VariantCollectionCoercer
241-
end
242-
end
243-
244225
eager_autoload do
245226
autoload :AttributesIterator
246227
autoload :MultipleAttributesIterator

lib/grape/dry_types.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/grape/validations/types.rb

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# frozen_string_literal: true
22

3+
require_relative 'types/build_coercer'
4+
require_relative 'types/custom_type_coercer'
5+
require_relative 'types/custom_type_collection_coercer'
6+
require_relative 'types/multiple_type_coercer'
7+
require_relative 'types/variant_collection_coercer'
8+
require_relative 'types/json'
9+
require_relative 'types/file'
10+
require_relative 'types/invalid_value'
11+
312
module Grape
413
module Validations
514
# Module for code related to grape's system for
@@ -134,89 +143,6 @@ def self.collection_of_custom?(type)
134143
def self.map_special(type)
135144
SPECIAL.fetch(type, type)
136145
end
137-
138-
# Chooses the best coercer for the given type. For example, if the type
139-
# is Integer, it will return a coercer which will be able to coerce a value
140-
# to the integer.
141-
#
142-
# There are a few very special coercers which might be returned.
143-
#
144-
# +Grape::Types::MultipleTypeCoercer+ is a coercer which is returned when
145-
# the given type implies values in an array with different types.
146-
# For example, +[Integer, String]+ allows integer and string values in
147-
# an array.
148-
#
149-
# +Grape::Types::CustomTypeCoercer+ is a coercer which is returned when
150-
# a method is specified by a user with +coerce_with+ option or the user
151-
# specifies a custom type which implements requirments of
152-
# +Grape::Types::CustomTypeCoercer+.
153-
#
154-
# +Grape::Types::CustomTypeCollectionCoercer+ is a very similar to the
155-
# previous one, but it expects an array or set of values having a custom
156-
# type implemented by the user.
157-
#
158-
# There is also a group of custom types implemented by Grape, check
159-
# +Grape::Validations::Types::SPECIAL+ to get the full list.
160-
#
161-
# @param type [Class] the type to which input strings
162-
# should be coerced
163-
# @param method [Class,#call] the coercion method to use
164-
# @return [Object] object to be used
165-
# for coercion and type validation
166-
def self.build_coercer(type, method: nil, strict: false)
167-
cache_instance(type, method, strict) do
168-
create_coercer_instance(type, method, strict)
169-
end
170-
end
171-
172-
def self.create_coercer_instance(type, method, strict)
173-
# Maps a custom type provided by Grape, it doesn't map types wrapped by collections!!!
174-
type = Types.map_special(type)
175-
176-
# Use a special coercer for multiply-typed parameters.
177-
if Types.multiple?(type)
178-
MultipleTypeCoercer.new(type, method)
179-
180-
# Use a special coercer for custom types and coercion methods.
181-
elsif method || Types.custom?(type)
182-
CustomTypeCoercer.new(type, method)
183-
184-
# Special coercer for collections of types that implement a parse method.
185-
# CustomTypeCoercer (above) already handles such types when an explicit coercion
186-
# method is supplied.
187-
elsif Types.collection_of_custom?(type)
188-
Types::CustomTypeCollectionCoercer.new(
189-
Types.map_special(type.first), type.is_a?(Set)
190-
)
191-
else
192-
DryTypeCoercer.coercer_instance_for(type, strict)
193-
end
194-
end
195-
196-
def self.cache_instance(type, method, strict, &_block)
197-
key = cache_key(type, method, strict)
198-
199-
return @__cache[key] if @__cache.key?(key)
200-
201-
instance = yield
202-
203-
@__cache_write_lock.synchronize do
204-
@__cache[key] = instance
205-
end
206-
207-
instance
208-
end
209-
210-
def self.cache_key(type, method, strict)
211-
[type, method, strict].each_with_object(+'_') do |val, memo|
212-
next if val.nil?
213-
214-
memo << '_' << val.to_s
215-
end
216-
end
217-
218-
instance_variable_set(:@__cache, {})
219-
instance_variable_set(:@__cache_write_lock, Mutex.new)
220146
end
221147
end
222148
end

lib/grape/validations/types/array_coercer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative 'dry_type_coercer'
4+
35
module Grape
46
module Validations
57
module Types
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'array_coercer'
4+
require_relative 'set_coercer'
5+
require_relative 'primitive_coercer'
6+
7+
module Grape
8+
module Validations
9+
module Types
10+
# Chooses the best coercer for the given type. For example, if the type
11+
# is Integer, it will return a coercer which will be able to coerce a value
12+
# to the integer.
13+
#
14+
# There are a few very special coercers which might be returned.
15+
#
16+
# +Grape::Types::MultipleTypeCoercer+ is a coercer which is returned when
17+
# the given type implies values in an array with different types.
18+
# For example, +[Integer, String]+ allows integer and string values in
19+
# an array.
20+
#
21+
# +Grape::Types::CustomTypeCoercer+ is a coercer which is returned when
22+
# a method is specified by a user with +coerce_with+ option or the user
23+
# specifies a custom type which implements requirments of
24+
# +Grape::Types::CustomTypeCoercer+.
25+
#
26+
# +Grape::Types::CustomTypeCollectionCoercer+ is a very similar to the
27+
# previous one, but it expects an array or set of values having a custom
28+
# type implemented by the user.
29+
#
30+
# There is also a group of custom types implemented by Grape, check
31+
# +Grape::Validations::Types::SPECIAL+ to get the full list.
32+
#
33+
# @param type [Class] the type to which input strings
34+
# should be coerced
35+
# @param method [Class,#call] the coercion method to use
36+
# @return [Object] object to be used
37+
# for coercion and type validation
38+
def self.build_coercer(type, method: nil, strict: false)
39+
cache_instance(type, method, strict) do
40+
create_coercer_instance(type, method, strict)
41+
end
42+
end
43+
44+
def self.create_coercer_instance(type, method, strict)
45+
# Maps a custom type provided by Grape, it doesn't map types wrapped by collections!!!
46+
type = Types.map_special(type)
47+
48+
# Use a special coercer for multiply-typed parameters.
49+
if Types.multiple?(type)
50+
MultipleTypeCoercer.new(type, method)
51+
52+
# Use a special coercer for custom types and coercion methods.
53+
elsif method || Types.custom?(type)
54+
CustomTypeCoercer.new(type, method)
55+
56+
# Special coercer for collections of types that implement a parse method.
57+
# CustomTypeCoercer (above) already handles such types when an explicit coercion
58+
# method is supplied.
59+
elsif Types.collection_of_custom?(type)
60+
Types::CustomTypeCollectionCoercer.new(
61+
Types.map_special(type.first), type.is_a?(Set)
62+
)
63+
else
64+
DryTypeCoercer.coercer_instance_for(type, strict)
65+
end
66+
end
67+
68+
def self.cache_instance(type, method, strict, &_block)
69+
key = cache_key(type, method, strict)
70+
71+
return @__cache[key] if @__cache.key?(key)
72+
73+
instance = yield
74+
75+
@__cache_write_lock.synchronize do
76+
@__cache[key] = instance
77+
end
78+
79+
instance
80+
end
81+
82+
def self.cache_key(type, method, strict)
83+
[type, method, strict].each_with_object(+'_') do |val, memo|
84+
next if val.nil?
85+
86+
memo << '_' << val.to_s
87+
end
88+
end
89+
90+
instance_variable_set(:@__cache, {})
91+
instance_variable_set(:@__cache_write_lock, Mutex.new)
92+
end
93+
end
94+
end

lib/grape/validations/types/dry_type_coercer.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# frozen_string_literal: true
22

3+
require 'dry-types'
4+
5+
module DryTypes
6+
# Call +Dry.Types()+ to add all registered types to +DryTypes+ which is
7+
# a container in this case. Check documentation for more information
8+
# https://dry-rb.org/gems/dry-types/1.2/getting-started/
9+
include Dry.Types()
10+
end
11+
312
module Grape
413
module Validations
514
module Types
@@ -43,7 +52,7 @@ def collection_coercers
4352
def initialize(type, strict = false)
4453
@type = type
4554
@strict = strict
46-
@scope = strict ? Grape::DryTypes::Strict : Grape::DryTypes::Params
55+
@scope = strict ? DryTypes::Strict : DryTypes::Params
4756
end
4857

4958
# Coerces the given value to a type which was specified during

lib/grape/validations/types/primitive_coercer.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative 'dry_type_coercer'
4+
35
module Grape
46
module Validations
57
module Types
@@ -8,16 +10,16 @@ module Types
810
# that it has the proper type.
911
class PrimitiveCoercer < DryTypeCoercer
1012
MAPPING = {
11-
Grape::API::Boolean => Grape::DryTypes::Params::Bool,
12-
BigDecimal => Grape::DryTypes::Params::Decimal,
13+
Grape::API::Boolean => DryTypes::Params::Bool,
14+
BigDecimal => DryTypes::Params::Decimal,
1315

1416
# unfortunately, a +Params+ scope doesn't contain String
15-
String => Grape::DryTypes::Coercible::String
17+
String => DryTypes::Coercible::String
1618
}.freeze
1719

1820
STRICT_MAPPING = {
19-
Grape::API::Boolean => Grape::DryTypes::Strict::Bool,
20-
BigDecimal => Grape::DryTypes::Strict::Decimal
21+
Grape::API::Boolean => DryTypes::Strict::Bool,
22+
BigDecimal => DryTypes::Strict::Decimal
2123
}.freeze
2224

2325
def initialize(type, strict = false)

lib/grape/validations/types/set_coercer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
require 'set'
4+
require_relative 'array_coercer'
5+
36
module Grape
47
module Validations
58
module Types

0 commit comments

Comments
 (0)