/
RubyMessage.java
1509 lines (1361 loc) · 58 KB
/
RubyMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Protocol Buffers - Google's data interchange format
* Copyright 2014 Google Inc. All rights reserved.
* https://developers.google.com/protocol-buffers/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.protobuf.jruby;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyOneofDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.UnknownFieldSet;
import com.google.protobuf.util.JsonFormat;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jruby.*;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
public class RubyMessage extends RubyObject {
private final String DEFAULT_VALUE = "google.protobuf.FieldDescriptorProto.default_value";
private final String TYPE = "type";
public RubyMessage(Ruby runtime, RubyClass klazz, Descriptor descriptor) {
super(runtime, klazz);
this.descriptor = descriptor;
this.cRepeatedField = (RubyClass) runtime.getClassFromPath("Google::Protobuf::RepeatedField");
this.cMap = (RubyClass) runtime.getClassFromPath("Google::Protobuf::Map");
this.builder = DynamicMessage.newBuilder(descriptor);
this.fields = new HashMap<FieldDescriptor, IRubyObject>();
this.oneofCases = new HashMap<OneofDescriptor, FieldDescriptor>();
this.proto3 =
LegacyFileDescriptor.getSyntax(descriptor.getFile()) == LegacyFileDescriptor.Syntax.PROTO3;
}
/*
* call-seq:
* Message.new(kwargs) => new_message
*
* Creates a new instance of the given message class. Keyword arguments may be
* provided with keywords corresponding to field names.
*
* Note that no literal Message class exists. Only concrete classes per message
* type exist, as provided by the #msgclass method on Descriptors after they
* have been added to a pool. The method definitions described here on the
* Message class are provided on each concrete message class.
*/
@JRubyMethod(optional = 1)
public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (args.length == 1) {
if (!(args[0] instanceof RubyHash)) {
throw runtime.newArgumentError("expected Hash arguments.");
}
RubyHash hash = args[0].convertToHash();
hash.visitAll(
context,
new RubyHash.Visitor() {
@Override
public void visit(IRubyObject key, IRubyObject value) {
if (!(key instanceof RubySymbol) && !(key instanceof RubyString)) {
throw Utils.createTypeError(
context, "Expected string or symbols as hash keys in initialization map.");
}
final FieldDescriptor fieldDescriptor =
findField(context, key, ignoreUnknownFieldsOnInit);
if (value == null || value.isNil()) return;
if (fieldDescriptor.isMapField()) {
if (!(value instanceof RubyHash))
throw runtime.newArgumentError(
"Expected Hash object as initializer value for map field '"
+ key.asJavaString()
+ "' (given "
+ value.getMetaClass()
+ ").");
final RubyMap map = newMapForField(context, fieldDescriptor);
map.mergeIntoSelf(context, value);
fields.put(fieldDescriptor, map);
} else if (fieldDescriptor.isRepeated()) {
if (!(value instanceof RubyArray))
throw runtime.newArgumentError(
"Expected array as initializer value for repeated field '"
+ key.asJavaString()
+ "' (given "
+ value.getMetaClass()
+ ").");
fields.put(fieldDescriptor, rubyToRepeatedField(context, fieldDescriptor, value));
} else {
OneofDescriptor oneof = fieldDescriptor.getContainingOneof();
if (oneof != null) {
oneofCases.put(oneof, fieldDescriptor);
}
if (value instanceof RubyHash
&& fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE) {
RubyDescriptor descriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
RubyClass typeClass = (RubyClass) descriptor.msgclass(context);
value = (IRubyObject) typeClass.newInstance(context, value, Block.NULL_BLOCK);
fields.put(fieldDescriptor, value);
} else {
indexSet(context, key, value);
}
}
}
},
null);
}
return this;
}
/*
* call-seq:
* Message.[]=(index, value)
*
* Sets a field's value by field name. The provided field name should be a
* string.
*/
@JRubyMethod(name = "[]=")
public IRubyObject indexSet(ThreadContext context, IRubyObject fieldName, IRubyObject value) {
FieldDescriptor fieldDescriptor = findField(context, fieldName);
return setFieldInternal(context, fieldDescriptor, value);
}
/*
* call-seq:
* Message.[](index) => value
*
* Accesses a field's value by field name. The provided field name should be a
* string.
*/
@JRubyMethod(name = "[]")
public IRubyObject index(ThreadContext context, IRubyObject fieldName) {
FieldDescriptor fieldDescriptor = findField(context, fieldName);
return getFieldInternal(context, fieldDescriptor);
}
/*
* call-seq:
* Message.inspect => string
*
* Returns a human-readable string representing this message. It will be
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
* field's value is represented according to its own #inspect method.
*/
@JRubyMethod(name = {"inspect", "to_s"})
public IRubyObject inspect() {
ThreadContext context = getRuntime().getCurrentContext();
String cname = metaClass.getName();
String colon = ": ";
String comma = ", ";
StringBuilder sb = new StringBuilder("<");
boolean addComma = false;
sb.append(cname).append(colon);
for (FieldDescriptor fd : descriptor.getFields()) {
if (fd.hasPresence() && !fields.containsKey(fd)) {
continue;
}
if (addComma) {
sb.append(comma);
} else {
addComma = true;
}
sb.append(fd.getName()).append(colon);
IRubyObject value = getFieldInternal(context, fd);
if (value instanceof RubyBoolean) {
// Booleans don't implement internal "inspect" methods so have to call handle them manually
sb.append(value.isTrue() ? "true" : "false");
} else {
sb.append(value.inspect());
}
}
sb.append(">");
return context.runtime.newString(sb.toString());
}
/*
* call-seq:
* Message.hash => hash_value
*
* Returns a hash value that represents this message's field values.
*/
@JRubyMethod
public IRubyObject hash(ThreadContext context) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
for (FieldDescriptor fd : descriptor.getFields()) {
digest.update((byte) getFieldInternal(context, fd).hashCode());
}
return context.runtime.newFixnum(ByteBuffer.wrap(digest.digest()).getLong());
} catch (NoSuchAlgorithmException ignore) {
return context.runtime.newFixnum(System.identityHashCode(this));
}
}
/*
* call-seq:
* Message.==(other) => boolean
*
* Performs a deep comparison of this message with another. Messages are equal
* if they have the same type and if each field is equal according to the :==
* method's semantics (a more efficient comparison may actually be done if the
* field is of a primitive type).
*/
@JRubyMethod(name = {"==", "eql?"})
public IRubyObject eq(ThreadContext context, IRubyObject other) {
Ruby runtime = context.runtime;
if (!(other instanceof RubyMessage)) return runtime.getFalse();
RubyMessage message = (RubyMessage) other;
if (descriptor != message.descriptor) {
return runtime.getFalse();
}
for (FieldDescriptor fdef : descriptor.getFields()) {
IRubyObject thisVal = getFieldInternal(context, fdef);
IRubyObject thatVal = message.getFieldInternal(context, fdef);
IRubyObject ret = thisVal.callMethod(context, "==", thatVal);
if (!ret.isTrue()) {
return runtime.getFalse();
}
}
return runtime.getTrue();
}
/*
* call-seq:
* Message.respond_to?(method_name, search_private_and_protected) => boolean
*
* Parallels method_missing, returning true when this object implements a method with the given
* method_name.
*/
@JRubyMethod(name = "respond_to?", required = 1, optional = 1)
public IRubyObject respondTo(ThreadContext context, IRubyObject[] args) {
String methodName = args[0].asJavaString();
if (descriptor.findFieldByName(methodName) != null) {
return context.runtime.getTrue();
}
RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass);
IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]);
if (!oneofDescriptor.isNil()) {
return context.runtime.getTrue();
}
if (methodName.startsWith(CLEAR_PREFIX)) {
String strippedMethodName = methodName.substring(6);
oneofDescriptor =
rubyDescriptor.lookupOneof(context, context.runtime.newSymbol(strippedMethodName));
if (!oneofDescriptor.isNil()) {
return context.runtime.getTrue();
}
if (descriptor.findFieldByName(strippedMethodName) != null) {
return context.runtime.getTrue();
}
}
if (methodName.startsWith(HAS_PREFIX) && methodName.endsWith(QUESTION_MARK)) {
String strippedMethodName = methodName.substring(4, methodName.length() - 1);
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(strippedMethodName);
if (fieldDescriptor != null && fieldDescriptor.hasPresence()) {
return context.runtime.getTrue();
}
oneofDescriptor =
rubyDescriptor.lookupOneof(
context, RubyString.newString(context.runtime, strippedMethodName));
if (!oneofDescriptor.isNil()) {
return context.runtime.getTrue();
}
}
if (methodName.endsWith(AS_VALUE_SUFFIX)) {
FieldDescriptor fieldDescriptor =
descriptor.findFieldByName(methodName.substring(0, methodName.length() - 9));
if (fieldDescriptor != null && isWrappable(fieldDescriptor)) {
return context.runtime.getTrue();
}
}
if (methodName.endsWith(CONST_SUFFIX)) {
FieldDescriptor fieldDescriptor =
descriptor.findFieldByName(methodName.substring(0, methodName.length() - 6));
if (fieldDescriptor != null) {
if (fieldDescriptor.getType() == FieldDescriptor.Type.ENUM) {
return context.runtime.getTrue();
}
}
}
if (methodName.endsWith(Utils.EQUAL_SIGN)) {
String strippedMethodName = methodName.substring(0, methodName.length() - 1);
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(strippedMethodName);
if (fieldDescriptor != null) {
return context.runtime.getTrue();
}
if (strippedMethodName.endsWith(AS_VALUE_SUFFIX)) {
strippedMethodName = methodName.substring(0, strippedMethodName.length() - 9);
fieldDescriptor = descriptor.findFieldByName(strippedMethodName);
if (fieldDescriptor != null && isWrappable(fieldDescriptor)) {
return context.runtime.getTrue();
}
}
}
boolean includePrivate = false;
if (args.length == 2) {
includePrivate = context.runtime.getTrue().equals(args[1]);
}
return metaClass.respondsToMethod(methodName, includePrivate)
? context.runtime.getTrue()
: context.runtime.getFalse();
}
/*
* call-seq:
* Message.method_missing(*args)
*
* Provides accessors and setters and methods to clear and check for presence of
* message fields according to their field names.
*
* For any field whose name does not conflict with a built-in method, an
* accessor is provided with the same name as the field, and a setter is
* provided with the name of the field plus the '=' suffix. Thus, given a
* message instance 'msg' with field 'foo', the following code is valid:
*
* msg.foo = 42
* puts msg.foo
*
* This method also provides read-only accessors for oneofs. If a oneof exists
* with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
* the name of the field in that oneof that is currently set, or nil if none.
*
* It also provides methods of the form 'clear_fieldname' to clear the value
* of the field 'fieldname'. For basic data types, this will set the default
* value of the field.
*
* Additionally, it provides methods of the form 'has_fieldname?', which returns
* true if the field 'fieldname' is set in the message object, else false. For
* 'proto3' syntax, calling this for a basic type field will result in an error.
*/
@JRubyMethod(name = "method_missing", rest = true)
public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;
String methodName = args[0].asJavaString();
RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass);
if (args.length == 1) {
// If we find a Oneof return it's name (use lookupOneof because it has an index)
IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]);
if (!oneofDescriptor.isNil()) {
RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor;
OneofDescriptor ood = rubyOneofDescriptor.getDescriptor();
// Check to see if we set this through ruby
FieldDescriptor fieldDescriptor = oneofCases.get(ood);
if (fieldDescriptor == null) {
// See if we set this from decoding a message
fieldDescriptor = builder.getOneofFieldDescriptor(ood);
if (fieldDescriptor == null) {
return context.nil;
} else {
// Cache it so we don't need to do multiple checks next time
oneofCases.put(ood, fieldDescriptor);
return runtime.newSymbol(fieldDescriptor.getName());
}
} else {
return runtime.newSymbol(fieldDescriptor.getName());
}
}
// If we find a field return its value
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null) {
return getFieldInternal(context, fieldDescriptor);
}
if (methodName.startsWith(CLEAR_PREFIX)) {
methodName = methodName.substring(6);
oneofDescriptor = rubyDescriptor.lookupOneof(context, runtime.newSymbol(methodName));
if (!oneofDescriptor.isNil()) {
fieldDescriptor = oneofCases.get(((RubyOneofDescriptor) oneofDescriptor).getDescriptor());
if (fieldDescriptor == null) {
// Clearing an already cleared oneof; return here to avoid NoMethodError.
return context.nil;
}
}
if (fieldDescriptor == null) {
fieldDescriptor = descriptor.findFieldByName(methodName);
}
if (fieldDescriptor != null) {
return clearFieldInternal(context, fieldDescriptor);
}
} else if (methodName.startsWith(HAS_PREFIX) && methodName.endsWith(QUESTION_MARK)) {
methodName =
methodName.substring(
4, methodName.length() - 1); // Trim "has_" and "?" off the field name
oneofDescriptor = rubyDescriptor.lookupOneof(context, runtime.newSymbol(methodName));
if (!oneofDescriptor.isNil()) {
RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor;
return oneofCases.containsKey(rubyOneofDescriptor.getDescriptor())
? runtime.getTrue()
: runtime.getFalse();
}
fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null && fieldDescriptor.hasPresence()) {
return fields.containsKey(fieldDescriptor) ? runtime.getTrue() : runtime.getFalse();
}
} else if (methodName.endsWith(AS_VALUE_SUFFIX)) {
methodName = methodName.substring(0, methodName.length() - 9);
fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null && isWrappable(fieldDescriptor)) {
IRubyObject value = getFieldInternal(context, fieldDescriptor);
if (!value.isNil() && value instanceof RubyMessage) {
return ((RubyMessage) value).index(context, runtime.newString("value"));
}
return value;
}
} else if (methodName.endsWith(CONST_SUFFIX)) {
methodName = methodName.substring(0, methodName.length() - 6);
fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null && fieldDescriptor.getType() == FieldDescriptor.Type.ENUM) {
IRubyObject enumValue = getFieldInternal(context, fieldDescriptor);
if (!enumValue.isNil()) {
EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType();
if (enumValue instanceof RubyRepeatedField) {
RubyArray values = (RubyArray) ((RubyRepeatedField) enumValue).toArray(context);
RubyArray retValues = runtime.newArray(values.getLength());
for (int i = 0; i < values.getLength(); i++) {
String val = values.eltInternal(i).toString();
retValues.store(
(long) i, runtime.newFixnum(enumDescriptor.findValueByName(val).getNumber()));
}
return retValues;
}
return runtime.newFixnum(
enumDescriptor.findValueByName(enumValue.asJavaString()).getNumber());
}
}
}
} else if (args.length == 2 && methodName.endsWith(Utils.EQUAL_SIGN)) {
methodName = methodName.substring(0, methodName.length() - 1); // Trim equals sign
FieldDescriptor fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null) {
return setFieldInternal(context, fieldDescriptor, args[1]);
}
IRubyObject oneofDescriptor =
rubyDescriptor.lookupOneof(context, RubyString.newString(context.runtime, methodName));
if (!oneofDescriptor.isNil()) {
throw runtime.newRuntimeError("Oneof accessors are read-only.");
}
if (methodName.endsWith(AS_VALUE_SUFFIX)) {
methodName = methodName.substring(0, methodName.length() - 9);
fieldDescriptor = descriptor.findFieldByName(methodName);
if (fieldDescriptor != null && isWrappable(fieldDescriptor)) {
if (args[1].isNil()) {
return setFieldInternal(context, fieldDescriptor, args[1]);
}
RubyClass typeClass =
(RubyClass)
((RubyDescriptor) getDescriptorForField(context, fieldDescriptor))
.msgclass(context);
RubyMessage msg = (RubyMessage) typeClass.newInstance(context, Block.NULL_BLOCK);
msg.indexSet(context, runtime.newString("value"), args[1]);
return setFieldInternal(context, fieldDescriptor, msg);
}
}
}
return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK);
}
/**
* call-seq: Message.dup => new_message Performs a shallow copy of this message and returns the
* new copy.
*/
@JRubyMethod
public IRubyObject dup(ThreadContext context) {
RubyMessage dup = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK);
for (FieldDescriptor fieldDescriptor : this.descriptor.getFields()) {
if (fieldDescriptor.isRepeated()) {
dup.fields.put(fieldDescriptor, this.getRepeatedField(context, fieldDescriptor));
} else if (fields.containsKey(fieldDescriptor)) {
dup.setFieldInternal(context, fieldDescriptor, fields.get(fieldDescriptor));
} else if (this.builder.hasField(fieldDescriptor)) {
dup.fields.put(
fieldDescriptor,
wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor)));
}
}
return dup;
}
/*
* call-seq:
* Message.descriptor => descriptor
*
* Class method that returns the Descriptor instance corresponding to this
* message class's type.
*/
@JRubyMethod(name = "descriptor", meta = true)
public static IRubyObject getDescriptor(ThreadContext context, IRubyObject recv) {
return ((RubyClass) recv).getInstanceVariable(Utils.DESCRIPTOR_INSTANCE_VAR);
}
/*
* call-seq:
* MessageClass.encode(msg, options = {}) => bytes
*
* Encodes the given message object to its serialized form in protocol buffers
* wire format.
* @param options [Hash] options for the encoder
* recursion_limit: set to maximum encoding depth for message (default is 64)
*/
@JRubyMethod(required = 1, optional = 1, meta = true)
public static IRubyObject encode(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
if (recv != args[0].getMetaClass()) {
throw context.runtime.newArgumentError(
"Tried to encode a " + args[0].getMetaClass() + " message with " + recv);
}
RubyMessage message = (RubyMessage) args[0];
int recursionLimitInt = SINK_MAXIMUM_NESTING;
if (args.length > 1) {
RubyHash options = (RubyHash) args[1];
IRubyObject recursionLimit = options.fastARef(context.runtime.newSymbol("recursion_limit"));
if (recursionLimit != null) {
recursionLimitInt = ((RubyNumeric) recursionLimit).getIntValue();
}
}
return context.runtime.newString(
new ByteList(message.build(context, 0, recursionLimitInt).toByteArray()));
}
/*
* call-seq:
* MessageClass.decode(data, options = {}) => message
*
* Decodes the given data (as a string containing bytes in protocol buffers wire
* format) under the interpretation given by this message class's definition
* and returns a message object with the corresponding field values.
* @param options [Hash] options for the decoder
* recursion_limit: set to maximum decoding depth for message (default is 100)
*/
@JRubyMethod(required = 1, optional = 1, meta = true)
public static IRubyObject decode(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
IRubyObject data = args[0];
byte[] bin = data.convertToString().getBytes();
CodedInputStream input = CodedInputStream.newInstance(bin);
RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK);
if (args.length == 2) {
if (!(args[1] instanceof RubyHash)) {
throw context.runtime.newArgumentError("Expected hash arguments.");
}
IRubyObject recursionLimit =
((RubyHash) args[1]).fastARef(context.runtime.newSymbol("recursion_limit"));
if (recursionLimit != null) {
input.setRecursionLimit(((RubyNumeric) recursionLimit).getIntValue());
}
}
return decodeBytes(context, ret, input, /*freeze*/ false);
}
public static IRubyObject decodeBytes(
ThreadContext context, RubyMessage ret, CodedInputStream input, boolean freeze) {
try {
ret.builder.mergeFrom(input, RubyDescriptorPool.registry);
} catch (Exception e) {
throw RaiseException.from(
context.runtime,
(RubyClass) context.runtime.getClassFromPath("Google::Protobuf::ParseError"),
e.getMessage());
}
if (!ret.proto3) {
// Need to reset unknown values in repeated enum fields
ret.builder
.getUnknownFields()
.asMap()
.forEach(
(i, values) -> {
FieldDescriptor fd = ret.builder.getDescriptorForType().findFieldByNumber(i);
if (fd != null && fd.isRepeated() && fd.getType() == FieldDescriptor.Type.ENUM) {
EnumDescriptor ed = fd.getEnumType();
values
.getVarintList()
.forEach(
value -> {
ret.builder.addRepeatedField(
fd, ed.findValueByNumberCreatingIfUnknown(value.intValue()));
});
}
});
}
if (freeze) {
ret.deepFreeze(context);
}
return ret;
}
/*
* call-seq:
* MessageClass.encode_json(msg, options = {}) => json_string
*
* Encodes the given message object into its serialized JSON representation.
* @param options [Hash] options for the decoder
* preserve_proto_fieldnames: set true to use original fieldnames (default is to camelCase)
* emit_defaults: set true to emit 0/false values (default is to omit them)
* format_enums_as_integers: set true to emit enum values as integer (default is string)
*/
@JRubyMethod(name = "encode_json", required = 1, optional = 1, meta = true)
public static IRubyObject encodeJson(
ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
RubyMessage message = (RubyMessage) args[0];
JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
String result;
if (args.length > 1) {
RubyHash options;
if (args[1] instanceof RubyHash) {
options = (RubyHash) args[1];
} else if (args[1].respondsTo("to_h")) {
options = (RubyHash) args[1].callMethod(context, "to_h");
} else {
throw runtime.newArgumentError("Expected hash arguments.");
}
IRubyObject emitDefaults = options.fastARef(runtime.newSymbol("emit_defaults"));
IRubyObject preserveNames = options.fastARef(runtime.newSymbol("preserve_proto_fieldnames"));
IRubyObject printingEnumsAsInts =
options.fastARef(runtime.newSymbol("format_enums_as_integers"));
if (emitDefaults != null && emitDefaults.isTrue()) {
printer = printer.includingDefaultValueFields();
}
if (preserveNames != null && preserveNames.isTrue()) {
printer = printer.preservingProtoFieldNames();
}
if (printingEnumsAsInts != null && printingEnumsAsInts.isTrue()) {
printer = printer.printingEnumsAsInts();
}
}
printer =
printer.usingTypeRegistry(
JsonFormat.TypeRegistry.newBuilder().add(message.descriptor).build());
try {
result = printer.print(message.build(context, 0, SINK_MAXIMUM_NESTING));
} catch (InvalidProtocolBufferException e) {
throw runtime.newRuntimeError(e.getMessage());
} catch (IllegalArgumentException e) {
throw createParseError(context, e.getMessage());
}
return runtime.newString(result);
}
/*
* call-seq:
* MessageClass.decode_json(data, options = {}) => message
*
* Decodes the given data (as a string containing bytes in protocol buffers wire
* format) under the interpretation given by this message class's definition
* and returns a message object with the corresponding field values.
*
* @param options [Hash] options for the decoder
* ignore_unknown_fields: set true to ignore unknown fields (default is to
* raise an error)
*/
@JRubyMethod(name = "decode_json", required = 1, optional = 1, meta = true)
public static IRubyObject decodeJson(
ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
boolean ignoreUnknownFields = false;
IRubyObject data = args[0];
JsonFormat.Parser parser = JsonFormat.parser();
if (args.length == 2) {
if (!(args[1] instanceof RubyHash)) {
throw runtime.newArgumentError("Expected hash arguments.");
}
IRubyObject ignoreSetting =
((RubyHash) args[1]).fastARef(runtime.newSymbol("ignore_unknown_fields"));
if (ignoreSetting != null && ignoreSetting.isTrue()) {
parser = parser.ignoringUnknownFields();
}
}
if (!(data instanceof RubyString)) {
throw runtime.newArgumentError("Expected string for JSON data.");
}
RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK);
parser =
parser.usingTypeRegistry(JsonFormat.TypeRegistry.newBuilder().add(ret.descriptor).build());
try {
parser.merge(data.asJavaString(), ret.builder);
} catch (InvalidProtocolBufferException e) {
throw createParseError(context, e.getMessage().replace("Cannot find", "No such"));
}
if (isWrapper(ret.descriptor)) {
throw runtime.newRuntimeError(
"Parsing a wrapper type from JSON at the top level does not work.");
}
return ret;
}
@JRubyMethod(name = "to_h")
public IRubyObject toHash(ThreadContext context) {
Ruby runtime = context.runtime;
RubyHash ret = RubyHash.newHash(runtime);
for (FieldDescriptor fdef : this.descriptor.getFields()) {
IRubyObject value = getFieldInternal(context, fdef, proto3);
if (!value.isNil()) {
if (fdef.isRepeated() && !fdef.isMapField()) {
if (!proto3 && ((RubyRepeatedField) value).size() == 0)
continue; // Don't output empty repeated fields for proto2
if (fdef.getType() != FieldDescriptor.Type.MESSAGE) {
value = Helpers.invoke(context, value, "to_a");
} else {
RubyArray ary = value.convertToArray();
for (int i = 0; i < ary.size(); i++) {
IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h");
ary.eltInternalSet(i, submsg);
}
value = ary.to_ary();
}
} else if (value.respondsTo("to_h")) {
value = Helpers.invoke(context, value, "to_h");
} else if (value.respondsTo("to_a")) {
value = Helpers.invoke(context, value, "to_a");
}
}
if (proto3 || !value.isNil()) {
ret.fastASet(runtime.newSymbol(fdef.getName()), value);
}
}
return ret;
}
protected IRubyObject deepFreeze(ThreadContext context) {
setFrozen(true);
for (FieldDescriptor fdef : descriptor.getFields()) {
if (fdef.isMapField()) {
((RubyMap) fields.get(fdef)).deepFreeze(context);
} else if (fdef.isRepeated()) {
this.getRepeatedField(context, fdef).deepFreeze(context);
} else if (fields.containsKey(fdef)) {
if (fdef.getType() == FieldDescriptor.Type.MESSAGE) {
((RubyMessage) fields.get(fdef)).deepFreeze(context);
}
}
}
return this;
}
protected DynamicMessage build(ThreadContext context, int depth, int recursionLimit) {
if (depth >= recursionLimit) {
throw context.runtime.newRuntimeError("Recursion limit exceeded during encoding.");
}
RubySymbol typeBytesSymbol = RubySymbol.newSymbol(context.runtime, "TYPE_BYTES");
// Handle the typical case where the fields.keySet contain the fieldDescriptors
for (FieldDescriptor fieldDescriptor : fields.keySet()) {
IRubyObject value = fields.get(fieldDescriptor);
if (value instanceof RubyMap) {
builder.clearField(fieldDescriptor);
RubyDescriptor mapDescriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
for (DynamicMessage kv :
((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
builder.addRepeatedField(fieldDescriptor, kv);
}
} else if (value instanceof RubyRepeatedField) {
RubyRepeatedField repeatedField = (RubyRepeatedField) value;
builder.clearField(fieldDescriptor);
for (int i = 0; i < repeatedField.size(); i++) {
Object item =
convert(
context,
fieldDescriptor,
repeatedField.get(i),
depth,
recursionLimit,
/*isDefaultValueForBytes*/ false);
builder.addRepeatedField(fieldDescriptor, item);
}
} else if (!value.isNil()) {
/**
* Detect the special case where default_value strings are provided for byte fields. If so,
* disable normal string encoding behavior within convert. For a more detailed explanation
* of other possible workarounds, see the comments above {@code
* com.google.protobuf.Internal#stringDefaultValue() stringDefaultValue}.
*/
boolean isDefaultStringForBytes = false;
if (DEFAULT_VALUE.equals(fieldDescriptor.getFullName())) {
FieldDescriptor enumFieldDescriptorForType =
this.builder.getDescriptorForType().findFieldByName(TYPE);
if (typeBytesSymbol.equals(fields.get(enumFieldDescriptorForType))) {
isDefaultStringForBytes = true;
}
}
builder.setField(
fieldDescriptor,
convert(
context, fieldDescriptor, value, depth, recursionLimit, isDefaultStringForBytes));
}
}
// Handle cases where {@code fields} doesn't contain the value until after getFieldInternal
// is called - typical of a deserialized message. Skip non-maps and descriptors that already
// have an entry in {@code fields}.
for (FieldDescriptor fieldDescriptor : descriptor.getFields()) {
if (!fieldDescriptor.isMapField()) {
continue;
}
IRubyObject value = fields.get(fieldDescriptor);
if (value != null) {
continue;
}
value = getFieldInternal(context, fieldDescriptor);
if (value instanceof RubyMap) {
builder.clearField(fieldDescriptor);
RubyDescriptor mapDescriptor =
(RubyDescriptor) getDescriptorForField(context, fieldDescriptor);
for (DynamicMessage kv :
((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) {
builder.addRepeatedField(fieldDescriptor, kv);
}
}
}
return builder.build();
}
// Internal use only, called by Google::Protobuf.deep_copy
protected IRubyObject deepCopy(ThreadContext context) {
RubyMessage copy = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK);
for (FieldDescriptor fdef : descriptor.getFields()) {
if (fdef.isRepeated()) {
copy.fields.put(fdef, this.getRepeatedField(context, fdef).deepCopy(context));
} else if (fields.containsKey(fdef)) {
if (fdef.getType() == FieldDescriptor.Type.MESSAGE) {
copy.setFieldInternal(context, fdef, ((RubyMessage) fields.get(fdef)).deepCopy(context));
} else {
copy.setFieldInternal(context, fdef, fields.get(fdef));
}
} else if (builder.hasField(fdef)) {
copy.fields.put(fdef, wrapField(context, fdef, builder.getField(fdef)));
}
}
return copy;
}
protected IRubyObject clearField(ThreadContext context, FieldDescriptor fieldDescriptor) {
validateMessageType(context, fieldDescriptor, "clear");
return clearFieldInternal(context, fieldDescriptor);
}
protected void discardUnknownFields(ThreadContext context) {
discardUnknownFields(context, builder);
}
protected IRubyObject getField(ThreadContext context, FieldDescriptor fieldDescriptor) {
validateMessageType(context, fieldDescriptor, "get");
return getFieldInternal(context, fieldDescriptor);
}
protected IRubyObject hasField(ThreadContext context, FieldDescriptor fieldDescriptor) {
validateMessageType(context, fieldDescriptor, "has?");
if (!fieldDescriptor.hasPresence()) {
throw context.runtime.newArgumentError("does not track presence");
}
return fields.containsKey(fieldDescriptor)
? context.runtime.getTrue()
: context.runtime.getFalse();
}
protected IRubyObject setField(
ThreadContext context, FieldDescriptor fieldDescriptor, IRubyObject value) {
validateMessageType(context, fieldDescriptor, "set");
return setFieldInternal(context, fieldDescriptor, value);
}
protected IRubyObject setField(
ThreadContext context, RubyFieldDescriptor fieldDescriptor, IRubyObject value) {
validateMessageType(context, fieldDescriptor.getDescriptor(), "set");
return setFieldInternal(context, fieldDescriptor.getDescriptor(), fieldDescriptor, value);
}
private RubyRepeatedField getRepeatedField(
ThreadContext context, FieldDescriptor fieldDescriptor) {
if (fields.containsKey(fieldDescriptor)) {
return (RubyRepeatedField) fields.get(fieldDescriptor);
}
int count = this.builder.getRepeatedFieldCount(fieldDescriptor);
RubyRepeatedField ret = repeatedFieldForFieldDescriptor(context, fieldDescriptor);
for (int i = 0; i < count; i++) {
ret.push(
context,
new IRubyObject[] {
wrapField(context, fieldDescriptor, this.builder.getRepeatedField(fieldDescriptor, i))
});
}
fields.put(fieldDescriptor, ret);
return ret;
}
private IRubyObject buildFrom(ThreadContext context, DynamicMessage dynamicMessage) {
this.builder.mergeFrom(dynamicMessage);
return this;
}
private IRubyObject clearFieldInternal(ThreadContext context, FieldDescriptor fieldDescriptor) {
OneofDescriptor ood = fieldDescriptor.getContainingOneof();
if (ood != null) oneofCases.remove(ood);