lithic 0.1.0.pre.alpha.32 → 0.1.0.pre.alpha.33

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5eb78c2204ed8fac68dfc7f66803f41c87c7c04dfd85380ca9d48d1c1fedb286
4
- data.tar.gz: d36cb38376196a1b2074cdfaa0da05051dbabe4b6972a2b6ef265ad6ce5cb441
3
+ metadata.gz: f8924d8ef7cad2f0246ba9420d2304fc5693c6684d32882f2ee6002616f69588
4
+ data.tar.gz: 8c95425e47cbfbcd622a7f0cea5632a27a4a413f54ecf90883ce62414e65867f
5
5
  SHA512:
6
- metadata.gz: cc6466f9b9f5e4022f40c7d0011ca70790fe51cb6fd0fd1ee70919b05f467d43503a621e2e90a6059899683e6bee8601242368ceddd28932ebb70e417be32d2b
7
- data.tar.gz: de563754d91056a8968b2d5b046be4ee6b464334a2db5a42969b7e805768812d980861935163adc4dc62f6d5ea08787026cb05ba9b031e8b7558c00f7bd5c6b0
6
+ metadata.gz: 668268fb4e37997bd3a854c9283b9c77f48b29f6301f4eab63dc36c3c86ed7faa26abc07ec531d9ac6e9c6faad8fe5a9dce150af99d64cc910db3c846f5a6e1a
7
+ data.tar.gz: bfba3fdfa14849cd336551d94000171413adcc1d5db58aa5c002006faf37e8eb14ea2e7e7d324ec05c2fceb6b94be99e62c694f3fe83f55d25c851cd02b8cf19
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.0-alpha.33 (2025-06-18)
4
+
5
+ Full Changelog: [v0.1.0-alpha.32...v0.1.0-alpha.33](https://github.com/lithic-com/lithic-ruby/compare/v0.1.0-alpha.32...v0.1.0-alpha.33)
6
+
7
+ ### Bug Fixes
8
+
9
+ * issue where we cannot mutate arrays on base model derivatives ([d9e95e7](https://github.com/lithic-com/lithic-ruby/commit/d9e95e745161befcf4908ea5fad28b5a9a30c131))
10
+
3
11
  ## 0.1.0-alpha.32 (2025-06-17)
4
12
 
5
13
  Full Changelog: [v0.1.0-alpha.31...v0.1.0-alpha.32](https://github.com/lithic-com/lithic-ruby/compare/v0.1.0-alpha.31...v0.1.0-alpha.32)
data/README.md CHANGED
@@ -15,7 +15,7 @@ To use this gem, install via Bundler by adding the following to your application
15
15
  <!-- x-release-please-start-version -->
16
16
 
17
17
  ```ruby
18
- gem "lithic", "~> 0.1.0.pre.alpha.32"
18
+ gem "lithic", "~> 0.1.0.pre.alpha.33"
19
19
  ```
20
20
 
21
21
  <!-- x-release-please-end -->
data/lib/lithic/errors.rb CHANGED
@@ -9,6 +9,28 @@ module Lithic
9
9
  end
10
10
 
11
11
  class ConversionError < Lithic::Errors::Error
12
+ # @return [StandardError, nil]
13
+ def cause = @cause.nil? ? super : @cause
14
+
15
+ # @api private
16
+ #
17
+ # @param on [Class<StandardError>]
18
+ # @param method [Symbol]
19
+ # @param target [Object]
20
+ # @param value [Object]
21
+ # @param cause [StandardError, nil]
22
+ def initialize(on:, method:, target:, value:, cause: nil)
23
+ cls = on.name.split("::").last
24
+
25
+ message = [
26
+ "Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
27
+ "To get the unparsed API response, use #{cls}[#{method.inspect}].",
28
+ cause && "Cause: #{cause.message}"
29
+ ].filter(&:itself).join(" ")
30
+
31
+ @cause = cause
32
+ super(message)
33
+ end
12
34
  end
13
35
 
14
36
  class APIError < Lithic::Errors::Error
@@ -62,10 +62,14 @@ module Lithic
62
62
  #
63
63
  # @param state [Hash{Symbol=>Object}] .
64
64
  #
65
- # @option state [Boolean, :strong] :strictness
65
+ # @option state [Boolean] :translate_names
66
+ #
67
+ # @option state [Boolean] :strictness
66
68
  #
67
69
  # @option state [Hash{Symbol=>Object}] :exactness
68
70
  #
71
+ # @option state [Class<StandardError>] :error
72
+ #
69
73
  # @option state [Integer] :branched
70
74
  #
71
75
  # @return [Array<Object>, Object]
@@ -74,6 +78,7 @@ module Lithic
74
78
 
75
79
  unless value.is_a?(Array)
76
80
  exactness[:no] += 1
81
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
77
82
  return value
78
83
  end
79
84
 
@@ -60,7 +60,7 @@ module Lithic
60
60
  [Lithic::Internal::Type::Converter.type_info(type_info), type_info]
61
61
  end
62
62
 
63
- setter = "#{name_sym}="
63
+ setter = :"#{name_sym}="
64
64
  api_name = info.fetch(:api_name, name_sym)
65
65
  nilable = info.fetch(:nil?, false)
66
66
  const = required && !nilable ? info.fetch(:const, Lithic::Internal::OMIT) : Lithic::Internal::OMIT
@@ -77,30 +77,61 @@ module Lithic
77
77
  type_fn: type_fn
78
78
  }
79
79
 
80
- define_method(setter) { @data.store(name_sym, _1) }
80
+ define_method(setter) do |value|
81
+ target = type_fn.call
82
+ state = Lithic::Internal::Type::Converter.new_coerce_state(translate_names: false)
83
+ coerced = Lithic::Internal::Type::Converter.coerce(target, value, state: state)
84
+ status = @coerced.store(name_sym, state.fetch(:error) || true)
85
+ stored =
86
+ case [target, status]
87
+ in [Lithic::Internal::Type::Converter | Symbol, true]
88
+ coerced
89
+ else
90
+ value
91
+ end
92
+ @data.store(name_sym, stored)
93
+ end
81
94
 
95
+ # rubocop:disable Style/CaseEquality
96
+ # rubocop:disable Metrics/BlockLength
82
97
  define_method(name_sym) do
83
98
  target = type_fn.call
84
- value = @data.fetch(name_sym) { const == Lithic::Internal::OMIT ? nil : const }
85
- state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
86
- if (nilable || !required) && value.nil?
87
- nil
88
- else
89
- Lithic::Internal::Type::Converter.coerce(
90
- target,
91
- value,
92
- state: state
99
+
100
+ case @coerced[name_sym]
101
+ in true | false if Lithic::Internal::Type::Converter === target
102
+ @data.fetch(name_sym)
103
+ in ::StandardError => e
104
+ raise Lithic::Errors::ConversionError.new(
105
+ on: self.class,
106
+ method: __method__,
107
+ target: target,
108
+ value: @data.fetch(name_sym),
109
+ cause: e
93
110
  )
111
+ else
112
+ Kernel.then do
113
+ value = @data.fetch(name_sym) { const == Lithic::Internal::OMIT ? nil : const }
114
+ state = Lithic::Internal::Type::Converter.new_coerce_state(translate_names: false)
115
+ if (nilable || !required) && value.nil?
116
+ nil
117
+ else
118
+ Lithic::Internal::Type::Converter.coerce(
119
+ target, value, state: state
120
+ )
121
+ end
122
+ rescue StandardError => e
123
+ raise Lithic::Errors::ConversionError.new(
124
+ on: self.class,
125
+ method: __method__,
126
+ target: target,
127
+ value: value,
128
+ cause: e
129
+ )
130
+ end
94
131
  end
95
- rescue StandardError => e
96
- cls = self.class.name.split("::").last
97
- message = [
98
- "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
99
- "To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
100
- "Cause: #{e.message}"
101
- ].join(" ")
102
- raise Lithic::Errors::ConversionError.new(message)
103
132
  end
133
+ # rubocop:enable Metrics/BlockLength
134
+ # rubocop:enable Style/CaseEquality
104
135
  end
105
136
 
106
137
  # @api private
@@ -200,23 +231,28 @@ module Lithic
200
231
  #
201
232
  # @param state [Hash{Symbol=>Object}] .
202
233
  #
203
- # @option state [Boolean, :strong] :strictness
234
+ # @option state [Boolean] :translate_names
235
+ #
236
+ # @option state [Boolean] :strictness
204
237
  #
205
238
  # @option state [Hash{Symbol=>Object}] :exactness
206
239
  #
240
+ # @option state [Class<StandardError>] :error
241
+ #
207
242
  # @option state [Integer] :branched
208
243
  #
209
244
  # @return [self, Object]
210
245
  def coerce(value, state:)
211
246
  exactness = state.fetch(:exactness)
212
247
 
213
- if value.is_a?(self.class)
248
+ if value.is_a?(self)
214
249
  exactness[:yes] += 1
215
250
  return value
216
251
  end
217
252
 
218
253
  unless (val = Lithic::Internal::Util.coerce_hash(value)).is_a?(Hash)
219
254
  exactness[:no] += 1
255
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
220
256
  return value
221
257
  end
222
258
  exactness[:yes] += 1
@@ -224,13 +260,15 @@ module Lithic
224
260
  keys = val.keys.to_set
225
261
  instance = new
226
262
  data = instance.to_h
263
+ status = instance.instance_variable_get(:@coerced)
227
264
 
228
265
  # rubocop:disable Metrics/BlockLength
229
266
  fields.each do |name, field|
230
267
  mode, required, target = field.fetch_values(:mode, :required, :type)
231
268
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
269
+ src_name = state.fetch(:translate_names) ? api_name : name
232
270
 
233
- unless val.key?(api_name)
271
+ unless val.key?(src_name)
234
272
  if required && mode != :dump && const == Lithic::Internal::OMIT
235
273
  exactness[nilable ? :maybe : :no] += 1
236
274
  else
@@ -239,9 +277,10 @@ module Lithic
239
277
  next
240
278
  end
241
279
 
242
- item = val.fetch(api_name)
243
- keys.delete(api_name)
280
+ item = val.fetch(src_name)
281
+ keys.delete(src_name)
244
282
 
283
+ state[:error] = nil
245
284
  converted =
246
285
  if item.nil? && (nilable || !required)
247
286
  exactness[nilable ? :yes : :maybe] += 1
@@ -255,6 +294,8 @@ module Lithic
255
294
  item
256
295
  end
257
296
  end
297
+
298
+ status.store(name, state.fetch(:error) || true)
258
299
  data.store(name, converted)
259
300
  end
260
301
  # rubocop:enable Metrics/BlockLength
@@ -430,7 +471,18 @@ module Lithic
430
471
  # Create a new instance of a model.
431
472
  #
432
473
  # @param data [Hash{Symbol=>Object}, self]
433
- def initialize(data = {}) = (@data = Lithic::Internal::Util.coerce_hash!(data).to_h)
474
+ def initialize(data = {})
475
+ @data = {}
476
+ @coerced = {}
477
+ Lithic::Internal::Util.coerce_hash!(data).each do
478
+ if self.class.known_fields.key?(_1)
479
+ public_send(:"#{_1}=", _2)
480
+ else
481
+ @data.store(_1, _2)
482
+ @coerced.store(_1, false)
483
+ end
484
+ end
485
+ end
434
486
 
435
487
  class << self
436
488
  # @api private
@@ -31,14 +31,20 @@ module Lithic
31
31
  class << self
32
32
  # @api private
33
33
  #
34
+ # Coerce value to Boolean if possible, otherwise return the original value.
35
+ #
34
36
  # @param value [Boolean, Object]
35
37
  #
36
38
  # @param state [Hash{Symbol=>Object}] .
37
39
  #
38
- # @option state [Boolean, :strong] :strictness
40
+ # @option state [Boolean] :translate_names
41
+ #
42
+ # @option state [Boolean] :strictness
39
43
  #
40
44
  # @option state [Hash{Symbol=>Object}] :exactness
41
45
  #
46
+ # @option state [Class<StandardError>] :error
47
+ #
42
48
  # @option state [Integer] :branched
43
49
  #
44
50
  # @return [Boolean, Object]
@@ -15,10 +15,14 @@ module Lithic
15
15
  #
16
16
  # @param state [Hash{Symbol=>Object}] .
17
17
  #
18
- # @option state [Boolean, :strong] :strictness
18
+ # @option state [Boolean] :translate_names
19
+ #
20
+ # @option state [Boolean] :strictness
19
21
  #
20
22
  # @option state [Hash{Symbol=>Object}] :exactness
21
23
  #
24
+ # @option state [Class<StandardError>] :error
25
+ #
22
26
  # @option state [Integer] :branched
23
27
  #
24
28
  # @return [Object]
@@ -94,6 +98,21 @@ module Lithic
94
98
  end
95
99
  end
96
100
 
101
+ # @api private
102
+ #
103
+ # @param translate_names [Boolean]
104
+ #
105
+ # @return [Hash{Symbol=>Object}]
106
+ def new_coerce_state(translate_names: true)
107
+ {
108
+ translate_names: translate_names,
109
+ strictness: true,
110
+ exactness: {yes: 0, no: 0, maybe: 0},
111
+ error: nil,
112
+ branched: 0
113
+ }
114
+ end
115
+
97
116
  # @api private
98
117
  #
99
118
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -110,14 +129,11 @@ module Lithic
110
129
  #
111
130
  # @param value [Object]
112
131
  #
113
- # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`, or `:strong`. This informs the
114
- # coercion strategy when we have to decide between multiple possible conversion
115
- # targets:
132
+ # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`. This informs the coercion strategy
133
+ # when we have to decide between multiple possible conversion targets:
116
134
  #
117
135
  # - `true`: the conversion must be exact, with minimum coercion.
118
136
  # - `false`: the conversion can be approximate, with some coercion.
119
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
120
- # if not possible.
121
137
  #
122
138
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
123
139
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -130,21 +146,20 @@ module Lithic
130
146
  #
131
147
  # See implementation below for more details.
132
148
  #
133
- # @option state [Boolean, :strong] :strictness
149
+ # @option state [Boolean] :translate_names
150
+ #
151
+ # @option state [Boolean] :strictness
134
152
  #
135
153
  # @option state [Hash{Symbol=>Object}] :exactness
136
154
  #
155
+ # @option state [Class<StandardError>] :error
156
+ #
137
157
  # @option state [Integer] :branched
138
158
  #
139
159
  # @return [Object]
140
- def coerce(
141
- target,
142
- value,
143
- state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
144
- )
145
- # rubocop:disable Lint/SuppressedException
160
+ def coerce(target, value, state: Lithic::Internal::Type::Converter.new_coerce_state)
146
161
  # rubocop:disable Metrics/BlockNesting
147
- strictness, exactness = state.fetch_values(:strictness, :exactness)
162
+ exactness = state.fetch(:exactness)
148
163
 
149
164
  case target
150
165
  in Lithic::Internal::Type::Converter
@@ -160,29 +175,26 @@ module Lithic
160
175
  exactness[value.nil? ? :yes : :maybe] += 1
161
176
  return nil
162
177
  in -> { _1 <= Integer }
163
- if value.is_a?(Integer)
178
+ case value
179
+ in Integer
164
180
  exactness[:yes] += 1
165
181
  return value
166
- elsif strictness == :strong && Integer(value, exception: false) != value
167
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
168
- raise value.is_a?(Numeric) ? ArgumentError.new(message) : TypeError.new(message)
169
182
  else
170
183
  Kernel.then do
171
184
  return Integer(value).tap { exactness[:maybe] += 1 }
172
- rescue ArgumentError, TypeError
185
+ rescue ArgumentError, TypeError => e
186
+ state[:error] = e
173
187
  end
174
188
  end
175
189
  in -> { _1 <= Float }
176
190
  if value.is_a?(Numeric)
177
191
  exactness[:yes] += 1
178
192
  return Float(value)
179
- elsif strictness == :strong
180
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
181
- raise TypeError.new(message)
182
193
  else
183
194
  Kernel.then do
184
195
  return Float(value).tap { exactness[:maybe] += 1 }
185
- rescue ArgumentError, TypeError
196
+ rescue ArgumentError, TypeError => e
197
+ state[:error] = e
186
198
  end
187
199
  end
188
200
  in -> { _1 <= String }
@@ -194,16 +206,13 @@ module Lithic
194
206
  exactness[:yes] += 1
195
207
  return value.string
196
208
  else
197
- if strictness == :strong
198
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
199
- raise TypeError.new(message)
200
- end
209
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{String}")
201
210
  end
202
211
  in -> { _1 <= Date || _1 <= Time }
203
212
  Kernel.then do
204
213
  return target.parse(value).tap { exactness[:yes] += 1 }
205
214
  rescue ArgumentError, TypeError => e
206
- raise e if strictness == :strong
215
+ state[:error] = e
207
216
  end
208
217
  in -> { _1 <= StringIO } if value.is_a?(String)
209
218
  exactness[:yes] += 1
@@ -221,10 +230,8 @@ module Lithic
221
230
  return value
222
231
  end
223
232
  else
224
- if strictness == :strong
225
- message = "cannot convert non-matching #{value.class} into #{target.inspect}"
226
- raise ArgumentError.new(message)
227
- end
233
+ message = "cannot convert non-matching #{value.class} into #{target.inspect}"
234
+ state[:error] = ArgumentError.new(message)
228
235
  end
229
236
  else
230
237
  end
@@ -232,7 +239,6 @@ module Lithic
232
239
  exactness[:no] += 1
233
240
  value
234
241
  # rubocop:enable Metrics/BlockNesting
235
- # rubocop:enable Lint/SuppressedException
236
242
  end
237
243
 
238
244
  # @api private
@@ -277,8 +283,10 @@ module Lithic
277
283
  define_sorbet_constant!(:CoerceState) do
278
284
  T.type_alias do
279
285
  {
280
- strictness: T.any(T::Boolean, Symbol),
286
+ translate_names: T::Boolean,
287
+ strictness: T::Boolean,
281
288
  exactness: {yes: Integer, no: Integer, maybe: Integer},
289
+ error: T::Class[StandardError],
282
290
  branched: Integer
283
291
  }
284
292
  end
@@ -77,10 +77,14 @@ module Lithic
77
77
  #
78
78
  # @param state [Hash{Symbol=>Object}] .
79
79
  #
80
- # @option state [Boolean, :strong] :strictness
80
+ # @option state [Boolean] :translate_names
81
+ #
82
+ # @option state [Boolean] :strictness
81
83
  #
82
84
  # @option state [Hash{Symbol=>Object}] :exactness
83
85
  #
86
+ # @option state [Class<StandardError>] :error
87
+ #
84
88
  # @option state [Integer] :branched
85
89
  #
86
90
  # @return [Symbol, Object]
@@ -91,8 +95,12 @@ module Lithic
91
95
  if values.include?(val)
92
96
  exactness[:yes] += 1
93
97
  val
98
+ elsif values.first&.class == val.class
99
+ exactness[:maybe] += 1
100
+ value
94
101
  else
95
- exactness[values.first&.class == val.class ? :maybe : :no] += 1
102
+ exactness[:no] += 1
103
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
96
104
  value
97
105
  end
98
106
  end
@@ -45,10 +45,14 @@ module Lithic
45
45
  #
46
46
  # @param state [Hash{Symbol=>Object}] .
47
47
  #
48
- # @option state [Boolean, :strong] :strictness
48
+ # @option state [Boolean] :translate_names
49
+ #
50
+ # @option state [Boolean] :strictness
49
51
  #
50
52
  # @option state [Hash{Symbol=>Object}] :exactness
51
53
  #
54
+ # @option state [Class<StandardError>] :error
55
+ #
52
56
  # @option state [Integer] :branched
53
57
  #
54
58
  # @return [StringIO, Object]
@@ -62,6 +66,7 @@ module Lithic
62
66
  exactness[:yes] += 1
63
67
  value
64
68
  else
69
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{StringIO}")
65
70
  exactness[:no] += 1
66
71
  value
67
72
  end
@@ -77,10 +77,14 @@ module Lithic
77
77
  #
78
78
  # @param state [Hash{Symbol=>Object}] .
79
79
  #
80
- # @option state [Boolean, :strong] :strictness
80
+ # @option state [Boolean] :translate_names
81
+ #
82
+ # @option state [Boolean] :strictness
81
83
  #
82
84
  # @option state [Hash{Symbol=>Object}] :exactness
83
85
  #
86
+ # @option state [Class<StandardError>] :error
87
+ #
84
88
  # @option state [Integer] :branched
85
89
  #
86
90
  # @return [Hash{Symbol=>Object}, Object]
@@ -89,6 +93,7 @@ module Lithic
89
93
 
90
94
  unless value.is_a?(Hash)
91
95
  exactness[:no] += 1
96
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
92
97
  return value
93
98
  end
94
99
 
@@ -126,14 +126,23 @@ module Lithic
126
126
 
127
127
  # @api private
128
128
  #
129
+ # Tries to efficiently coerce the given value to one of the known variants.
130
+ #
131
+ # If the value cannot match any of the known variants, the coercion is considered
132
+ # non-viable and returns the original value.
133
+ #
129
134
  # @param value [Object]
130
135
  #
131
136
  # @param state [Hash{Symbol=>Object}] .
132
137
  #
133
- # @option state [Boolean, :strong] :strictness
138
+ # @option state [Boolean] :translate_names
139
+ #
140
+ # @option state [Boolean] :strictness
134
141
  #
135
142
  # @option state [Hash{Symbol=>Object}] :exactness
136
143
  #
144
+ # @option state [Class<StandardError>] :error
145
+ #
137
146
  # @option state [Integer] :branched
138
147
  #
139
148
  # @return [Object]
@@ -144,7 +153,6 @@ module Lithic
144
153
 
145
154
  strictness = state.fetch(:strictness)
146
155
  exactness = state.fetch(:exactness)
147
- state[:strictness] = strictness == :strong ? true : strictness
148
156
 
149
157
  alternatives = []
150
158
  known_variants.each do |_, variant_fn|
@@ -163,13 +171,10 @@ module Lithic
163
171
  end
164
172
  end
165
173
 
166
- case alternatives.sort_by(&:first)
174
+ case alternatives.sort_by!(&:first)
167
175
  in []
168
176
  exactness[:no] += 1
169
- if strictness == :strong
170
- message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
171
- raise ArgumentError.new(message)
172
- end
177
+ state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
173
178
  value
174
179
  in [[_, exact, coerced], *]
175
180
  exact.each { exactness[_1] += _2 }
@@ -33,14 +33,20 @@ module Lithic
33
33
  class << self
34
34
  # @api private
35
35
  #
36
+ # No coercion needed for Unknown type.
37
+ #
36
38
  # @param value [Object]
37
39
  #
38
40
  # @param state [Hash{Symbol=>Object}] .
39
41
  #
40
- # @option state [Boolean, :strong] :strictness
42
+ # @option state [Boolean] :translate_names
43
+ #
44
+ # @option state [Boolean] :strictness
41
45
  #
42
46
  # @option state [Hash{Symbol=>Object}] :exactness
43
47
  #
48
+ # @option state [Class<StandardError>] :error
49
+ #
44
50
  # @option state [Integer] :branched
45
51
  #
46
52
  # @return [Object]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lithic
4
- VERSION = "0.1.0.pre.alpha.32"
4
+ VERSION = "0.1.0.pre.alpha.33"
5
5
  end
@@ -8,6 +8,22 @@ module Lithic
8
8
  end
9
9
 
10
10
  class ConversionError < Lithic::Errors::Error
11
+ sig { returns(T.nilable(StandardError)) }
12
+ def cause
13
+ end
14
+
15
+ # @api private
16
+ sig do
17
+ params(
18
+ on: T::Class[StandardError],
19
+ method: Symbol,
20
+ target: T.anything,
21
+ value: T.anything,
22
+ cause: T.nilable(StandardError)
23
+ ).returns(T.attached_class)
24
+ end
25
+ def self.new(on:, method:, target:, value:, cause: nil)
26
+ end
11
27
  end
12
28
 
13
29
  class APIError < Lithic::Errors::Error
@@ -22,6 +22,8 @@ module Lithic
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # Coerce value to Boolean if possible, otherwise return the original value.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -15,12 +15,14 @@ module Lithic
15
15
  CoerceState =
16
16
  T.type_alias do
17
17
  {
18
- strictness: T.any(T::Boolean, Symbol),
18
+ translate_names: T::Boolean,
19
+ strictness: T::Boolean,
19
20
  exactness: {
20
21
  yes: Integer,
21
22
  no: Integer,
22
23
  maybe: Integer
23
24
  },
25
+ error: T::Class[StandardError],
24
26
  branched: Integer
25
27
  }
26
28
  end
@@ -84,6 +86,15 @@ module Lithic
84
86
  def self.type_info(spec)
85
87
  end
86
88
 
89
+ # @api private
90
+ sig do
91
+ params(translate_names: T::Boolean).returns(
92
+ Lithic::Internal::Type::Converter::CoerceState
93
+ )
94
+ end
95
+ def self.new_coerce_state(translate_names: true)
96
+ end
97
+
87
98
  # @api private
88
99
  #
89
100
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -105,14 +116,11 @@ module Lithic
105
116
  def self.coerce(
106
117
  target,
107
118
  value,
108
- # The `strictness` is one of `true`, `false`, or `:strong`. This informs the
109
- # coercion strategy when we have to decide between multiple possible conversion
110
- # targets:
119
+ # The `strictness` is one of `true`, `false`. This informs the coercion strategy
120
+ # when we have to decide between multiple possible conversion targets:
111
121
  #
112
122
  # - `true`: the conversion must be exact, with minimum coercion.
113
123
  # - `false`: the conversion can be approximate, with some coercion.
114
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
115
- # if not possible.
116
124
  #
117
125
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
118
126
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -124,15 +132,7 @@ module Lithic
124
132
  # - `no`: the value cannot be converted to the target type.
125
133
  #
126
134
  # See implementation below for more details.
127
- state: {
128
- strictness: true,
129
- exactness: {
130
- yes: 0,
131
- no: 0,
132
- maybe: 0
133
- },
134
- branched: 0
135
- }
135
+ state: Lithic::Internal::Type::Converter.new_coerce_state
136
136
  )
137
137
  end
138
138
 
@@ -78,6 +78,11 @@ module Lithic
78
78
  end
79
79
 
80
80
  # @api private
81
+ #
82
+ # Tries to efficiently coerce the given value to one of the known variants.
83
+ #
84
+ # If the value cannot match any of the known variants, the coercion is considered
85
+ # non-viable and returns the original value.
81
86
  sig do
82
87
  override
83
88
  .params(
@@ -22,6 +22,8 @@ module Lithic
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # No coercion needed for Unknown type.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -5,6 +5,15 @@ module Lithic
5
5
  end
6
6
 
7
7
  class ConversionError < Lithic::Errors::Error
8
+ def cause: -> StandardError?
9
+
10
+ def initialize: (
11
+ on: Class,
12
+ method: Symbol,
13
+ target: top,
14
+ value: top,
15
+ ?cause: StandardError?
16
+ ) -> void
8
17
  end
9
18
 
10
19
  class APIError < Lithic::Errors::Error
@@ -8,8 +8,10 @@ module Lithic
8
8
 
9
9
  type coerce_state =
10
10
  {
11
- strictness: bool | :strong,
11
+ translate_names: bool,
12
+ strictness: bool,
12
13
  exactness: { yes: Integer, no: Integer, maybe: Integer },
14
+ error: Class,
13
15
  branched: Integer
14
16
  }
15
17
 
@@ -37,6 +39,10 @@ module Lithic
37
39
  | Lithic::Internal::Type::Converter::input spec
38
40
  ) -> (^-> top)
39
41
 
42
+ def self.new_coerce_state: (
43
+ ?translate_names: bool
44
+ ) -> Lithic::Internal::Type::Converter::coerce_state
45
+
40
46
  def self.coerce: (
41
47
  Lithic::Internal::Type::Converter::input target,
42
48
  top value,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lithic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.alpha.32
4
+ version: 0.1.0.pre.alpha.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lithic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-17 00:00:00.000000000 Z
11
+ date: 2025-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool