e-invoice-api 0.1.0.pre.alpha.8 → 0.1.0.pre.alpha.9

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: 29f299e79acc7b28113415c7b35518ce360e31b2e6b4e2d36d4c9735f386177e
4
- data.tar.gz: 89176daa65b4cc1f40607fc98923683dae4415bfd0a13df618b569c3a5c1cc0b
3
+ metadata.gz: 63d8a2650c5071d02e5d983c81dc2b15a1a3b675e9089854d322c30c82993e75
4
+ data.tar.gz: c685af117abd63131cfc8c0ff2fb1cffbaa1a9a30aeda7ae54a4ba3ff5f603a4
5
5
  SHA512:
6
- metadata.gz: 99e65b329f01c92709d21230d53397c59f988abf3078c1b46b596c876f63c7e2b616379b630e2e13bbeb59d9b03cd0978b7cf87e6cf7e539e58ad512fef0cffd
7
- data.tar.gz: 21c9875807fbad0ca51dd935c8bad061e525efa6abed9eebf1ef9446fc70888b563e5cd619f6c80b7e1f097545e510cbc54e68f6e6649d085e4eba9f1623b43b
6
+ metadata.gz: 90c098916fdd7bb8cccfe173ba5ec981bc03e52277b9268badad6c435312f9279fedbb76d1c50dee0977235251ccb3d45bf999069ff98eb32624478d84a05be4
7
+ data.tar.gz: 0c04ef2a3f746dbf89371e6ba26a2093a46dc44541a3903d0ce98636ce4994759a15f517121048fa204d9734c4f0ff9c3570e3cf7be103fc2c369f68edb841b8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.0-alpha.9 (2025-06-18)
4
+
5
+ Full Changelog: [v0.1.0-alpha.8...v0.1.0-alpha.9](https://github.com/e-invoice-be/e-invoice-rb/compare/v0.1.0-alpha.8...v0.1.0-alpha.9)
6
+
7
+ ### Bug Fixes
8
+
9
+ * issue where we cannot mutate arrays on base model derivatives ([24f07da](https://github.com/e-invoice-be/e-invoice-rb/commit/24f07da0a277afd9b9462b15334d405502444976))
10
+
11
+
12
+ ### Chores
13
+
14
+ * **internal:** version bump ([71e3d19](https://github.com/e-invoice-be/e-invoice-rb/commit/71e3d199793955c3571ed8ee975c2670c96db1b8))
15
+
3
16
  ## 0.1.0-alpha.8 (2025-06-17)
4
17
 
5
18
  Full Changelog: [v0.1.0-alpha.7...v0.1.0-alpha.8](https://github.com/e-invoice-be/e-invoice-rb/compare/v0.1.0-alpha.7...v0.1.0-alpha.8)
data/README.md CHANGED
@@ -17,7 +17,7 @@ To use this gem, install via Bundler by adding the following to your application
17
17
  <!-- x-release-please-start-version -->
18
18
 
19
19
  ```ruby
20
- gem "e-invoice-api", "~> 0.1.0.pre.alpha.8"
20
+ gem "e-invoice-api", "~> 0.1.0.pre.alpha.9"
21
21
  ```
22
22
 
23
23
  <!-- x-release-please-end -->
@@ -9,6 +9,28 @@ module EInvoiceAPI
9
9
  end
10
10
 
11
11
  class ConversionError < EInvoiceAPI::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 < EInvoiceAPI::Errors::Error
@@ -62,10 +62,14 @@ module EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
60
60
  [EInvoiceAPI::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 = if required && !nilable
@@ -84,28 +84,61 @@ module EInvoiceAPI
84
84
  type_fn: type_fn
85
85
  }
86
86
 
87
- define_method(setter) { @data.store(name_sym, _1) }
87
+ define_method(setter) do |value|
88
+ target = type_fn.call
89
+ state = EInvoiceAPI::Internal::Type::Converter.new_coerce_state(translate_names: false)
90
+ coerced = EInvoiceAPI::Internal::Type::Converter.coerce(target, value, state: state)
91
+ status = @coerced.store(name_sym, state.fetch(:error) || true)
92
+ stored =
93
+ case [target, status]
94
+ in [EInvoiceAPI::Internal::Type::Converter | Symbol, true]
95
+ coerced
96
+ else
97
+ value
98
+ end
99
+ @data.store(name_sym, stored)
100
+ end
88
101
 
102
+ # rubocop:disable Style/CaseEquality
103
+ # rubocop:disable Metrics/BlockLength
89
104
  define_method(name_sym) do
90
105
  target = type_fn.call
91
- value = @data.fetch(name_sym) { const == EInvoiceAPI::Internal::OMIT ? nil : const }
92
- state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
93
- if (nilable || !required) && value.nil?
94
- nil
95
- else
96
- EInvoiceAPI::Internal::Type::Converter.coerce(
97
- target, value, state: state
106
+
107
+ case @coerced[name_sym]
108
+ in true | false if EInvoiceAPI::Internal::Type::Converter === target
109
+ @data.fetch(name_sym)
110
+ in ::StandardError => e
111
+ raise EInvoiceAPI::Errors::ConversionError.new(
112
+ on: self.class,
113
+ method: __method__,
114
+ target: target,
115
+ value: @data.fetch(name_sym),
116
+ cause: e
98
117
  )
118
+ else
119
+ Kernel.then do
120
+ value = @data.fetch(name_sym) { const == EInvoiceAPI::Internal::OMIT ? nil : const }
121
+ state = EInvoiceAPI::Internal::Type::Converter.new_coerce_state(translate_names: false)
122
+ if (nilable || !required) && value.nil?
123
+ nil
124
+ else
125
+ EInvoiceAPI::Internal::Type::Converter.coerce(
126
+ target, value, state: state
127
+ )
128
+ end
129
+ rescue StandardError => e
130
+ raise EInvoiceAPI::Errors::ConversionError.new(
131
+ on: self.class,
132
+ method: __method__,
133
+ target: target,
134
+ value: value,
135
+ cause: e
136
+ )
137
+ end
99
138
  end
100
- rescue StandardError => e
101
- cls = self.class.name.split("::").last
102
- message = [
103
- "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
104
- "To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
105
- "Cause: #{e.message}"
106
- ].join(" ")
107
- raise EInvoiceAPI::Errors::ConversionError.new(message)
108
139
  end
140
+ # rubocop:enable Metrics/BlockLength
141
+ # rubocop:enable Style/CaseEquality
109
142
  end
110
143
 
111
144
  # @api private
@@ -205,23 +238,28 @@ module EInvoiceAPI
205
238
  #
206
239
  # @param state [Hash{Symbol=>Object}] .
207
240
  #
208
- # @option state [Boolean, :strong] :strictness
241
+ # @option state [Boolean] :translate_names
242
+ #
243
+ # @option state [Boolean] :strictness
209
244
  #
210
245
  # @option state [Hash{Symbol=>Object}] :exactness
211
246
  #
247
+ # @option state [Class<StandardError>] :error
248
+ #
212
249
  # @option state [Integer] :branched
213
250
  #
214
251
  # @return [self, Object]
215
252
  def coerce(value, state:)
216
253
  exactness = state.fetch(:exactness)
217
254
 
218
- if value.is_a?(self.class)
255
+ if value.is_a?(self)
219
256
  exactness[:yes] += 1
220
257
  return value
221
258
  end
222
259
 
223
260
  unless (val = EInvoiceAPI::Internal::Util.coerce_hash(value)).is_a?(Hash)
224
261
  exactness[:no] += 1
262
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
225
263
  return value
226
264
  end
227
265
  exactness[:yes] += 1
@@ -229,13 +267,15 @@ module EInvoiceAPI
229
267
  keys = val.keys.to_set
230
268
  instance = new
231
269
  data = instance.to_h
270
+ status = instance.instance_variable_get(:@coerced)
232
271
 
233
272
  # rubocop:disable Metrics/BlockLength
234
273
  fields.each do |name, field|
235
274
  mode, required, target = field.fetch_values(:mode, :required, :type)
236
275
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
276
+ src_name = state.fetch(:translate_names) ? api_name : name
237
277
 
238
- unless val.key?(api_name)
278
+ unless val.key?(src_name)
239
279
  if required && mode != :dump && const == EInvoiceAPI::Internal::OMIT
240
280
  exactness[nilable ? :maybe : :no] += 1
241
281
  else
@@ -244,9 +284,10 @@ module EInvoiceAPI
244
284
  next
245
285
  end
246
286
 
247
- item = val.fetch(api_name)
248
- keys.delete(api_name)
287
+ item = val.fetch(src_name)
288
+ keys.delete(src_name)
249
289
 
290
+ state[:error] = nil
250
291
  converted =
251
292
  if item.nil? && (nilable || !required)
252
293
  exactness[nilable ? :yes : :maybe] += 1
@@ -260,6 +301,8 @@ module EInvoiceAPI
260
301
  item
261
302
  end
262
303
  end
304
+
305
+ status.store(name, state.fetch(:error) || true)
263
306
  data.store(name, converted)
264
307
  end
265
308
  # rubocop:enable Metrics/BlockLength
@@ -435,7 +478,18 @@ module EInvoiceAPI
435
478
  # Create a new instance of a model.
436
479
  #
437
480
  # @param data [Hash{Symbol=>Object}, self]
438
- def initialize(data = {}) = (@data = EInvoiceAPI::Internal::Util.coerce_hash!(data).to_h)
481
+ def initialize(data = {})
482
+ @data = {}
483
+ @coerced = {}
484
+ EInvoiceAPI::Internal::Util.coerce_hash!(data).each do
485
+ if self.class.known_fields.key?(_1)
486
+ public_send(:"#{_1}=", _2)
487
+ else
488
+ @data.store(_1, _2)
489
+ @coerced.store(_1, false)
490
+ end
491
+ end
492
+ end
439
493
 
440
494
  class << self
441
495
  # @api private
@@ -31,14 +31,20 @@ module EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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: EInvoiceAPI::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 EInvoiceAPI::Internal::Type::Converter
@@ -160,29 +175,26 @@ module EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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
@@ -81,10 +81,14 @@ module EInvoiceAPI
81
81
  #
82
82
  # @param state [Hash{Symbol=>Object}] .
83
83
  #
84
- # @option state [Boolean, :strong] :strictness
84
+ # @option state [Boolean] :translate_names
85
+ #
86
+ # @option state [Boolean] :strictness
85
87
  #
86
88
  # @option state [Hash{Symbol=>Object}] :exactness
87
89
  #
90
+ # @option state [Class<StandardError>] :error
91
+ #
88
92
  # @option state [Integer] :branched
89
93
  #
90
94
  # @return [Symbol, Object]
@@ -95,8 +99,12 @@ module EInvoiceAPI
95
99
  if values.include?(val)
96
100
  exactness[:yes] += 1
97
101
  val
102
+ elsif values.first&.class == val.class
103
+ exactness[:maybe] += 1
104
+ value
98
105
  else
99
- exactness[values.first&.class == val.class ? :maybe : :no] += 1
106
+ exactness[:no] += 1
107
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
100
108
  value
101
109
  end
102
110
  end
@@ -45,10 +45,14 @@ module EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
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
 
@@ -115,14 +115,23 @@ module EInvoiceAPI
115
115
 
116
116
  # @api private
117
117
  #
118
+ # Tries to efficiently coerce the given value to one of the known variants.
119
+ #
120
+ # If the value cannot match any of the known variants, the coercion is considered
121
+ # non-viable and returns the original value.
122
+ #
118
123
  # @param value [Object]
119
124
  #
120
125
  # @param state [Hash{Symbol=>Object}] .
121
126
  #
122
- # @option state [Boolean, :strong] :strictness
127
+ # @option state [Boolean] :translate_names
128
+ #
129
+ # @option state [Boolean] :strictness
123
130
  #
124
131
  # @option state [Hash{Symbol=>Object}] :exactness
125
132
  #
133
+ # @option state [Class<StandardError>] :error
134
+ #
126
135
  # @option state [Integer] :branched
127
136
  #
128
137
  # @return [Object]
@@ -133,7 +142,6 @@ module EInvoiceAPI
133
142
 
134
143
  strictness = state.fetch(:strictness)
135
144
  exactness = state.fetch(:exactness)
136
- state[:strictness] = strictness == :strong ? true : strictness
137
145
 
138
146
  alternatives = []
139
147
  known_variants.each do |_, variant_fn|
@@ -152,13 +160,10 @@ module EInvoiceAPI
152
160
  end
153
161
  end
154
162
 
155
- case alternatives.sort_by(&:first)
163
+ case alternatives.sort_by!(&:first)
156
164
  in []
157
165
  exactness[:no] += 1
158
- if strictness == :strong
159
- message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
160
- raise ArgumentError.new(message)
161
- end
166
+ state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
162
167
  value
163
168
  in [[_, exact, coerced], *]
164
169
  exact.each { exactness[_1] += _2 }
@@ -33,14 +33,20 @@ module EInvoiceAPI
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 EInvoiceAPI
4
- VERSION = "0.1.0.pre.alpha.8"
4
+ VERSION = "0.1.0.pre.alpha.9"
5
5
  end
@@ -8,6 +8,22 @@ module EInvoiceAPI
8
8
  end
9
9
 
10
10
  class ConversionError < EInvoiceAPI::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 < EInvoiceAPI::Errors::Error
@@ -22,6 +22,8 @@ module EInvoiceAPI
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 EInvoiceAPI
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
@@ -88,6 +90,15 @@ module EInvoiceAPI
88
90
  def self.type_info(spec)
89
91
  end
90
92
 
93
+ # @api private
94
+ sig do
95
+ params(translate_names: T::Boolean).returns(
96
+ EInvoiceAPI::Internal::Type::Converter::CoerceState
97
+ )
98
+ end
99
+ def self.new_coerce_state(translate_names: true)
100
+ end
101
+
91
102
  # @api private
92
103
  #
93
104
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -109,14 +120,11 @@ module EInvoiceAPI
109
120
  def self.coerce(
110
121
  target,
111
122
  value,
112
- # The `strictness` is one of `true`, `false`, or `:strong`. This informs the
113
- # coercion strategy when we have to decide between multiple possible conversion
114
- # targets:
123
+ # The `strictness` is one of `true`, `false`. This informs the coercion strategy
124
+ # when we have to decide between multiple possible conversion targets:
115
125
  #
116
126
  # - `true`: the conversion must be exact, with minimum coercion.
117
127
  # - `false`: the conversion can be approximate, with some coercion.
118
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
119
- # if not possible.
120
128
  #
121
129
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
122
130
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -128,15 +136,7 @@ module EInvoiceAPI
128
136
  # - `no`: the value cannot be converted to the target type.
129
137
  #
130
138
  # See implementation below for more details.
131
- state: {
132
- strictness: true,
133
- exactness: {
134
- yes: 0,
135
- no: 0,
136
- maybe: 0
137
- },
138
- branched: 0
139
- }
139
+ state: EInvoiceAPI::Internal::Type::Converter.new_coerce_state
140
140
  )
141
141
  end
142
142
 
@@ -78,6 +78,11 @@ module EInvoiceAPI
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 EInvoiceAPI
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 EInvoiceAPI
5
5
  end
6
6
 
7
7
  class ConversionError < EInvoiceAPI::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 < EInvoiceAPI::Errors::Error
@@ -8,8 +8,10 @@ module EInvoiceAPI
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 EInvoiceAPI
37
39
  | EInvoiceAPI::Internal::Type::Converter::input spec
38
40
  ) -> (^-> top)
39
41
 
42
+ def self.new_coerce_state: (
43
+ ?translate_names: bool
44
+ ) -> EInvoiceAPI::Internal::Type::Converter::coerce_state
45
+
40
46
  def self.coerce: (
41
47
  EInvoiceAPI::Internal::Type::Converter::input target,
42
48
  top value,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e-invoice-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.alpha.8
4
+ version: 0.1.0.pre.alpha.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - e-invoice
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-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool