straddle 1.0.0 → 1.1.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.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -0
  3. data/README.md +84 -89
  4. data/SKILL.md +4 -2
  5. data/api.md +26 -26
  6. data/lib/straddle/client.rb +24 -7
  7. data/lib/straddle/errors.rb +40 -0
  8. data/lib/straddle/file_part.rb +13 -1
  9. data/lib/straddle/internal/transport/base_client.rb +186 -42
  10. data/lib/straddle/internal/transport/connection_pool.rb +160 -0
  11. data/lib/straddle/internal/transport/pooled_net_requester.rb +77 -10
  12. data/lib/straddle/internal/type/base_model.rb +69 -18
  13. data/lib/straddle/internal/type/base_page.rb +14 -0
  14. data/lib/straddle/internal/type/boolean.rb +3 -1
  15. data/lib/straddle/internal/type/converter.rb +13 -3
  16. data/lib/straddle/internal/type/enum.rb +9 -4
  17. data/lib/straddle/internal/type/file_input.rb +8 -2
  18. data/lib/straddle/internal/type/hash_of.rb +3 -1
  19. data/lib/straddle/internal/type/union.rb +21 -7
  20. data/lib/straddle/internal/type/unknown.rb +3 -1
  21. data/lib/straddle/internal/util.rb +273 -15
  22. data/lib/straddle/internal/webhook_verifier.rb +240 -0
  23. data/lib/straddle/internal.rb +5 -1
  24. data/lib/straddle/models/capability_request_create_params.rb +18 -18
  25. data/lib/straddle/models/paykey.rb +3 -2
  26. data/lib/straddle/models/paykey_created_v1_webhook_event.rb +7 -2
  27. data/lib/straddle/models/paykey_event_v1_webhook_event.rb +7 -2
  28. data/lib/straddle/models/payout.rb +14 -14
  29. data/lib/straddle/models/unmasked_payout.rb +14 -14
  30. data/lib/straddle/models/webhook_unwrap_params.rb +2 -0
  31. data/lib/straddle/models.rb +30 -31
  32. data/lib/straddle/request_options.rb +13 -5
  33. data/lib/straddle/resources/accounts.rb +11 -12
  34. data/lib/straddle/resources/bridge.rb +8 -4
  35. data/lib/straddle/resources/capability_requests.rb +4 -5
  36. data/lib/straddle/resources/charges.rb +18 -15
  37. data/lib/straddle/resources/customers/review.rb +5 -6
  38. data/lib/straddle/resources/customers.rb +34 -35
  39. data/lib/straddle/resources/funding_events.rb +20 -20
  40. data/lib/straddle/resources/linked_bank_accounts.rb +9 -11
  41. data/lib/straddle/resources/organizations.rb +6 -6
  42. data/lib/straddle/resources/paykeys/review.rb +5 -6
  43. data/lib/straddle/resources/paykeys.rb +34 -39
  44. data/lib/straddle/resources/payments.rb +8 -7
  45. data/lib/straddle/resources/payouts.rb +18 -15
  46. data/lib/straddle/resources/representatives.rb +7 -6
  47. data/lib/straddle/resources/webhooks.rb +63 -6
  48. data/lib/straddle/version.rb +1 -1
  49. data/lib/straddle.rb +5 -6
  50. data/rbi/straddle/client.rbi +13 -0
  51. data/rbi/straddle/errors.rbi +25 -27
  52. data/rbi/straddle/internal/transport/base_client.rbi +45 -54
  53. data/rbi/straddle/internal/transport/connection_pool.rbi +44 -0
  54. data/rbi/straddle/internal/transport/pooled_net_requester.rbi +15 -16
  55. data/rbi/straddle/internal/type/base_model.rbi +24 -38
  56. data/rbi/straddle/internal/type/base_page.rbi +5 -0
  57. data/rbi/straddle/internal/type/converter.rbi +9 -42
  58. data/rbi/straddle/internal/type/enum.rbi +1 -5
  59. data/rbi/straddle/internal/type/request_parameters.rbi +1 -3
  60. data/rbi/straddle/internal/type/union.rbi +2 -11
  61. data/rbi/straddle/internal/util.rbi +101 -115
  62. data/rbi/straddle/internal/webhook_verifier.rbi +86 -0
  63. data/rbi/straddle/internal.rbi +1 -2
  64. data/rbi/straddle/models/capability_request_create_params.rbi +18 -18
  65. data/rbi/straddle/models/paykey.rbi +4 -2
  66. data/rbi/straddle/models/paykey_created_v1_webhook_event.rbi +6 -2
  67. data/rbi/straddle/models/paykey_event_v1_webhook_event.rbi +6 -2
  68. data/rbi/straddle/models/payout.rbi +18 -18
  69. data/rbi/straddle/models/unmasked_payout.rbi +18 -18
  70. data/rbi/straddle/models/webhook_unwrap_params.rbi +2 -0
  71. data/rbi/straddle/models.rbi +2 -0
  72. data/rbi/straddle/request_options.rbi +4 -10
  73. data/rbi/straddle/resources/accounts.rbi +2 -1
  74. data/rbi/straddle/resources/bridge.rbi +8 -4
  75. data/rbi/straddle/resources/charges.rbi +4 -2
  76. data/rbi/straddle/resources/customers.rbi +7 -3
  77. data/rbi/straddle/resources/funding_events.rbi +5 -2
  78. data/rbi/straddle/resources/organizations.rbi +2 -1
  79. data/rbi/straddle/resources/paykeys.rbi +2 -1
  80. data/rbi/straddle/resources/payments.rbi +3 -1
  81. data/rbi/straddle/resources/payouts.rbi +4 -2
  82. data/rbi/straddle/resources/representatives.rbi +3 -1
  83. data/rbi/straddle/resources/webhooks.rbi +107 -2
  84. data/sig/straddle/client.rbs +4 -0
  85. data/sig/straddle/errors.rbs +13 -0
  86. data/sig/straddle/internal/transport/base_client.rbs +19 -1
  87. data/sig/straddle/internal/transport/connection_pool.rbs +19 -0
  88. data/sig/straddle/internal/transport/pooled_net_requester.rbs +11 -1
  89. data/sig/straddle/internal/type/base_model.rbs +4 -0
  90. data/sig/straddle/internal/type/base_page.rbs +2 -0
  91. data/sig/straddle/internal/util.rbs +29 -1
  92. data/sig/straddle/internal/webhook_verifier.rbs +53 -0
  93. data/sig/straddle/models/capability_request_create_params.rbs +24 -24
  94. data/sig/straddle/models/payout.rbs +12 -12
  95. data/sig/straddle/models/unmasked_payout.rbs +12 -12
  96. data/sig/straddle/models/webhook_unwrap_params.rbs +4 -0
  97. data/sig/straddle/models.rbs +2 -0
  98. data/sig/straddle/resources/webhooks.rbs +59 -1
  99. metadata +12 -60
@@ -13,6 +13,24 @@ module Straddle
13
13
 
14
14
  DEFAULT_MAX_CONNECTIONS = [Etc.nprocessors, 99].max
15
15
 
16
+ # Most origins the requester keeps a connection pool for at once.
17
+ MAX_POOLS = 16
18
+
19
+ # Seconds each socket wait gets once the deadline has passed between two body chunks: enough
20
+ # for a read that finds the rest of the body already buffered, never enough to wait for more.
21
+ ELAPSED_SOCKET_TIMEOUT = 0.001
22
+
23
+ # @api private
24
+ #
25
+ # The trust store every TLS connection of this requester verifies against: OpenSSL's default
26
+ # paths, read when the client is constructed, so `SSL_CERT_FILE` and `SSL_CERT_DIR` as they
27
+ # stand then. A signed upload and a requested-URL fetch, which go to other hosts on connections
28
+ # of their own, verify against this same store, so those hosts are trusted exactly as the
29
+ # client's own are.
30
+ #
31
+ # @return [OpenSSL::X509::Store]
32
+ attr_reader :cert_store
33
+
16
34
  class << self
17
35
  # @api private
18
36
  #
@@ -43,10 +61,30 @@ module Straddle
43
61
 
44
62
  # @api private
45
63
  #
64
+ # Sets every socket timeout of `conn` to what is left of `deadline`.
65
+ #
66
+ # A passed deadline never reaches `Net::HTTP` as a negative or zero timeout: a negative one
67
+ # makes the next socket wait raise `ArgumentError`, which would surface as a connection
68
+ # error, and a zero open timeout means no timeout at all. It raises instead, which `execute`
69
+ # maps to the SDK's timeout error, unless `finishing` is set.
70
+ #
46
71
  # @param conn [Net::HTTP]
47
72
  # @param deadline [Float]
48
- def calibrate_socket_timeout(conn, deadline)
73
+ # @param finishing [Boolean] whether `conn` is between two chunks of a response body. The
74
+ # consumer's time on the chunk it was handed can carry the deadline past, and the body may
75
+ # be complete already, so the timeouts drop to `ELAPSED_SOCKET_TIMEOUT` rather than
76
+ # raising: a read that still has to wait raises `Net::ReadTimeout`, and the next chunk's
77
+ # own check raises when there is more to come.
78
+ #
79
+ # @raise [Timeout::Error] when the deadline has already passed and `finishing` is not set
80
+ def calibrate_socket_timeout(conn, deadline, finishing: false)
49
81
  timeout = deadline - Straddle::Internal::Util.monotonic_secs
82
+ unless timeout.positive?
83
+ raise Timeout::Error, "The request did not finish within its timeout" unless finishing
84
+
85
+ timeout = self::ELAPSED_SOCKET_TIMEOUT
86
+ end
87
+
50
88
  conn.open_timeout = conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
51
89
  end
52
90
 
@@ -105,12 +143,30 @@ module Straddle
105
143
  private def with_pool(url, deadline:, &blk)
106
144
  origin = Straddle::Internal::Util.uri_origin(url)
107
145
  timeout = deadline - Straddle::Internal::Util.monotonic_secs
146
+ # A request whose deadline has already passed times out before it takes a connection, as
147
+ # `calibrate_socket_timeout` would raise on it first thing, rather than checking one out (or
148
+ # opening a pool for its origin) only to hand it straight back.
149
+ raise Timeout::Error, "The request did not finish within its timeout" unless timeout.positive?
150
+
151
+ # One pool per origin, created on first use. `@mutex` only guards the lookup: waiting for a
152
+ # connection happens in the pool, so a full pool for one origin never blocks another's.
153
+ evicted = nil
108
154
  pool =
109
155
  @mutex.synchronize do
110
- @pools[origin] ||= ConnectionPool.new(size: @size) do
111
- self.class.connect(cert_store: @cert_store, url: url)
112
- end
156
+ # Re-inserted on every use, so the hash's insertion order is least recently used first.
157
+ found = @pools.delete(origin)
158
+ found ||=
159
+ Straddle::Internal::Transport::ConnectionPool.new(size: @size) do
160
+ self.class.connect(cert_store: @cert_store, url: url)
161
+ end
162
+ # Origins come from redirects and next-page links as well as the base URL, so a server
163
+ # could otherwise name a new one on every response and hold a socket open for each.
164
+ # Past `MAX_POOLS` the least recently used pool is shut down.
165
+ evicted = @pools.shift&.last if @pools.size >= self.class::MAX_POOLS
166
+ @pools[origin] = found
113
167
  end
168
+ # Outside `@mutex`, since closing a connection can wait on the network.
169
+ evicted&.shutdown { _1.finish if _1.started? }
114
170
 
115
171
  pool.with(timeout: timeout, &blk)
116
172
  end
@@ -133,10 +189,9 @@ module Straddle
133
189
  def execute(request)
134
190
  url, deadline = request.fetch_values(:url, :deadline)
135
191
 
136
- req = nil
137
192
  finished = false
138
193
 
139
- # rubocop:disable Metrics/BlockLength
194
+ # rubocop:disable-next Metrics/BlockLength
140
195
  enum =
141
196
  Enumerator.new do |y|
142
197
  next if finished
@@ -164,10 +219,15 @@ module Straddle
164
219
  ::Kernel.throw(:jump) if finished
165
220
 
166
221
  rsp.read_body do |bytes|
222
+ # A chunk read once the deadline has passed means the body did not finish
223
+ # within it. Checked here, not only before a wait, because a read that finds
224
+ # bytes already buffered never waits, so a server that keeps a body coming
225
+ # would otherwise outlast the deadline indefinitely.
226
+ self.class.calibrate_socket_timeout(conn, deadline)
167
227
  y << bytes.force_encoding(Encoding::BINARY)
168
228
  ::Kernel.throw(:jump) if finished
169
229
 
170
- self.class.calibrate_socket_timeout(conn, deadline)
230
+ self.class.calibrate_socket_timeout(conn, deadline, finishing: true)
171
231
  end
172
232
  eof = true
173
233
  end
@@ -181,12 +241,19 @@ module Straddle
181
241
  end
182
242
  end
183
243
  end
244
+ # `request: nil`, as every other error site passes: the built request carries the client's
245
+ # credential headers, and an error is the kind of object that gets logged or inspected.
184
246
  rescue Timeout::Error
185
- raise Straddle::Errors::APITimeoutError.new(url: url, request: req)
247
+ raise Straddle::Errors::APITimeoutError.new(
248
+ url: url,
249
+ request: nil
250
+ )
186
251
  rescue StandardError
187
- raise Straddle::Errors::APIConnectionError.new(url: url, request: req)
252
+ raise Straddle::Errors::APIConnectionError.new(
253
+ url: url,
254
+ request: nil
255
+ )
188
256
  end
189
- # rubocop:enable Metrics/BlockLength
190
257
 
191
258
  _, response = enum.next
192
259
  body =
@@ -18,6 +18,7 @@ module Straddle
18
18
  def inherited(child)
19
19
  super
20
20
  child.known_fields.replace(known_fields.dup)
21
+ child.dropped_keys.replace(dropped_keys.dup)
21
22
  end
22
23
 
23
24
  # @api private
@@ -25,6 +26,16 @@ module Straddle
25
26
  # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
26
27
  def known_fields = @known_fields ||= {}
27
28
 
29
+ # @api private
30
+ #
31
+ # Keys `.#dump` leaves out of a request even though no field declares them: the
32
+ # `readOnly` members of the model an input shape stands in for, under their wire and
33
+ # Ruby names, so a decoded value passed back into a request does not send them. Any
34
+ # other undeclared key is still sent as given.
35
+ #
36
+ # @return [Hash{Symbol=>true}]
37
+ def dropped_keys = @dropped_keys ||= {}
38
+
28
39
  # @api private
29
40
  #
30
41
  # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
@@ -56,7 +67,12 @@ module Straddle
56
67
  type_fn, info =
57
68
  case type_info
58
69
  in Proc | Straddle::Internal::Type::Converter | Class
59
- [Straddle::Internal::Type::Converter.type_info({**spec, union: type_info}), spec]
70
+ [
71
+ Straddle::Internal::Type::Converter.type_info(
72
+ {**spec, union: type_info}
73
+ ),
74
+ spec
75
+ ]
60
76
  in Hash
61
77
  [Straddle::Internal::Type::Converter.type_info(type_info), type_info]
62
78
  end
@@ -65,9 +81,18 @@ module Straddle
65
81
  api_name = info.fetch(:api_name, name_sym)
66
82
  nilable = info.fetch(:nil?, false)
67
83
  const =
68
- required && !nilable ? info.fetch(:const, Straddle::Internal::OMIT) : Straddle::Internal::OMIT
84
+ (
85
+ if required && !nilable
86
+ info.fetch(:const, Straddle::Internal::OMIT)
87
+ else
88
+ Straddle::Internal::OMIT
89
+ end
90
+ )
69
91
 
70
92
  [name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
93
+ # A declared field always wins over a key a parent dropped: `dump` looks fields up by Ruby
94
+ # name, so the wire spelling would otherwise still hit the inherited entry.
95
+ [name_sym, api_name].each { dropped_keys.delete(_1) }
71
96
 
72
97
  known_fields[name_sym] = {
73
98
  mode: @mode,
@@ -81,8 +106,10 @@ module Straddle
81
106
 
82
107
  define_method(setter) do |value|
83
108
  target = type_fn.call
84
- state = Straddle::Internal::Type::Converter.new_coerce_state(translate_names: false)
85
- coerced = Straddle::Internal::Type::Converter.coerce(target, value, state: state)
109
+ state =
110
+ Straddle::Internal::Type::Converter.new_coerce_state(translate_names: false)
111
+ coerced =
112
+ Straddle::Internal::Type::Converter.coerce(target, value, state: state)
86
113
  status = @coerced.store(name_sym, state.fetch(:error) || true)
87
114
  stored =
88
115
  case [target, status]
@@ -94,8 +121,7 @@ module Straddle
94
121
  @data.store(name_sym, stored)
95
122
  end
96
123
 
97
- # rubocop:disable Style/CaseEquality
98
- # rubocop:disable Metrics/BlockLength
124
+ # rubocop:disable-next Style/CaseEquality, Metrics/BlockLength
99
125
  define_method(name_sym) do
100
126
  target = type_fn.call
101
127
 
@@ -112,8 +138,14 @@ module Straddle
112
138
  )
113
139
  else
114
140
  Kernel.then do
115
- value = @data.fetch(name_sym) { const == Straddle::Internal::OMIT ? nil : const }
116
- state = Straddle::Internal::Type::Converter.new_coerce_state(translate_names: false)
141
+ value =
142
+ @data.fetch(name_sym) do
143
+ const == Straddle::Internal::OMIT ? nil : const
144
+ end
145
+ state =
146
+ Straddle::Internal::Type::Converter.new_coerce_state(
147
+ translate_names: false
148
+ )
117
149
  if (nilable || !required) && value.nil?
118
150
  nil
119
151
  else
@@ -130,8 +162,6 @@ module Straddle
130
162
  end
131
163
  end
132
164
  end
133
- # rubocop:enable Metrics/BlockLength
134
- # rubocop:enable Style/CaseEquality
135
165
  end
136
166
 
137
167
  # @api private
@@ -197,13 +227,23 @@ module Straddle
197
227
  @mode = nil
198
228
  end
199
229
 
230
+ # @api private
231
+ #
232
+ # Declares keys `.#dump` drops without declaring an attribute for them.
233
+ #
234
+ # @param names [Array<Symbol>]
235
+ private def drop_on_dump(*names)
236
+ names.each { dropped_keys.store(_1, true) }
237
+ end
238
+
200
239
  # @api public
201
240
  #
202
241
  # @param other [Object]
203
242
  #
204
243
  # @return [Boolean]
205
244
  def ==(other)
206
- other.is_a?(Class) && other <= Straddle::Internal::Type::BaseModel && other.fields == fields
245
+ other.is_a?(Class) && other <= Straddle::Internal::Type::BaseModel &&
246
+ other.fields == fields
207
247
  end
208
248
 
209
249
  # @api public
@@ -262,7 +302,7 @@ module Straddle
262
302
  data = instance.to_h
263
303
  status = instance.instance_variable_get(:@coerced)
264
304
 
265
- # rubocop:disable Metrics/BlockLength
305
+ # rubocop:disable-next Metrics/BlockLength
266
306
  fields.each do |name, field|
267
307
  mode, required, target = field.fetch_values(:mode, :required, :type)
268
308
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
@@ -286,7 +326,8 @@ module Straddle
286
326
  exactness[nilable ? :yes : :maybe] += 1
287
327
  nil
288
328
  else
289
- coerced = Straddle::Internal::Type::Converter.coerce(target, item, state: state)
329
+ coerced =
330
+ Straddle::Internal::Type::Converter.coerce(target, item, state: state)
290
331
  case target
291
332
  in Straddle::Internal::Type::Converter | Symbol
292
333
  coerced
@@ -298,7 +339,6 @@ module Straddle
298
339
  status.store(name, state.fetch(:error) || true)
299
340
  data.store(name, converted)
300
341
  end
301
- # rubocop:enable Metrics/BlockLength
302
342
 
303
343
  keys.each { data.store(_1, val.fetch(_1)) }
304
344
  instance
@@ -324,6 +364,8 @@ module Straddle
324
364
  name = key.is_a?(String) ? key.to_sym : key
325
365
  case (field = known_fields[name])
326
366
  in nil
367
+ next if dropped_keys.key?(name)
368
+
327
369
  acc.store(name, super(val, state: state))
328
370
  else
329
371
  api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn)
@@ -332,14 +374,19 @@ module Straddle
332
374
  next
333
375
  else
334
376
  target = type_fn.call
335
- acc.store(api_name, Straddle::Internal::Type::Converter.dump(target, val, state: state))
377
+ acc.store(
378
+ api_name,
379
+ Straddle::Internal::Type::Converter.dump(target, val, state: state)
380
+ )
336
381
  end
337
382
  end
338
383
  end
339
384
 
340
385
  known_fields.each_value do |field|
341
386
  api_name, mode, const = field.fetch_values(:api_name, :mode, :const)
342
- next if mode == :coerce || acc.key?(api_name) || const == Straddle::Internal::OMIT
387
+ if mode == :coerce || acc.key?(api_name) || const == Straddle::Internal::OMIT
388
+ next
389
+ end
343
390
  acc.store(api_name, const)
344
391
  end
345
392
 
@@ -450,14 +497,18 @@ module Straddle
450
497
  # @param a [Object]
451
498
  #
452
499
  # @return [String]
453
- def to_json(*a) = Straddle::Internal::Type::Converter.dump(self.class, self).to_json(*a)
500
+ def to_json(*a)
501
+ Straddle::Internal::Type::Converter.dump(self.class, self).to_json(*a)
502
+ end
454
503
 
455
504
  # @api public
456
505
  #
457
506
  # @param a [Object]
458
507
  #
459
508
  # @return [String]
460
- def to_yaml(*a) = Straddle::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
509
+ def to_yaml(*a)
510
+ Straddle::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
511
+ end
461
512
 
462
513
  # Create a new instance of a model.
463
514
  #
@@ -48,6 +48,20 @@ module Straddle
48
48
  super()
49
49
  end
50
50
 
51
+ # @api public
52
+ #
53
+ # The page's own fields, which is what ActiveSupport's `to_json` (and Rails' `render json:`)
54
+ # serializes. Its default walks every instance variable, so a page would otherwise carry the
55
+ # client along, its configuration and credentials included, or loop through the client's
56
+ # resources back to the client until the stack runs out.
57
+ #
58
+ # @return [Hash{String=>Object}]
59
+ def as_json(*)
60
+ (instance_variables - %i[@client @req @model]).to_h do |name|
61
+ [name.to_s.delete_prefix("@"), instance_variable_get(name)]
62
+ end
63
+ end
64
+
51
65
  # rubocop:enable Lint/UnusedMethodArgument
52
66
  end
53
67
  end
@@ -26,7 +26,9 @@ module Straddle
26
26
  # @param other [Object]
27
27
  #
28
28
  # @return [Boolean]
29
- def self.==(other) = other.is_a?(Class) && other <= Straddle::Internal::Type::Boolean
29
+ def self.==(other)
30
+ other.is_a?(Class) && other <= Straddle::Internal::Type::Boolean
31
+ end
30
32
 
31
33
  class << self
32
34
  # @api private
@@ -42,7 +42,9 @@ module Straddle
42
42
  in Array
43
43
  value.map { Straddle::Internal::Type::Unknown.dump(_1, state: state) }
44
44
  in Hash
45
- value.transform_values { Straddle::Internal::Type::Unknown.dump(_1, state: state) }
45
+ value.transform_values do
46
+ Straddle::Internal::Type::Unknown.dump(_1, state: state)
47
+ end
46
48
  in Straddle::Internal::Type::BaseModel
47
49
  value.class.dump(value, state: state)
48
50
  in StringIO
@@ -53,6 +55,8 @@ module Straddle
53
55
  in Straddle::FilePart
54
56
  state[:can_retry] = false if value.content.is_a?(IO)
55
57
  value
58
+ in Time
59
+ value.iso8601
56
60
  else
57
61
  value
58
62
  end
@@ -188,7 +192,11 @@ module Straddle
188
192
  # @option state [Integer] :branched
189
193
  #
190
194
  # @return [Object]
191
- def coerce(target, value, state: Straddle::Internal::Type::Converter.new_coerce_state)
195
+ def coerce(
196
+ target,
197
+ value,
198
+ state: Straddle::Internal::Type::Converter.new_coerce_state
199
+ )
192
200
  # rubocop:disable Metrics/BlockNesting
193
201
  exactness = state.fetch(:exactness)
194
202
 
@@ -309,7 +317,9 @@ module Straddle
309
317
  end
310
318
 
311
319
  define_sorbet_constant!(:Input) do
312
- T.type_alias { T.any(Straddle::Internal::Type::Converter, T::Class[T.anything]) }
320
+ T.type_alias do
321
+ T.any(Straddle::Internal::Type::Converter, T::Class[T.anything])
322
+ end
313
323
  end
314
324
  define_sorbet_constant!(:CoerceState) do
315
325
  T.type_alias do
@@ -38,9 +38,8 @@ module Straddle
38
38
  #
39
39
  # @return [Boolean]
40
40
  def ==(other)
41
- # rubocop:disable Style/CaseEquality
41
+ # rubocop:disable-next Style/CaseEquality
42
42
  Straddle::Internal::Type::Enum === other && other.values.to_set == values.to_set
43
- # rubocop:enable Style/CaseEquality
44
43
  end
45
44
 
46
45
  # @api public
@@ -100,7 +99,10 @@ module Straddle
100
99
  #
101
100
  # @return [Object]
102
101
  def to_sorbet_type
103
- types = values.map { Straddle::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
102
+ types =
103
+ values
104
+ .map { Straddle::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }
105
+ .uniq
104
106
  case types
105
107
  in []
106
108
  T.noreturn
@@ -119,7 +121,10 @@ module Straddle
119
121
  def inspect(depth: 0)
120
122
  return is_a?(Module) ? super() : self.class.name if depth.positive?
121
123
 
122
- members = values.map { Straddle::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
124
+ members =
125
+ values.map do
126
+ Straddle::Internal::Type::Converter.inspect(_1, depth: depth.succ)
127
+ end
123
128
  prefix = is_a?(Module) ? name : self.class.name
124
129
 
125
130
  "#{prefix}[#{members.join(" | ")}]"
@@ -36,7 +36,9 @@ module Straddle
36
36
  # @param other [Object]
37
37
  #
38
38
  # @return [Boolean]
39
- def self.==(other) = other.is_a?(Class) && other <= Straddle::Internal::Type::FileInput
39
+ def self.==(other)
40
+ other.is_a?(Class) && other <= Straddle::Internal::Type::FileInput
41
+ end
40
42
 
41
43
  class << self
42
44
  # @api private
@@ -89,7 +91,11 @@ module Straddle
89
91
  Straddle::FilePart.new(value, filename: "upload")
90
92
  in IO
91
93
  state[:can_retry] = false
92
- value.to_path.nil? ? Straddle::FilePart.new(value, filename: "upload") : value
94
+ if value.to_path.nil?
95
+ Straddle::FilePart.new(value, filename: "upload")
96
+ else
97
+ value
98
+ end
93
99
  in Straddle::FilePart if value.content.is_a?(IO)
94
100
  state[:can_retry] = false
95
101
  value
@@ -126,7 +126,9 @@ module Straddle
126
126
  def dump(value, state:)
127
127
  target = item_type
128
128
  if value.is_a?(Hash)
129
- value.transform_values { Straddle::Internal::Type::Converter.dump(target, _1, state: state) }
129
+ value.transform_values do
130
+ Straddle::Internal::Type::Converter.dump(target, _1, state: state)
131
+ end
130
132
  else
131
133
  super
132
134
  end
@@ -74,7 +74,10 @@ module Straddle
74
74
  in [_, Straddle::Internal::Type::BaseModel]
75
75
  value.class
76
76
  in [Symbol, Hash]
77
- key = value.fetch(@discriminator) { value.fetch(@discriminator.to_s, Straddle::Internal::OMIT) }
77
+ key =
78
+ value.fetch(@discriminator) do
79
+ value.fetch(@discriminator.to_s, Straddle::Internal::OMIT)
80
+ end
78
81
 
79
82
  return nil if key == Straddle::Internal::OMIT
80
83
 
@@ -104,7 +107,8 @@ module Straddle
104
107
  #
105
108
  # @return [Boolean]
106
109
  def ==(other)
107
- Straddle::Internal::Type::Union === other && other.derefed_variants == derefed_variants
110
+ Straddle::Internal::Type::Union === other &&
111
+ other.derefed_variants == derefed_variants
108
112
  end
109
113
 
110
114
  # @api public
@@ -135,11 +139,12 @@ module Straddle
135
139
  #
136
140
  # @return [Object]
137
141
  def coerce(value, state:)
142
+ # Even discriminator-selected variants return through ensure below.
143
+ strictness = state.fetch(:strictness)
138
144
  if (target = resolve_variant(value))
139
145
  return Straddle::Internal::Type::Converter.coerce(target, value, state: state)
140
146
  end
141
147
 
142
- strictness = state.fetch(:strictness)
143
148
  exactness = state.fetch(:exactness)
144
149
 
145
150
  alternatives = []
@@ -148,7 +153,8 @@ module Straddle
148
153
  exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
149
154
  state[:branched] += 1
150
155
 
151
- coerced = Straddle::Internal::Type::Converter.coerce(target, value, state: state)
156
+ coerced =
157
+ Straddle::Internal::Type::Converter.coerce(target, value, state: state)
152
158
  yes, no, maybe = exact.values
153
159
  if (no + maybe).zero? || (!strictness && yes.positive?)
154
160
  exact.each { exactness[_1] += _2 }
@@ -188,7 +194,9 @@ module Straddle
188
194
 
189
195
  known_variants.each do
190
196
  target = _2.call
191
- return Straddle::Internal::Type::Converter.dump(target, value, state: state) if target === value
197
+ if target === value
198
+ return Straddle::Internal::Type::Converter.dump(target, value, state: state)
199
+ end
192
200
  end
193
201
 
194
202
  super
@@ -198,7 +206,10 @@ module Straddle
198
206
  #
199
207
  # @return [Object]
200
208
  def to_sorbet_type
201
- types = variants.map { Straddle::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
209
+ types =
210
+ variants
211
+ .map { Straddle::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }
212
+ .uniq
202
213
  case types
203
214
  in []
204
215
  T.noreturn
@@ -220,7 +231,10 @@ module Straddle
220
231
  def inspect(depth: 0)
221
232
  return is_a?(Module) ? super() : self.class.name if depth.positive?
222
233
 
223
- members = variants.map { Straddle::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
234
+ members =
235
+ variants.map do
236
+ Straddle::Internal::Type::Converter.inspect(_1, depth: depth.succ)
237
+ end
224
238
  prefix = is_a?(Module) ? name : self.class.name
225
239
 
226
240
  "#{prefix}[#{members.join(" | ")}]"
@@ -28,7 +28,9 @@ module Straddle
28
28
  # @param other [Object]
29
29
  #
30
30
  # @return [Boolean]
31
- def self.==(other) = other.is_a?(Class) && other <= Straddle::Internal::Type::Unknown
31
+ def self.==(other)
32
+ other.is_a?(Class) && other <= Straddle::Internal::Type::Unknown
33
+ end
32
34
 
33
35
  class << self
34
36
  # @api private