knockapi 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/README.md +1 -1
- data/lib/knockapi/errors.rb +22 -0
- data/lib/knockapi/internal/type/array_of.rb +6 -1
- data/lib/knockapi/internal/type/base_model.rb +77 -25
- data/lib/knockapi/internal/type/boolean.rb +7 -1
- data/lib/knockapi/internal/type/converter.rb +42 -34
- data/lib/knockapi/internal/type/enum.rb +10 -2
- data/lib/knockapi/internal/type/file_input.rb +6 -1
- data/lib/knockapi/internal/type/hash_of.rb +6 -1
- data/lib/knockapi/internal/type/union.rb +12 -7
- data/lib/knockapi/internal/type/unknown.rb +7 -1
- data/lib/knockapi/models/audience_add_members_params.rb +2 -2
- data/lib/knockapi/models/audience_member.rb +2 -2
- data/lib/knockapi/models/audience_remove_members_params.rb +2 -2
- data/lib/knockapi/models/identify_user_request.rb +2 -2
- data/lib/knockapi/models/inline_identify_user_request.rb +4 -4
- data/lib/knockapi/models/message.rb +9 -1
- data/lib/knockapi/models/message_get_content_response.rb +2 -2
- data/lib/knockapi/models/message_list_activities_params.rb +2 -2
- data/lib/knockapi/models/message_list_delivery_logs_params.rb +2 -2
- data/lib/knockapi/models/message_list_events_params.rb +2 -2
- data/lib/knockapi/models/message_list_params.rb +2 -2
- data/lib/knockapi/models/messages/batch_get_content_response.rb +2 -2
- data/lib/knockapi/models/object_list_messages_params.rb +2 -2
- data/lib/knockapi/models/object_list_params.rb +2 -2
- data/lib/knockapi/models/object_list_schedules_params.rb +2 -2
- data/lib/knockapi/models/object_list_subscriptions_params.rb +2 -2
- data/lib/knockapi/models/page_info.rb +2 -2
- data/lib/knockapi/models/recipients/channel_data.rb +3 -3
- data/lib/knockapi/models/recipients/channel_data_request.rb +3 -3
- data/lib/knockapi/models/recipients/inline_channel_data_request.rb +3 -3
- data/lib/knockapi/models/recipients/ms_teams_channel_data.rb +1 -1
- data/lib/knockapi/models/recipients/push_channel_data.rb +1 -1
- data/lib/knockapi/models/recipients/slack_channel_data.rb +1 -1
- data/lib/knockapi/models/schedule_list_params.rb +2 -2
- data/lib/knockapi/models/tenant_list_params.rb +2 -2
- data/lib/knockapi/models/user.rb +4 -4
- data/lib/knockapi/models/user_list_messages_params.rb +2 -2
- data/lib/knockapi/models/user_list_params.rb +2 -2
- data/lib/knockapi/models/user_list_schedules_params.rb +2 -2
- data/lib/knockapi/models/user_list_subscriptions_params.rb +2 -2
- data/lib/knockapi/models/users/feed_list_items_params.rb +4 -4
- data/lib/knockapi/resources/messages.rb +4 -4
- data/lib/knockapi/resources/objects.rb +4 -4
- data/lib/knockapi/resources/schedules.rb +1 -1
- data/lib/knockapi/resources/tenants.rb +1 -1
- data/lib/knockapi/resources/users/feeds.rb +18 -6
- data/lib/knockapi/resources/users/guides.rb +4 -4
- data/lib/knockapi/resources/users.rb +15 -15
- data/lib/knockapi/version.rb +1 -1
- data/rbi/knockapi/errors.rbi +16 -0
- data/rbi/knockapi/internal/type/boolean.rbi +2 -0
- data/rbi/knockapi/internal/type/converter.rbi +15 -15
- data/rbi/knockapi/internal/type/union.rbi +5 -0
- data/rbi/knockapi/internal/type/unknown.rbi +2 -0
- data/rbi/knockapi/models/audience_add_members_params.rbi +2 -2
- data/rbi/knockapi/models/audience_member.rbi +2 -2
- data/rbi/knockapi/models/audience_remove_members_params.rbi +2 -2
- data/rbi/knockapi/models/identify_user_request.rbi +2 -2
- data/rbi/knockapi/models/inline_identify_user_request.rbi +4 -4
- data/rbi/knockapi/models/message.rbi +11 -3
- data/rbi/knockapi/models/message_get_content_response.rbi +1 -1
- data/rbi/knockapi/models/message_list_activities_params.rbi +2 -2
- data/rbi/knockapi/models/message_list_delivery_logs_params.rbi +2 -2
- data/rbi/knockapi/models/message_list_events_params.rbi +2 -2
- data/rbi/knockapi/models/message_list_params.rbi +2 -2
- data/rbi/knockapi/models/messages/batch_get_content_response.rbi +1 -1
- data/rbi/knockapi/models/object_list_messages_params.rbi +2 -2
- data/rbi/knockapi/models/object_list_params.rbi +2 -2
- data/rbi/knockapi/models/object_list_schedules_params.rbi +2 -2
- data/rbi/knockapi/models/object_list_subscriptions_params.rbi +2 -2
- data/rbi/knockapi/models/page_info.rbi +2 -2
- data/rbi/knockapi/models/recipients/ms_teams_channel_data.rbi +1 -1
- data/rbi/knockapi/models/recipients/push_channel_data.rbi +1 -1
- data/rbi/knockapi/models/recipients/slack_channel_data.rbi +1 -1
- data/rbi/knockapi/models/schedule_list_params.rbi +2 -2
- data/rbi/knockapi/models/tenant_list_params.rbi +2 -2
- data/rbi/knockapi/models/user.rbi +4 -4
- data/rbi/knockapi/models/user_list_messages_params.rbi +2 -2
- data/rbi/knockapi/models/user_list_params.rbi +2 -2
- data/rbi/knockapi/models/user_list_schedules_params.rbi +2 -2
- data/rbi/knockapi/models/user_list_subscriptions_params.rbi +2 -2
- data/rbi/knockapi/models/users/feed_list_items_params.rbi +4 -4
- data/rbi/knockapi/resources/messages.rbi +4 -4
- data/rbi/knockapi/resources/objects.rbi +4 -4
- data/rbi/knockapi/resources/schedules.rbi +1 -1
- data/rbi/knockapi/resources/tenants.rbi +1 -1
- data/rbi/knockapi/resources/users/feeds.rbi +18 -6
- data/rbi/knockapi/resources/users/guides.rbi +4 -4
- data/rbi/knockapi/resources/users.rbi +15 -15
- data/sig/knockapi/errors.rbs +9 -0
- data/sig/knockapi/internal/type/converter.rbs +7 -1
- data/sig/knockapi/models/message.rbs +8 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d32e3d8651dc87c73b9b678f58a739ebae4ec3bf39566add6a6deea3713f572
|
4
|
+
data.tar.gz: 2f67c250f6d52a2e923ed33796b7d12ce0d45f74605c5a3a52e2d9590ea658c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60af3d7b09617d1f215e696682c0b0e870d41676446f681de43189ae5f150af62d5e76af8e5b95ecd7b819a3db31a23903d1c65e4dd912b7f7cdf466be110436
|
7
|
+
data.tar.gz: e4b370e0705f79f236d1d8796d47c3846dd0891d38a83336ea64ef3c1c383c8d22b38ddbdfa1bc34d9efbfc3eb69761383d3dad1c1f56e574af0662fe9f46d40
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,32 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.9.0 (2025-06-27)
|
4
|
+
|
5
|
+
Full Changelog: [v1.8.1...v1.9.0](https://github.com/knocklabs/knock-ruby/compare/v1.8.1...v1.9.0)
|
6
|
+
|
7
|
+
### Features
|
8
|
+
|
9
|
+
* **api:** api update ([fc8b2d2](https://github.com/knocklabs/knock-ruby/commit/fc8b2d240d0ae0185fea80a1be818505f94f1d16))
|
10
|
+
* **api:** api update ([f4f2f96](https://github.com/knocklabs/knock-ruby/commit/f4f2f96ded6152ac1d38ddbded881e306cd682ec))
|
11
|
+
|
12
|
+
|
13
|
+
### Bug Fixes
|
14
|
+
|
15
|
+
* **ci:** release-doctor — report correct token name ([dd5f4e2](https://github.com/knocklabs/knock-ruby/commit/dd5f4e20bca8d0881af1593f0a164963e31a1bb9))
|
16
|
+
|
17
|
+
## 1.8.1 (2025-06-18)
|
18
|
+
|
19
|
+
Full Changelog: [v1.8.0...v1.8.1](https://github.com/knocklabs/knock-ruby/compare/v1.8.0...v1.8.1)
|
20
|
+
|
21
|
+
### Bug Fixes
|
22
|
+
|
23
|
+
* issue where we cannot mutate arrays on base model derivatives ([2c56679](https://github.com/knocklabs/knock-ruby/commit/2c56679d7f62da36dcae21fe5944d24211afe854))
|
24
|
+
|
25
|
+
|
26
|
+
### Chores
|
27
|
+
|
28
|
+
* **ci:** enable for pull requests ([985de4e](https://github.com/knocklabs/knock-ruby/commit/985de4e91baf5684d7cf308f6e3de661fbb8163d))
|
29
|
+
|
3
30
|
## 1.8.0 (2025-06-13)
|
4
31
|
|
5
32
|
Full Changelog: [v1.7.0...v1.8.0](https://github.com/knocklabs/knock-ruby/compare/v1.7.0...v1.8.0)
|
data/README.md
CHANGED
data/lib/knockapi/errors.rb
CHANGED
@@ -9,6 +9,28 @@ module Knockapi
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class ConversionError < Knockapi::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 < Knockapi::Errors::Error
|
@@ -62,10 +62,14 @@ module Knockapi
|
|
62
62
|
#
|
63
63
|
# @param state [Hash{Symbol=>Object}] .
|
64
64
|
#
|
65
|
-
# @option state [Boolean
|
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 Knockapi
|
|
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 Knockapi
|
|
60
60
|
[Knockapi::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,30 +84,61 @@ module Knockapi
|
|
84
84
|
type_fn: type_fn
|
85
85
|
}
|
86
86
|
|
87
|
-
define_method(setter)
|
87
|
+
define_method(setter) do |value|
|
88
|
+
target = type_fn.call
|
89
|
+
state = Knockapi::Internal::Type::Converter.new_coerce_state(translate_names: false)
|
90
|
+
coerced = Knockapi::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 [Knockapi::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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
Knockapi::
|
97
|
-
|
98
|
-
|
99
|
-
|
106
|
+
|
107
|
+
case @coerced[name_sym]
|
108
|
+
in true | false if Knockapi::Internal::Type::Converter === target
|
109
|
+
@data.fetch(name_sym)
|
110
|
+
in ::StandardError => e
|
111
|
+
raise Knockapi::Errors::ConversionError.new(
|
112
|
+
on: self.class,
|
113
|
+
method: __method__,
|
114
|
+
target: target,
|
115
|
+
value: @data.fetch(name_sym),
|
116
|
+
cause: e
|
100
117
|
)
|
118
|
+
else
|
119
|
+
Kernel.then do
|
120
|
+
value = @data.fetch(name_sym) { const == Knockapi::Internal::OMIT ? nil : const }
|
121
|
+
state = Knockapi::Internal::Type::Converter.new_coerce_state(translate_names: false)
|
122
|
+
if (nilable || !required) && value.nil?
|
123
|
+
nil
|
124
|
+
else
|
125
|
+
Knockapi::Internal::Type::Converter.coerce(
|
126
|
+
target, value, state: state
|
127
|
+
)
|
128
|
+
end
|
129
|
+
rescue StandardError => e
|
130
|
+
raise Knockapi::Errors::ConversionError.new(
|
131
|
+
on: self.class,
|
132
|
+
method: __method__,
|
133
|
+
target: target,
|
134
|
+
value: value,
|
135
|
+
cause: e
|
136
|
+
)
|
137
|
+
end
|
101
138
|
end
|
102
|
-
rescue StandardError => e
|
103
|
-
cls = self.class.name.split("::").last
|
104
|
-
message = [
|
105
|
-
"Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
|
106
|
-
"To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
|
107
|
-
"Cause: #{e.message}"
|
108
|
-
].join(" ")
|
109
|
-
raise Knockapi::Errors::ConversionError.new(message)
|
110
139
|
end
|
140
|
+
# rubocop:enable Metrics/BlockLength
|
141
|
+
# rubocop:enable Style/CaseEquality
|
111
142
|
end
|
112
143
|
|
113
144
|
# @api private
|
@@ -207,23 +238,28 @@ module Knockapi
|
|
207
238
|
#
|
208
239
|
# @param state [Hash{Symbol=>Object}] .
|
209
240
|
#
|
210
|
-
# @option state [Boolean
|
241
|
+
# @option state [Boolean] :translate_names
|
242
|
+
#
|
243
|
+
# @option state [Boolean] :strictness
|
211
244
|
#
|
212
245
|
# @option state [Hash{Symbol=>Object}] :exactness
|
213
246
|
#
|
247
|
+
# @option state [Class<StandardError>] :error
|
248
|
+
#
|
214
249
|
# @option state [Integer] :branched
|
215
250
|
#
|
216
251
|
# @return [self, Object]
|
217
252
|
def coerce(value, state:)
|
218
253
|
exactness = state.fetch(:exactness)
|
219
254
|
|
220
|
-
if value.is_a?(self
|
255
|
+
if value.is_a?(self)
|
221
256
|
exactness[:yes] += 1
|
222
257
|
return value
|
223
258
|
end
|
224
259
|
|
225
260
|
unless (val = Knockapi::Internal::Util.coerce_hash(value)).is_a?(Hash)
|
226
261
|
exactness[:no] += 1
|
262
|
+
state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
|
227
263
|
return value
|
228
264
|
end
|
229
265
|
exactness[:yes] += 1
|
@@ -231,13 +267,15 @@ module Knockapi
|
|
231
267
|
keys = val.keys.to_set
|
232
268
|
instance = new
|
233
269
|
data = instance.to_h
|
270
|
+
status = instance.instance_variable_get(:@coerced)
|
234
271
|
|
235
272
|
# rubocop:disable Metrics/BlockLength
|
236
273
|
fields.each do |name, field|
|
237
274
|
mode, required, target = field.fetch_values(:mode, :required, :type)
|
238
275
|
api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
|
276
|
+
src_name = state.fetch(:translate_names) ? api_name : name
|
239
277
|
|
240
|
-
unless val.key?(
|
278
|
+
unless val.key?(src_name)
|
241
279
|
if required && mode != :dump && const == Knockapi::Internal::OMIT
|
242
280
|
exactness[nilable ? :maybe : :no] += 1
|
243
281
|
else
|
@@ -246,9 +284,10 @@ module Knockapi
|
|
246
284
|
next
|
247
285
|
end
|
248
286
|
|
249
|
-
item = val.fetch(
|
250
|
-
keys.delete(
|
287
|
+
item = val.fetch(src_name)
|
288
|
+
keys.delete(src_name)
|
251
289
|
|
290
|
+
state[:error] = nil
|
252
291
|
converted =
|
253
292
|
if item.nil? && (nilable || !required)
|
254
293
|
exactness[nilable ? :yes : :maybe] += 1
|
@@ -262,6 +301,8 @@ module Knockapi
|
|
262
301
|
item
|
263
302
|
end
|
264
303
|
end
|
304
|
+
|
305
|
+
status.store(name, state.fetch(:error) || true)
|
265
306
|
data.store(name, converted)
|
266
307
|
end
|
267
308
|
# rubocop:enable Metrics/BlockLength
|
@@ -437,7 +478,18 @@ module Knockapi
|
|
437
478
|
# Create a new instance of a model.
|
438
479
|
#
|
439
480
|
# @param data [Hash{Symbol=>Object}, self]
|
440
|
-
def initialize(data = {})
|
481
|
+
def initialize(data = {})
|
482
|
+
@data = {}
|
483
|
+
@coerced = {}
|
484
|
+
Knockapi::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
|
441
493
|
|
442
494
|
class << self
|
443
495
|
# @api private
|
@@ -31,14 +31,20 @@ module Knockapi
|
|
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
|
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 Knockapi
|
|
15
15
|
#
|
16
16
|
# @param state [Hash{Symbol=>Object}] .
|
17
17
|
#
|
18
|
-
# @option state [Boolean
|
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 Knockapi
|
|
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 Knockapi
|
|
110
129
|
#
|
111
130
|
# @param value [Object]
|
112
131
|
#
|
113
|
-
# @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false
|
114
|
-
#
|
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 Knockapi
|
|
130
146
|
#
|
131
147
|
# See implementation below for more details.
|
132
148
|
#
|
133
|
-
# @option state [Boolean
|
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: Knockapi::Internal::Type::Converter.new_coerce_state)
|
146
161
|
# rubocop:disable Metrics/BlockNesting
|
147
|
-
|
162
|
+
exactness = state.fetch(:exactness)
|
148
163
|
|
149
164
|
case target
|
150
165
|
in Knockapi::Internal::Type::Converter
|
@@ -160,29 +175,26 @@ module Knockapi
|
|
160
175
|
exactness[value.nil? ? :yes : :maybe] += 1
|
161
176
|
return nil
|
162
177
|
in -> { _1 <= Integer }
|
163
|
-
|
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 Knockapi
|
|
194
206
|
exactness[:yes] += 1
|
195
207
|
return value.string
|
196
208
|
else
|
197
|
-
|
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
|
-
|
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 Knockapi
|
|
221
230
|
return value
|
222
231
|
end
|
223
232
|
else
|
224
|
-
|
225
|
-
|
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 Knockapi
|
|
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 Knockapi
|
|
277
283
|
define_sorbet_constant!(:CoerceState) do
|
278
284
|
T.type_alias do
|
279
285
|
{
|
280
|
-
|
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
|
@@ -56,10 +56,14 @@ module Knockapi
|
|
56
56
|
#
|
57
57
|
# @param state [Hash{Symbol=>Object}] .
|
58
58
|
#
|
59
|
-
# @option state [Boolean
|
59
|
+
# @option state [Boolean] :translate_names
|
60
|
+
#
|
61
|
+
# @option state [Boolean] :strictness
|
60
62
|
#
|
61
63
|
# @option state [Hash{Symbol=>Object}] :exactness
|
62
64
|
#
|
65
|
+
# @option state [Class<StandardError>] :error
|
66
|
+
#
|
63
67
|
# @option state [Integer] :branched
|
64
68
|
#
|
65
69
|
# @return [Symbol, Object]
|
@@ -70,8 +74,12 @@ module Knockapi
|
|
70
74
|
if values.include?(val)
|
71
75
|
exactness[:yes] += 1
|
72
76
|
val
|
77
|
+
elsif values.first&.class == val.class
|
78
|
+
exactness[:maybe] += 1
|
79
|
+
value
|
73
80
|
else
|
74
|
-
exactness[
|
81
|
+
exactness[:no] += 1
|
82
|
+
state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
|
75
83
|
value
|
76
84
|
end
|
77
85
|
end
|
@@ -45,10 +45,14 @@ module Knockapi
|
|
45
45
|
#
|
46
46
|
# @param state [Hash{Symbol=>Object}] .
|
47
47
|
#
|
48
|
-
# @option state [Boolean
|
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 Knockapi
|
|
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 Knockapi
|
|
77
77
|
#
|
78
78
|
# @param state [Hash{Symbol=>Object}] .
|
79
79
|
#
|
80
|
-
# @option state [Boolean
|
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 Knockapi
|
|
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 Knockapi
|
|
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
|
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 Knockapi
|
|
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 Knockapi
|
|
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
|
-
|
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 Knockapi
|
|
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
|
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]
|
@@ -41,7 +41,7 @@ module Knockapi
|
|
41
41
|
# @see Knockapi::Models::AudienceAddMembersParams::Member#user
|
42
42
|
class User < Knockapi::Internal::Type::BaseModel
|
43
43
|
# @!attribute id
|
44
|
-
# The
|
44
|
+
# The unique identifier of the user.
|
45
45
|
#
|
46
46
|
# @return [String, nil]
|
47
47
|
optional :id, String
|
@@ -49,7 +49,7 @@ module Knockapi
|
|
49
49
|
# @!method initialize(id: nil)
|
50
50
|
# An object containing the user's ID.
|
51
51
|
#
|
52
|
-
# @param id [String] The
|
52
|
+
# @param id [String] The unique identifier of the user.
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
@@ -24,7 +24,7 @@ module Knockapi
|
|
24
24
|
required :user, -> { Knockapi::User }
|
25
25
|
|
26
26
|
# @!attribute user_id
|
27
|
-
# The
|
27
|
+
# The unique identifier of the user.
|
28
28
|
#
|
29
29
|
# @return [String]
|
30
30
|
required :user_id, String
|
@@ -47,7 +47,7 @@ module Knockapi
|
|
47
47
|
#
|
48
48
|
# @param user [Knockapi::Models::User] A [User](/concepts/users) represents an individual in your system who can receiv
|
49
49
|
#
|
50
|
-
# @param user_id [String] The
|
50
|
+
# @param user_id [String] The unique identifier of the user.
|
51
51
|
#
|
52
52
|
# @param tenant [String, nil] The unique identifier for the tenant.
|
53
53
|
end
|