boto-rb 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/lib/boto/configuration.rb +113 -0
- data/lib/boto/endpoint/boolean.rb +16 -0
- data/lib/boto/endpoint/endpoint.rb +227 -0
- data/lib/boto/endpoint/params.rb +100 -0
- data/lib/boto/http/app.rb +283 -0
- data/lib/boto/http/error.rb +121 -0
- data/lib/boto/http/problem.rb +27 -0
- data/lib/boto/http/request_body.rb +59 -0
- data/lib/boto/http/router.rb +168 -0
- data/lib/boto/openapi/operation.rb +116 -0
- data/lib/boto/openapi/schema_catalog.rb +32 -0
- data/lib/boto/openapi.rb +107 -0
- data/lib/boto/plugin/stack.rb +17 -0
- data/lib/boto/plugin.rb +53 -0
- data/lib/boto/schema/compiler.rb +103 -0
- data/lib/boto/schema.rb +172 -0
- data/lib/boto/solution/boot.rb +83 -0
- data/lib/boto/solution/botofile.rb +95 -0
- data/lib/boto/solution/isolation/scanner.rb +151 -0
- data/lib/boto/solution/isolation.rb +79 -0
- data/lib/boto/solution/isolation_error.rb +56 -0
- data/lib/boto/solution/project.rb +24 -0
- data/lib/boto/solution/project_loader.rb +55 -0
- data/lib/boto/solution/solution.rb +84 -0
- data/lib/boto/types/coercion.rb +61 -0
- data/lib/boto/types/constraints.rb +121 -0
- data/lib/boto/types/enum.rb +28 -0
- data/lib/boto/types/invalid.rb +27 -0
- data/lib/boto/types/list.rb +56 -0
- data/lib/boto/types/scalar.rb +73 -0
- data/lib/boto/types/schema_ref.rb +46 -0
- data/lib/boto/types.rb +31 -0
- data/lib/boto/version.rb +5 -0
- data/lib/boto.rb +17 -0
- metadata +103 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1cd10ccf7fe1d0af78e6f22eb49be47e583eef3b2c2ebacaa0e766cbfc53f265
|
|
4
|
+
data.tar.gz: d070e053b257ec4a48452854c004945fd2b35daa71fe03855392de381dc8759b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 490b67478f845c9f6fb327a1175e30adeb8052f79571a824cc79f333c06e009a9043cb2801e1988b078a218e644946a8fd9fafa6959a3c4556927b3ae44a5c0e
|
|
7
|
+
data.tar.gz: a3383ad8277bdf4b568c61e360b3f8318e94a8b9f028c5647732096ae17c17fc871e107e182ac18ba4a863b63447642f0ffa82caf2d11412193240c579e7278d
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Evo Tech Builder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Plugins declared by one config/app.rb. Filled while Configuration.load evaluates the file.
|
|
5
|
+
class Configuration
|
|
6
|
+
class OutsideBootError < StandardError; end
|
|
7
|
+
|
|
8
|
+
PLUGIN_NAME = /\A[a-z][a-z0-9_]*\z/
|
|
9
|
+
SECURITY_GEM = "boto/security"
|
|
10
|
+
CURRENT_KEY = :boto_configuration
|
|
11
|
+
OUTSIDE_BOOT_MESSAGE = "Boto.configure must run while a project boots (config/app.rb)"
|
|
12
|
+
|
|
13
|
+
attr_reader :env_name
|
|
14
|
+
|
|
15
|
+
def self.load(path, env_name:)
|
|
16
|
+
config = new(env_name: env_name)
|
|
17
|
+
evaluate(path, config) if File.file?(path)
|
|
18
|
+
config
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.capture
|
|
22
|
+
Thread.current[CURRENT_KEY] || raise(OutsideBootError, OUTSIDE_BOOT_MESSAGE)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The thread-local is set only while the file runs, so nested loads restore the outer one.
|
|
26
|
+
def self.evaluate(path, config)
|
|
27
|
+
previous = Thread.current[CURRENT_KEY]
|
|
28
|
+
Thread.current[CURRENT_KEY] = config
|
|
29
|
+
Kernel.load(path)
|
|
30
|
+
ensure
|
|
31
|
+
Thread.current[CURRENT_KEY] = previous
|
|
32
|
+
end
|
|
33
|
+
private_class_method :evaluate
|
|
34
|
+
|
|
35
|
+
def initialize(env_name:)
|
|
36
|
+
@env_name = env_name
|
|
37
|
+
@plugins = []
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def use(plugin_ref, **options)
|
|
41
|
+
klass = resolve(plugin_ref)
|
|
42
|
+
raise ArgumentError, "plugin :#{klass.plugin_name} already enabled" if plugin(klass.plugin_name)
|
|
43
|
+
|
|
44
|
+
@plugins << klass.new(env_name: env_name, **options)
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def plugins
|
|
49
|
+
@plugins.sort_by.with_index { |plugin, index| [plugin.class.order_index, index] }.freeze
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def global_plugins
|
|
53
|
+
plugins_in(:global)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def endpoint_plugins
|
|
57
|
+
plugins_in(:endpoint)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def plugin(name)
|
|
61
|
+
@plugins.find { |instance| instance.class.plugin_name == name.to_sym }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def plugins_in(scope)
|
|
67
|
+
plugins.select { |plugin| plugin.class.scope == scope }.freeze
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def resolve(plugin_ref)
|
|
71
|
+
case plugin_ref
|
|
72
|
+
when Class then check_class(plugin_ref)
|
|
73
|
+
when Symbol, String then lookup(plugin_ref.to_s)
|
|
74
|
+
else raise ArgumentError, "plugin must be a name or a Boto::Plugin class, got #{plugin_ref.inspect}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def check_class(klass)
|
|
79
|
+
raise ArgumentError, "#{klass} must inherit from Boto::Plugin" unless klass < Plugin
|
|
80
|
+
|
|
81
|
+
klass
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def lookup(name)
|
|
85
|
+
raise ArgumentError, "invalid plugin name #{name.inspect}" unless name.match?(PLUGIN_NAME)
|
|
86
|
+
|
|
87
|
+
klass = find_plugin(name) || load_security_gem_and_find(name)
|
|
88
|
+
raise ArgumentError, %(unknown plugin :#{name} (add gem "boto-rb-security" to the Gemfile)) unless klass
|
|
89
|
+
|
|
90
|
+
check_class(klass)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def find_plugin(name)
|
|
94
|
+
return unless defined?(Boto::Plugins)
|
|
95
|
+
|
|
96
|
+
constant = camelize(name)
|
|
97
|
+
Boto::Plugins.const_get(constant, false) if Boto::Plugins.const_defined?(constant, false)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def load_security_gem_and_find(name)
|
|
101
|
+
require SECURITY_GEM
|
|
102
|
+
find_plugin(name)
|
|
103
|
+
rescue LoadError => error
|
|
104
|
+
raise unless error.path == SECURITY_GEM
|
|
105
|
+
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def camelize(name)
|
|
110
|
+
name.split("_").map(&:capitalize).join
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Param type for booleans. Accepts true/false and their string/"1"/"0" forms.
|
|
5
|
+
module Boolean
|
|
6
|
+
TRUE_VALUES = [true, "true", "1"].freeze
|
|
7
|
+
FALSE_VALUES = [false, "false", "0"].freeze
|
|
8
|
+
|
|
9
|
+
def self.coerce(value)
|
|
10
|
+
return true if TRUE_VALUES.include?(value)
|
|
11
|
+
return false if FALSE_VALUES.include?(value)
|
|
12
|
+
|
|
13
|
+
raise ArgumentError, "must be a boolean"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/request"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
# Base class for one endpoint. Subclasses declare route, params and status at class level,
|
|
7
|
+
# and implement #call with the already-extracted params as keyword arguments.
|
|
8
|
+
class Endpoint
|
|
9
|
+
class RouteError < StandardError; end
|
|
10
|
+
|
|
11
|
+
VERBS = %w[GET POST PUT PATCH DELETE QUERY].freeze
|
|
12
|
+
DEFAULT_STATUS = 200
|
|
13
|
+
STATUS_RANGE = (100..599)
|
|
14
|
+
DYNAMIC_PREFIX = ":"
|
|
15
|
+
ERROR_STATUS_RANGE = (400..599)
|
|
16
|
+
NO_ERROR_STATUSES = [].freeze
|
|
17
|
+
BODY_MESSAGE = "body must be a Boto::Schema subclass"
|
|
18
|
+
RETURNS_MESSAGE = "returns must be a Boto::Schema subclass or [subclass]"
|
|
19
|
+
SUMMARY_MESSAGE = "summary must be a non-empty String"
|
|
20
|
+
PLUGIN_NAME = /\A[a-z][a-z0-9_]*\z/
|
|
21
|
+
PRINCIPAL_KEY = "boto.principal"
|
|
22
|
+
NO_SKIPS = [].freeze
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
def get(path)
|
|
26
|
+
define_route("GET", path)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def post(path)
|
|
30
|
+
define_route("POST", path)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def put(path)
|
|
34
|
+
define_route("PUT", path)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def patch(path)
|
|
38
|
+
define_route("PATCH", path)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def delete(path)
|
|
42
|
+
define_route("DELETE", path)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def query(path)
|
|
46
|
+
define_route("QUERY", path)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def route
|
|
50
|
+
@route
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def path_param_names
|
|
54
|
+
return [] unless route
|
|
55
|
+
|
|
56
|
+
route.last.split("/").filter_map do |segment|
|
|
57
|
+
segment.delete_prefix(DYNAMIC_PREFIX).to_sym if segment.start_with?(DYNAMIC_PREFIX)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def param(name, type, **options)
|
|
62
|
+
params.declare(name, type, **options)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def params
|
|
66
|
+
@params ||= Boto::Params.new
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def status(code)
|
|
70
|
+
raise ArgumentError, "invalid status #{code.inspect}" unless valid_status?(code)
|
|
71
|
+
|
|
72
|
+
@status = code
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def default_status
|
|
76
|
+
@status || DEFAULT_STATUS
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def body(schema)
|
|
80
|
+
raise ArgumentError, "body already declared" if @body_schema
|
|
81
|
+
raise ArgumentError, BODY_MESSAGE unless schema_class?(schema)
|
|
82
|
+
|
|
83
|
+
@body_schema = schema
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
attr_reader :body_schema
|
|
87
|
+
|
|
88
|
+
def returns(schema)
|
|
89
|
+
raise ArgumentError, "returns already declared" if @return_schema
|
|
90
|
+
|
|
91
|
+
@return_schema = single_return_schema(schema)
|
|
92
|
+
@returns_list = schema.is_a?(Array)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
attr_reader :return_schema
|
|
96
|
+
|
|
97
|
+
def returns_list?
|
|
98
|
+
@returns_list == true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def summary(text)
|
|
102
|
+
raise ArgumentError, SUMMARY_MESSAGE unless text.is_a?(String) && !text.strip.empty?
|
|
103
|
+
|
|
104
|
+
@summary_text = text.dup.freeze
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
attr_reader :summary_text
|
|
108
|
+
|
|
109
|
+
def errors(*statuses)
|
|
110
|
+
statuses.each do |status|
|
|
111
|
+
raise ArgumentError, "invalid error status #{status.inspect}" unless valid_error_status?(status)
|
|
112
|
+
end
|
|
113
|
+
@error_statuses = (error_statuses + statuses).uniq.sort.freeze
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def error_statuses
|
|
117
|
+
@error_statuses || NO_ERROR_STATUSES
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def skip(*names)
|
|
121
|
+
raise ArgumentError, "skip needs at least one plugin name" if names.empty?
|
|
122
|
+
|
|
123
|
+
@skipped_plugins = (skipped_plugins + names.map { |name| plugin_name(name) }).uniq.freeze
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def skipped_plugins
|
|
127
|
+
@skipped_plugins || NO_SKIPS
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def authenticate(strategy, **options)
|
|
131
|
+
raise ArgumentError, "authenticate already declared" if @authentication
|
|
132
|
+
|
|
133
|
+
@authentication = [plugin_name(strategy), options.freeze].freeze
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
attr_reader :authentication
|
|
137
|
+
|
|
138
|
+
def compile!
|
|
139
|
+
ensure_no_body_conflict!
|
|
140
|
+
ensure_no_auth_skip_conflict!
|
|
141
|
+
params.freeze
|
|
142
|
+
body_schema&.compile!
|
|
143
|
+
return_schema&.compile!
|
|
144
|
+
self
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
private
|
|
148
|
+
|
|
149
|
+
def single_return_schema(schema)
|
|
150
|
+
candidate = schema.is_a?(Array) && schema.size == 1 ? schema.first : schema
|
|
151
|
+
raise ArgumentError, RETURNS_MESSAGE unless schema_class?(candidate)
|
|
152
|
+
|
|
153
|
+
candidate
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def schema_class?(value)
|
|
157
|
+
value.is_a?(Class) && value < Boto::Schema
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def valid_error_status?(code)
|
|
161
|
+
code.is_a?(Integer) && ERROR_STATUS_RANGE.cover?(code)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def ensure_no_body_conflict!
|
|
165
|
+
return unless body_schema && params.names.include?(:body)
|
|
166
|
+
|
|
167
|
+
raise ArgumentError, "param :body conflicts with the body schema"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def plugin_name(name)
|
|
171
|
+
valid = (name.is_a?(String) || name.is_a?(Symbol)) && PLUGIN_NAME.match?(name.to_s)
|
|
172
|
+
raise ArgumentError, "invalid plugin name #{name.inspect}" unless valid
|
|
173
|
+
|
|
174
|
+
name.to_sym
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def ensure_no_auth_skip_conflict!
|
|
178
|
+
return unless authentication && skipped_plugins.include?(:auth)
|
|
179
|
+
|
|
180
|
+
raise ArgumentError, "skip :auth conflicts with authenticate"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def define_route(verb, path)
|
|
184
|
+
raise RouteError, "route already defined: #{route.join(" ")}" if route
|
|
185
|
+
raise ArgumentError, "path must start with /: #{path.inspect}" unless path.is_a?(String) && path.start_with?("/")
|
|
186
|
+
|
|
187
|
+
@route = [verb, path].freeze
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def valid_status?(code)
|
|
191
|
+
code.is_a?(Integer) && STATUS_RANGE.cover?(code)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
attr_reader :env
|
|
196
|
+
|
|
197
|
+
def initialize(env = {})
|
|
198
|
+
@env = env
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def request
|
|
202
|
+
@request ||= Rack::Request.new(env)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def principal
|
|
206
|
+
env[PRINCIPAL_KEY]
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def call(**)
|
|
210
|
+
raise NotImplementedError, "#{self.class} must implement #call"
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
private
|
|
214
|
+
|
|
215
|
+
def not_found(detail = nil)
|
|
216
|
+
raise Boto::Error::NotFound.new(detail)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def bad_request(detail = nil)
|
|
220
|
+
raise Boto::Error::BadRequest.new(detail)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def unprocessable(errors, detail = nil)
|
|
224
|
+
raise Boto::Error::Unprocessable.new(errors, detail)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Declares typed request params and extracts them from path, query and body.
|
|
5
|
+
class Params
|
|
6
|
+
Definition = Data.define(:name, :key, :type, :default, :required, :list)
|
|
7
|
+
|
|
8
|
+
MISSING = Object.new.freeze
|
|
9
|
+
EMPTY = {}.freeze
|
|
10
|
+
ABSENT_MESSAGE = "is required"
|
|
11
|
+
|
|
12
|
+
private_constant :ABSENT_MESSAGE
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@definitions = []
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def declare(name, type, default: MISSING, required: true, **constraints)
|
|
19
|
+
name = name.to_sym
|
|
20
|
+
definition = build_definition(name, type, default, required, constraints)
|
|
21
|
+
raise ArgumentError, "duplicate param #{name}" if names.include?(name)
|
|
22
|
+
|
|
23
|
+
@definitions << definition
|
|
24
|
+
self
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def names
|
|
28
|
+
@definitions.map(&:name)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :definitions
|
|
32
|
+
|
|
33
|
+
def freeze
|
|
34
|
+
@definitions.freeze
|
|
35
|
+
super
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def call(path:, query:, body:)
|
|
39
|
+
return EMPTY if @definitions.empty?
|
|
40
|
+
|
|
41
|
+
values = {}
|
|
42
|
+
errors = nil
|
|
43
|
+
@definitions.each do |definition|
|
|
44
|
+
values[definition.name] = resolve(definition, path, query, body)
|
|
45
|
+
rescue Types::Invalid => e
|
|
46
|
+
errors ||= []
|
|
47
|
+
e.errors.each { |error| errors << error_entry(definition, error) }
|
|
48
|
+
end
|
|
49
|
+
raise Boto::Error::Unprocessable.new(errors) if errors
|
|
50
|
+
|
|
51
|
+
values
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def build_definition(name, type, default, required, constraints)
|
|
57
|
+
Definition.new(
|
|
58
|
+
name: name,
|
|
59
|
+
key: name.name,
|
|
60
|
+
type: Types.build(type, **constraints),
|
|
61
|
+
default: default,
|
|
62
|
+
required: required && default.equal?(MISSING),
|
|
63
|
+
list: type.is_a?(Array)
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Types::Invalid is the error channel, rescued in #call, so the happy path builds no error objects.
|
|
68
|
+
def resolve(definition, path, query, body)
|
|
69
|
+
raw = raw_value(definition, path, query, body)
|
|
70
|
+
return missing_value(definition) if raw.nil?
|
|
71
|
+
|
|
72
|
+
definition.type.coerce(wrap_list(definition, raw))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Query strings give a single value for a list param (`?ids=1`), so it is wrapped before coercion.
|
|
76
|
+
def wrap_list(definition, raw)
|
|
77
|
+
return raw if !definition.list || raw.is_a?(Array)
|
|
78
|
+
|
|
79
|
+
[raw]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Precedence: path > query > body. Only nil counts as absent, so false is kept.
|
|
83
|
+
def raw_value(definition, path, query, body)
|
|
84
|
+
value = path[definition.name]
|
|
85
|
+
value = query[definition.key] if value.nil?
|
|
86
|
+
value.nil? ? body[definition.key] : value
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def missing_value(definition)
|
|
90
|
+
return definition.default unless definition.default.equal?(MISSING)
|
|
91
|
+
raise Types::Invalid.single(ABSENT_MESSAGE) if definition.required
|
|
92
|
+
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def error_entry(definition, error)
|
|
97
|
+
{ param: Types.join_path(definition.key, error[:path]), message: error[:message] }
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|