google-protobuf 3.0.0.alpha.5.0.3-universal-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 790962508c1843a8e28f597a202ee9db0de05c64
4
+ data.tar.gz: 29161264892bb71f758dc41187187b5a226ec5b9
5
+ SHA512:
6
+ metadata.gz: c0f2d5848ed27e8f78e0e67656b509417822769d3df01ac6b18a0f9a419892758e62ad0df8281aabb173e0fea2225e18b5f9115ea7190f67ced61078f97a3716
7
+ data.tar.gz: 90e8d703172b2013ff3850c56e242e4619a4595260e1c0d686d2826f0826eb1026a27fc7ae77882c5092490759f53c323bd94152a2d22eec21d8ef6ab2c43868
@@ -0,0 +1,1753 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are
7
+ // met:
8
+ //
9
+ // * Redistributions of source code must retain the above copyright
10
+ // notice, this list of conditions and the following disclaimer.
11
+ // * Redistributions in binary form must reproduce the above
12
+ // copyright notice, this list of conditions and the following disclaimer
13
+ // in the documentation and/or other materials provided with the
14
+ // distribution.
15
+ // * Neither the name of Google Inc. nor the names of its
16
+ // contributors may be used to endorse or promote products derived from
17
+ // this software without specific prior written permission.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ #include "protobuf.h"
32
+
33
+ // -----------------------------------------------------------------------------
34
+ // Common utilities.
35
+ // -----------------------------------------------------------------------------
36
+
37
+ static const char* get_str(VALUE str) {
38
+ Check_Type(str, T_STRING);
39
+ return RSTRING_PTR(str);
40
+ }
41
+
42
+ static VALUE rb_str_maybe_null(const char* s) {
43
+ if (s == NULL) {
44
+ s = "";
45
+ }
46
+ return rb_str_new2(s);
47
+ }
48
+
49
+ static upb_def* check_notfrozen(const upb_def* def) {
50
+ if (upb_def_isfrozen(def)) {
51
+ rb_raise(rb_eRuntimeError,
52
+ "Attempt to modify a frozen descriptor. Once descriptors are "
53
+ "added to the descriptor pool, they may not be modified.");
54
+ }
55
+ return (upb_def*)def;
56
+ }
57
+
58
+ static upb_msgdef* check_msg_notfrozen(const upb_msgdef* def) {
59
+ return upb_downcast_msgdef_mutable(check_notfrozen((const upb_def*)def));
60
+ }
61
+
62
+ static upb_fielddef* check_field_notfrozen(const upb_fielddef* def) {
63
+ return upb_downcast_fielddef_mutable(check_notfrozen((const upb_def*)def));
64
+ }
65
+
66
+ static upb_oneofdef* check_oneof_notfrozen(const upb_oneofdef* def) {
67
+ return (upb_oneofdef*)check_notfrozen((const upb_def*)def);
68
+ }
69
+
70
+ static upb_enumdef* check_enum_notfrozen(const upb_enumdef* def) {
71
+ return (upb_enumdef*)check_notfrozen((const upb_def*)def);
72
+ }
73
+
74
+ // -----------------------------------------------------------------------------
75
+ // DescriptorPool.
76
+ // -----------------------------------------------------------------------------
77
+
78
+ #define DEFINE_CLASS(name, string_name) \
79
+ VALUE c ## name; \
80
+ const rb_data_type_t _ ## name ## _type = { \
81
+ string_name, \
82
+ { name ## _mark, name ## _free, NULL }, \
83
+ }; \
84
+ name* ruby_to_ ## name(VALUE val) { \
85
+ name* ret; \
86
+ TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
87
+ return ret; \
88
+ } \
89
+
90
+ #define DEFINE_SELF(type, var, rb_var) \
91
+ type* var = ruby_to_ ## type(rb_var)
92
+
93
+ // Global singleton DescriptorPool. The user is free to create others, but this
94
+ // is used by generated code.
95
+ VALUE generated_pool;
96
+
97
+ DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
98
+
99
+ void DescriptorPool_mark(void* _self) {
100
+ }
101
+
102
+ void DescriptorPool_free(void* _self) {
103
+ DescriptorPool* self = _self;
104
+ upb_symtab_unref(self->symtab, &self->symtab);
105
+ xfree(self);
106
+ }
107
+
108
+ /*
109
+ * call-seq:
110
+ * DescriptorPool.new => pool
111
+ *
112
+ * Creates a new, empty, descriptor pool.
113
+ */
114
+ VALUE DescriptorPool_alloc(VALUE klass) {
115
+ DescriptorPool* self = ALLOC(DescriptorPool);
116
+ self->symtab = upb_symtab_new(&self->symtab);
117
+ return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
118
+ }
119
+
120
+ void DescriptorPool_register(VALUE module) {
121
+ VALUE klass = rb_define_class_under(
122
+ module, "DescriptorPool", rb_cObject);
123
+ rb_define_alloc_func(klass, DescriptorPool_alloc);
124
+ rb_define_method(klass, "add", DescriptorPool_add, 1);
125
+ rb_define_method(klass, "build", DescriptorPool_build, 0);
126
+ rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
127
+ rb_define_singleton_method(klass, "generated_pool",
128
+ DescriptorPool_generated_pool, 0);
129
+ cDescriptorPool = klass;
130
+ rb_gc_register_address(&cDescriptorPool);
131
+
132
+ generated_pool = rb_class_new_instance(0, NULL, klass);
133
+ rb_gc_register_address(&generated_pool);
134
+ }
135
+
136
+ static void add_descriptor_to_pool(DescriptorPool* self,
137
+ Descriptor* descriptor) {
138
+ CHECK_UPB(
139
+ upb_symtab_add(self->symtab, (upb_def**)&descriptor->msgdef, 1,
140
+ NULL, &status),
141
+ "Adding Descriptor to DescriptorPool failed");
142
+ }
143
+
144
+ static void add_enumdesc_to_pool(DescriptorPool* self,
145
+ EnumDescriptor* enumdesc) {
146
+ CHECK_UPB(
147
+ upb_symtab_add(self->symtab, (upb_def**)&enumdesc->enumdef, 1,
148
+ NULL, &status),
149
+ "Adding EnumDescriptor to DescriptorPool failed");
150
+ }
151
+
152
+ /*
153
+ * call-seq:
154
+ * DescriptorPool.add(descriptor)
155
+ *
156
+ * Adds the given Descriptor or EnumDescriptor to this pool. All references to
157
+ * other types in a Descriptor's fields must be resolvable within this pool or
158
+ * an exception will be raised.
159
+ */
160
+ VALUE DescriptorPool_add(VALUE _self, VALUE def) {
161
+ DEFINE_SELF(DescriptorPool, self, _self);
162
+ VALUE def_klass = rb_obj_class(def);
163
+ if (def_klass == cDescriptor) {
164
+ add_descriptor_to_pool(self, ruby_to_Descriptor(def));
165
+ } else if (def_klass == cEnumDescriptor) {
166
+ add_enumdesc_to_pool(self, ruby_to_EnumDescriptor(def));
167
+ } else {
168
+ rb_raise(rb_eArgError,
169
+ "Second argument must be a Descriptor or EnumDescriptor.");
170
+ }
171
+ return Qnil;
172
+ }
173
+
174
+ /*
175
+ * call-seq:
176
+ * DescriptorPool.build(&block)
177
+ *
178
+ * Invokes the block with a Builder instance as self. All message and enum types
179
+ * added within the block are committed to the pool atomically, and may refer
180
+ * (co)recursively to each other. The user should call Builder#add_message and
181
+ * Builder#add_enum within the block as appropriate. This is the recommended,
182
+ * idiomatic way to define new message and enum types.
183
+ */
184
+ VALUE DescriptorPool_build(VALUE _self) {
185
+ VALUE ctx = rb_class_new_instance(0, NULL, cBuilder);
186
+ VALUE block = rb_block_proc();
187
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
188
+ rb_funcall(ctx, rb_intern("finalize_to_pool"), 1, _self);
189
+ return Qnil;
190
+ }
191
+
192
+ /*
193
+ * call-seq:
194
+ * DescriptorPool.lookup(name) => descriptor
195
+ *
196
+ * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
197
+ * exists with the given name.
198
+ */
199
+ VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
200
+ DEFINE_SELF(DescriptorPool, self, _self);
201
+ const char* name_str = get_str(name);
202
+ const upb_def* def = upb_symtab_lookup(self->symtab, name_str);
203
+ if (!def) {
204
+ return Qnil;
205
+ }
206
+ return get_def_obj(def);
207
+ }
208
+
209
+ /*
210
+ * call-seq:
211
+ * DescriptorPool.generated_pool => descriptor_pool
212
+ *
213
+ * Class method that returns the global DescriptorPool. This is a singleton into
214
+ * which generated-code message and enum types are registered. The user may also
215
+ * register types in this pool for convenience so that they do not have to hold
216
+ * a reference to a private pool instance.
217
+ */
218
+ VALUE DescriptorPool_generated_pool(VALUE _self) {
219
+ return generated_pool;
220
+ }
221
+
222
+ // -----------------------------------------------------------------------------
223
+ // Descriptor.
224
+ // -----------------------------------------------------------------------------
225
+
226
+ DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
227
+
228
+ void Descriptor_mark(void* _self) {
229
+ Descriptor* self = _self;
230
+ rb_gc_mark(self->klass);
231
+ rb_gc_mark(self->typeclass_references);
232
+ }
233
+
234
+ void Descriptor_free(void* _self) {
235
+ Descriptor* self = _self;
236
+ upb_msgdef_unref(self->msgdef, &self->msgdef);
237
+ if (self->layout) {
238
+ free_layout(self->layout);
239
+ }
240
+ if (self->fill_handlers) {
241
+ upb_handlers_unref(self->fill_handlers, &self->fill_handlers);
242
+ }
243
+ if (self->fill_method) {
244
+ upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
245
+ }
246
+ if (self->pb_serialize_handlers) {
247
+ upb_handlers_unref(self->pb_serialize_handlers,
248
+ &self->pb_serialize_handlers);
249
+ }
250
+ if (self->json_serialize_handlers) {
251
+ upb_handlers_unref(self->json_serialize_handlers,
252
+ &self->json_serialize_handlers);
253
+ }
254
+ xfree(self);
255
+ }
256
+
257
+ /*
258
+ * call-seq:
259
+ * Descriptor.new => descriptor
260
+ *
261
+ * Creates a new, empty, message type descriptor. At a minimum, its name must be
262
+ * set before it is added to a pool. It cannot be used to create messages until
263
+ * it is added to a pool, after which it becomes immutable (as part of a
264
+ * finalization process).
265
+ */
266
+ VALUE Descriptor_alloc(VALUE klass) {
267
+ Descriptor* self = ALLOC(Descriptor);
268
+ VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
269
+ self->msgdef = upb_msgdef_new(&self->msgdef);
270
+ self->klass = Qnil;
271
+ self->layout = NULL;
272
+ self->fill_handlers = NULL;
273
+ self->fill_method = NULL;
274
+ self->pb_serialize_handlers = NULL;
275
+ self->json_serialize_handlers = NULL;
276
+ self->typeclass_references = rb_ary_new();
277
+ return ret;
278
+ }
279
+
280
+ void Descriptor_register(VALUE module) {
281
+ VALUE klass = rb_define_class_under(
282
+ module, "Descriptor", rb_cObject);
283
+ rb_define_alloc_func(klass, Descriptor_alloc);
284
+ rb_define_method(klass, "each", Descriptor_each, 0);
285
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
286
+ rb_define_method(klass, "add_field", Descriptor_add_field, 1);
287
+ rb_define_method(klass, "add_oneof", Descriptor_add_oneof, 1);
288
+ rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
289
+ rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
290
+ rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
291
+ rb_define_method(klass, "name", Descriptor_name, 0);
292
+ rb_define_method(klass, "name=", Descriptor_name_set, 1);
293
+ rb_include_module(klass, rb_mEnumerable);
294
+ cDescriptor = klass;
295
+ rb_gc_register_address(&cDescriptor);
296
+ }
297
+
298
+ /*
299
+ * call-seq:
300
+ * Descriptor.name => name
301
+ *
302
+ * Returns the name of this message type as a fully-qualfied string (e.g.,
303
+ * My.Package.MessageType).
304
+ */
305
+ VALUE Descriptor_name(VALUE _self) {
306
+ DEFINE_SELF(Descriptor, self, _self);
307
+ return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
308
+ }
309
+
310
+ /*
311
+ * call-seq:
312
+ * Descriptor.name = name
313
+ *
314
+ * Assigns a name to this message type. The descriptor must not have been added
315
+ * to a pool yet.
316
+ */
317
+ VALUE Descriptor_name_set(VALUE _self, VALUE str) {
318
+ DEFINE_SELF(Descriptor, self, _self);
319
+ upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
320
+ const char* name = get_str(str);
321
+ CHECK_UPB(
322
+ upb_msgdef_setfullname(mut_def, name, &status),
323
+ "Error setting Descriptor name");
324
+ return Qnil;
325
+ }
326
+
327
+ /*
328
+ * call-seq:
329
+ * Descriptor.each(&block)
330
+ *
331
+ * Iterates over fields in this message type, yielding to the block on each one.
332
+ */
333
+ VALUE Descriptor_each(VALUE _self) {
334
+ DEFINE_SELF(Descriptor, self, _self);
335
+
336
+ upb_msg_field_iter it;
337
+ for (upb_msg_field_begin(&it, self->msgdef);
338
+ !upb_msg_field_done(&it);
339
+ upb_msg_field_next(&it)) {
340
+ const upb_fielddef* field = upb_msg_iter_field(&it);
341
+ VALUE obj = get_def_obj(field);
342
+ rb_yield(obj);
343
+ }
344
+ return Qnil;
345
+ }
346
+
347
+ /*
348
+ * call-seq:
349
+ * Descriptor.lookup(name) => FieldDescriptor
350
+ *
351
+ * Returns the field descriptor for the field with the given name, if present,
352
+ * or nil if none.
353
+ */
354
+ VALUE Descriptor_lookup(VALUE _self, VALUE name) {
355
+ DEFINE_SELF(Descriptor, self, _self);
356
+ const char* s = get_str(name);
357
+ const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
358
+ if (field == NULL) {
359
+ return Qnil;
360
+ }
361
+ return get_def_obj(field);
362
+ }
363
+
364
+ /*
365
+ * call-seq:
366
+ * Descriptor.add_field(field) => nil
367
+ *
368
+ * Adds the given FieldDescriptor to this message type. This descriptor must not
369
+ * have been added to a pool yet. Raises an exception if a field with the same
370
+ * name or number already exists. Sub-type references (e.g. for fields of type
371
+ * message) are not resolved at this point.
372
+ */
373
+ VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
374
+ DEFINE_SELF(Descriptor, self, _self);
375
+ upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
376
+ FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
377
+ upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
378
+ CHECK_UPB(
379
+ upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
380
+ "Adding field to Descriptor failed");
381
+ add_def_obj(def->fielddef, obj);
382
+ return Qnil;
383
+ }
384
+
385
+ /*
386
+ * call-seq:
387
+ * Descriptor.add_oneof(oneof) => nil
388
+ *
389
+ * Adds the given OneofDescriptor to this message type. This descriptor must not
390
+ * have been added to a pool yet. Raises an exception if a oneof with the same
391
+ * name already exists, or if any of the oneof's fields' names or numbers
392
+ * conflict with an existing field in this message type. All fields in the oneof
393
+ * are added to the message descriptor. Sub-type references (e.g. for fields of
394
+ * type message) are not resolved at this point.
395
+ */
396
+ VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
397
+ DEFINE_SELF(Descriptor, self, _self);
398
+ upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
399
+ OneofDescriptor* def = ruby_to_OneofDescriptor(obj);
400
+ upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef);
401
+ CHECK_UPB(
402
+ upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status),
403
+ "Adding oneof to Descriptor failed");
404
+ add_def_obj(def->oneofdef, obj);
405
+ return Qnil;
406
+ }
407
+
408
+ /*
409
+ * call-seq:
410
+ * Descriptor.each_oneof(&block) => nil
411
+ *
412
+ * Invokes the given block for each oneof in this message type, passing the
413
+ * corresponding OneofDescriptor.
414
+ */
415
+ VALUE Descriptor_each_oneof(VALUE _self) {
416
+ DEFINE_SELF(Descriptor, self, _self);
417
+
418
+ upb_msg_oneof_iter it;
419
+ for (upb_msg_oneof_begin(&it, self->msgdef);
420
+ !upb_msg_oneof_done(&it);
421
+ upb_msg_oneof_next(&it)) {
422
+ const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
423
+ VALUE obj = get_def_obj(oneof);
424
+ rb_yield(obj);
425
+ }
426
+ return Qnil;
427
+ }
428
+
429
+ /*
430
+ * call-seq:
431
+ * Descriptor.lookup_oneof(name) => OneofDescriptor
432
+ *
433
+ * Returns the oneof descriptor for the oneof with the given name, if present,
434
+ * or nil if none.
435
+ */
436
+ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
437
+ DEFINE_SELF(Descriptor, self, _self);
438
+ const char* s = get_str(name);
439
+ const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
440
+ if (oneof == NULL) {
441
+ return Qnil;
442
+ }
443
+ return get_def_obj(oneof);
444
+ }
445
+
446
+ /*
447
+ * call-seq:
448
+ * Descriptor.msgclass => message_klass
449
+ *
450
+ * Returns the Ruby class created for this message type. Valid only once the
451
+ * message type has been added to a pool.
452
+ */
453
+ VALUE Descriptor_msgclass(VALUE _self) {
454
+ DEFINE_SELF(Descriptor, self, _self);
455
+ if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
456
+ rb_raise(rb_eRuntimeError,
457
+ "Cannot fetch message class from a Descriptor not yet in a pool.");
458
+ }
459
+ if (self->klass == Qnil) {
460
+ self->klass = build_class_from_descriptor(self);
461
+ }
462
+ return self->klass;
463
+ }
464
+
465
+ // -----------------------------------------------------------------------------
466
+ // FieldDescriptor.
467
+ // -----------------------------------------------------------------------------
468
+
469
+ DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
470
+
471
+ void FieldDescriptor_mark(void* _self) {
472
+ }
473
+
474
+ void FieldDescriptor_free(void* _self) {
475
+ FieldDescriptor* self = _self;
476
+ upb_fielddef_unref(self->fielddef, &self->fielddef);
477
+ xfree(self);
478
+ }
479
+
480
+ /*
481
+ * call-seq:
482
+ * FieldDescriptor.new => field
483
+ *
484
+ * Returns a new field descriptor. Its name, type, etc. must be set before it is
485
+ * added to a message type.
486
+ */
487
+ VALUE FieldDescriptor_alloc(VALUE klass) {
488
+ FieldDescriptor* self = ALLOC(FieldDescriptor);
489
+ VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
490
+ upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef);
491
+ upb_fielddef_setpacked(fielddef, false);
492
+ self->fielddef = fielddef;
493
+ return ret;
494
+ }
495
+
496
+ void FieldDescriptor_register(VALUE module) {
497
+ VALUE klass = rb_define_class_under(
498
+ module, "FieldDescriptor", rb_cObject);
499
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
500
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
501
+ rb_define_method(klass, "name=", FieldDescriptor_name_set, 1);
502
+ rb_define_method(klass, "type", FieldDescriptor_type, 0);
503
+ rb_define_method(klass, "type=", FieldDescriptor_type_set, 1);
504
+ rb_define_method(klass, "label", FieldDescriptor_label, 0);
505
+ rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
506
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
507
+ rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
508
+ rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
509
+ rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1);
510
+ rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
511
+ rb_define_method(klass, "get", FieldDescriptor_get, 1);
512
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
513
+ cFieldDescriptor = klass;
514
+ rb_gc_register_address(&cFieldDescriptor);
515
+ }
516
+
517
+ /*
518
+ * call-seq:
519
+ * FieldDescriptor.name => name
520
+ *
521
+ * Returns the name of this field.
522
+ */
523
+ VALUE FieldDescriptor_name(VALUE _self) {
524
+ DEFINE_SELF(FieldDescriptor, self, _self);
525
+ return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
526
+ }
527
+
528
+ /*
529
+ * call-seq:
530
+ * FieldDescriptor.name = name
531
+ *
532
+ * Sets the name of this field. Cannot be called once the containing message
533
+ * type, if any, is added to a pool.
534
+ */
535
+ VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) {
536
+ DEFINE_SELF(FieldDescriptor, self, _self);
537
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
538
+ const char* name = get_str(str);
539
+ CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
540
+ "Error setting FieldDescriptor name");
541
+ return Qnil;
542
+ }
543
+
544
+ upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
545
+ if (TYPE(type) != T_SYMBOL) {
546
+ rb_raise(rb_eArgError, "Expected symbol for field type.");
547
+ }
548
+
549
+ #define CONVERT(upb, ruby) \
550
+ if (SYM2ID(type) == rb_intern( # ruby )) { \
551
+ return UPB_TYPE_ ## upb; \
552
+ }
553
+
554
+ CONVERT(FLOAT, float);
555
+ CONVERT(DOUBLE, double);
556
+ CONVERT(BOOL, bool);
557
+ CONVERT(STRING, string);
558
+ CONVERT(BYTES, bytes);
559
+ CONVERT(MESSAGE, message);
560
+ CONVERT(ENUM, enum);
561
+ CONVERT(INT32, int32);
562
+ CONVERT(INT64, int64);
563
+ CONVERT(UINT32, uint32);
564
+ CONVERT(UINT64, uint64);
565
+
566
+ #undef CONVERT
567
+
568
+ rb_raise(rb_eArgError, "Unknown field type.");
569
+ return 0;
570
+ }
571
+
572
+ VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
573
+ switch (type) {
574
+ #define CONVERT(upb, ruby) \
575
+ case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
576
+ CONVERT(FLOAT, float);
577
+ CONVERT(DOUBLE, double);
578
+ CONVERT(BOOL, bool);
579
+ CONVERT(STRING, string);
580
+ CONVERT(BYTES, bytes);
581
+ CONVERT(MESSAGE, message);
582
+ CONVERT(ENUM, enum);
583
+ CONVERT(INT32, int32);
584
+ CONVERT(INT64, int64);
585
+ CONVERT(UINT32, uint32);
586
+ CONVERT(UINT64, uint64);
587
+ #undef CONVERT
588
+ }
589
+ return Qnil;
590
+ }
591
+
592
+ upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
593
+ if (TYPE(type) != T_SYMBOL) {
594
+ rb_raise(rb_eArgError, "Expected symbol for field type.");
595
+ }
596
+
597
+ #define CONVERT(upb, ruby) \
598
+ if (SYM2ID(type) == rb_intern( # ruby )) { \
599
+ return UPB_DESCRIPTOR_TYPE_ ## upb; \
600
+ }
601
+
602
+ CONVERT(FLOAT, float);
603
+ CONVERT(DOUBLE, double);
604
+ CONVERT(BOOL, bool);
605
+ CONVERT(STRING, string);
606
+ CONVERT(BYTES, bytes);
607
+ CONVERT(MESSAGE, message);
608
+ CONVERT(GROUP, group);
609
+ CONVERT(ENUM, enum);
610
+ CONVERT(INT32, int32);
611
+ CONVERT(INT64, int64);
612
+ CONVERT(UINT32, uint32);
613
+ CONVERT(UINT64, uint64);
614
+ CONVERT(SINT32, sint32);
615
+ CONVERT(SINT64, sint64);
616
+ CONVERT(FIXED32, fixed32);
617
+ CONVERT(FIXED64, fixed64);
618
+ CONVERT(SFIXED32, sfixed32);
619
+ CONVERT(SFIXED64, sfixed64);
620
+
621
+ #undef CONVERT
622
+
623
+ rb_raise(rb_eArgError, "Unknown field type.");
624
+ return 0;
625
+ }
626
+
627
+ VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
628
+ switch (type) {
629
+ #define CONVERT(upb, ruby) \
630
+ case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
631
+ CONVERT(FLOAT, float);
632
+ CONVERT(DOUBLE, double);
633
+ CONVERT(BOOL, bool);
634
+ CONVERT(STRING, string);
635
+ CONVERT(BYTES, bytes);
636
+ CONVERT(MESSAGE, message);
637
+ CONVERT(GROUP, group);
638
+ CONVERT(ENUM, enum);
639
+ CONVERT(INT32, int32);
640
+ CONVERT(INT64, int64);
641
+ CONVERT(UINT32, uint32);
642
+ CONVERT(UINT64, uint64);
643
+ CONVERT(SINT32, sint32);
644
+ CONVERT(SINT64, sint64);
645
+ CONVERT(FIXED32, fixed32);
646
+ CONVERT(FIXED64, fixed64);
647
+ CONVERT(SFIXED32, sfixed32);
648
+ CONVERT(SFIXED64, sfixed64);
649
+ #undef CONVERT
650
+ }
651
+ return Qnil;
652
+ }
653
+
654
+ /*
655
+ * call-seq:
656
+ * FieldDescriptor.type => type
657
+ *
658
+ * Returns this field's type, as a Ruby symbol, or nil if not yet set.
659
+ *
660
+ * Valid field types are:
661
+ * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
662
+ * :bytes, :message.
663
+ */
664
+ VALUE FieldDescriptor_type(VALUE _self) {
665
+ DEFINE_SELF(FieldDescriptor, self, _self);
666
+ if (!upb_fielddef_typeisset(self->fielddef)) {
667
+ return Qnil;
668
+ }
669
+ return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
670
+ }
671
+
672
+ /*
673
+ * call-seq:
674
+ * FieldDescriptor.type = type
675
+ *
676
+ * Sets this field's type. Cannot be called if field is part of a message type
677
+ * already in a pool.
678
+ */
679
+ VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
680
+ DEFINE_SELF(FieldDescriptor, self, _self);
681
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
682
+ upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type));
683
+ return Qnil;
684
+ }
685
+
686
+ /*
687
+ * call-seq:
688
+ * FieldDescriptor.label => label
689
+ *
690
+ * Returns this field's label (i.e., plurality), as a Ruby symbol.
691
+ *
692
+ * Valid field labels are:
693
+ * :optional, :repeated
694
+ */
695
+ VALUE FieldDescriptor_label(VALUE _self) {
696
+ DEFINE_SELF(FieldDescriptor, self, _self);
697
+ switch (upb_fielddef_label(self->fielddef)) {
698
+ #define CONVERT(upb, ruby) \
699
+ case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
700
+
701
+ CONVERT(OPTIONAL, optional);
702
+ CONVERT(REQUIRED, required);
703
+ CONVERT(REPEATED, repeated);
704
+
705
+ #undef CONVERT
706
+ }
707
+
708
+ return Qnil;
709
+ }
710
+
711
+ /*
712
+ * call-seq:
713
+ * FieldDescriptor.label = label
714
+ *
715
+ * Sets the label on this field. Cannot be called if field is part of a message
716
+ * type already in a pool.
717
+ */
718
+ VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
719
+ DEFINE_SELF(FieldDescriptor, self, _self);
720
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
721
+ upb_label_t upb_label = -1;
722
+ bool converted = false;
723
+
724
+ if (TYPE(label) != T_SYMBOL) {
725
+ rb_raise(rb_eArgError, "Expected symbol for field label.");
726
+ }
727
+
728
+ #define CONVERT(upb, ruby) \
729
+ if (SYM2ID(label) == rb_intern( # ruby )) { \
730
+ upb_label = UPB_LABEL_ ## upb; \
731
+ converted = true; \
732
+ }
733
+
734
+ CONVERT(OPTIONAL, optional);
735
+ CONVERT(REQUIRED, required);
736
+ CONVERT(REPEATED, repeated);
737
+
738
+ #undef CONVERT
739
+
740
+ if (!converted) {
741
+ rb_raise(rb_eArgError, "Unknown field label.");
742
+ }
743
+
744
+ upb_fielddef_setlabel(mut_def, upb_label);
745
+
746
+ return Qnil;
747
+ }
748
+
749
+ /*
750
+ * call-seq:
751
+ * FieldDescriptor.number => number
752
+ *
753
+ * Returns the tag number for this field.
754
+ */
755
+ VALUE FieldDescriptor_number(VALUE _self) {
756
+ DEFINE_SELF(FieldDescriptor, self, _self);
757
+ return INT2NUM(upb_fielddef_number(self->fielddef));
758
+ }
759
+
760
+ /*
761
+ * call-seq:
762
+ * FieldDescriptor.number = number
763
+ *
764
+ * Sets the tag number for this field. Cannot be called if field is part of a
765
+ * message type already in a pool.
766
+ */
767
+ VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
768
+ DEFINE_SELF(FieldDescriptor, self, _self);
769
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
770
+ CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
771
+ "Error setting field number");
772
+ return Qnil;
773
+ }
774
+
775
+ /*
776
+ * call-seq:
777
+ * FieldDescriptor.submsg_name => submsg_name
778
+ *
779
+ * Returns the name of the message or enum type corresponding to this field, if
780
+ * it is a message or enum field (respectively), or nil otherwise. This type
781
+ * name will be resolved within the context of the pool to which the containing
782
+ * message type is added.
783
+ */
784
+ VALUE FieldDescriptor_submsg_name(VALUE _self) {
785
+ DEFINE_SELF(FieldDescriptor, self, _self);
786
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
787
+ return Qnil;
788
+ }
789
+ return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
790
+ }
791
+
792
+ /*
793
+ * call-seq:
794
+ * FieldDescriptor.submsg_name = submsg_name
795
+ *
796
+ * Sets the name of the message or enum type corresponding to this field, if it
797
+ * is a message or enum field (respectively). This type name will be resolved
798
+ * within the context of the pool to which the containing message type is added.
799
+ * Cannot be called on field that are not of message or enum type, or on fields
800
+ * that are part of a message type already added to a pool.
801
+ */
802
+ VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
803
+ DEFINE_SELF(FieldDescriptor, self, _self);
804
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
805
+ const char* str = get_str(value);
806
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
807
+ rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef.");
808
+ }
809
+ CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
810
+ "Error setting submessage name");
811
+ return Qnil;
812
+ }
813
+
814
+ /*
815
+ * call-seq:
816
+ * FieldDescriptor.subtype => message_or_enum_descriptor
817
+ *
818
+ * Returns the message or enum descriptor corresponding to this field's type if
819
+ * it is a message or enum field, respectively, or nil otherwise. Cannot be
820
+ * called *until* the containing message type is added to a pool (and thus
821
+ * resolved).
822
+ */
823
+ VALUE FieldDescriptor_subtype(VALUE _self) {
824
+ DEFINE_SELF(FieldDescriptor, self, _self);
825
+ const upb_def* def;
826
+
827
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
828
+ return Qnil;
829
+ }
830
+ def = upb_fielddef_subdef(self->fielddef);
831
+ if (def == NULL) {
832
+ return Qnil;
833
+ }
834
+ return get_def_obj(def);
835
+ }
836
+
837
+ /*
838
+ * call-seq:
839
+ * FieldDescriptor.get(message) => value
840
+ *
841
+ * Returns the value set for this field on the given message. Raises an
842
+ * exception if message is of the wrong type.
843
+ */
844
+ VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
845
+ DEFINE_SELF(FieldDescriptor, self, _self);
846
+ MessageHeader* msg;
847
+ TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
848
+ if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
849
+ rb_raise(rb_eTypeError, "get method called on wrong message type");
850
+ }
851
+ return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
852
+ }
853
+
854
+ /*
855
+ * call-seq:
856
+ * FieldDescriptor.set(message, value)
857
+ *
858
+ * Sets the value corresponding to this field to the given value on the given
859
+ * message. Raises an exception if message is of the wrong type. Performs the
860
+ * ordinary type-checks for field setting.
861
+ */
862
+ VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
863
+ DEFINE_SELF(FieldDescriptor, self, _self);
864
+ MessageHeader* msg;
865
+ TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
866
+ if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
867
+ rb_raise(rb_eTypeError, "set method called on wrong message type");
868
+ }
869
+ layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
870
+ return Qnil;
871
+ }
872
+
873
+ // -----------------------------------------------------------------------------
874
+ // OneofDescriptor.
875
+ // -----------------------------------------------------------------------------
876
+
877
+ DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
878
+
879
+ void OneofDescriptor_mark(void* _self) {
880
+ }
881
+
882
+ void OneofDescriptor_free(void* _self) {
883
+ OneofDescriptor* self = _self;
884
+ upb_oneofdef_unref(self->oneofdef, &self->oneofdef);
885
+ xfree(self);
886
+ }
887
+
888
+ /*
889
+ * call-seq:
890
+ * OneofDescriptor.new => oneof_descriptor
891
+ *
892
+ * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
893
+ * to being added to a message descriptor which is subsequently added to a pool.
894
+ */
895
+ VALUE OneofDescriptor_alloc(VALUE klass) {
896
+ OneofDescriptor* self = ALLOC(OneofDescriptor);
897
+ VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
898
+ self->oneofdef = upb_oneofdef_new(&self->oneofdef);
899
+ return ret;
900
+ }
901
+
902
+ void OneofDescriptor_register(VALUE module) {
903
+ VALUE klass = rb_define_class_under(
904
+ module, "OneofDescriptor", rb_cObject);
905
+ rb_define_alloc_func(klass, OneofDescriptor_alloc);
906
+ rb_define_method(klass, "name", OneofDescriptor_name, 0);
907
+ rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
908
+ rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
909
+ rb_define_method(klass, "each", OneofDescriptor_each, 0);
910
+ rb_include_module(klass, rb_mEnumerable);
911
+ cOneofDescriptor = klass;
912
+ rb_gc_register_address(&cOneofDescriptor);
913
+ }
914
+
915
+ /*
916
+ * call-seq:
917
+ * OneofDescriptor.name => name
918
+ *
919
+ * Returns the name of this oneof.
920
+ */
921
+ VALUE OneofDescriptor_name(VALUE _self) {
922
+ DEFINE_SELF(OneofDescriptor, self, _self);
923
+ return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
924
+ }
925
+
926
+ /*
927
+ * call-seq:
928
+ * OneofDescriptor.name = name
929
+ *
930
+ * Sets a new name for this oneof. The oneof must not have been added to a
931
+ * message descriptor yet.
932
+ */
933
+ VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
934
+ DEFINE_SELF(OneofDescriptor, self, _self);
935
+ upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
936
+ const char* str = get_str(value);
937
+ CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
938
+ "Error setting oneof name");
939
+ return Qnil;
940
+ }
941
+
942
+ /*
943
+ * call-seq:
944
+ * OneofDescriptor.add_field(field) => nil
945
+ *
946
+ * Adds a field to this oneof. The field may have been added to this oneof in
947
+ * the past, or the message to which this oneof belongs (if any), but may not
948
+ * have already been added to any other oneof or message. Otherwise, an
949
+ * exception is raised.
950
+ *
951
+ * All fields added to the oneof via this method will be automatically added to
952
+ * the message to which this oneof belongs, if it belongs to one currently, or
953
+ * else will be added to any message to which the oneof is later added at the
954
+ * time that it is added.
955
+ */
956
+ VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
957
+ DEFINE_SELF(OneofDescriptor, self, _self);
958
+ upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
959
+ FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
960
+ upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
961
+ CHECK_UPB(
962
+ upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
963
+ "Adding field to OneofDescriptor failed");
964
+ add_def_obj(def->fielddef, obj);
965
+ return Qnil;
966
+ }
967
+
968
+ /*
969
+ * call-seq:
970
+ * OneofDescriptor.each(&block) => nil
971
+ *
972
+ * Iterates through fields in this oneof, yielding to the block on each one.
973
+ */
974
+ VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
975
+ DEFINE_SELF(OneofDescriptor, self, _self);
976
+ upb_oneof_iter it;
977
+ for (upb_oneof_begin(&it, self->oneofdef);
978
+ !upb_oneof_done(&it);
979
+ upb_oneof_next(&it)) {
980
+ const upb_fielddef* f = upb_oneof_iter_field(&it);
981
+ VALUE obj = get_def_obj(f);
982
+ rb_yield(obj);
983
+ }
984
+ return Qnil;
985
+ }
986
+
987
+ // -----------------------------------------------------------------------------
988
+ // EnumDescriptor.
989
+ // -----------------------------------------------------------------------------
990
+
991
+ DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
992
+
993
+ void EnumDescriptor_mark(void* _self) {
994
+ EnumDescriptor* self = _self;
995
+ rb_gc_mark(self->module);
996
+ }
997
+
998
+ void EnumDescriptor_free(void* _self) {
999
+ EnumDescriptor* self = _self;
1000
+ upb_enumdef_unref(self->enumdef, &self->enumdef);
1001
+ xfree(self);
1002
+ }
1003
+
1004
+ /*
1005
+ * call-seq:
1006
+ * EnumDescriptor.new => enum_descriptor
1007
+ *
1008
+ * Creates a new, empty, enum descriptor. Must be added to a pool before the
1009
+ * enum type can be used. The enum type may only be modified prior to adding to
1010
+ * a pool.
1011
+ */
1012
+ VALUE EnumDescriptor_alloc(VALUE klass) {
1013
+ EnumDescriptor* self = ALLOC(EnumDescriptor);
1014
+ VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
1015
+ self->enumdef = upb_enumdef_new(&self->enumdef);
1016
+ self->module = Qnil;
1017
+ return ret;
1018
+ }
1019
+
1020
+ void EnumDescriptor_register(VALUE module) {
1021
+ VALUE klass = rb_define_class_under(
1022
+ module, "EnumDescriptor", rb_cObject);
1023
+ rb_define_alloc_func(klass, EnumDescriptor_alloc);
1024
+ rb_define_method(klass, "name", EnumDescriptor_name, 0);
1025
+ rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
1026
+ rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
1027
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
1028
+ rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
1029
+ rb_define_method(klass, "each", EnumDescriptor_each, 0);
1030
+ rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
1031
+ rb_include_module(klass, rb_mEnumerable);
1032
+ cEnumDescriptor = klass;
1033
+ rb_gc_register_address(&cEnumDescriptor);
1034
+ }
1035
+
1036
+ /*
1037
+ * call-seq:
1038
+ * EnumDescriptor.name => name
1039
+ *
1040
+ * Returns the name of this enum type.
1041
+ */
1042
+ VALUE EnumDescriptor_name(VALUE _self) {
1043
+ DEFINE_SELF(EnumDescriptor, self, _self);
1044
+ return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
1045
+ }
1046
+
1047
+ /*
1048
+ * call-seq:
1049
+ * EnumDescriptor.name = name
1050
+ *
1051
+ * Sets the name of this enum type. Cannot be called if the enum type has
1052
+ * already been added to a pool.
1053
+ */
1054
+ VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
1055
+ DEFINE_SELF(EnumDescriptor, self, _self);
1056
+ upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
1057
+ const char* name = get_str(str);
1058
+ CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
1059
+ "Error setting EnumDescriptor name");
1060
+ return Qnil;
1061
+ }
1062
+
1063
+ /*
1064
+ * call-seq:
1065
+ * EnumDescriptor.add_value(key, value)
1066
+ *
1067
+ * Adds a new key => value mapping to this enum type. Key must be given as a
1068
+ * Ruby symbol. Cannot be called if the enum type has already been added to a
1069
+ * pool. Will raise an exception if the key or value is already in use.
1070
+ */
1071
+ VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
1072
+ DEFINE_SELF(EnumDescriptor, self, _self);
1073
+ upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
1074
+ const char* name_str = rb_id2name(SYM2ID(name));
1075
+ int32_t val = NUM2INT(number);
1076
+ CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
1077
+ "Error adding value to enum");
1078
+ return Qnil;
1079
+ }
1080
+
1081
+ /*
1082
+ * call-seq:
1083
+ * EnumDescriptor.lookup_name(name) => value
1084
+ *
1085
+ * Returns the numeric value corresponding to the given key name (as a Ruby
1086
+ * symbol), or nil if none.
1087
+ */
1088
+ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
1089
+ DEFINE_SELF(EnumDescriptor, self, _self);
1090
+ const char* name_str= rb_id2name(SYM2ID(name));
1091
+ int32_t val = 0;
1092
+ if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
1093
+ return INT2NUM(val);
1094
+ } else {
1095
+ return Qnil;
1096
+ }
1097
+ }
1098
+
1099
+ /*
1100
+ * call-seq:
1101
+ * EnumDescriptor.lookup_value(name) => value
1102
+ *
1103
+ * Returns the key name (as a Ruby symbol) corresponding to the integer value,
1104
+ * or nil if none.
1105
+ */
1106
+ VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
1107
+ DEFINE_SELF(EnumDescriptor, self, _self);
1108
+ int32_t val = NUM2INT(number);
1109
+ const char* name = upb_enumdef_iton(self->enumdef, val);
1110
+ if (name != NULL) {
1111
+ return ID2SYM(rb_intern(name));
1112
+ } else {
1113
+ return Qnil;
1114
+ }
1115
+ }
1116
+
1117
+ /*
1118
+ * call-seq:
1119
+ * EnumDescriptor.each(&block)
1120
+ *
1121
+ * Iterates over key => value mappings in this enum's definition, yielding to
1122
+ * the block with (key, value) arguments for each one.
1123
+ */
1124
+ VALUE EnumDescriptor_each(VALUE _self) {
1125
+ DEFINE_SELF(EnumDescriptor, self, _self);
1126
+
1127
+ upb_enum_iter it;
1128
+ for (upb_enum_begin(&it, self->enumdef);
1129
+ !upb_enum_done(&it);
1130
+ upb_enum_next(&it)) {
1131
+ VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
1132
+ VALUE number = INT2NUM(upb_enum_iter_number(&it));
1133
+ rb_yield_values(2, key, number);
1134
+ }
1135
+
1136
+ return Qnil;
1137
+ }
1138
+
1139
+ /*
1140
+ * call-seq:
1141
+ * EnumDescriptor.enummodule => module
1142
+ *
1143
+ * Returns the Ruby module corresponding to this enum type. Cannot be called
1144
+ * until the enum descriptor has been added to a pool.
1145
+ */
1146
+ VALUE EnumDescriptor_enummodule(VALUE _self) {
1147
+ DEFINE_SELF(EnumDescriptor, self, _self);
1148
+ if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
1149
+ rb_raise(rb_eRuntimeError,
1150
+ "Cannot fetch enum module from an EnumDescriptor not yet "
1151
+ "in a pool.");
1152
+ }
1153
+ if (self->module == Qnil) {
1154
+ self->module = build_module_from_enumdesc(self);
1155
+ }
1156
+ return self->module;
1157
+ }
1158
+
1159
+ // -----------------------------------------------------------------------------
1160
+ // MessageBuilderContext.
1161
+ // -----------------------------------------------------------------------------
1162
+
1163
+ DEFINE_CLASS(MessageBuilderContext,
1164
+ "Google::Protobuf::Internal::MessageBuilderContext");
1165
+
1166
+ void MessageBuilderContext_mark(void* _self) {
1167
+ MessageBuilderContext* self = _self;
1168
+ rb_gc_mark(self->descriptor);
1169
+ rb_gc_mark(self->builder);
1170
+ }
1171
+
1172
+ void MessageBuilderContext_free(void* _self) {
1173
+ MessageBuilderContext* self = _self;
1174
+ xfree(self);
1175
+ }
1176
+
1177
+ VALUE MessageBuilderContext_alloc(VALUE klass) {
1178
+ MessageBuilderContext* self = ALLOC(MessageBuilderContext);
1179
+ VALUE ret = TypedData_Wrap_Struct(
1180
+ klass, &_MessageBuilderContext_type, self);
1181
+ self->descriptor = Qnil;
1182
+ self->builder = Qnil;
1183
+ return ret;
1184
+ }
1185
+
1186
+ void MessageBuilderContext_register(VALUE module) {
1187
+ VALUE klass = rb_define_class_under(
1188
+ module, "MessageBuilderContext", rb_cObject);
1189
+ rb_define_alloc_func(klass, MessageBuilderContext_alloc);
1190
+ rb_define_method(klass, "initialize",
1191
+ MessageBuilderContext_initialize, 2);
1192
+ rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
1193
+ rb_define_method(klass, "required", MessageBuilderContext_required, -1);
1194
+ rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
1195
+ rb_define_method(klass, "map", MessageBuilderContext_map, -1);
1196
+ rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
1197
+ cMessageBuilderContext = klass;
1198
+ rb_gc_register_address(&cMessageBuilderContext);
1199
+ }
1200
+
1201
+ /*
1202
+ * call-seq:
1203
+ * MessageBuilderContext.new(desc, builder) => context
1204
+ *
1205
+ * Create a new message builder context around the given message descriptor and
1206
+ * builder context. This class is intended to serve as a DSL context to be used
1207
+ * with #instance_eval.
1208
+ */
1209
+ VALUE MessageBuilderContext_initialize(VALUE _self,
1210
+ VALUE msgdef,
1211
+ VALUE builder) {
1212
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1213
+ self->descriptor = msgdef;
1214
+ self->builder = builder;
1215
+ return Qnil;
1216
+ }
1217
+
1218
+ static VALUE msgdef_add_field(VALUE msgdef,
1219
+ const char* label, VALUE name,
1220
+ VALUE type, VALUE number,
1221
+ VALUE type_class) {
1222
+ VALUE fielddef = rb_class_new_instance(0, NULL, cFieldDescriptor);
1223
+ VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1224
+
1225
+ rb_funcall(fielddef, rb_intern("label="), 1, ID2SYM(rb_intern(label)));
1226
+ rb_funcall(fielddef, rb_intern("name="), 1, name_str);
1227
+ rb_funcall(fielddef, rb_intern("type="), 1, type);
1228
+ rb_funcall(fielddef, rb_intern("number="), 1, number);
1229
+
1230
+ if (type_class != Qnil) {
1231
+ if (TYPE(type_class) != T_STRING) {
1232
+ rb_raise(rb_eArgError, "Expected string for type class");
1233
+ }
1234
+ // Make it an absolute type name by prepending a dot.
1235
+ type_class = rb_str_append(rb_str_new2("."), type_class);
1236
+ rb_funcall(fielddef, rb_intern("submsg_name="), 1, type_class);
1237
+ }
1238
+
1239
+ rb_funcall(msgdef, rb_intern("add_field"), 1, fielddef);
1240
+ return fielddef;
1241
+ }
1242
+
1243
+ /*
1244
+ * call-seq:
1245
+ * MessageBuilderContext.optional(name, type, number, type_class = nil)
1246
+ *
1247
+ * Defines a new optional field on this message type with the given type, tag
1248
+ * number, and type class (for message and enum fields). The type must be a Ruby
1249
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1250
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
1251
+ */
1252
+ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1253
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1254
+ VALUE name, type, number, type_class;
1255
+
1256
+ if (argc < 3) {
1257
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1258
+ }
1259
+ name = argv[0];
1260
+ type = argv[1];
1261
+ number = argv[2];
1262
+ type_class = (argc > 3) ? argv[3] : Qnil;
1263
+
1264
+ return msgdef_add_field(self->descriptor, "optional",
1265
+ name, type, number, type_class);
1266
+ }
1267
+
1268
+ /*
1269
+ * call-seq:
1270
+ * MessageBuilderContext.required(name, type, number, type_class = nil)
1271
+ *
1272
+ * Defines a new required field on this message type with the given type, tag
1273
+ * number, and type class (for message and enum fields). The type must be a Ruby
1274
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1275
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
1276
+ *
1277
+ * Proto3 does not have required fields, but this method exists for
1278
+ * completeness. Any attempt to add a message type with required fields to a
1279
+ * pool will currently result in an error.
1280
+ */
1281
+ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
1282
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1283
+ VALUE name, type, number, type_class;
1284
+
1285
+ if (argc < 3) {
1286
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1287
+ }
1288
+ name = argv[0];
1289
+ type = argv[1];
1290
+ number = argv[2];
1291
+ type_class = (argc > 3) ? argv[3] : Qnil;
1292
+
1293
+ return msgdef_add_field(self->descriptor, "required",
1294
+ name, type, number, type_class);
1295
+ }
1296
+
1297
+ /*
1298
+ * call-seq:
1299
+ * MessageBuilderContext.repeated(name, type, number, type_class = nil)
1300
+ *
1301
+ * Defines a new repeated field on this message type with the given type, tag
1302
+ * number, and type class (for message and enum fields). The type must be a Ruby
1303
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
1304
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
1305
+ */
1306
+ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
1307
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1308
+ VALUE name, type, number, type_class;
1309
+
1310
+ if (argc < 3) {
1311
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1312
+ }
1313
+ name = argv[0];
1314
+ type = argv[1];
1315
+ number = argv[2];
1316
+ type_class = (argc > 3) ? argv[3] : Qnil;
1317
+
1318
+ return msgdef_add_field(self->descriptor, "repeated",
1319
+ name, type, number, type_class);
1320
+ }
1321
+
1322
+ /*
1323
+ * call-seq:
1324
+ * MessageBuilderContext.map(name, key_type, value_type, number,
1325
+ * value_type_class = nil)
1326
+ *
1327
+ * Defines a new map field on this message type with the given key and value
1328
+ * types, tag number, and type class (for message and enum value types). The key
1329
+ * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
1330
+ * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
1331
+ * type_class must be a string, if present (as accepted by
1332
+ * FieldDescriptor#submsg_name=).
1333
+ */
1334
+ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
1335
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1336
+ VALUE name, key_type, value_type, number, type_class;
1337
+ VALUE mapentry_desc, mapentry_desc_name;
1338
+
1339
+ if (argc < 4) {
1340
+ rb_raise(rb_eArgError, "Expected at least 4 arguments.");
1341
+ }
1342
+ name = argv[0];
1343
+ key_type = argv[1];
1344
+ value_type = argv[2];
1345
+ number = argv[3];
1346
+ type_class = (argc > 4) ? argv[4] : Qnil;
1347
+
1348
+ // Validate the key type. We can't accept enums, messages, or floats/doubles
1349
+ // as map keys. (We exclude these explicitly, and the field-descriptor setter
1350
+ // below then ensures that the type is one of the remaining valid options.)
1351
+ if (SYM2ID(key_type) == rb_intern("float") ||
1352
+ SYM2ID(key_type) == rb_intern("double") ||
1353
+ SYM2ID(key_type) == rb_intern("enum") ||
1354
+ SYM2ID(key_type) == rb_intern("message")) {
1355
+ rb_raise(rb_eArgError,
1356
+ "Cannot add a map field with a float, double, enum, or message "
1357
+ "type.");
1358
+ }
1359
+
1360
+ // Create a new message descriptor for the map entry message, and create a
1361
+ // repeated submessage field here with that type.
1362
+ mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor);
1363
+ mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
1364
+ mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
1365
+ mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
1366
+ rb_id2name(SYM2ID(name)));
1367
+ Descriptor_name_set(mapentry_desc, mapentry_desc_name);
1368
+
1369
+ {
1370
+ // The 'mapentry' attribute has no Ruby setter because we do not want the
1371
+ // user attempting to DIY the setup below; we want to ensure that the fields
1372
+ // are correct. So we reach into the msgdef here to set the bit manually.
1373
+ Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
1374
+ upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
1375
+ }
1376
+
1377
+ {
1378
+ // optional <type> key = 1;
1379
+ VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1380
+ FieldDescriptor_name_set(key_field, rb_str_new2("key"));
1381
+ FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
1382
+ FieldDescriptor_number_set(key_field, INT2NUM(1));
1383
+ FieldDescriptor_type_set(key_field, key_type);
1384
+ Descriptor_add_field(mapentry_desc, key_field);
1385
+ }
1386
+
1387
+ {
1388
+ // optional <type> value = 2;
1389
+ VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1390
+ FieldDescriptor_name_set(value_field, rb_str_new2("value"));
1391
+ FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
1392
+ FieldDescriptor_number_set(value_field, INT2NUM(2));
1393
+ FieldDescriptor_type_set(value_field, value_type);
1394
+ if (type_class != Qnil) {
1395
+ VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
1396
+ submsg_name = rb_str_append(submsg_name, type_class);
1397
+ FieldDescriptor_submsg_name_set(value_field, submsg_name);
1398
+ }
1399
+ Descriptor_add_field(mapentry_desc, value_field);
1400
+ }
1401
+
1402
+ {
1403
+ // Add the map-entry message type to the current builder, and use the type
1404
+ // to create the map field itself.
1405
+ Builder* builder_self = ruby_to_Builder(self->builder);
1406
+ rb_ary_push(builder_self->pending_list, mapentry_desc);
1407
+ }
1408
+
1409
+ {
1410
+ VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
1411
+ VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1412
+ VALUE submsg_name;
1413
+
1414
+ FieldDescriptor_name_set(map_field, name_str);
1415
+ FieldDescriptor_number_set(map_field, number);
1416
+ FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
1417
+ FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
1418
+ submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
1419
+ submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
1420
+ FieldDescriptor_submsg_name_set(map_field, submsg_name);
1421
+ Descriptor_add_field(self->descriptor, map_field);
1422
+ }
1423
+
1424
+ return Qnil;
1425
+ }
1426
+
1427
+ /*
1428
+ * call-seq:
1429
+ * MessageBuilderContext.oneof(name, &block) => nil
1430
+ *
1431
+ * Creates a new OneofDescriptor with the given name, creates a
1432
+ * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
1433
+ * block in the context of that OneofBuilderContext with #instance_eval, and
1434
+ * then adds the oneof to the message.
1435
+ *
1436
+ * This is the recommended, idiomatic way to build oneof definitions.
1437
+ */
1438
+ VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
1439
+ DEFINE_SELF(MessageBuilderContext, self, _self);
1440
+ VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
1441
+ VALUE args[2] = { oneofdef, self->builder };
1442
+ VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
1443
+ VALUE block = rb_block_proc();
1444
+ VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
1445
+ rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
1446
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1447
+ Descriptor_add_oneof(self->descriptor, oneofdef);
1448
+
1449
+ return Qnil;
1450
+ }
1451
+
1452
+ // -----------------------------------------------------------------------------
1453
+ // OneofBuilderContext.
1454
+ // -----------------------------------------------------------------------------
1455
+
1456
+ DEFINE_CLASS(OneofBuilderContext,
1457
+ "Google::Protobuf::Internal::OneofBuilderContext");
1458
+
1459
+ void OneofBuilderContext_mark(void* _self) {
1460
+ OneofBuilderContext* self = _self;
1461
+ rb_gc_mark(self->descriptor);
1462
+ rb_gc_mark(self->builder);
1463
+ }
1464
+
1465
+ void OneofBuilderContext_free(void* _self) {
1466
+ OneofBuilderContext* self = _self;
1467
+ xfree(self);
1468
+ }
1469
+
1470
+ VALUE OneofBuilderContext_alloc(VALUE klass) {
1471
+ OneofBuilderContext* self = ALLOC(OneofBuilderContext);
1472
+ VALUE ret = TypedData_Wrap_Struct(
1473
+ klass, &_OneofBuilderContext_type, self);
1474
+ self->descriptor = Qnil;
1475
+ self->builder = Qnil;
1476
+ return ret;
1477
+ }
1478
+
1479
+ void OneofBuilderContext_register(VALUE module) {
1480
+ VALUE klass = rb_define_class_under(
1481
+ module, "OneofBuilderContext", rb_cObject);
1482
+ rb_define_alloc_func(klass, OneofBuilderContext_alloc);
1483
+ rb_define_method(klass, "initialize",
1484
+ OneofBuilderContext_initialize, 2);
1485
+ rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
1486
+ cOneofBuilderContext = klass;
1487
+ rb_gc_register_address(&cOneofBuilderContext);
1488
+ }
1489
+
1490
+ /*
1491
+ * call-seq:
1492
+ * OneofBuilderContext.new(desc, builder) => context
1493
+ *
1494
+ * Create a new oneof builder context around the given oneof descriptor and
1495
+ * builder context. This class is intended to serve as a DSL context to be used
1496
+ * with #instance_eval.
1497
+ */
1498
+ VALUE OneofBuilderContext_initialize(VALUE _self,
1499
+ VALUE oneofdef,
1500
+ VALUE builder) {
1501
+ DEFINE_SELF(OneofBuilderContext, self, _self);
1502
+ self->descriptor = oneofdef;
1503
+ self->builder = builder;
1504
+ return Qnil;
1505
+ }
1506
+
1507
+ /*
1508
+ * call-seq:
1509
+ * OneofBuilderContext.optional(name, type, number, type_class = nil)
1510
+ *
1511
+ * Defines a new optional field in this oneof with the given type, tag number,
1512
+ * and type class (for message and enum fields). The type must be a Ruby symbol
1513
+ * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
1514
+ * if present (as accepted by FieldDescriptor#submsg_name=).
1515
+ */
1516
+ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
1517
+ DEFINE_SELF(OneofBuilderContext, self, _self);
1518
+ VALUE name, type, number, type_class;
1519
+
1520
+ if (argc < 3) {
1521
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
1522
+ }
1523
+ name = argv[0];
1524
+ type = argv[1];
1525
+ number = argv[2];
1526
+ type_class = (argc > 3) ? argv[3] : Qnil;
1527
+
1528
+ return msgdef_add_field(self->descriptor, "optional",
1529
+ name, type, number, type_class);
1530
+ }
1531
+
1532
+ // -----------------------------------------------------------------------------
1533
+ // EnumBuilderContext.
1534
+ // -----------------------------------------------------------------------------
1535
+
1536
+ DEFINE_CLASS(EnumBuilderContext,
1537
+ "Google::Protobuf::Internal::EnumBuilderContext");
1538
+
1539
+ void EnumBuilderContext_mark(void* _self) {
1540
+ EnumBuilderContext* self = _self;
1541
+ rb_gc_mark(self->enumdesc);
1542
+ }
1543
+
1544
+ void EnumBuilderContext_free(void* _self) {
1545
+ EnumBuilderContext* self = _self;
1546
+ xfree(self);
1547
+ }
1548
+
1549
+ VALUE EnumBuilderContext_alloc(VALUE klass) {
1550
+ EnumBuilderContext* self = ALLOC(EnumBuilderContext);
1551
+ VALUE ret = TypedData_Wrap_Struct(
1552
+ klass, &_EnumBuilderContext_type, self);
1553
+ self->enumdesc = Qnil;
1554
+ return ret;
1555
+ }
1556
+
1557
+ void EnumBuilderContext_register(VALUE module) {
1558
+ VALUE klass = rb_define_class_under(
1559
+ module, "EnumBuilderContext", rb_cObject);
1560
+ rb_define_alloc_func(klass, EnumBuilderContext_alloc);
1561
+ rb_define_method(klass, "initialize",
1562
+ EnumBuilderContext_initialize, 1);
1563
+ rb_define_method(klass, "value", EnumBuilderContext_value, 2);
1564
+ cEnumBuilderContext = klass;
1565
+ rb_gc_register_address(&cEnumBuilderContext);
1566
+ }
1567
+
1568
+ /*
1569
+ * call-seq:
1570
+ * EnumBuilderContext.new(enumdesc) => context
1571
+ *
1572
+ * Create a new builder context around the given enum descriptor. This class is
1573
+ * intended to serve as a DSL context to be used with #instance_eval.
1574
+ */
1575
+ VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
1576
+ DEFINE_SELF(EnumBuilderContext, self, _self);
1577
+ self->enumdesc = enumdef;
1578
+ return Qnil;
1579
+ }
1580
+
1581
+ static VALUE enumdef_add_value(VALUE enumdef,
1582
+ VALUE name, VALUE number) {
1583
+ rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
1584
+ return Qnil;
1585
+ }
1586
+
1587
+ /*
1588
+ * call-seq:
1589
+ * EnumBuilder.add_value(name, number)
1590
+ *
1591
+ * Adds the given name => number mapping to the enum type. Name must be a Ruby
1592
+ * symbol.
1593
+ */
1594
+ VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
1595
+ DEFINE_SELF(EnumBuilderContext, self, _self);
1596
+ return enumdef_add_value(self->enumdesc, name, number);
1597
+ }
1598
+
1599
+ // -----------------------------------------------------------------------------
1600
+ // Builder.
1601
+ // -----------------------------------------------------------------------------
1602
+
1603
+ DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
1604
+
1605
+ void Builder_mark(void* _self) {
1606
+ Builder* self = _self;
1607
+ rb_gc_mark(self->pending_list);
1608
+ }
1609
+
1610
+ void Builder_free(void* _self) {
1611
+ Builder* self = _self;
1612
+ xfree(self->defs);
1613
+ xfree(self);
1614
+ }
1615
+
1616
+ /*
1617
+ * call-seq:
1618
+ * Builder.new => builder
1619
+ *
1620
+ * Creates a new Builder. A Builder can accumulate a set of new message and enum
1621
+ * descriptors and atomically register them into a pool in a way that allows for
1622
+ * (co)recursive type references.
1623
+ */
1624
+ VALUE Builder_alloc(VALUE klass) {
1625
+ Builder* self = ALLOC(Builder);
1626
+ VALUE ret = TypedData_Wrap_Struct(
1627
+ klass, &_Builder_type, self);
1628
+ self->pending_list = rb_ary_new();
1629
+ self->defs = NULL;
1630
+ return ret;
1631
+ }
1632
+
1633
+ void Builder_register(VALUE module) {
1634
+ VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
1635
+ rb_define_alloc_func(klass, Builder_alloc);
1636
+ rb_define_method(klass, "add_message", Builder_add_message, 1);
1637
+ rb_define_method(klass, "add_enum", Builder_add_enum, 1);
1638
+ rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
1639
+ cBuilder = klass;
1640
+ rb_gc_register_address(&cBuilder);
1641
+ }
1642
+
1643
+ /*
1644
+ * call-seq:
1645
+ * Builder.add_message(name, &block)
1646
+ *
1647
+ * Creates a new, empty descriptor with the given name, and invokes the block in
1648
+ * the context of a MessageBuilderContext on that descriptor. The block can then
1649
+ * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
1650
+ * methods to define the message fields.
1651
+ *
1652
+ * This is the recommended, idiomatic way to build message definitions.
1653
+ */
1654
+ VALUE Builder_add_message(VALUE _self, VALUE name) {
1655
+ DEFINE_SELF(Builder, self, _self);
1656
+ VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor);
1657
+ VALUE args[2] = { msgdef, _self };
1658
+ VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
1659
+ VALUE block = rb_block_proc();
1660
+ rb_funcall(msgdef, rb_intern("name="), 1, name);
1661
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1662
+ rb_ary_push(self->pending_list, msgdef);
1663
+ return Qnil;
1664
+ }
1665
+
1666
+ /*
1667
+ * call-seq:
1668
+ * Builder.add_enum(name, &block)
1669
+ *
1670
+ * Creates a new, empty enum descriptor with the given name, and invokes the
1671
+ * block in the context of an EnumBuilderContext on that descriptor. The block
1672
+ * can then call EnumBuilderContext#add_value to define the enum values.
1673
+ *
1674
+ * This is the recommended, idiomatic way to build enum definitions.
1675
+ */
1676
+ VALUE Builder_add_enum(VALUE _self, VALUE name) {
1677
+ DEFINE_SELF(Builder, self, _self);
1678
+ VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor);
1679
+ VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
1680
+ VALUE block = rb_block_proc();
1681
+ rb_funcall(enumdef, rb_intern("name="), 1, name);
1682
+ rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
1683
+ rb_ary_push(self->pending_list, enumdef);
1684
+ return Qnil;
1685
+ }
1686
+
1687
+ static void validate_msgdef(const upb_msgdef* msgdef) {
1688
+ // Verify that no required fields exist. proto3 does not support these.
1689
+ upb_msg_field_iter it;
1690
+ for (upb_msg_field_begin(&it, msgdef);
1691
+ !upb_msg_field_done(&it);
1692
+ upb_msg_field_next(&it)) {
1693
+ const upb_fielddef* field = upb_msg_iter_field(&it);
1694
+ if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
1695
+ rb_raise(rb_eTypeError, "Required fields are unsupported in proto3.");
1696
+ }
1697
+ }
1698
+ }
1699
+
1700
+ static void validate_enumdef(const upb_enumdef* enumdef) {
1701
+ // Verify that an entry exists with integer value 0. (This is the default
1702
+ // value.)
1703
+ const char* lookup = upb_enumdef_iton(enumdef, 0);
1704
+ if (lookup == NULL) {
1705
+ rb_raise(rb_eTypeError,
1706
+ "Enum definition does not contain a value for '0'.");
1707
+ }
1708
+ }
1709
+
1710
+ /*
1711
+ * call-seq:
1712
+ * Builder.finalize_to_pool(pool)
1713
+ *
1714
+ * Adds all accumulated message and enum descriptors created in this builder
1715
+ * context to the given pool. The operation occurs atomically, and all
1716
+ * descriptors can refer to each other (including in cycles). This is the only
1717
+ * way to build (co)recursive message definitions.
1718
+ *
1719
+ * This method is usually called automatically by DescriptorPool#build after it
1720
+ * invokes the given user block in the context of the builder. The user should
1721
+ * not normally need to call this manually because a Builder is not normally
1722
+ * created manually.
1723
+ */
1724
+ VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
1725
+ DEFINE_SELF(Builder, self, _self);
1726
+
1727
+ DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
1728
+
1729
+ REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
1730
+
1731
+ for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
1732
+ VALUE def_rb = rb_ary_entry(self->pending_list, i);
1733
+ if (CLASS_OF(def_rb) == cDescriptor) {
1734
+ self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
1735
+ validate_msgdef((const upb_msgdef*)self->defs[i]);
1736
+ } else if (CLASS_OF(def_rb) == cEnumDescriptor) {
1737
+ self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
1738
+ validate_enumdef((const upb_enumdef*)self->defs[i]);
1739
+ }
1740
+ }
1741
+
1742
+ CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
1743
+ RARRAY_LEN(self->pending_list), NULL, &status),
1744
+ "Unable to add defs to DescriptorPool");
1745
+
1746
+ for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
1747
+ VALUE def_rb = rb_ary_entry(self->pending_list, i);
1748
+ add_def_obj(self->defs[i], def_rb);
1749
+ }
1750
+
1751
+ self->pending_list = rb_ary_new();
1752
+ return Qnil;
1753
+ }