36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
|
# File 'lib/google/protobuf/ffi/message.rb', line 36
def build_message_class
descriptor = self
Class.new(Google::Protobuf::const_get(:AbstractMessage)) do
@descriptor = descriptor
class << self
attr_accessor :descriptor
private
attr_accessor :oneof_field_names
include ::Google::Protobuf::Internal::Convert
end
alias original_method_missing method_missing
def method_missing(method_name, *args)
method_missing_internal method_name, *args, mode: :method_missing
end
def respond_to_missing?(method_name, include_private = false)
method_missing_internal(method_name, mode: :respond_to_missing?) || super
end
def self.new(initial_value = nil)
instance = allocate
instance.send(:initialize, initial_value)
instance
end
def frozen?
unless Google::Protobuf::FFI.message_frozen? @msg
raise RuntimeError.new "Ruby frozen Message with mutable representation" if super
return false
end
method(:freeze).super_method.call unless super
true
end
def freeze
if method(:frozen?).super_method.call
unless Google::Protobuf::FFI.message_frozen? @msg
raise RuntimeError.new "Underlying representation of message still mutable despite frozen wrapper"
end
return self
end
unless Google::Protobuf::FFI.message_frozen? @msg
Google::Protobuf::FFI.message_freeze(@msg, Google::Protobuf::FFI.get_mini_table(self.class.descriptor))
end
super
end
def dup
duplicate = self.class.private_constructor(@arena)
mini_table = Google::Protobuf::FFI.get_mini_table(self.class.descriptor)
size = mini_table[:size]
duplicate.instance_variable_get(:@msg).write_string_length(@msg.read_string_length(size), size)
duplicate
end
alias clone dup
def eql?(other)
return false unless self.class === other
encoding_options = Google::Protobuf::FFI::Upb_Encode_Deterministic | Google::Protobuf::FFI::Upb_Encode_SkipUnknown
temporary_arena = Google::Protobuf::FFI.create_arena
mini_table = Google::Protobuf::FFI.get_mini_table(self.class.descriptor)
size_one = ::FFI::MemoryPointer.new(:size_t, 1)
encoding_one = ::FFI::MemoryPointer.new(:pointer, 1)
encoding_status = Google::Protobuf::FFI.encode_message(@msg, mini_table, encoding_options, temporary_arena, encoding_one.to_ptr, size_one)
raise ParseError.new "Error comparing messages due to #{encoding_status} while encoding LHS of `eql?()`" unless encoding_status == :Ok
size_two = ::FFI::MemoryPointer.new(:size_t, 1)
encoding_two = ::FFI::MemoryPointer.new(:pointer, 1)
encoding_status = Google::Protobuf::FFI.encode_message(other.instance_variable_get(:@msg), mini_table, encoding_options, temporary_arena, encoding_two.to_ptr, size_two)
raise ParseError.new "Error comparing messages due to #{encoding_status} while encoding RHS of `eql?()`" unless encoding_status == :Ok
if encoding_one.null? or encoding_two.null?
raise ParseError.new "Error comparing messages"
end
size_one.read(:size_t) == size_two.read(:size_t) and Google::Protobuf::FFI.memcmp(encoding_one.read(:pointer), encoding_two.read(:pointer), size_one.read(:size_t)).zero?
end
alias == eql?
def hash
encoding_options = Google::Protobuf::FFI::Upb_Encode_Deterministic | Google::Protobuf::FFI::Upb_Encode_SkipUnknown
temporary_arena = Google::Protobuf::FFI.create_arena
mini_table_ptr = Google::Protobuf::FFI.get_mini_table(self.class.descriptor)
size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
encoding = ::FFI::MemoryPointer.new(:pointer, 1)
encoding_status = Google::Protobuf::FFI.encode_message(@msg, mini_table_ptr, encoding_options, temporary_arena, encoding.to_ptr, size_ptr)
if encoding_status != :Ok or encoding.null?
raise ParseError.new "Error calculating hash"
end
encoding.read(:pointer).read_string(size_ptr.read(:size_t)).hash
end
def to_h
to_h_internal @msg, self.class.descriptor
end
def inspect
self.class.inspect_internal @msg
end
def to_s
self.inspect
end
def [](name)
raise TypeError.new "Expected String for name but got #{name.class}" unless name.is_a? String
index_internal name
end
def []=(name, value)
raise TypeError.new "Expected String for name but got #{name.class}" unless name.is_a? String
index_assign_internal(value, name: name)
end
def self.decode(data, options = {})
raise ArgumentError.new "Expected hash arguments." unless options.is_a? Hash
raise ArgumentError.new "Expected string for binary protobuf data." unless data.is_a? String
decoding_options = 0
depth = options[:recursion_limit]
if depth.is_a? Numeric
decoding_options |= Google::Protobuf::FFI.decode_max_depth(depth.to_i)
end
message = new
mini_table_ptr = Google::Protobuf::FFI.get_mini_table(message.class.descriptor)
status = Google::Protobuf::FFI.decode_message(
data,
data.bytesize,
message.instance_variable_get(:@msg),
mini_table_ptr,
Google::Protobuf::FFI.get_extension_registry(message.class.descriptor.send(:pool).descriptor_pool),
decoding_options,
message.instance_variable_get(:@arena)
)
raise ParseError.new "Error occurred during parsing" unless status == :Ok
message
end
def self.encode(message, options = {})
raise ArgumentError.new "Message of wrong type." unless message.is_a? self
raise ArgumentError.new "Expected hash arguments." unless options.is_a? Hash
encoding_options = 0
depth = options[:recursion_limit]
if depth.is_a? Numeric
encoding_options |= Google::Protobuf::FFI.decode_max_depth(depth.to_i)
end
encode_internal(message.instance_variable_get(:@msg), encoding_options) do |encoding, size, _|
if encoding.nil? or encoding.null?
raise RuntimeError.new "Exceeded maximum depth (possibly cycle)"
else
encoding.read_string_length(size).force_encoding("ASCII-8BIT").freeze
end
end
end
def self.decode_json(data, options = {})
decoding_options = 0
unless options.is_a? Hash
if options.respond_to? :to_h
options options.to_h
else
raise ArgumentError.new "Expected hash arguments"
end
end
raise ArgumentError.new "Expected string for JSON data." unless data.is_a? String
raise RuntimeError.new "Cannot parse a wrapper directly" if descriptor.send(:wrapper?)
if options[:ignore_unknown_fields]
decoding_options |= Google::Protobuf::FFI::Upb_JsonDecode_IgnoreUnknown
end
message = new
pool_def = message.class.descriptor.instance_variable_get(:@descriptor_pool).descriptor_pool
status = Google::Protobuf::FFI::Status.new
unless Google::Protobuf::FFI.json_decode_message(data, data.bytesize, message.instance_variable_get(:@msg), message.class.descriptor, pool_def, decoding_options, message.instance_variable_get(:@arena), status)
raise ParseError.new "Error occurred during parsing: #{Google::Protobuf::FFI.error_message(status)}"
end
message
end
def self.encode_json(message, options = {})
encoding_options = 0
unless options.is_a? Hash
if options.respond_to? :to_h
options = options.to_h
else
raise ArgumentError.new "Expected hash arguments"
end
end
if options[:preserve_proto_fieldnames]
encoding_options |= Google::Protobuf::FFI::Upb_JsonEncode_UseProtoNames
end
if options[:emit_defaults]
encoding_options |= Google::Protobuf::FFI::Upb_JsonEncode_EmitDefaults
end
if options[:format_enums_as_integers]
encoding_options |= Google::Protobuf::FFI::Upb_JsonEncode_FormatEnumsAsIntegers
end
buffer_size = 1024
buffer = ::FFI::MemoryPointer.new(:char, buffer_size)
status = Google::Protobuf::FFI::Status.new
msg = message.instance_variable_get(:@msg)
pool_def = message.class.descriptor.instance_variable_get(:@descriptor_pool).descriptor_pool
size = Google::Protobuf::FFI::json_encode_message(msg, message.class.descriptor, pool_def, encoding_options, buffer, buffer_size, status)
unless status[:ok]
raise ParseError.new "Error occurred during encoding: #{Google::Protobuf::FFI.error_message(status)}"
end
if size >= buffer_size
buffer_size = size + 1
buffer = ::FFI::MemoryPointer.new(:char, buffer_size)
status.clear
size = Google::Protobuf::FFI::json_encode_message(msg, message.class.descriptor, pool_def, encoding_options, buffer, buffer_size, status)
unless status[:ok]
raise ParseError.new "Error occurred during encoding: #{Google::Protobuf::FFI.error_message(status)}"
end
if size >= buffer_size
raise ParseError.new "Inconsistent JSON encoding sizes - was #{buffer_size - 1}, now #{size}"
end
end
buffer.read_string_length(size).force_encoding("UTF-8").freeze
end
private
include Google::Protobuf::Internal::Convert
def empty_frozen_map(field_descriptor)
sub_message_def = Google::Protobuf::FFI.get_subtype_as_message field_descriptor
frozen_map = OBJECT_CACHE.get sub_message_def
if frozen_map.nil?
frozen_map = Google::Protobuf::Map.send(:construct_for_field, field_descriptor)
OBJECT_CACHE.try_add(sub_message_def, frozen_map.freeze)
end
raise "Empty Frozen Map is not frozen" unless frozen_map.frozen?
frozen_map
end
def frozen_map_from_field_descriptor(field_descriptor)
message_value = Google::Protobuf::FFI.get_message_value @msg, field_descriptor
return empty_frozen_map field_descriptor if message_value[:map_val].null?
get_map_field(message_value[:map_val], field_descriptor).freeze
end
def map_from_field_descriptor(field_descriptor)
return frozen_map_from_field_descriptor field_descriptor if frozen?
mutable_message_value = Google::Protobuf::FFI.get_mutable_message @msg, field_descriptor, @arena
get_map_field(mutable_message_value[:map], field_descriptor)
end
def empty_frozen_repeated_field(field_descriptor)
sub_message_def = Google::Protobuf::FFI.get_subtype_as_message field_descriptor
frozen_repeated_field = OBJECT_CACHE.get sub_message_def
if frozen_repeated_field.nil?
frozen_repeated_field = Google::Protobuf::RepeatedField.send(:construct_for_field, field_descriptor)
OBJECT_CACHE.try_add(sub_message_def, frozen_repeated_field.freeze)
end
raise "Empty frozen RepeatedField is not frozen" unless frozen_repeated_field.frozen?
frozen_repeated_field
end
def frozen_repeated_field_from_field_descriptor(field_descriptor)
message_value = Google::Protobuf::FFI.get_message_value @msg, field_descriptor
return empty_frozen_repeated_field field_descriptor if message_value[:array_val].null?
get_repeated_field(message_value[:array_val], field_descriptor).freeze
end
def repeated_field_from_field_descriptor(field_descriptor)
return frozen_repeated_field_from_field_descriptor field_descriptor if frozen?
mutable_message_value = Google::Protobuf::FFI.get_mutable_message @msg, field_descriptor, @arena
get_repeated_field(mutable_message_value[:array], field_descriptor)
end
def message_from_field_descriptor(field_descriptor)
return nil if frozen?
return nil unless Google::Protobuf::FFI.get_message_has @msg, field_descriptor
mutable_message = Google::Protobuf::FFI.get_mutable_message @msg, field_descriptor, @arena
sub_message = mutable_message[:msg]
sub_message_def = Google::Protobuf::FFI.get_subtype_as_message field_descriptor
Descriptor.send(:get_message, sub_message, sub_message_def, @arena)
end
def scalar_from_field_descriptor(field_descriptor)
c_type = field_descriptor.send(:c_type)
message_value = Google::Protobuf::FFI.get_message_value @msg, field_descriptor
msg_or_enum_def = c_type == :enum ? Google::Protobuf::FFI.get_subtype_as_enum(field_descriptor) : nil
return_value = convert_upb_to_ruby message_value, c_type, msg_or_enum_def
frozen? ? return_value.freeze : return_value
end
def self.setup_accessors!
@descriptor.each do |field_descriptor|
field_name = field_descriptor.name
unless instance_methods(true).include?(field_name.to_sym)
if field_descriptor.map?
define_method(field_name) do
map_from_field_descriptor field_descriptor
end
elsif field_descriptor.repeated?
define_method(field_name) do
repeated_field_from_field_descriptor field_descriptor
end
elsif field_descriptor.sub_message?
define_method(field_name) do
message_from_field_descriptor field_descriptor
end
else
define_method(field_name) do
scalar_from_field_descriptor field_descriptor
end
end
define_method("#{field_name}=") do |value|
index_assign_internal(value, field_descriptor: field_descriptor)
end
define_method("clear_#{field_name}") do
clear_internal(field_descriptor)
end
if field_descriptor.type == :enum
if field_descriptor.repeated?
define_method("#{field_name}_const") do
return_value = []
repeated_field_from_field_descriptor(field_descriptor).send(:each_msg_val) do |msg_val|
return_value << msg_val[:int32_val]
end
return_value
end
else
define_method("#{field_name}_const") do
message_value = Google::Protobuf::FFI.get_message_value @msg, field_descriptor
message_value[:int32_val]
end
end
end
if !field_descriptor.repeated? and field_descriptor.wrapper?
define_method("#{field_name}_as_value") do
get_field(field_descriptor, unwrap: true)
end
define_method("#{field_name}_as_value=") do |value|
if value.nil?
clear_internal(field_descriptor)
else
index_assign_internal(value, field_descriptor: field_descriptor, wrap: true)
end
end
end
if field_descriptor.has_presence?
define_method("has_#{field_name}?") do
Google::Protobuf::FFI.get_message_has(@msg, field_descriptor)
end
end
end
end
end
def self.setup_oneof_accessors!
@oneof_field_names = []
@descriptor.each_oneof do |oneof_descriptor|
self.add_oneof_accessors_for! oneof_descriptor
end
end
def self.add_oneof_accessors_for!(oneof_descriptor)
field_name = oneof_descriptor.name.to_sym
@oneof_field_names << field_name
unless instance_methods(true).include?(field_name)
define_method(field_name) do
field_descriptor = Google::Protobuf::FFI.get_message_which_oneof(@msg, oneof_descriptor)
if field_descriptor.nil?
return
else
return field_descriptor.name.to_sym
end
end
define_method("clear_#{field_name}") do
field_descriptor = Google::Protobuf::FFI.get_message_which_oneof(@msg, oneof_descriptor)
unless field_descriptor.nil?
clear_internal(field_descriptor)
end
end
define_method("has_#{field_name}?") do
!Google::Protobuf::FFI.get_message_which_oneof(@msg, oneof_descriptor).nil?
end
end
end
setup_accessors!
setup_oneof_accessors!
def self.private_constructor(arena, msg: nil, initial_value: nil)
instance = allocate
instance.send(:initialize, initial_value, arena, msg)
instance
end
def self.inspect_field(field_descriptor, c_type, message_value)
if field_descriptor.sub_message?
sub_msg_descriptor = Google::Protobuf::FFI.get_subtype_as_message(field_descriptor)
sub_msg_descriptor.msgclass.send(:inspect_internal, message_value[:msg_val])
else
convert_upb_to_ruby(message_value, c_type, field_descriptor.subtype).inspect
end
end
def self.inspect_internal(msg)
field_output = []
descriptor.each do |field_descriptor|
next if field_descriptor.has_presence? && !Google::Protobuf::FFI.get_message_has(msg, field_descriptor)
if field_descriptor.map?
message_descriptor = field_descriptor.subtype
key_field_def = Google::Protobuf::FFI.get_field_by_number(message_descriptor, 1)
key_field_type = Google::Protobuf::FFI.get_type(key_field_def)
value_field_def = Google::Protobuf::FFI.get_field_by_number(message_descriptor, 2)
value_field_type = Google::Protobuf::FFI.get_type(value_field_def)
message_value = Google::Protobuf::FFI.get_message_value(msg, field_descriptor)
iter = ::FFI::MemoryPointer.new(:size_t, 1)
iter.write(:size_t, Google::Protobuf::FFI::Upb_Map_Begin)
key_value_pairs = []
while Google::Protobuf::FFI.map_next(message_value[:map_val], iter) do
iter_size_t = iter.read(:size_t)
key_message_value = Google::Protobuf::FFI.map_key(message_value[:map_val], iter_size_t)
value_message_value = Google::Protobuf::FFI.map_value(message_value[:map_val], iter_size_t)
key_string = convert_upb_to_ruby(key_message_value, key_field_type).inspect
value_string = inspect_field(value_field_def, value_field_type, value_message_value)
key_value_pairs << "#{key_string}=>#{value_string}"
end
field_output << "#{field_descriptor.name}: {#{key_value_pairs.join(", ")}}"
elsif field_descriptor.repeated?
repeated_field_output = []
message_value = Google::Protobuf::FFI.get_message_value(msg, field_descriptor)
array = message_value[:array_val]
n = array.null? ? 0 : Google::Protobuf::FFI.array_size(array)
0.upto(n - 1) do |i|
element = Google::Protobuf::FFI.get_msgval_at(array, i)
repeated_field_output << inspect_field(field_descriptor, field_descriptor.send(:c_type), element)
end
field_output << "#{field_descriptor.name}: [#{repeated_field_output.join(", ")}]"
else
message_value = Google::Protobuf::FFI.get_message_value msg, field_descriptor
rendered_value = inspect_field(field_descriptor, field_descriptor.send(:c_type), message_value)
field_output << "#{field_descriptor.name}: #{rendered_value}"
end
end
"<#{name}: #{field_output.join(', ')}>"
end
def self.deep_copy(msg, arena = nil)
arena ||= Google::Protobuf::FFI.create_arena
encode_internal(msg) do |encoding, size, mini_table_ptr|
message = private_constructor(arena)
if encoding.nil? or encoding.null? or Google::Protobuf::FFI.decode_message(encoding, size, message.instance_variable_get(:@msg), mini_table_ptr, nil, 0, arena) != :Ok
raise ParseError.new "Error occurred copying proto"
end
message
end
end
def self.encode_internal(msg, encoding_options = 0)
temporary_arena = Google::Protobuf::FFI.create_arena
mini_table_ptr = Google::Protobuf::FFI.get_mini_table(descriptor)
size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
pointer_ptr = ::FFI::MemoryPointer.new(:pointer, 1)
encoding_status = Google::Protobuf::FFI.encode_message(msg, mini_table_ptr, encoding_options, temporary_arena, pointer_ptr.to_ptr, size_ptr)
raise "Encoding failed due to #{encoding_status}" unless encoding_status == :Ok
yield pointer_ptr.read(:pointer), size_ptr.read(:size_t), mini_table_ptr
end
def method_missing_internal(method_name, *args, mode: nil)
raise ArgumentError.new "method_missing_internal called with invalid mode #{mode.inspect}" unless [:respond_to_missing?, :method_missing].include? mode
if method_name.to_s.end_with? '='
if self.class.send(:oneof_field_names).include? method_name.to_s[0..-2].to_sym
return false if mode == :respond_to_missing?
raise RuntimeError.new "Oneof accessors are read-only."
end
end
original_method_missing(method_name, *args) if mode == :method_missing
end
def clear_internal(field_def)
raise FrozenError.new "can't modify frozen #{self.class}" if frozen?
Google::Protobuf::FFI.clear_message_field(@msg, field_def)
end
def index_internal(name)
field_descriptor = self.class.descriptor.lookup(name)
get_field field_descriptor unless field_descriptor.nil?
end
def index_assign_internal(value, name: nil, field_descriptor: nil, wrap: false)
raise FrozenError.new "can't modify frozen #{self.class}" if frozen?
if field_descriptor.nil?
field_descriptor = self.class.descriptor.lookup(name)
if field_descriptor.nil?
raise ArgumentError.new "Unknown field: #{name}"
end
end
unless field_descriptor.send :set_value_on_message, value, @msg, @arena, wrap: wrap
raise RuntimeError.new "allocation failed"
end
end
def initialize(initial_value = nil, arena = nil, msg = nil)
@arena = arena || Google::Protobuf::FFI.create_arena
@msg = msg || Google::Protobuf::FFI.new_message_from_def(Google::Protobuf::FFI.get_mini_table(self.class.descriptor), @arena)
unless initial_value.nil?
raise ArgumentError.new "Expected hash arguments or message, not #{initial_value.class}" unless initial_value.respond_to? :each
field_def_ptr = ::FFI::MemoryPointer.new :pointer
oneof_def_ptr = ::FFI::MemoryPointer.new :pointer
initial_value.each do |key, value|
raise ArgumentError.new "Expected string or symbols as hash keys when initializing proto from hash." unless [String, Symbol].include? key.class
unless Google::Protobuf::FFI.find_msg_def_by_name self.class.descriptor, key.to_s, key.to_s.bytesize, field_def_ptr, oneof_def_ptr
raise ArgumentError.new "Unknown field name '#{key}' in initialization map entry."
end
raise NotImplementedError.new "Haven't added oneofsupport yet" unless oneof_def_ptr.get_pointer(0).null?
raise NotImplementedError.new "Expected a field def" if field_def_ptr.get_pointer(0).null?
field_descriptor = FieldDescriptor.from_native field_def_ptr.get_pointer(0)
next if value.nil?
if field_descriptor.map?
index_assign_internal(Google::Protobuf::Map.send(:construct_for_field, field_descriptor, arena: @arena, value: value), name: key.to_s)
elsif field_descriptor.repeated?
index_assign_internal(RepeatedField.send(:construct_for_field, field_descriptor, arena: @arena, values: value), name: key.to_s)
else
index_assign_internal(value, name: key.to_s)
end
end
end
Google::Protobuf::OBJECT_CACHE.try_add @msg.address, self
end
def get_field(field, unwrap: false)
if field.map?
map_from_field_descriptor field
elsif field.repeated?
repeated_field_from_field_descriptor field
elsif field.sub_message?
return nil unless Google::Protobuf::FFI.get_message_has @msg, field
if unwrap
if field.has?(self)
sub_message_def = Google::Protobuf::FFI.get_subtype_as_message field
wrapper_message_value = Google::Protobuf::FFI.get_message_value @msg, field
fields = Google::Protobuf::FFI.field_count(sub_message_def)
raise "Sub message has #{fields} fields! Expected exactly 1." unless fields == 1
value_field_def = Google::Protobuf::FFI.get_field_by_number sub_message_def, 1
message_value = Google::Protobuf::FFI.get_message_value wrapper_message_value[:msg_val], value_field_def
convert_upb_to_ruby message_value, Google::Protobuf::FFI.get_c_type(value_field_def)
else
nil
end
else
message_from_field_descriptor field
end
else
scalar_from_field_descriptor field
end
end
def get_repeated_field(array, field)
return nil if array.nil? or array.null?
repeated_field = OBJECT_CACHE.get(array.address)
if repeated_field.nil?
repeated_field = RepeatedField.send(:construct_for_field, field, arena: @arena, array: array)
repeated_field.freeze if frozen?
end
repeated_field
end
def get_map_field(map, field)
return nil if map.nil? or map.null?
map_field = OBJECT_CACHE.get(map.address)
if map_field.nil?
map_field = Google::Protobuf::Map.send(:construct_for_field, field, arena: @arena, map: map)
map_field.freeze if frozen?
end
map_field
end
end
end
|