galaxy-ruby 0.4.0 → 0.6.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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +188 -0
  3. data/README.md +26 -5
  4. data/SKILL.md +6 -4
  5. data/api.md +8 -8
  6. data/lib/galaxy-ruby/client.rb +52 -41
  7. data/lib/galaxy-ruby/errors.rb +40 -0
  8. data/lib/galaxy-ruby/file_part.rb +13 -1
  9. data/lib/galaxy-ruby/internal/transport/base_client.rb +278 -42
  10. data/lib/galaxy-ruby/internal/transport/connection_pool.rb +160 -0
  11. data/lib/galaxy-ruby/internal/transport/pooled_net_requester.rb +77 -10
  12. data/lib/galaxy-ruby/internal/type/base_model.rb +62 -17
  13. data/lib/galaxy-ruby/internal/type/boolean.rb +3 -1
  14. data/lib/galaxy-ruby/internal/type/converter.rb +13 -3
  15. data/lib/galaxy-ruby/internal/type/enum.rb +9 -4
  16. data/lib/galaxy-ruby/internal/type/file_input.rb +8 -2
  17. data/lib/galaxy-ruby/internal/type/hash_of.rb +3 -1
  18. data/lib/galaxy-ruby/internal/type/union.rb +17 -6
  19. data/lib/galaxy-ruby/internal/type/unknown.rb +3 -1
  20. data/lib/galaxy-ruby/internal/util.rb +273 -15
  21. data/lib/galaxy-ruby/internal/webhook_verifier.rb +240 -0
  22. data/lib/galaxy-ruby/internal.rb +4 -1
  23. data/lib/galaxy-ruby/models/authentication_create_token_params.rb +7 -1
  24. data/lib/galaxy-ruby/models/authentication_create_user_params.rb +2 -0
  25. data/lib/galaxy-ruby/models/celestial_body_create_params.rb +3 -3
  26. data/lib/galaxy-ruby/models/celestial_body_param.rb +72 -0
  27. data/lib/galaxy-ruby/models/credentials.rb +1 -9
  28. data/lib/galaxy-ruby/models/planet_create_params.rb +11 -1
  29. data/lib/galaxy-ruby/models/{planet_list_all_data_params.rb → planet_list_params.rb} +2 -2
  30. data/lib/galaxy-ruby/models/{planet_list_all_data_response.rb → planet_list_response.rb} +9 -9
  31. data/lib/galaxy-ruby/models/planet_param.rb +204 -0
  32. data/lib/galaxy-ruby/models/planet_update_params.rb +12 -1
  33. data/lib/galaxy-ruby/models/{planet_delte_image_params.rb → planet_upload_image_params.rb} +2 -2
  34. data/lib/galaxy-ruby/models/{planet_delte_image_response.rb → planet_upload_image_response.rb} +2 -2
  35. data/lib/galaxy-ruby/models/user_param.rb +19 -0
  36. data/lib/galaxy-ruby/models/webhook_unwrap_params.rb +2 -0
  37. data/lib/galaxy-ruby/models.rb +38 -33
  38. data/lib/galaxy-ruby/request_options.rb +13 -5
  39. data/lib/galaxy-ruby/resources/celestial_bodies.rb +1 -1
  40. data/lib/galaxy-ruby/resources/planets.rb +37 -47
  41. data/lib/galaxy-ruby/resources/webhooks.rb +63 -6
  42. data/lib/galaxy-ruby/version.rb +1 -1
  43. data/lib/galaxy-ruby.rb +12 -10
  44. data/rbi/galaxy-ruby/client.rbi +28 -5
  45. data/rbi/galaxy-ruby/errors.rbi +25 -27
  46. data/rbi/galaxy-ruby/internal/transport/base_client.rbi +88 -68
  47. data/rbi/galaxy-ruby/internal/transport/connection_pool.rbi +44 -0
  48. data/rbi/galaxy-ruby/internal/transport/pooled_net_requester.rbi +18 -19
  49. data/rbi/galaxy-ruby/internal/type/base_model.rbi +27 -49
  50. data/rbi/galaxy-ruby/internal/type/base_page.rbi +1 -2
  51. data/rbi/galaxy-ruby/internal/type/converter.rbi +12 -51
  52. data/rbi/galaxy-ruby/internal/type/enum.rbi +1 -5
  53. data/rbi/galaxy-ruby/internal/type/request_parameters.rbi +2 -6
  54. data/rbi/galaxy-ruby/internal/type/union.rbi +2 -11
  55. data/rbi/galaxy-ruby/internal/util.rbi +101 -115
  56. data/rbi/galaxy-ruby/internal/webhook_verifier.rbi +86 -0
  57. data/rbi/galaxy-ruby/internal.rbi +1 -4
  58. data/rbi/galaxy-ruby/models/authentication_create_token_params.rbi +11 -5
  59. data/rbi/galaxy-ruby/models/celestial_body_create_params.rbi +8 -5
  60. data/rbi/galaxy-ruby/models/celestial_body_param.rbi +138 -0
  61. data/rbi/galaxy-ruby/models/credentials.rbi +2 -5
  62. data/rbi/galaxy-ruby/models/planet_create_params.rbi +22 -5
  63. data/rbi/galaxy-ruby/models/{planet_list_all_data_params.rbi → planet_list_params.rbi} +2 -5
  64. data/rbi/galaxy-ruby/models/{planet_list_all_data_response.rbi → planet_list_response.rbi} +11 -15
  65. data/rbi/galaxy-ruby/models/planet_param.rbi +346 -0
  66. data/rbi/galaxy-ruby/models/planet_update_params.rbi +19 -2
  67. data/rbi/galaxy-ruby/models/{planet_delte_image_params.rbi → planet_upload_image_params.rbi} +2 -2
  68. data/rbi/galaxy-ruby/models/{planet_delte_image_response.rbi → planet_upload_image_response.rbi} +2 -2
  69. data/rbi/galaxy-ruby/models/unwrap_webhook_event.rbi +1 -1
  70. data/rbi/galaxy-ruby/models/user_param.rbi +27 -0
  71. data/rbi/galaxy-ruby/models/webhook_unwrap_params.rbi +2 -0
  72. data/rbi/galaxy-ruby/models.rbi +10 -2
  73. data/rbi/galaxy-ruby/request_options.rbi +4 -10
  74. data/rbi/galaxy-ruby/resources/celestial_bodies.rbi +2 -2
  75. data/rbi/galaxy-ruby/resources/planets.rbi +22 -30
  76. data/rbi/galaxy-ruby/resources/webhooks.rbi +64 -3
  77. data/sig/galaxy-ruby/client.rbs +13 -5
  78. data/sig/galaxy-ruby/errors.rbs +13 -0
  79. data/sig/galaxy-ruby/internal/transport/base_client.rbs +38 -2
  80. data/sig/galaxy-ruby/internal/transport/connection_pool.rbs +19 -0
  81. data/sig/galaxy-ruby/internal/transport/pooled_net_requester.rbs +13 -2
  82. data/sig/galaxy-ruby/internal/type/base_model.rbs +4 -0
  83. data/sig/galaxy-ruby/internal/util.rbs +29 -1
  84. data/sig/galaxy-ruby/internal/webhook_verifier.rbs +53 -0
  85. data/sig/galaxy-ruby/models/authentication_create_token_params.rbs +13 -3
  86. data/sig/galaxy-ruby/models/celestial_body_create_params.rbs +4 -4
  87. data/sig/galaxy-ruby/models/celestial_body_param.rbs +69 -0
  88. data/sig/galaxy-ruby/models/credentials.rbs +1 -3
  89. data/sig/galaxy-ruby/models/planet_create_params.rbs +14 -3
  90. data/sig/galaxy-ruby/models/{planet_list_all_data_params.rbs → planet_list_params.rbs} +2 -2
  91. data/sig/galaxy-ruby/models/{planet_list_all_data_response.rbs → planet_list_response.rbs} +15 -13
  92. data/sig/galaxy-ruby/models/planet_param.rbs +199 -0
  93. data/sig/galaxy-ruby/models/planet_update_params.rbs +8 -1
  94. data/sig/galaxy-ruby/models/{planet_delte_image_params.rbs → planet_upload_image_params.rbs} +2 -2
  95. data/sig/galaxy-ruby/models/{planet_delte_image_response.rbs → planet_upload_image_response.rbs} +2 -2
  96. data/sig/galaxy-ruby/models/user_param.rbs +15 -0
  97. data/sig/galaxy-ruby/models/webhook_unwrap_params.rbs +4 -0
  98. data/sig/galaxy-ruby/models.rbs +10 -2
  99. data/sig/galaxy-ruby/resources/celestial_bodies.rbs +1 -1
  100. data/sig/galaxy-ruby/resources/planets.rbs +10 -14
  101. data/sig/galaxy-ruby/resources/webhooks.rbs +17 -1
  102. metadata +32 -71
@@ -13,6 +13,24 @@ module ScalarGalaxy
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 ScalarGalaxy
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 - ScalarGalaxy::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 ScalarGalaxy
105
143
  private def with_pool(url, deadline:, &blk)
106
144
  origin = ScalarGalaxy::Internal::Util.uri_origin(url)
107
145
  timeout = deadline - ScalarGalaxy::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
+ ScalarGalaxy::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 ScalarGalaxy
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 ScalarGalaxy
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 ScalarGalaxy
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 ScalarGalaxy::Errors::APITimeoutError.new(url: url, request: req)
247
+ raise ScalarGalaxy::Errors::APITimeoutError.new(
248
+ url: request.fetch(:error_url, url),
249
+ request: nil
250
+ )
186
251
  rescue StandardError
187
- raise ScalarGalaxy::Errors::APIConnectionError.new(url: url, request: req)
252
+ raise ScalarGalaxy::Errors::APIConnectionError.new(
253
+ url: request.fetch(:error_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 ScalarGalaxy
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 ScalarGalaxy
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 ScalarGalaxy
56
67
  type_fn, info =
57
68
  case type_info
58
69
  in Proc | ScalarGalaxy::Internal::Type::Converter | Class
59
- [ScalarGalaxy::Internal::Type::Converter.type_info({**spec, union: type_info}), spec]
70
+ [
71
+ ScalarGalaxy::Internal::Type::Converter.type_info(
72
+ {**spec, union: type_info}
73
+ ),
74
+ spec
75
+ ]
60
76
  in Hash
61
77
  [ScalarGalaxy::Internal::Type::Converter.type_info(type_info), type_info]
62
78
  end
@@ -74,6 +90,9 @@ module ScalarGalaxy
74
90
  )
75
91
 
76
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) }
77
96
 
78
97
  known_fields[name_sym] = {
79
98
  mode: @mode,
@@ -87,8 +106,10 @@ module ScalarGalaxy
87
106
 
88
107
  define_method(setter) do |value|
89
108
  target = type_fn.call
90
- state = ScalarGalaxy::Internal::Type::Converter.new_coerce_state(translate_names: false)
91
- coerced = ScalarGalaxy::Internal::Type::Converter.coerce(target, value, state: state)
109
+ state =
110
+ ScalarGalaxy::Internal::Type::Converter.new_coerce_state(translate_names: false)
111
+ coerced =
112
+ ScalarGalaxy::Internal::Type::Converter.coerce(target, value, state: state)
92
113
  status = @coerced.store(name_sym, state.fetch(:error) || true)
93
114
  stored =
94
115
  case [target, status]
@@ -100,8 +121,7 @@ module ScalarGalaxy
100
121
  @data.store(name_sym, stored)
101
122
  end
102
123
 
103
- # rubocop:disable Style/CaseEquality
104
- # rubocop:disable Metrics/BlockLength
124
+ # rubocop:disable-next Style/CaseEquality, Metrics/BlockLength
105
125
  define_method(name_sym) do
106
126
  target = type_fn.call
107
127
 
@@ -118,8 +138,14 @@ module ScalarGalaxy
118
138
  )
119
139
  else
120
140
  Kernel.then do
121
- value = @data.fetch(name_sym) { const == ScalarGalaxy::Internal::OMIT ? nil : const }
122
- state = ScalarGalaxy::Internal::Type::Converter.new_coerce_state(translate_names: false)
141
+ value =
142
+ @data.fetch(name_sym) do
143
+ const == ScalarGalaxy::Internal::OMIT ? nil : const
144
+ end
145
+ state =
146
+ ScalarGalaxy::Internal::Type::Converter.new_coerce_state(
147
+ translate_names: false
148
+ )
123
149
  if (nilable || !required) && value.nil?
124
150
  nil
125
151
  else
@@ -136,8 +162,6 @@ module ScalarGalaxy
136
162
  end
137
163
  end
138
164
  end
139
- # rubocop:enable Metrics/BlockLength
140
- # rubocop:enable Style/CaseEquality
141
165
  end
142
166
 
143
167
  # @api private
@@ -203,13 +227,23 @@ module ScalarGalaxy
203
227
  @mode = nil
204
228
  end
205
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
+
206
239
  # @api public
207
240
  #
208
241
  # @param other [Object]
209
242
  #
210
243
  # @return [Boolean]
211
244
  def ==(other)
212
- other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::BaseModel && other.fields == fields
245
+ other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::BaseModel &&
246
+ other.fields == fields
213
247
  end
214
248
 
215
249
  # @api public
@@ -268,7 +302,7 @@ module ScalarGalaxy
268
302
  data = instance.to_h
269
303
  status = instance.instance_variable_get(:@coerced)
270
304
 
271
- # rubocop:disable Metrics/BlockLength
305
+ # rubocop:disable-next Metrics/BlockLength
272
306
  fields.each do |name, field|
273
307
  mode, required, target = field.fetch_values(:mode, :required, :type)
274
308
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
@@ -292,7 +326,8 @@ module ScalarGalaxy
292
326
  exactness[nilable ? :yes : :maybe] += 1
293
327
  nil
294
328
  else
295
- coerced = ScalarGalaxy::Internal::Type::Converter.coerce(target, item, state: state)
329
+ coerced =
330
+ ScalarGalaxy::Internal::Type::Converter.coerce(target, item, state: state)
296
331
  case target
297
332
  in ScalarGalaxy::Internal::Type::Converter | Symbol
298
333
  coerced
@@ -304,7 +339,6 @@ module ScalarGalaxy
304
339
  status.store(name, state.fetch(:error) || true)
305
340
  data.store(name, converted)
306
341
  end
307
- # rubocop:enable Metrics/BlockLength
308
342
 
309
343
  keys.each { data.store(_1, val.fetch(_1)) }
310
344
  instance
@@ -330,6 +364,8 @@ module ScalarGalaxy
330
364
  name = key.is_a?(String) ? key.to_sym : key
331
365
  case (field = known_fields[name])
332
366
  in nil
367
+ next if dropped_keys.key?(name)
368
+
333
369
  acc.store(name, super(val, state: state))
334
370
  else
335
371
  api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn)
@@ -338,14 +374,19 @@ module ScalarGalaxy
338
374
  next
339
375
  else
340
376
  target = type_fn.call
341
- acc.store(api_name, ScalarGalaxy::Internal::Type::Converter.dump(target, val, state: state))
377
+ acc.store(
378
+ api_name,
379
+ ScalarGalaxy::Internal::Type::Converter.dump(target, val, state: state)
380
+ )
342
381
  end
343
382
  end
344
383
  end
345
384
 
346
385
  known_fields.each_value do |field|
347
386
  api_name, mode, const = field.fetch_values(:api_name, :mode, :const)
348
- next if mode == :coerce || acc.key?(api_name) || const == ScalarGalaxy::Internal::OMIT
387
+ if mode == :coerce || acc.key?(api_name) || const == ScalarGalaxy::Internal::OMIT
388
+ next
389
+ end
349
390
  acc.store(api_name, const)
350
391
  end
351
392
 
@@ -456,14 +497,18 @@ module ScalarGalaxy
456
497
  # @param a [Object]
457
498
  #
458
499
  # @return [String]
459
- def to_json(*a) = ScalarGalaxy::Internal::Type::Converter.dump(self.class, self).to_json(*a)
500
+ def to_json(*a)
501
+ ScalarGalaxy::Internal::Type::Converter.dump(self.class, self).to_json(*a)
502
+ end
460
503
 
461
504
  # @api public
462
505
  #
463
506
  # @param a [Object]
464
507
  #
465
508
  # @return [String]
466
- def to_yaml(*a) = ScalarGalaxy::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
509
+ def to_yaml(*a)
510
+ ScalarGalaxy::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
511
+ end
467
512
 
468
513
  # Create a new instance of a model.
469
514
  #
@@ -26,7 +26,9 @@ module ScalarGalaxy
26
26
  # @param other [Object]
27
27
  #
28
28
  # @return [Boolean]
29
- def self.==(other) = other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::Boolean
29
+ def self.==(other)
30
+ other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::Boolean
31
+ end
30
32
 
31
33
  class << self
32
34
  # @api private
@@ -42,7 +42,9 @@ module ScalarGalaxy
42
42
  in Array
43
43
  value.map { ScalarGalaxy::Internal::Type::Unknown.dump(_1, state: state) }
44
44
  in Hash
45
- value.transform_values { ScalarGalaxy::Internal::Type::Unknown.dump(_1, state: state) }
45
+ value.transform_values do
46
+ ScalarGalaxy::Internal::Type::Unknown.dump(_1, state: state)
47
+ end
46
48
  in ScalarGalaxy::Internal::Type::BaseModel
47
49
  value.class.dump(value, state: state)
48
50
  in StringIO
@@ -53,6 +55,8 @@ module ScalarGalaxy
53
55
  in ScalarGalaxy::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 ScalarGalaxy
188
192
  # @option state [Integer] :branched
189
193
  #
190
194
  # @return [Object]
191
- def coerce(target, value, state: ScalarGalaxy::Internal::Type::Converter.new_coerce_state)
195
+ def coerce(
196
+ target,
197
+ value,
198
+ state: ScalarGalaxy::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 ScalarGalaxy
309
317
  end
310
318
 
311
319
  define_sorbet_constant!(:Input) do
312
- T.type_alias { T.any(ScalarGalaxy::Internal::Type::Converter, T::Class[T.anything]) }
320
+ T.type_alias do
321
+ T.any(ScalarGalaxy::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 ScalarGalaxy
38
38
  #
39
39
  # @return [Boolean]
40
40
  def ==(other)
41
- # rubocop:disable Style/CaseEquality
41
+ # rubocop:disable-next Style/CaseEquality
42
42
  ScalarGalaxy::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 ScalarGalaxy
100
99
  #
101
100
  # @return [Object]
102
101
  def to_sorbet_type
103
- types = values.map { ScalarGalaxy::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
102
+ types =
103
+ values
104
+ .map { ScalarGalaxy::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 ScalarGalaxy
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 { ScalarGalaxy::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
124
+ members =
125
+ values.map do
126
+ ScalarGalaxy::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 ScalarGalaxy
36
36
  # @param other [Object]
37
37
  #
38
38
  # @return [Boolean]
39
- def self.==(other) = other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::FileInput
39
+ def self.==(other)
40
+ other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::FileInput
41
+ end
40
42
 
41
43
  class << self
42
44
  # @api private
@@ -89,7 +91,11 @@ module ScalarGalaxy
89
91
  ScalarGalaxy::FilePart.new(value, filename: "upload")
90
92
  in IO
91
93
  state[:can_retry] = false
92
- value.to_path.nil? ? ScalarGalaxy::FilePart.new(value, filename: "upload") : value
94
+ if value.to_path.nil?
95
+ ScalarGalaxy::FilePart.new(value, filename: "upload")
96
+ else
97
+ value
98
+ end
93
99
  in ScalarGalaxy::FilePart if value.content.is_a?(IO)
94
100
  state[:can_retry] = false
95
101
  value
@@ -126,7 +126,9 @@ module ScalarGalaxy
126
126
  def dump(value, state:)
127
127
  target = item_type
128
128
  if value.is_a?(Hash)
129
- value.transform_values { ScalarGalaxy::Internal::Type::Converter.dump(target, _1, state: state) }
129
+ value.transform_values do
130
+ ScalarGalaxy::Internal::Type::Converter.dump(target, _1, state: state)
131
+ end
130
132
  else
131
133
  super
132
134
  end
@@ -75,7 +75,9 @@ module ScalarGalaxy
75
75
  value.class
76
76
  in [Symbol, Hash]
77
77
  key =
78
- value.fetch(@discriminator) { value.fetch(@discriminator.to_s, ScalarGalaxy::Internal::OMIT) }
78
+ value.fetch(@discriminator) do
79
+ value.fetch(@discriminator.to_s, ScalarGalaxy::Internal::OMIT)
80
+ end
79
81
 
80
82
  return nil if key == ScalarGalaxy::Internal::OMIT
81
83
 
@@ -105,7 +107,8 @@ module ScalarGalaxy
105
107
  #
106
108
  # @return [Boolean]
107
109
  def ==(other)
108
- ScalarGalaxy::Internal::Type::Union === other && other.derefed_variants == derefed_variants
110
+ ScalarGalaxy::Internal::Type::Union === other &&
111
+ other.derefed_variants == derefed_variants
109
112
  end
110
113
 
111
114
  # @api public
@@ -136,11 +139,12 @@ module ScalarGalaxy
136
139
  #
137
140
  # @return [Object]
138
141
  def coerce(value, state:)
142
+ # Even discriminator-selected variants return through ensure below.
143
+ strictness = state.fetch(:strictness)
139
144
  if (target = resolve_variant(value))
140
145
  return ScalarGalaxy::Internal::Type::Converter.coerce(target, value, state: state)
141
146
  end
142
147
 
143
- strictness = state.fetch(:strictness)
144
148
  exactness = state.fetch(:exactness)
145
149
 
146
150
  alternatives = []
@@ -149,7 +153,8 @@ module ScalarGalaxy
149
153
  exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
150
154
  state[:branched] += 1
151
155
 
152
- coerced = ScalarGalaxy::Internal::Type::Converter.coerce(target, value, state: state)
156
+ coerced =
157
+ ScalarGalaxy::Internal::Type::Converter.coerce(target, value, state: state)
153
158
  yes, no, maybe = exact.values
154
159
  if (no + maybe).zero? || (!strictness && yes.positive?)
155
160
  exact.each { exactness[_1] += _2 }
@@ -201,7 +206,10 @@ module ScalarGalaxy
201
206
  #
202
207
  # @return [Object]
203
208
  def to_sorbet_type
204
- types = variants.map { ScalarGalaxy::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
209
+ types =
210
+ variants
211
+ .map { ScalarGalaxy::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }
212
+ .uniq
205
213
  case types
206
214
  in []
207
215
  T.noreturn
@@ -223,7 +231,10 @@ module ScalarGalaxy
223
231
  def inspect(depth: 0)
224
232
  return is_a?(Module) ? super() : self.class.name if depth.positive?
225
233
 
226
- members = variants.map { ScalarGalaxy::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
234
+ members =
235
+ variants.map do
236
+ ScalarGalaxy::Internal::Type::Converter.inspect(_1, depth: depth.succ)
237
+ end
227
238
  prefix = is_a?(Module) ? name : self.class.name
228
239
 
229
240
  "#{prefix}[#{members.join(" | ")}]"
@@ -28,7 +28,9 @@ module ScalarGalaxy
28
28
  # @param other [Object]
29
29
  #
30
30
  # @return [Boolean]
31
- def self.==(other) = other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::Unknown
31
+ def self.==(other)
32
+ other.is_a?(Class) && other <= ScalarGalaxy::Internal::Type::Unknown
33
+ end
32
34
 
33
35
  class << self
34
36
  # @api private