skir-client 0.1.8

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.
@@ -0,0 +1,497 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+ require_relative "../reflection"
6
+ require_relative "../spec"
7
+ require_relative "binary"
8
+ require_relative "repr"
9
+ require_relative "structs"
10
+ require_relative "type_adapter"
11
+
12
+ module Skir
13
+ module Internal
14
+ module Enums
15
+ extend T::Sig
16
+
17
+ class ResolvedWrapperVariant
18
+ extend T::Sig
19
+
20
+ sig { returns(Spec::WrapperVariant) }
21
+ attr_reader :spec
22
+
23
+ sig { returns(TypeAdapter) }
24
+ attr_reader :value_type
25
+
26
+ sig { returns(T.untyped) }
27
+ attr_reader :variant_class
28
+
29
+ sig { returns(String) }
30
+ attr_reader :bytes_prefix
31
+
32
+ sig { params(spec: Spec::WrapperVariant, value_type: TypeAdapter, variant_class: T.untyped).void }
33
+ def initialize(spec, value_type, variant_class)
34
+ @spec = T.let(spec, Spec::WrapperVariant)
35
+ @value_type = T.let(value_type, TypeAdapter)
36
+ @variant_class = T.let(variant_class, T.untyped)
37
+ prefix = +"".b
38
+ num = spec.number
39
+ if num >= 1 && num <= 4
40
+ prefix << (250 + num)
41
+ else
42
+ prefix << 248
43
+ Binary.encode_int64(num, prefix)
44
+ end
45
+ @bytes_prefix = T.let(prefix.freeze, String)
46
+ freeze
47
+ end
48
+ end
49
+
50
+ class EnumAdapter < TypeAdapter
51
+ extend T::Sig
52
+
53
+ sig { returns(Spec::Enum) }
54
+ attr_reader :spec
55
+
56
+ sig { returns(T.untyped) }
57
+ attr_reader :gen_class
58
+
59
+ sig { returns(T::Array[ResolvedWrapperVariant]) }
60
+ attr_reader :wrapper_variants
61
+
62
+ sig { params(spec: Spec::Enum, existing_class: T.untyped).void }
63
+ def initialize(spec, existing_class = nil)
64
+ super()
65
+ @spec = T.let(spec, Spec::Enum)
66
+ @finalization_state = T.let(0, Integer)
67
+ @record_hash = T.let(spec.id.hash, Integer)
68
+ @wrapper_variants = T.let([], T::Array[ResolvedWrapperVariant])
69
+ @key_to_constant = T.let({}, T::Hash[T.any(Integer, String), T.untyped])
70
+ @number_to_constant = T.let({}, T::Hash[Integer, T.untyped])
71
+ @number_to_wrapper = T.let({}, T::Hash[Integer, ResolvedWrapperVariant])
72
+ @name_to_wrapper = T.let({}, T::Hash[String, ResolvedWrapperVariant])
73
+ @removed_set = T.let(spec.removed_numbers.to_h { |n| [n, true] }.freeze, T::Hash[Integer, T::Boolean])
74
+
75
+ base_class = existing_class || Class.new
76
+ @gen_class = T.let(base_class, T.untyped)
77
+
78
+ setup_base_class
79
+ setup_constants
80
+ end
81
+
82
+ sig { returns(T::Array[Spec::ConstantVariant]) }
83
+ def all_constant_variants
84
+ unknown_variant = Spec::ConstantVariant.new(
85
+ name: "UNKNOWN",
86
+ number: 0,
87
+ attribute: "UNKNOWN"
88
+ )
89
+ @spec.constant_variants + [unknown_variant]
90
+ end
91
+
92
+ sig { override.params(resolve_type_fn: T.proc.params(arg0: T.untyped).returns(TypeAdapter)).void }
93
+ def finalize(resolve_type_fn)
94
+ return if @finalization_state != 0
95
+
96
+ @finalization_state = 1
97
+ base_class = @gen_class
98
+
99
+ resolved_wrappers = @spec.wrapper_variants.map do |wv|
100
+ v_type = resolve_type_fn.call(wv.type)
101
+ v_class = ensure_wrapper_variant_class(base_class, wv, v_type)
102
+ ResolvedWrapperVariant.new(wv, v_type, v_class)
103
+ end
104
+ @wrapper_variants = resolved_wrappers.freeze
105
+
106
+ @wrapper_variants.each do |rwv|
107
+ @number_to_wrapper[rwv.spec.number] = rwv
108
+ @name_to_wrapper[rwv.spec.name] = rwv
109
+ rwv.value_type.finalize(resolve_type_fn)
110
+ end
111
+ @number_to_wrapper.freeze
112
+ @name_to_wrapper.freeze
113
+
114
+ setup_wrapper_factories(resolve_type_fn)
115
+
116
+ @finalization_state = 2
117
+ end
118
+
119
+ sig { override.returns(T.untyped) }
120
+ def default_value
121
+ @number_to_constant.fetch(0)
122
+ end
123
+
124
+ sig { override.params(arg: T.untyped).returns(T.untyped) }
125
+ def to_frozen(arg)
126
+ return arg if arg.is_a?(@gen_class)
127
+
128
+ raise TypeError, "expected: #{@spec.class_qualname}; found: #{arg.class}"
129
+ end
130
+
131
+ sig { override.params(val: T.untyped).returns(T::Boolean) }
132
+ def not_default?(val)
133
+ val.instance_variable_get(:@_number) != 0
134
+ end
135
+
136
+ sig { override.params(val: T.untyped, readable: T::Boolean).returns(T.untyped) }
137
+ def to_json(val, readable)
138
+ readable ? val._rj : val._dj
139
+ end
140
+
141
+ sig { override.params(json: T.untyped, keep_unrecognized: T::Boolean).returns(T.untyped) }
142
+ def from_json(json, keep_unrecognized)
143
+ enum_from_json(json, keep_unrecognized)
144
+ end
145
+
146
+ sig { override.params(val: T.untyped, buffer: String).void }
147
+ def encode(val, buffer)
148
+ b = val.instance_variable_get(:@_bytes)
149
+ if b
150
+ buffer << b
151
+ else
152
+ val._enc(buffer)
153
+ end
154
+ end
155
+
156
+ sig { override.params(stream: ByteStream).returns(T.untyped) }
157
+ def decode(stream)
158
+ enum_decode(stream)
159
+ end
160
+
161
+ sig { override.returns(Reflection::Type) }
162
+ def reflection_type
163
+ Reflection::RecordType.new(value: @spec.id)
164
+ end
165
+
166
+ sig { override.params(registry: T::Hash[String, Reflection::Record]).void }
167
+ def register_records(registry)
168
+ record_id = @spec.id
169
+ return if registry.key?(record_id)
170
+
171
+ const_vars = all_constant_variants.filter_map do |cv|
172
+ next if cv.number == 0
173
+
174
+ Reflection::Variant.new(
175
+ name: cv.name.downcase,
176
+ number: cv.number,
177
+ type: nil,
178
+ doc: cv.doc
179
+ )
180
+ end
181
+ wrap_vars = @wrapper_variants.map do |rwv|
182
+ Reflection::Variant.new(
183
+ name: rwv.spec.name,
184
+ number: rwv.spec.number,
185
+ type: rwv.value_type.reflection_type,
186
+ doc: rwv.spec.doc
187
+ )
188
+ end
189
+
190
+ registry[record_id] = Reflection::Enum.new(
191
+ id: record_id,
192
+ doc: @spec.doc,
193
+ variants: const_vars + wrap_vars,
194
+ removed_numbers: @spec.removed_numbers
195
+ )
196
+ @wrapper_variants.each { |rwv| rwv.value_type.register_records(registry) }
197
+ end
198
+
199
+ sig { params(json: T.untyped, keep_unrecognized: T::Boolean).returns(T.untyped) }
200
+ def enum_from_json(json, keep_unrecognized)
201
+ unknown = @number_to_constant.fetch(0)
202
+ case json
203
+ when Integer
204
+ const_val = @key_to_constant[json]
205
+ return const_val unless const_val.nil?
206
+
207
+ # Constant -> wrapper evolution compatibility
208
+ rwv = @number_to_wrapper[json]
209
+ if rwv
210
+ dval = rwv.value_type.to_frozen(rwv.value_type.default_value)
211
+ return instantiate_wrapper(rwv, dval)
212
+ end
213
+
214
+ return unknown if @removed_set.key?(json) || !keep_unrecognized
215
+
216
+ instantiate_unrecognized(Structs::UnrecognizedFields.deep_copy(json), "\x00".b)
217
+ when Array
218
+ number = json[0]
219
+ raise TypeError, "Expected Integer as first element of enum array JSON" unless number.is_a?(Integer)
220
+
221
+ rwv = @number_to_wrapper[number]
222
+ if rwv
223
+ val = rwv.value_type.from_json(json[1], keep_unrecognized)
224
+ return instantiate_wrapper(rwv, val)
225
+ end
226
+
227
+ # Wrapper -> constant evolution compatibility
228
+ const_val = @key_to_constant[number]
229
+ return const_val unless const_val.nil?
230
+
231
+ return unknown if @removed_set.key?(number) || !keep_unrecognized
232
+
233
+ instantiate_unrecognized(Structs::UnrecognizedFields.deep_copy(json), "\x00".b)
234
+ when String
235
+ @key_to_constant[json] || unknown
236
+ when Hash
237
+ kind = json["kind"]
238
+ return unknown unless kind.is_a?(String)
239
+
240
+ rwv = @name_to_wrapper[kind]
241
+ return unknown if rwv.nil?
242
+
243
+ val = rwv.value_type.from_json(json["value"], keep_unrecognized)
244
+ instantiate_wrapper(rwv, val)
245
+ else
246
+ raise TypeError, "Cannot parse Enum from #{json.class}"
247
+ end
248
+ end
249
+
250
+ sig { params(stream: ByteStream).returns(T.untyped) }
251
+ def enum_decode(stream)
252
+ unknown = @number_to_constant.fetch(0)
253
+ start_offset = stream.position
254
+ wire = stream.buffer.getbyte(start_offset)
255
+ raise EOFError, "Unexpected end of stream decoding enum" if wire.nil?
256
+
257
+ if wire <= 238
258
+ if wire < 232
259
+ stream.position += 1
260
+ number = wire
261
+ else
262
+ number = Binary.decode_int64(stream)
263
+ end
264
+ const_val = @number_to_constant[number]
265
+ return const_val unless const_val.nil?
266
+
267
+ # Constant -> wrapper evolution compatibility
268
+ rwv = @number_to_wrapper[number]
269
+ if rwv
270
+ dval = rwv.value_type.to_frozen(rwv.value_type.default_value)
271
+ return instantiate_wrapper(rwv, dval)
272
+ end
273
+
274
+ return unknown if @removed_set.key?(number) || !stream.keep_unrecognized_values
275
+
276
+ raw_bytes = stream.buffer.byteslice(start_offset, stream.position - start_offset) || "\x00".b
277
+ return instantiate_unrecognized(0, raw_bytes.b.freeze)
278
+ end
279
+
280
+ stream.position += 1
281
+ number = wire == 248 ? Binary.decode_int64(stream) : (wire - 250)
282
+ rwv = @number_to_wrapper[number]
283
+ if rwv
284
+ val = rwv.value_type.decode(stream)
285
+ return instantiate_wrapper(rwv, val)
286
+ end
287
+
288
+ Binary.decode_unused(stream)
289
+ # Wrapper -> constant evolution compatibility
290
+ const_val = @number_to_constant[number]
291
+ return const_val unless const_val.nil?
292
+
293
+ return unknown if @removed_set.key?(number) || !stream.keep_unrecognized_values
294
+
295
+ raw_bytes = stream.buffer.byteslice(start_offset, stream.position - start_offset) || "\x00".b
296
+ instantiate_unrecognized(0, raw_bytes.b.freeze)
297
+ end
298
+
299
+ private
300
+
301
+ sig { params(rwv: ResolvedWrapperVariant, frozen_val: T.untyped).returns(T.untyped) }
302
+ def instantiate_wrapper(rwv, frozen_val)
303
+ inst = rwv.variant_class.allocate
304
+ inst.instance_variable_set(:@kind, rwv.spec.name)
305
+ inst.instance_variable_set(:@value, frozen_val)
306
+ inst.instance_variable_set(:@_number, rwv.spec.number)
307
+ inst.instance_variable_set(:@_bytes, nil)
308
+ inst.freeze
309
+ end
310
+
311
+ sig { params(dj: T.untyped, raw_bytes: String).returns(T.untyped) }
312
+ def instantiate_unrecognized(dj, raw_bytes)
313
+ unknown_v_class = @gen_class.const_get(:V_UNKNOWN, false)
314
+ inst = unknown_v_class.allocate
315
+ inst.instance_variable_set(:@kind, "UNKNOWN")
316
+ inst.instance_variable_set(:@value, nil)
317
+ inst.instance_variable_set(:@_number, 0)
318
+ inst.instance_variable_set(:@_dj, dj)
319
+ inst.instance_variable_set(:@_rj, "unknown")
320
+ inst.instance_variable_set(:@_bytes, raw_bytes.b.freeze)
321
+ inst.freeze
322
+ end
323
+
324
+ sig { void }
325
+ def setup_base_class
326
+ base_class = @gen_class
327
+ record_hash = @record_hash
328
+
329
+ Structs.safe_remove_method(base_class, :kind)
330
+ Structs.safe_remove_method(base_class, :value)
331
+ base_class.send(:attr_reader, :kind, :value)
332
+
333
+ Structs.safe_remove_method(base_class, :initialize)
334
+ base_class.define_method(:initialize) do |*_args|
335
+ raise TypeError, "Cannot call the constructor of a skir enum"
336
+ end
337
+
338
+ Structs.safe_remove_method(base_class, :union)
339
+ base_class.define_method(:union) { self }
340
+
341
+ Structs.safe_remove_method(base_class, :unknown?)
342
+ base_class.define_method(:unknown?) { instance_variable_get(:@kind) == "UNKNOWN" }
343
+
344
+ Structs.safe_remove_method(base_class, :present?)
345
+ base_class.define_method(:present?) { instance_variable_get(:@kind) != "UNKNOWN" }
346
+
347
+ Structs.safe_remove_method(base_class, :==)
348
+ base_class.define_method(:==) do |other|
349
+ return true if equal?(other)
350
+ return false unless other.is_a?(base_class)
351
+
352
+ instance_variable_get(:@kind) == other.kind && instance_variable_get(:@value) == other.value
353
+ end
354
+
355
+ Structs.safe_remove_method(base_class, :eql?)
356
+ base_class.define_method(:eql?) { |other| self == other }
357
+
358
+ Structs.safe_remove_method(base_class, :hash)
359
+ base_class.define_method(:hash) { [record_hash, instance_variable_get(:@kind), instance_variable_get(:@value)].hash }
360
+
361
+ Structs.safe_const_set(base_class, :Kind, String) unless base_class.const_defined?(:Kind, false)
362
+ end
363
+
364
+ sig { void }
365
+ def setup_constants
366
+ base_class = @gen_class
367
+ qualname = @spec.class_qualname
368
+
369
+ all_constant_variants.each do |cv|
370
+ v_class_name = :"V_#{cv.name}"
371
+ v_class =
372
+ if base_class.const_defined?(v_class_name, false)
373
+ base_class.const_get(v_class_name, false)
374
+ else
375
+ c = Class.new(base_class)
376
+ base_class.const_set(v_class_name, c)
377
+ c
378
+ end
379
+
380
+ encoded_bytes = +"".b
381
+ Binary.encode_int64(cv.number, encoded_bytes)
382
+ encoded_bytes.freeze
383
+ rj_name = cv.name.downcase.freeze
384
+ dj_num = cv.number
385
+ attr_name = cv.attribute
386
+
387
+ Structs.safe_remove_method(v_class, :kind)
388
+ Structs.safe_remove_method(v_class, :value)
389
+ Structs.safe_remove_method(v_class, :_dj)
390
+ Structs.safe_remove_method(v_class, :_rj)
391
+ Structs.safe_remove_method(v_class, :inspect)
392
+ Structs.safe_remove_method(v_class, :to_s)
393
+ v_class.define_method(:_dj) do
394
+ instance_variable_defined?(:@_dj) ? instance_variable_get(:@_dj) : dj_num
395
+ end
396
+ v_class.define_method(:_rj) { rj_name }
397
+ v_class.define_method(:inspect) { "#{qualname}::#{attr_name}" }
398
+ v_class.define_method(:to_s) { inspect }
399
+
400
+ inst = v_class.allocate
401
+ inst.instance_variable_set(:@kind, cv.name)
402
+ inst.instance_variable_set(:@value, nil)
403
+ inst.instance_variable_set(:@_number, cv.number)
404
+ inst.instance_variable_set(:@_bytes, encoded_bytes)
405
+ inst.freeze
406
+
407
+ # Ensure const_set works even if attribute starts with lowercase (e.g. in dynamic tests)
408
+ const_sym = attr_name[0]&.match?(/[A-Z]/) ? attr_name.to_sym : nil
409
+ Structs.safe_const_set(base_class, const_sym, inst) if const_sym
410
+ Structs.safe_remove_singleton_method(base_class, attr_name.to_sym)
411
+ base_class.define_singleton_method(attr_name.to_sym) { inst } unless const_sym
412
+
413
+ @number_to_constant[cv.number] = inst
414
+ @key_to_constant[cv.number] = inst
415
+ @key_to_constant[cv.name] = inst
416
+ @key_to_constant[cv.name.upcase] = inst
417
+ @key_to_constant[cv.name.downcase] = inst
418
+ end
419
+ @number_to_constant.freeze
420
+ @key_to_constant.freeze
421
+ end
422
+
423
+ sig { params(base_class: T.untyped, wv: Spec::WrapperVariant, v_type: TypeAdapter).returns(T.untyped) }
424
+ def ensure_wrapper_variant_class(base_class, wv, v_type)
425
+ qualname = @spec.class_qualname
426
+ v_class_name = :"V_#{wv.name}"
427
+ v_class =
428
+ if base_class.const_defined?(v_class_name, false)
429
+ base_class.const_get(v_class_name, false)
430
+ else
431
+ c = Class.new(base_class)
432
+ base_class.const_set(v_class_name, c)
433
+ c
434
+ end
435
+
436
+ num = wv.number
437
+ vname = wv.name
438
+ prefix = +"".b
439
+ if num >= 1 && num <= 4
440
+ prefix << (250 + num)
441
+ else
442
+ prefix << 248
443
+ Binary.encode_int64(num, prefix)
444
+ end
445
+ prefix.freeze
446
+
447
+ Structs.safe_remove_method(v_class, :kind)
448
+ Structs.safe_remove_method(v_class, :value)
449
+ Structs.safe_remove_method(v_class, :_dj)
450
+ Structs.safe_remove_method(v_class, :_rj)
451
+ Structs.safe_remove_method(v_class, :_enc)
452
+ Structs.safe_remove_method(v_class, :inspect)
453
+ Structs.safe_remove_method(v_class, :to_s)
454
+
455
+ v_class.define_method(:_dj) { [num, v_type.to_json(instance_variable_get(:@value), false)] }
456
+ v_class.define_method(:_rj) { { "kind" => vname, "value" => v_type.to_json(instance_variable_get(:@value), true) } }
457
+ v_class.define_method(:_enc) do |buffer|
458
+ buffer << prefix
459
+ v_type.encode(instance_variable_get(:@value), buffer)
460
+ end
461
+ v_class.define_method(:inspect) do
462
+ val_repr = Repr.repr_impl(instance_variable_get(:@value))
463
+ body = val_repr.complex ? "\n #{val_repr.indented}\n" : val_repr.repr
464
+ "#{qualname}.wrap_#{vname}(#{body})"
465
+ end
466
+ v_class.define_method(:to_s) { inspect }
467
+ v_class
468
+ end
469
+
470
+ sig { params(resolve_type_fn: T.proc.params(arg0: T.untyped).returns(TypeAdapter)).void }
471
+ def setup_wrapper_factories(resolve_type_fn)
472
+ base_class = @gen_class
473
+ adapter = self
474
+ @wrapper_variants.each do |rwv|
475
+ wrap_name = :"wrap_#{rwv.spec.name}"
476
+ Structs.safe_remove_singleton_method(base_class, wrap_name)
477
+ base_class.define_singleton_method(wrap_name) do |value|
478
+ frozen_val = rwv.value_type.to_frozen(value)
479
+ adapter.send(:instantiate_wrapper, rwv, frozen_val)
480
+ end
481
+
482
+ value_type = resolve_type_fn.call(rwv.spec.type)
483
+ struct_frozen_class = value_type.frozen_class_of_struct
484
+ next unless struct_frozen_class
485
+
486
+ create_name = :"create_#{rwv.spec.name}"
487
+ Structs.safe_remove_singleton_method(base_class, create_name)
488
+ base_class.define_singleton_method(create_name) do |**kwargs|
489
+ struct_inst = struct_frozen_class.new(**kwargs)
490
+ adapter.send(:instantiate_wrapper, rwv, struct_inst)
491
+ end
492
+ end
493
+ end
494
+ end
495
+ end
496
+ end
497
+ end
@@ -0,0 +1,93 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "sorbet-runtime"
5
+ require_relative "../reflection"
6
+ require_relative "type_adapter"
7
+
8
+ module Skir
9
+ module Internal
10
+ module Optionals
11
+ extend T::Sig
12
+
13
+ class OptionalAdapter < TypeAdapter
14
+ extend T::Sig
15
+
16
+ sig { returns(TypeAdapter) }
17
+ attr_reader :other_adapter
18
+
19
+ sig { params(other_adapter: TypeAdapter).void }
20
+ def initialize(other_adapter)
21
+ super()
22
+ @other_adapter = T.let(other_adapter, TypeAdapter)
23
+ end
24
+
25
+ sig { override.returns(NilClass) }
26
+ def default_value
27
+ nil
28
+ end
29
+
30
+ sig { override.params(arg: T.untyped).returns(T.untyped) }
31
+ def to_frozen(arg)
32
+ arg.nil? ? nil : @other_adapter.to_frozen(arg)
33
+ end
34
+
35
+ sig { override.params(val: T.untyped).returns(T::Boolean) }
36
+ def not_default?(val)
37
+ !val.nil?
38
+ end
39
+
40
+ sig { override.params(val: T.untyped, readable: T::Boolean).returns(T.untyped) }
41
+ def to_json(val, readable)
42
+ val.nil? ? nil : @other_adapter.to_json(val, readable)
43
+ end
44
+
45
+ sig { override.params(json: T.untyped, keep_unrecognized: T::Boolean).returns(T.untyped) }
46
+ def from_json(json, keep_unrecognized)
47
+ json.nil? ? nil : @other_adapter.from_json(json, keep_unrecognized)
48
+ end
49
+
50
+ sig { override.params(val: T.untyped, buffer: String).void }
51
+ def encode(val, buffer)
52
+ if val.nil?
53
+ buffer << 255
54
+ else
55
+ @other_adapter.encode(val, buffer)
56
+ end
57
+ end
58
+
59
+ sig { override.params(stream: ByteStream).returns(T.untyped) }
60
+ def decode(stream)
61
+ if stream.buffer.getbyte(stream.position) == 255
62
+ stream.position += 1
63
+ nil
64
+ else
65
+ @other_adapter.decode(stream)
66
+ end
67
+ end
68
+
69
+ sig { override.params(resolve_type_fn: T.proc.params(arg0: T.untyped).returns(TypeAdapter)).void }
70
+ def finalize(resolve_type_fn)
71
+ @other_adapter.finalize(resolve_type_fn)
72
+ end
73
+
74
+ sig { override.returns(Reflection::Type) }
75
+ def reflection_type
76
+ Reflection::OptionalType.new(value: @other_adapter.reflection_type)
77
+ end
78
+
79
+ sig { override.params(registry: T::Hash[String, Reflection::Record]).void }
80
+ def register_records(registry)
81
+ @other_adapter.register_records(registry)
82
+ end
83
+ end
84
+
85
+ CACHE = T.let({}, T::Hash[Integer, OptionalAdapter])
86
+
87
+ sig { params(other_adapter: TypeAdapter).returns(OptionalAdapter) }
88
+ def self.get_optional_adapter(other_adapter)
89
+ CACHE[other_adapter.object_id] ||= OptionalAdapter.new(other_adapter)
90
+ end
91
+ end
92
+ end
93
+ end