google-protobuf 3.19.6-x86-linux → 3.20.0.rc.1-x86-linux
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.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/convert.c +124 -111
- data/ext/google/protobuf_c/convert.h +12 -9
- data/ext/google/protobuf_c/defs.c +196 -201
- data/ext/google/protobuf_c/defs.h +19 -19
- data/ext/google/protobuf_c/extconf.rb +11 -3
- data/ext/google/protobuf_c/map.c +109 -101
- data/ext/google/protobuf_c/map.h +7 -8
- data/ext/google/protobuf_c/message.c +382 -303
- data/ext/google/protobuf_c/message.h +22 -19
- data/ext/google/protobuf_c/protobuf.c +57 -58
- data/ext/google/protobuf_c/protobuf.h +13 -10
- data/ext/google/protobuf_c/repeated_field.c +82 -84
- data/ext/google/protobuf_c/repeated_field.h +6 -6
- data/ext/google/protobuf_c/ruby-upb.c +4945 -3084
- data/ext/google/protobuf_c/ruby-upb.h +2285 -1701
- data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +21 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +395 -0
- data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
- data/lib/google/2.5/protobuf_c.so +0 -0
- data/lib/google/2.6/protobuf_c.so +0 -0
- data/lib/google/2.7/protobuf_c.so +0 -0
- data/lib/google/3.0/protobuf_c.so +0 -0
- data/lib/google/protobuf/descriptor_dsl.rb +8 -1
- data/lib/google/protobuf/descriptor_pb.rb +3 -2
- data/lib/google/protobuf/message_exts.rb +2 -2
- data/lib/google/protobuf/well_known_types.rb +11 -6
- data/lib/google/protobuf.rb +4 -4
- data/tests/basic.rb +14 -0
- metadata +8 -5
@@ -40,7 +40,7 @@ static VALUE cParseError = Qnil;
|
|
40
40
|
static ID descriptor_instancevar_interned;
|
41
41
|
|
42
42
|
static VALUE initialize_rb_class_with_no_args(VALUE klass) {
|
43
|
-
|
43
|
+
return rb_funcall(klass, rb_intern("new"), 0);
|
44
44
|
}
|
45
45
|
|
46
46
|
VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
|
@@ -53,19 +53,20 @@ VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
|
|
53
53
|
|
54
54
|
typedef struct {
|
55
55
|
VALUE arena;
|
56
|
-
const
|
57
|
-
const
|
56
|
+
const upb_Message* msg; // Can get as mutable when non-frozen.
|
57
|
+
const upb_MessageDef*
|
58
|
+
msgdef; // kept alive by self.class.descriptor reference.
|
58
59
|
} Message;
|
59
60
|
|
60
61
|
static void Message_mark(void* _self) {
|
61
|
-
Message* self = (Message
|
62
|
+
Message* self = (Message*)_self;
|
62
63
|
rb_gc_mark(self->arena);
|
63
64
|
}
|
64
65
|
|
65
66
|
static rb_data_type_t Message_type = {
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
"Message",
|
68
|
+
{Message_mark, RUBY_DEFAULT_FREE, NULL},
|
69
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
69
70
|
};
|
70
71
|
|
71
72
|
static Message* ruby_to_Message(VALUE msg_rb) {
|
@@ -89,18 +90,18 @@ static VALUE Message_alloc(VALUE klass) {
|
|
89
90
|
return ret;
|
90
91
|
}
|
91
92
|
|
92
|
-
const
|
93
|
+
const upb_Message* Message_Get(VALUE msg_rb, const upb_MessageDef** m) {
|
93
94
|
Message* msg = ruby_to_Message(msg_rb);
|
94
95
|
if (m) *m = msg->msgdef;
|
95
96
|
return msg->msg;
|
96
97
|
}
|
97
98
|
|
98
|
-
|
99
|
+
upb_Message* Message_GetMutable(VALUE msg_rb, const upb_MessageDef** m) {
|
99
100
|
rb_check_frozen(msg_rb);
|
100
|
-
return (
|
101
|
+
return (upb_Message*)Message_Get(msg_rb, m);
|
101
102
|
}
|
102
103
|
|
103
|
-
void Message_InitPtr(VALUE self_,
|
104
|
+
void Message_InitPtr(VALUE self_, upb_Message* msg, VALUE arena) {
|
104
105
|
Message* self = ruby_to_Message(self_);
|
105
106
|
self->msg = msg;
|
106
107
|
self->arena = arena;
|
@@ -119,7 +120,8 @@ void Message_CheckClass(VALUE klass) {
|
|
119
120
|
}
|
120
121
|
}
|
121
122
|
|
122
|
-
VALUE Message_GetRubyWrapper(
|
123
|
+
VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
|
124
|
+
VALUE arena) {
|
123
125
|
if (msg == NULL) return Qnil;
|
124
126
|
|
125
127
|
VALUE val = ObjectCache_Get(msg);
|
@@ -133,17 +135,17 @@ VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena) {
|
|
133
135
|
return val;
|
134
136
|
}
|
135
137
|
|
136
|
-
void Message_PrintMessage(StringBuilder* b, const
|
137
|
-
const
|
138
|
+
void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
|
139
|
+
const upb_MessageDef* m) {
|
138
140
|
bool first = true;
|
139
|
-
int n =
|
141
|
+
int n = upb_MessageDef_FieldCount(m);
|
140
142
|
VALUE klass = Descriptor_DefToClass(m);
|
141
143
|
StringBuilder_Printf(b, "<%s: ", rb_class2name(klass));
|
142
144
|
|
143
145
|
for (int i = 0; i < n; i++) {
|
144
|
-
const
|
146
|
+
const upb_FieldDef* field = upb_MessageDef_Field(m, i);
|
145
147
|
|
146
|
-
if (
|
148
|
+
if (upb_FieldDef_HasPresence(field) && !upb_Message_Has(msg, field)) {
|
147
149
|
continue;
|
148
150
|
}
|
149
151
|
|
@@ -153,17 +155,19 @@ void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
|
|
153
155
|
first = false;
|
154
156
|
}
|
155
157
|
|
156
|
-
|
158
|
+
upb_MessageValue msgval = upb_Message_Get(msg, field);
|
157
159
|
|
158
|
-
StringBuilder_Printf(b, "%s: ",
|
160
|
+
StringBuilder_Printf(b, "%s: ", upb_FieldDef_Name(field));
|
159
161
|
|
160
|
-
if (
|
161
|
-
const
|
162
|
-
const
|
163
|
-
|
162
|
+
if (upb_FieldDef_IsMap(field)) {
|
163
|
+
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
|
164
|
+
const upb_FieldDef* key_f =
|
165
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 1);
|
166
|
+
const upb_FieldDef* val_f =
|
167
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 2);
|
164
168
|
TypeInfo val_info = TypeInfo_get(val_f);
|
165
|
-
Map_Inspect(b, msgval.map_val,
|
166
|
-
} else if (
|
169
|
+
Map_Inspect(b, msgval.map_val, upb_FieldDef_CType(key_f), val_info);
|
170
|
+
} else if (upb_FieldDef_IsRepeated(field)) {
|
167
171
|
RepeatedField_Inspect(b, msgval.array_val, TypeInfo_get(field));
|
168
172
|
} else {
|
169
173
|
StringBuilder_PrintMsgval(b, msgval, TypeInfo_get(field));
|
@@ -187,14 +191,31 @@ enum {
|
|
187
191
|
};
|
188
192
|
|
189
193
|
// Check if the field is a well known wrapper type
|
190
|
-
static bool IsWrapper(const
|
191
|
-
|
192
|
-
|
194
|
+
static bool IsWrapper(const upb_MessageDef* m) {
|
195
|
+
if (!m) return false;
|
196
|
+
switch (upb_MessageDef_WellKnownType(m)) {
|
197
|
+
case kUpb_WellKnown_DoubleValue:
|
198
|
+
case kUpb_WellKnown_FloatValue:
|
199
|
+
case kUpb_WellKnown_Int64Value:
|
200
|
+
case kUpb_WellKnown_UInt64Value:
|
201
|
+
case kUpb_WellKnown_Int32Value:
|
202
|
+
case kUpb_WellKnown_UInt32Value:
|
203
|
+
case kUpb_WellKnown_StringValue:
|
204
|
+
case kUpb_WellKnown_BytesValue:
|
205
|
+
case kUpb_WellKnown_BoolValue:
|
206
|
+
return true;
|
207
|
+
default:
|
208
|
+
return false;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
|
212
|
+
static bool IsFieldWrapper(const upb_FieldDef* f) {
|
213
|
+
return IsWrapper(upb_FieldDef_MessageSubDef(f));
|
193
214
|
}
|
194
215
|
|
195
|
-
static bool Match(const
|
196
|
-
const
|
197
|
-
const char* suffix) {
|
216
|
+
static bool Match(const upb_MessageDef* m, const char* name,
|
217
|
+
const upb_FieldDef** f, const upb_OneofDef** o,
|
218
|
+
const char* prefix, const char* suffix) {
|
198
219
|
size_t sp = strlen(prefix);
|
199
220
|
size_t ss = strlen(suffix);
|
200
221
|
size_t sn = strlen(name);
|
@@ -206,12 +227,12 @@ static bool Match(const upb_msgdef* m, const char* name, const upb_fielddef** f,
|
|
206
227
|
return false;
|
207
228
|
}
|
208
229
|
|
209
|
-
return
|
230
|
+
return upb_MessageDef_FindByNameWithSize(m, name + sp, sn - sp - ss, f, o);
|
210
231
|
}
|
211
232
|
|
212
233
|
static int extract_method_call(VALUE method_name, Message* self,
|
213
|
-
const
|
214
|
-
const
|
234
|
+
const upb_FieldDef** f, const upb_OneofDef** o) {
|
235
|
+
const upb_MessageDef* m = self->msgdef;
|
215
236
|
const char* name;
|
216
237
|
|
217
238
|
Check_Type(method_name, T_SYMBOL);
|
@@ -221,156 +242,159 @@ static int extract_method_call(VALUE method_name, Message* self,
|
|
221
242
|
if (Match(m, name, f, o, "", "=")) return METHOD_SETTER;
|
222
243
|
if (Match(m, name, f, o, "clear_", "")) return METHOD_CLEAR;
|
223
244
|
if (Match(m, name, f, o, "has_", "?") &&
|
224
|
-
(*o || (*f &&
|
245
|
+
(*o || (*f && upb_FieldDef_HasPresence(*f)))) {
|
225
246
|
// Disallow oneof hazzers for proto3.
|
226
247
|
// TODO(haberman): remove this test when we are enabling oneof hazzers for
|
227
248
|
// proto3.
|
228
|
-
if (*f && !
|
229
|
-
|
230
|
-
|
231
|
-
|
249
|
+
if (*f && !upb_FieldDef_IsSubMessage(*f) &&
|
250
|
+
upb_FieldDef_RealContainingOneof(*f) &&
|
251
|
+
upb_MessageDef_Syntax(upb_FieldDef_ContainingType(*f)) !=
|
252
|
+
kUpb_Syntax_Proto2) {
|
232
253
|
return METHOD_UNKNOWN;
|
233
254
|
}
|
234
255
|
return METHOD_PRESENCE;
|
235
256
|
}
|
236
|
-
if (Match(m, name, f, o, "", "_as_value") && *f &&
|
237
|
-
|
257
|
+
if (Match(m, name, f, o, "", "_as_value") && *f &&
|
258
|
+
!upb_FieldDef_IsRepeated(*f) && IsFieldWrapper(*f)) {
|
238
259
|
return METHOD_WRAPPER_GETTER;
|
239
260
|
}
|
240
|
-
if (Match(m, name, f, o, "", "_as_value=") && *f &&
|
241
|
-
|
261
|
+
if (Match(m, name, f, o, "", "_as_value=") && *f &&
|
262
|
+
!upb_FieldDef_IsRepeated(*f) && IsFieldWrapper(*f)) {
|
242
263
|
return METHOD_WRAPPER_SETTER;
|
243
264
|
}
|
244
265
|
if (Match(m, name, f, o, "", "_const") && *f &&
|
245
|
-
|
266
|
+
upb_FieldDef_CType(*f) == kUpb_CType_Enum) {
|
246
267
|
return METHOD_ENUM_GETTER;
|
247
268
|
}
|
248
269
|
|
249
270
|
return METHOD_UNKNOWN;
|
250
271
|
}
|
251
272
|
|
252
|
-
static VALUE Message_oneof_accessor(VALUE _self, const
|
273
|
+
static VALUE Message_oneof_accessor(VALUE _self, const upb_OneofDef* o,
|
253
274
|
int accessor_type) {
|
254
275
|
Message* self = ruby_to_Message(_self);
|
255
|
-
const
|
276
|
+
const upb_FieldDef* oneof_field = upb_Message_WhichOneof(self->msg, o);
|
256
277
|
|
257
278
|
switch (accessor_type) {
|
258
279
|
case METHOD_PRESENCE:
|
259
280
|
return oneof_field == NULL ? Qfalse : Qtrue;
|
260
281
|
case METHOD_CLEAR:
|
261
282
|
if (oneof_field != NULL) {
|
262
|
-
|
283
|
+
upb_Message_ClearField(Message_GetMutable(_self, NULL), oneof_field);
|
263
284
|
}
|
264
285
|
return Qnil;
|
265
286
|
case METHOD_GETTER:
|
266
287
|
return oneof_field == NULL
|
267
288
|
? Qnil
|
268
|
-
: ID2SYM(rb_intern(
|
289
|
+
: ID2SYM(rb_intern(upb_FieldDef_Name(oneof_field)));
|
269
290
|
case METHOD_SETTER:
|
270
291
|
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
271
292
|
}
|
272
293
|
rb_raise(rb_eRuntimeError, "Invalid access of oneof field.");
|
273
294
|
}
|
274
295
|
|
275
|
-
static void Message_setfield(
|
276
|
-
|
277
|
-
|
278
|
-
if (
|
296
|
+
static void Message_setfield(upb_Message* msg, const upb_FieldDef* f, VALUE val,
|
297
|
+
upb_Arena* arena) {
|
298
|
+
upb_MessageValue msgval;
|
299
|
+
if (upb_FieldDef_IsMap(f)) {
|
279
300
|
msgval.map_val = Map_GetUpbMap(val, f, arena);
|
280
|
-
} else if (
|
301
|
+
} else if (upb_FieldDef_IsRepeated(f)) {
|
281
302
|
msgval.array_val = RepeatedField_GetUpbArray(val, f, arena);
|
282
303
|
} else {
|
283
304
|
if (val == Qnil &&
|
284
|
-
(
|
285
|
-
|
305
|
+
(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_RealContainingOneof(f))) {
|
306
|
+
upb_Message_ClearField(msg, f);
|
286
307
|
return;
|
287
308
|
}
|
288
309
|
msgval =
|
289
|
-
Convert_RubyToUpb(val,
|
310
|
+
Convert_RubyToUpb(val, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
|
290
311
|
}
|
291
|
-
|
312
|
+
upb_Message_Set(msg, f, msgval, arena);
|
292
313
|
}
|
293
314
|
|
294
|
-
VALUE Message_getfield(VALUE _self, const
|
315
|
+
VALUE Message_getfield(VALUE _self, const upb_FieldDef* f) {
|
295
316
|
Message* self = ruby_to_Message(_self);
|
296
|
-
// This is a special-case:
|
317
|
+
// This is a special-case: upb_Message_Mutable() for map & array are logically
|
297
318
|
// const (they will not change what is serialized) but physically
|
298
319
|
// non-const, as they do allocate a repeated field or map. The logical
|
299
320
|
// constness means it's ok to do even if the message is frozen.
|
300
|
-
|
301
|
-
|
302
|
-
if (
|
303
|
-
|
304
|
-
const
|
305
|
-
const
|
306
|
-
|
321
|
+
upb_Message* msg = (upb_Message*)self->msg;
|
322
|
+
upb_Arena* arena = Arena_get(self->arena);
|
323
|
+
if (upb_FieldDef_IsMap(f)) {
|
324
|
+
upb_Map* map = upb_Message_Mutable(msg, f, arena).map;
|
325
|
+
const upb_FieldDef* key_f = map_field_key(f);
|
326
|
+
const upb_FieldDef* val_f = map_field_value(f);
|
327
|
+
upb_CType key_type = upb_FieldDef_CType(key_f);
|
307
328
|
TypeInfo value_type_info = TypeInfo_get(val_f);
|
308
329
|
return Map_GetRubyWrapper(map, key_type, value_type_info, self->arena);
|
309
|
-
} else if (
|
310
|
-
|
330
|
+
} else if (upb_FieldDef_IsRepeated(f)) {
|
331
|
+
upb_Array* arr = upb_Message_Mutable(msg, f, arena).array;
|
311
332
|
return RepeatedField_GetRubyWrapper(arr, TypeInfo_get(f), self->arena);
|
312
|
-
} else if (
|
313
|
-
if (!
|
314
|
-
|
315
|
-
const
|
333
|
+
} else if (upb_FieldDef_IsSubMessage(f)) {
|
334
|
+
if (!upb_Message_Has(self->msg, f)) return Qnil;
|
335
|
+
upb_Message* submsg = upb_Message_Mutable(msg, f, arena).msg;
|
336
|
+
const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
|
316
337
|
return Message_GetRubyWrapper(submsg, m, self->arena);
|
317
338
|
} else {
|
318
|
-
|
339
|
+
upb_MessageValue msgval = upb_Message_Get(self->msg, f);
|
319
340
|
return Convert_UpbToRuby(msgval, TypeInfo_get(f), self->arena);
|
320
341
|
}
|
321
342
|
}
|
322
343
|
|
323
|
-
static VALUE Message_field_accessor(VALUE _self, const
|
344
|
+
static VALUE Message_field_accessor(VALUE _self, const upb_FieldDef* f,
|
324
345
|
int accessor_type, int argc, VALUE* argv) {
|
325
|
-
|
346
|
+
upb_Arena* arena = Arena_get(Message_GetArena(_self));
|
326
347
|
|
327
348
|
switch (accessor_type) {
|
328
349
|
case METHOD_SETTER:
|
329
350
|
Message_setfield(Message_GetMutable(_self, NULL), f, argv[1], arena);
|
330
351
|
return Qnil;
|
331
352
|
case METHOD_CLEAR:
|
332
|
-
|
353
|
+
upb_Message_ClearField(Message_GetMutable(_self, NULL), f);
|
333
354
|
return Qnil;
|
334
355
|
case METHOD_PRESENCE:
|
335
|
-
if (!
|
356
|
+
if (!upb_FieldDef_HasPresence(f)) {
|
336
357
|
rb_raise(rb_eRuntimeError, "Field does not have presence.");
|
337
358
|
}
|
338
|
-
return
|
359
|
+
return upb_Message_Has(Message_Get(_self, NULL), f);
|
339
360
|
case METHOD_WRAPPER_GETTER: {
|
340
361
|
Message* self = ruby_to_Message(_self);
|
341
|
-
if (
|
342
|
-
PBRUBY_ASSERT(
|
343
|
-
|
344
|
-
|
345
|
-
const
|
346
|
-
|
362
|
+
if (upb_Message_Has(self->msg, f)) {
|
363
|
+
PBRUBY_ASSERT(upb_FieldDef_IsSubMessage(f) &&
|
364
|
+
!upb_FieldDef_IsRepeated(f));
|
365
|
+
upb_MessageValue wrapper = upb_Message_Get(self->msg, f);
|
366
|
+
const upb_MessageDef* wrapper_m = upb_FieldDef_MessageSubDef(f);
|
367
|
+
const upb_FieldDef* value_f =
|
368
|
+
upb_MessageDef_FindFieldByNumberWithSize(wrapper_m, 1);
|
369
|
+
upb_MessageValue value = upb_Message_Get(wrapper.msg_val, value_f);
|
347
370
|
return Convert_UpbToRuby(value, TypeInfo_get(value_f), self->arena);
|
348
371
|
} else {
|
349
372
|
return Qnil;
|
350
373
|
}
|
351
374
|
}
|
352
375
|
case METHOD_WRAPPER_SETTER: {
|
353
|
-
|
376
|
+
upb_Message* msg = Message_GetMutable(_self, NULL);
|
354
377
|
if (argv[1] == Qnil) {
|
355
|
-
|
378
|
+
upb_Message_ClearField(msg, f);
|
356
379
|
} else {
|
357
|
-
const
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
380
|
+
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumberWithSize(
|
381
|
+
upb_FieldDef_MessageSubDef(f), 1);
|
382
|
+
upb_MessageValue msgval = Convert_RubyToUpb(
|
383
|
+
argv[1], upb_FieldDef_Name(f), TypeInfo_get(val_f), arena);
|
384
|
+
upb_Message* wrapper = upb_Message_Mutable(msg, f, arena).msg;
|
385
|
+
upb_Message_Set(wrapper, val_f, msgval, arena);
|
362
386
|
}
|
363
387
|
return Qnil;
|
364
388
|
}
|
365
389
|
case METHOD_ENUM_GETTER: {
|
366
|
-
|
390
|
+
upb_MessageValue msgval = upb_Message_Get(Message_Get(_self, NULL), f);
|
367
391
|
|
368
|
-
if (
|
392
|
+
if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
|
369
393
|
// Map repeated fields to a new type with ints
|
370
394
|
VALUE arr = rb_ary_new();
|
371
|
-
size_t i, n =
|
395
|
+
size_t i, n = upb_Array_Size(msgval.array_val);
|
372
396
|
for (i = 0; i < n; i++) {
|
373
|
-
|
397
|
+
upb_MessageValue elem = upb_Array_Get(msgval.array_val, i);
|
374
398
|
rb_ary_push(arr, INT2NUM(elem.int32_val));
|
375
399
|
}
|
376
400
|
return arr;
|
@@ -415,8 +439,8 @@ static VALUE Message_field_accessor(VALUE _self, const upb_fielddef* f,
|
|
415
439
|
*/
|
416
440
|
static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
417
441
|
Message* self = ruby_to_Message(_self);
|
418
|
-
const
|
419
|
-
const
|
442
|
+
const upb_OneofDef* o;
|
443
|
+
const upb_FieldDef* f;
|
420
444
|
int accessor_type;
|
421
445
|
|
422
446
|
if (argc < 1) {
|
@@ -453,8 +477,8 @@ static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
|
453
477
|
|
454
478
|
static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
455
479
|
Message* self = ruby_to_Message(_self);
|
456
|
-
const
|
457
|
-
const
|
480
|
+
const upb_OneofDef* o;
|
481
|
+
const upb_FieldDef* f;
|
458
482
|
int accessor_type;
|
459
483
|
|
460
484
|
if (argc < 1) {
|
@@ -472,53 +496,56 @@ static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
|
472
496
|
}
|
473
497
|
}
|
474
498
|
|
475
|
-
void Message_InitFromValue(
|
476
|
-
|
499
|
+
void Message_InitFromValue(upb_Message* msg, const upb_MessageDef* m, VALUE val,
|
500
|
+
upb_Arena* arena);
|
477
501
|
|
478
502
|
typedef struct {
|
479
|
-
|
503
|
+
upb_Map* map;
|
480
504
|
TypeInfo key_type;
|
481
505
|
TypeInfo val_type;
|
482
|
-
|
506
|
+
upb_Arena* arena;
|
483
507
|
} MapInit;
|
484
508
|
|
485
509
|
static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
486
|
-
MapInit
|
487
|
-
|
510
|
+
MapInit* map_init = (MapInit*)_self;
|
511
|
+
upb_MessageValue k, v;
|
488
512
|
k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
|
489
513
|
|
490
|
-
if (map_init->val_type.type ==
|
491
|
-
|
514
|
+
if (map_init->val_type.type == kUpb_CType_Message && TYPE(val) == T_HASH) {
|
515
|
+
upb_Message* msg =
|
516
|
+
upb_Message_New(map_init->val_type.def.msgdef, map_init->arena);
|
492
517
|
Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
|
493
518
|
map_init->arena);
|
494
519
|
v.msg_val = msg;
|
495
520
|
} else {
|
496
521
|
v = Convert_RubyToUpb(val, "", map_init->val_type, map_init->arena);
|
497
522
|
}
|
498
|
-
|
523
|
+
upb_Map_Set(map_init->map, k, v, map_init->arena);
|
499
524
|
return ST_CONTINUE;
|
500
525
|
}
|
501
526
|
|
502
|
-
static void Map_InitFromValue(
|
503
|
-
|
504
|
-
const
|
505
|
-
const
|
506
|
-
|
527
|
+
static void Map_InitFromValue(upb_Map* map, const upb_FieldDef* f, VALUE val,
|
528
|
+
upb_Arena* arena) {
|
529
|
+
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
|
530
|
+
const upb_FieldDef* key_f =
|
531
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 1);
|
532
|
+
const upb_FieldDef* val_f =
|
533
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 2);
|
507
534
|
if (TYPE(val) != T_HASH) {
|
508
535
|
rb_raise(rb_eArgError,
|
509
536
|
"Expected Hash object as initializer value for map field '%s' "
|
510
537
|
"(given %s).",
|
511
|
-
|
538
|
+
upb_FieldDef_Name(f), rb_class2name(CLASS_OF(val)));
|
512
539
|
}
|
513
540
|
MapInit map_init = {map, TypeInfo_get(key_f), TypeInfo_get(val_f), arena};
|
514
541
|
rb_hash_foreach(val, Map_initialize_kwarg, (VALUE)&map_init);
|
515
542
|
}
|
516
543
|
|
517
|
-
static
|
518
|
-
|
519
|
-
if (info.type ==
|
520
|
-
|
521
|
-
|
544
|
+
static upb_MessageValue MessageValue_FromValue(VALUE val, TypeInfo info,
|
545
|
+
upb_Arena* arena) {
|
546
|
+
if (info.type == kUpb_CType_Message) {
|
547
|
+
upb_MessageValue msgval;
|
548
|
+
upb_Message* msg = upb_Message_New(info.def.msgdef, arena);
|
522
549
|
Message_InitFromValue(msg, info.def.msgdef, val, arena);
|
523
550
|
msgval.msg_val = msg;
|
524
551
|
return msgval;
|
@@ -527,61 +554,62 @@ static upb_msgval MessageValue_FromValue(VALUE val, TypeInfo info,
|
|
527
554
|
}
|
528
555
|
}
|
529
556
|
|
530
|
-
static void RepeatedField_InitFromValue(
|
531
|
-
VALUE val,
|
557
|
+
static void RepeatedField_InitFromValue(upb_Array* arr, const upb_FieldDef* f,
|
558
|
+
VALUE val, upb_Arena* arena) {
|
532
559
|
TypeInfo type_info = TypeInfo_get(f);
|
533
560
|
|
534
561
|
if (TYPE(val) != T_ARRAY) {
|
535
562
|
rb_raise(rb_eArgError,
|
536
|
-
"Expected array as initializer value for repeated field '%s'
|
537
|
-
|
563
|
+
"Expected array as initializer value for repeated field '%s' "
|
564
|
+
"(given %s).",
|
565
|
+
upb_FieldDef_Name(f), rb_class2name(CLASS_OF(val)));
|
538
566
|
}
|
539
567
|
|
540
568
|
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
541
569
|
VALUE entry = rb_ary_entry(val, i);
|
542
|
-
|
543
|
-
if (
|
570
|
+
upb_MessageValue msgval;
|
571
|
+
if (upb_FieldDef_IsSubMessage(f) && TYPE(entry) == T_HASH) {
|
544
572
|
msgval = MessageValue_FromValue(entry, type_info, arena);
|
545
573
|
} else {
|
546
|
-
msgval = Convert_RubyToUpb(entry,
|
574
|
+
msgval = Convert_RubyToUpb(entry, upb_FieldDef_Name(f), type_info, arena);
|
547
575
|
}
|
548
|
-
|
576
|
+
upb_Array_Append(arr, msgval, arena);
|
549
577
|
}
|
550
578
|
}
|
551
579
|
|
552
|
-
static void Message_InitFieldFromValue(
|
553
|
-
VALUE val,
|
580
|
+
static void Message_InitFieldFromValue(upb_Message* msg, const upb_FieldDef* f,
|
581
|
+
VALUE val, upb_Arena* arena) {
|
554
582
|
if (TYPE(val) == T_NIL) return;
|
555
583
|
|
556
|
-
if (
|
557
|
-
|
584
|
+
if (upb_FieldDef_IsMap(f)) {
|
585
|
+
upb_Map* map = upb_Message_Mutable(msg, f, arena).map;
|
558
586
|
Map_InitFromValue(map, f, val, arena);
|
559
|
-
} else if (
|
560
|
-
|
587
|
+
} else if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
|
588
|
+
upb_Array* arr = upb_Message_Mutable(msg, f, arena).array;
|
561
589
|
RepeatedField_InitFromValue(arr, f, val, arena);
|
562
|
-
} else if (
|
590
|
+
} else if (upb_FieldDef_IsSubMessage(f)) {
|
563
591
|
if (TYPE(val) == T_HASH) {
|
564
|
-
|
565
|
-
Message_InitFromValue(submsg,
|
592
|
+
upb_Message* submsg = upb_Message_Mutable(msg, f, arena).msg;
|
593
|
+
Message_InitFromValue(submsg, upb_FieldDef_MessageSubDef(f), val, arena);
|
566
594
|
} else {
|
567
595
|
Message_setfield(msg, f, val, arena);
|
568
596
|
}
|
569
597
|
} else {
|
570
|
-
|
571
|
-
Convert_RubyToUpb(val,
|
572
|
-
|
598
|
+
upb_MessageValue msgval =
|
599
|
+
Convert_RubyToUpb(val, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
|
600
|
+
upb_Message_Set(msg, f, msgval, arena);
|
573
601
|
}
|
574
602
|
}
|
575
603
|
|
576
604
|
typedef struct {
|
577
|
-
|
578
|
-
const
|
579
|
-
|
605
|
+
upb_Message* msg;
|
606
|
+
const upb_MessageDef* msgdef;
|
607
|
+
upb_Arena* arena;
|
580
608
|
} MsgInit;
|
581
609
|
|
582
610
|
static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
583
|
-
MsgInit
|
584
|
-
const char
|
611
|
+
MsgInit* msg_init = (MsgInit*)_self;
|
612
|
+
const char* name;
|
585
613
|
|
586
614
|
if (TYPE(key) == T_STRING) {
|
587
615
|
name = RSTRING_PTR(key);
|
@@ -589,10 +617,12 @@ static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
589
617
|
name = RSTRING_PTR(rb_id2str(SYM2ID(key)));
|
590
618
|
} else {
|
591
619
|
rb_raise(rb_eArgError,
|
592
|
-
"Expected string or symbols as hash keys when initializing proto
|
620
|
+
"Expected string or symbols as hash keys when initializing proto "
|
621
|
+
"from hash.");
|
593
622
|
}
|
594
623
|
|
595
|
-
const
|
624
|
+
const upb_FieldDef* f =
|
625
|
+
upb_MessageDef_FindFieldByName(msg_init->msgdef, name);
|
596
626
|
|
597
627
|
if (f == NULL) {
|
598
628
|
rb_raise(rb_eArgError,
|
@@ -603,8 +633,8 @@ static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
603
633
|
return ST_CONTINUE;
|
604
634
|
}
|
605
635
|
|
606
|
-
void Message_InitFromValue(
|
607
|
-
|
636
|
+
void Message_InitFromValue(upb_Message* msg, const upb_MessageDef* m, VALUE val,
|
637
|
+
upb_Arena* arena) {
|
608
638
|
MsgInit msg_init = {msg, m, arena};
|
609
639
|
if (TYPE(val) == T_HASH) {
|
610
640
|
rb_hash_foreach(val, Message_initialize_kwarg, (VALUE)&msg_init);
|
@@ -629,8 +659,8 @@ void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
|
|
629
659
|
static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
630
660
|
Message* self = ruby_to_Message(_self);
|
631
661
|
VALUE arena_rb = Arena_new();
|
632
|
-
|
633
|
-
|
662
|
+
upb_Arena* arena = Arena_get(arena_rb);
|
663
|
+
upb_Message* msg = upb_Message_New(self->msgdef, arena);
|
634
664
|
|
635
665
|
Message_InitPtr(_self, msg, arena_rb);
|
636
666
|
|
@@ -640,7 +670,7 @@ static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
640
670
|
if (argc != 1) {
|
641
671
|
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
642
672
|
}
|
643
|
-
Message_InitFromValue((
|
673
|
+
Message_InitFromValue((upb_Message*)self->msg, self->msgdef, argv[0], arena);
|
644
674
|
return Qnil;
|
645
675
|
}
|
646
676
|
|
@@ -654,34 +684,35 @@ static VALUE Message_dup(VALUE _self) {
|
|
654
684
|
Message* self = ruby_to_Message(_self);
|
655
685
|
VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
656
686
|
Message* new_msg_self = ruby_to_Message(new_msg);
|
657
|
-
size_t size =
|
687
|
+
size_t size = upb_MessageDef_MiniTable(self->msgdef)->size;
|
658
688
|
|
659
689
|
// TODO(copy unknown fields?)
|
660
690
|
// TODO(use official upb msg copy function)
|
661
|
-
memcpy((
|
691
|
+
memcpy((upb_Message*)new_msg_self->msg, self->msg, size);
|
662
692
|
Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
|
663
693
|
return new_msg;
|
664
694
|
}
|
665
695
|
|
666
696
|
// Support function for Message_eq, and also used by other #eq functions.
|
667
|
-
bool Message_Equal(const
|
697
|
+
bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
|
698
|
+
const upb_MessageDef* m) {
|
668
699
|
if (m1 == m2) return true;
|
669
700
|
|
670
701
|
size_t size1, size2;
|
671
|
-
int encode_opts =
|
672
|
-
|
673
|
-
const
|
702
|
+
int encode_opts = kUpb_Encode_SkipUnknown | kUpb_Encode_Deterministic;
|
703
|
+
upb_Arena* arena_tmp = upb_Arena_New();
|
704
|
+
const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
|
674
705
|
|
675
706
|
// Compare deterministically serialized payloads with no unknown fields.
|
676
|
-
char
|
677
|
-
char
|
707
|
+
char* data1 = upb_Encode(m1, layout, encode_opts, arena_tmp, &size1);
|
708
|
+
char* data2 = upb_Encode(m2, layout, encode_opts, arena_tmp, &size2);
|
678
709
|
|
679
710
|
if (data1 && data2) {
|
680
711
|
bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
|
681
|
-
|
712
|
+
upb_Arena_Free(arena_tmp);
|
682
713
|
return ret;
|
683
714
|
} else {
|
684
|
-
|
715
|
+
upb_Arena_Free(arena_tmp);
|
685
716
|
rb_raise(cParseError, "Error comparing messages");
|
686
717
|
}
|
687
718
|
}
|
@@ -705,22 +736,23 @@ static VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
705
736
|
return Message_Equal(self->msg, other->msg, self->msgdef) ? Qtrue : Qfalse;
|
706
737
|
}
|
707
738
|
|
708
|
-
uint64_t Message_Hash(const
|
709
|
-
|
710
|
-
|
739
|
+
uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
|
740
|
+
uint64_t seed) {
|
741
|
+
upb_Arena* arena = upb_Arena_New();
|
742
|
+
const char* data;
|
711
743
|
size_t size;
|
712
744
|
|
713
745
|
// Hash a deterministically serialized payloads with no unknown fields.
|
714
|
-
data =
|
715
|
-
|
716
|
-
|
746
|
+
data = upb_Encode(msg, upb_MessageDef_MiniTable(m),
|
747
|
+
kUpb_Encode_SkipUnknown | kUpb_Encode_Deterministic, arena,
|
748
|
+
&size);
|
717
749
|
|
718
750
|
if (data) {
|
719
751
|
uint64_t ret = Wyhash(data, size, seed, kWyhashSalt);
|
720
|
-
|
752
|
+
upb_Arena_Free(arena);
|
721
753
|
return ret;
|
722
754
|
} else {
|
723
|
-
|
755
|
+
upb_Arena_Free(arena);
|
724
756
|
rb_raise(cParseError, "Error calculating hash");
|
725
757
|
}
|
726
758
|
}
|
@@ -759,13 +791,13 @@ static VALUE Message_inspect(VALUE _self) {
|
|
759
791
|
|
760
792
|
// Support functions for Message_to_h //////////////////////////////////////////
|
761
793
|
|
762
|
-
static VALUE RepeatedField_CreateArray(const
|
794
|
+
static VALUE RepeatedField_CreateArray(const upb_Array* arr,
|
763
795
|
TypeInfo type_info) {
|
764
|
-
int size = arr ?
|
796
|
+
int size = arr ? upb_Array_Size(arr) : 0;
|
765
797
|
VALUE ary = rb_ary_new2(size);
|
766
798
|
|
767
799
|
for (int i = 0; i < size; i++) {
|
768
|
-
|
800
|
+
upb_MessageValue msgval = upb_Array_Get(arr, i);
|
769
801
|
VALUE val = Scalar_CreateHash(msgval, type_info);
|
770
802
|
rb_ary_push(ary, val);
|
771
803
|
}
|
@@ -773,54 +805,57 @@ static VALUE RepeatedField_CreateArray(const upb_array* arr,
|
|
773
805
|
return ary;
|
774
806
|
}
|
775
807
|
|
776
|
-
static VALUE Message_CreateHash(const
|
808
|
+
static VALUE Message_CreateHash(const upb_Message* msg,
|
809
|
+
const upb_MessageDef* m) {
|
777
810
|
if (!msg) return Qnil;
|
778
811
|
|
779
812
|
VALUE hash = rb_hash_new();
|
780
|
-
int n =
|
813
|
+
int n = upb_MessageDef_FieldCount(m);
|
781
814
|
bool is_proto2;
|
782
815
|
|
783
816
|
// We currently have a few behaviors that are specific to proto2.
|
784
817
|
// This is unfortunate, we should key behaviors off field attributes (like
|
785
818
|
// whether a field has presence), not proto2 vs. proto3. We should see if we
|
786
819
|
// can change this without breaking users.
|
787
|
-
is_proto2 =
|
820
|
+
is_proto2 = upb_MessageDef_Syntax(m) == kUpb_Syntax_Proto2;
|
788
821
|
|
789
822
|
for (int i = 0; i < n; i++) {
|
790
|
-
const
|
823
|
+
const upb_FieldDef* field = upb_MessageDef_Field(m, i);
|
791
824
|
TypeInfo type_info = TypeInfo_get(field);
|
792
|
-
|
825
|
+
upb_MessageValue msgval;
|
793
826
|
VALUE msg_value;
|
794
827
|
VALUE msg_key;
|
795
828
|
|
796
|
-
if (!is_proto2 &&
|
797
|
-
!
|
829
|
+
if (!is_proto2 && upb_FieldDef_IsSubMessage(field) &&
|
830
|
+
!upb_FieldDef_IsRepeated(field) && !upb_Message_Has(msg, field)) {
|
798
831
|
// TODO: Legacy behavior, remove when we fix the is_proto2 differences.
|
799
|
-
msg_key = ID2SYM(rb_intern(
|
832
|
+
msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
800
833
|
rb_hash_aset(hash, msg_key, Qnil);
|
801
834
|
continue;
|
802
835
|
}
|
803
836
|
|
804
837
|
// Do not include fields that are not present (oneof or optional fields).
|
805
|
-
if (is_proto2 &&
|
806
|
-
!
|
838
|
+
if (is_proto2 && upb_FieldDef_HasPresence(field) &&
|
839
|
+
!upb_Message_Has(msg, field)) {
|
807
840
|
continue;
|
808
841
|
}
|
809
842
|
|
810
|
-
msg_key = ID2SYM(rb_intern(
|
811
|
-
msgval =
|
843
|
+
msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
844
|
+
msgval = upb_Message_Get(msg, field);
|
812
845
|
|
813
846
|
// Proto2 omits empty map/repeated filds also.
|
814
847
|
|
815
|
-
if (
|
816
|
-
const
|
817
|
-
const
|
818
|
-
|
819
|
-
|
848
|
+
if (upb_FieldDef_IsMap(field)) {
|
849
|
+
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(field);
|
850
|
+
const upb_FieldDef* key_f =
|
851
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 1);
|
852
|
+
const upb_FieldDef* val_f =
|
853
|
+
upb_MessageDef_FindFieldByNumberWithSize(entry_m, 2);
|
854
|
+
upb_CType key_type = upb_FieldDef_CType(key_f);
|
820
855
|
msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
|
821
|
-
} else if (
|
856
|
+
} else if (upb_FieldDef_IsRepeated(field)) {
|
822
857
|
if (is_proto2 &&
|
823
|
-
(!msgval.array_val ||
|
858
|
+
(!msgval.array_val || upb_Array_Size(msgval.array_val) == 0)) {
|
824
859
|
continue;
|
825
860
|
}
|
826
861
|
msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
|
@@ -834,8 +869,8 @@ static VALUE Message_CreateHash(const upb_msg *msg, const upb_msgdef *m) {
|
|
834
869
|
return hash;
|
835
870
|
}
|
836
871
|
|
837
|
-
VALUE Scalar_CreateHash(
|
838
|
-
if (type_info.type ==
|
872
|
+
VALUE Scalar_CreateHash(upb_MessageValue msgval, TypeInfo type_info) {
|
873
|
+
if (type_info.type == kUpb_CType_Message) {
|
839
874
|
return Message_CreateHash(msgval.msg_val, type_info.def.msgdef);
|
840
875
|
} else {
|
841
876
|
return Convert_UpbToRuby(msgval, type_info, Qnil);
|
@@ -878,10 +913,10 @@ static VALUE Message_freeze(VALUE _self) {
|
|
878
913
|
*/
|
879
914
|
static VALUE Message_index(VALUE _self, VALUE field_name) {
|
880
915
|
Message* self = ruby_to_Message(_self);
|
881
|
-
const
|
916
|
+
const upb_FieldDef* field;
|
882
917
|
|
883
918
|
Check_Type(field_name, T_STRING);
|
884
|
-
field =
|
919
|
+
field = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
|
885
920
|
|
886
921
|
if (field == NULL) {
|
887
922
|
return Qnil;
|
@@ -899,32 +934,54 @@ static VALUE Message_index(VALUE _self, VALUE field_name) {
|
|
899
934
|
*/
|
900
935
|
static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
901
936
|
Message* self = ruby_to_Message(_self);
|
902
|
-
const
|
903
|
-
|
904
|
-
|
937
|
+
const upb_FieldDef* f;
|
938
|
+
upb_MessageValue val;
|
939
|
+
upb_Arena* arena = Arena_get(self->arena);
|
905
940
|
|
906
941
|
Check_Type(field_name, T_STRING);
|
907
|
-
f =
|
942
|
+
f = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
|
908
943
|
|
909
944
|
if (f == NULL) {
|
910
945
|
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
911
946
|
}
|
912
947
|
|
913
|
-
val = Convert_RubyToUpb(value,
|
914
|
-
|
948
|
+
val = Convert_RubyToUpb(value, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
|
949
|
+
upb_Message_Set(Message_GetMutable(_self, NULL), f, val, arena);
|
915
950
|
|
916
951
|
return Qnil;
|
917
952
|
}
|
918
953
|
|
919
954
|
/*
|
920
955
|
* call-seq:
|
921
|
-
* MessageClass.decode(data) => message
|
956
|
+
* MessageClass.decode(data, options) => message
|
922
957
|
*
|
923
958
|
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
924
959
|
* format) under the interpretration given by this message class's definition
|
925
960
|
* and returns a message object with the corresponding field values.
|
961
|
+
* @param options [Hash] options for the decoder
|
962
|
+
* recursion_limit: set to maximum decoding depth for message (default is 64)
|
926
963
|
*/
|
927
|
-
static VALUE Message_decode(VALUE
|
964
|
+
static VALUE Message_decode(int argc, VALUE* argv, VALUE klass) {
|
965
|
+
VALUE data = argv[0];
|
966
|
+
int options = 0;
|
967
|
+
|
968
|
+
if (argc < 1 || argc > 2) {
|
969
|
+
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
970
|
+
}
|
971
|
+
|
972
|
+
if (argc == 2) {
|
973
|
+
VALUE hash_args = argv[1];
|
974
|
+
if (TYPE(hash_args) != T_HASH) {
|
975
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
976
|
+
}
|
977
|
+
|
978
|
+
VALUE depth = rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
979
|
+
|
980
|
+
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
981
|
+
options |= UPB_DECODE_MAXDEPTH(FIX2INT(depth));
|
982
|
+
}
|
983
|
+
}
|
984
|
+
|
928
985
|
if (TYPE(data) != T_STRING) {
|
929
986
|
rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
|
930
987
|
}
|
@@ -932,9 +989,11 @@ static VALUE Message_decode(VALUE klass, VALUE data) {
|
|
932
989
|
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
933
990
|
Message* msg = ruby_to_Message(msg_rb);
|
934
991
|
|
935
|
-
|
936
|
-
|
937
|
-
|
992
|
+
upb_DecodeStatus status = upb_Decode(
|
993
|
+
RSTRING_PTR(data), RSTRING_LEN(data), (upb_Message*)msg->msg,
|
994
|
+
upb_MessageDef_MiniTable(msg->msgdef), NULL, options, Arena_get(msg->arena));
|
995
|
+
|
996
|
+
if (status != kUpb_DecodeStatus_Ok) {
|
938
997
|
rb_raise(cParseError, "Error occurred during parsing");
|
939
998
|
}
|
940
999
|
|
@@ -956,10 +1015,10 @@ static VALUE Message_decode(VALUE klass, VALUE data) {
|
|
956
1015
|
static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
957
1016
|
VALUE data = argv[0];
|
958
1017
|
int options = 0;
|
959
|
-
|
1018
|
+
upb_Status status;
|
960
1019
|
|
961
1020
|
// TODO(haberman): use this message's pool instead.
|
962
|
-
const
|
1021
|
+
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
963
1022
|
|
964
1023
|
if (argc < 1 || argc > 2) {
|
965
1024
|
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
@@ -971,8 +1030,9 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
971
1030
|
rb_raise(rb_eArgError, "Expected hash arguments.");
|
972
1031
|
}
|
973
1032
|
|
974
|
-
if (RTEST(rb_hash_lookup2(
|
975
|
-
|
1033
|
+
if (RTEST(rb_hash_lookup2(
|
1034
|
+
hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse))) {
|
1035
|
+
options |= upb_JsonDecode_IgnoreUnknown;
|
976
1036
|
}
|
977
1037
|
}
|
978
1038
|
|
@@ -988,16 +1048,16 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
988
1048
|
Message* msg = ruby_to_Message(msg_rb);
|
989
1049
|
|
990
1050
|
// We don't allow users to decode a wrapper type directly.
|
991
|
-
if (
|
1051
|
+
if (IsWrapper(msg->msgdef)) {
|
992
1052
|
rb_raise(rb_eRuntimeError, "Cannot parse a wrapper directly.");
|
993
1053
|
}
|
994
1054
|
|
995
|
-
|
996
|
-
if (!
|
997
|
-
|
998
|
-
|
1055
|
+
upb_Status_Clear(&status);
|
1056
|
+
if (!upb_JsonDecode(RSTRING_PTR(data), RSTRING_LEN(data),
|
1057
|
+
(upb_Message*)msg->msg, msg->msgdef, symtab, options,
|
1058
|
+
Arena_get(msg->arena), &status)) {
|
999
1059
|
rb_raise(cParseError, "Error occurred during parsing: %s",
|
1000
|
-
|
1060
|
+
upb_Status_ErrorMessage(&status));
|
1001
1061
|
}
|
1002
1062
|
|
1003
1063
|
return msg_rb;
|
@@ -1005,32 +1065,51 @@ static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1005
1065
|
|
1006
1066
|
/*
|
1007
1067
|
* call-seq:
|
1008
|
-
* MessageClass.encode(msg) => bytes
|
1068
|
+
* MessageClass.encode(msg, options) => bytes
|
1009
1069
|
*
|
1010
1070
|
* Encodes the given message object to its serialized form in protocol buffers
|
1011
1071
|
* wire format.
|
1072
|
+
* @param options [Hash] options for the encoder
|
1073
|
+
* recursion_limit: set to maximum encoding depth for message (default is 64)
|
1012
1074
|
*/
|
1013
|
-
static VALUE Message_encode(VALUE
|
1014
|
-
Message* msg = ruby_to_Message(
|
1015
|
-
|
1075
|
+
static VALUE Message_encode(int argc, VALUE* argv, VALUE klass) {
|
1076
|
+
Message* msg = ruby_to_Message(argv[0]);
|
1077
|
+
int options = 0;
|
1078
|
+
const char* data;
|
1016
1079
|
size_t size;
|
1017
1080
|
|
1018
|
-
if (CLASS_OF(
|
1081
|
+
if (CLASS_OF(argv[0]) != klass) {
|
1019
1082
|
rb_raise(rb_eArgError, "Message of wrong type.");
|
1020
1083
|
}
|
1021
1084
|
|
1022
|
-
|
1085
|
+
if (argc < 1 || argc > 2) {
|
1086
|
+
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
1087
|
+
}
|
1023
1088
|
|
1024
|
-
|
1025
|
-
|
1089
|
+
if (argc == 2) {
|
1090
|
+
VALUE hash_args = argv[1];
|
1091
|
+
if (TYPE(hash_args) != T_HASH) {
|
1092
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
1093
|
+
}
|
1094
|
+
VALUE depth = rb_hash_lookup(hash_args, ID2SYM(rb_intern("recursion_limit")));
|
1095
|
+
|
1096
|
+
if (depth != Qnil && TYPE(depth) == T_FIXNUM) {
|
1097
|
+
options |= UPB_DECODE_MAXDEPTH(FIX2INT(depth));
|
1098
|
+
}
|
1099
|
+
}
|
1100
|
+
|
1101
|
+
upb_Arena *arena = upb_Arena_New();
|
1102
|
+
|
1103
|
+
data = upb_Encode(msg->msg, upb_MessageDef_MiniTable(msg->msgdef),
|
1104
|
+
options, arena, &size);
|
1026
1105
|
|
1027
1106
|
if (data) {
|
1028
1107
|
VALUE ret = rb_str_new(data, size);
|
1029
1108
|
rb_enc_associate(ret, rb_ascii8bit_encoding());
|
1030
|
-
|
1109
|
+
upb_Arena_Free(arena);
|
1031
1110
|
return ret;
|
1032
1111
|
} else {
|
1033
|
-
|
1112
|
+
upb_Arena_Free(arena);
|
1034
1113
|
rb_raise(rb_eRuntimeError, "Exceeded maximum depth (possibly cycle)");
|
1035
1114
|
}
|
1036
1115
|
}
|
@@ -1041,18 +1120,19 @@ static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
|
1041
1120
|
*
|
1042
1121
|
* Encodes the given message object into its serialized JSON representation.
|
1043
1122
|
* @param options [Hash] options for the decoder
|
1044
|
-
* preserve_proto_fieldnames: set true to use original fieldnames (default is
|
1045
|
-
*
|
1123
|
+
* preserve_proto_fieldnames: set true to use original fieldnames (default is
|
1124
|
+
* to camelCase) emit_defaults: set true to emit 0/false values (default is to
|
1125
|
+
* omit them)
|
1046
1126
|
*/
|
1047
1127
|
static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
1048
1128
|
Message* msg = ruby_to_Message(argv[0]);
|
1049
1129
|
int options = 0;
|
1050
1130
|
char buf[1024];
|
1051
1131
|
size_t size;
|
1052
|
-
|
1132
|
+
upb_Status status;
|
1053
1133
|
|
1054
1134
|
// TODO(haberman): use this message's pool instead.
|
1055
|
-
const
|
1135
|
+
const upb_DefPool* symtab = DescriptorPool_GetSymtab(generated_pool);
|
1056
1136
|
|
1057
1137
|
if (argc < 1 || argc > 2) {
|
1058
1138
|
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
@@ -1067,29 +1147,29 @@ static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
|
1067
1147
|
if (RTEST(rb_hash_lookup2(hash_args,
|
1068
1148
|
ID2SYM(rb_intern("preserve_proto_fieldnames")),
|
1069
1149
|
Qfalse))) {
|
1070
|
-
options |=
|
1150
|
+
options |= upb_JsonEncode_UseProtoNames;
|
1071
1151
|
}
|
1072
1152
|
|
1073
1153
|
if (RTEST(rb_hash_lookup2(hash_args, ID2SYM(rb_intern("emit_defaults")),
|
1074
1154
|
Qfalse))) {
|
1075
|
-
options |=
|
1155
|
+
options |= upb_JsonEncode_EmitDefaults;
|
1076
1156
|
}
|
1077
1157
|
}
|
1078
1158
|
|
1079
|
-
|
1080
|
-
size =
|
1081
|
-
|
1159
|
+
upb_Status_Clear(&status);
|
1160
|
+
size = upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf,
|
1161
|
+
sizeof(buf), &status);
|
1082
1162
|
|
1083
|
-
if (!
|
1163
|
+
if (!upb_Status_IsOk(&status)) {
|
1084
1164
|
rb_raise(cParseError, "Error occurred during encoding: %s",
|
1085
|
-
|
1165
|
+
upb_Status_ErrorMessage(&status));
|
1086
1166
|
}
|
1087
1167
|
|
1088
1168
|
VALUE ret;
|
1089
1169
|
if (size >= sizeof(buf)) {
|
1090
1170
|
char* buf2 = malloc(size + 1);
|
1091
|
-
|
1092
|
-
|
1171
|
+
upb_JsonEncode(msg->msg, msg->msgdef, symtab, options, buf2, size + 1,
|
1172
|
+
&status);
|
1093
1173
|
ret = rb_str_new(buf2, size);
|
1094
1174
|
free(buf2);
|
1095
1175
|
} else {
|
@@ -1112,10 +1192,10 @@ static VALUE Message_descriptor(VALUE klass) {
|
|
1112
1192
|
}
|
1113
1193
|
|
1114
1194
|
VALUE build_class_from_descriptor(VALUE descriptor) {
|
1115
|
-
const char
|
1195
|
+
const char* name;
|
1116
1196
|
VALUE klass;
|
1117
1197
|
|
1118
|
-
name =
|
1198
|
+
name = upb_MessageDef_FullName(Descriptor_GetMsgDef(descriptor));
|
1119
1199
|
if (name == NULL) {
|
1120
1200
|
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
1121
1201
|
}
|
@@ -1123,8 +1203,7 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
1123
1203
|
klass = rb_define_class_id(
|
1124
1204
|
// Docs say this parameter is ignored. User will assign return value to
|
1125
1205
|
// their own toplevel constant class name.
|
1126
|
-
rb_intern("Message"),
|
1127
|
-
rb_cObject);
|
1206
|
+
rb_intern("Message"), rb_cObject);
|
1128
1207
|
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
|
1129
1208
|
rb_define_alloc_func(klass, Message_alloc);
|
1130
1209
|
rb_require("google/protobuf/message_exts");
|
@@ -1132,10 +1211,9 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
1132
1211
|
rb_extend_object(
|
1133
1212
|
klass, rb_eval_string("::Google::Protobuf::MessageExts::ClassMethods"));
|
1134
1213
|
|
1135
|
-
rb_define_method(klass, "method_missing",
|
1136
|
-
|
1137
|
-
|
1138
|
-
Message_respond_to_missing, -1);
|
1214
|
+
rb_define_method(klass, "method_missing", Message_method_missing, -1);
|
1215
|
+
rb_define_method(klass, "respond_to_missing?", Message_respond_to_missing,
|
1216
|
+
-1);
|
1139
1217
|
rb_define_method(klass, "initialize", Message_initialize, -1);
|
1140
1218
|
rb_define_method(klass, "dup", Message_dup, 0);
|
1141
1219
|
// Also define #clone so that we don't inherit Object#clone.
|
@@ -1149,8 +1227,8 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
1149
1227
|
rb_define_method(klass, "to_s", Message_inspect, 0);
|
1150
1228
|
rb_define_method(klass, "[]", Message_index, 1);
|
1151
1229
|
rb_define_method(klass, "[]=", Message_index_set, 2);
|
1152
|
-
rb_define_singleton_method(klass, "decode", Message_decode, 1);
|
1153
|
-
rb_define_singleton_method(klass, "encode", Message_encode, 1);
|
1230
|
+
rb_define_singleton_method(klass, "decode", Message_decode, -1);
|
1231
|
+
rb_define_singleton_method(klass, "encode", Message_encode, -1);
|
1154
1232
|
rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
|
1155
1233
|
rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
|
1156
1234
|
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
|
@@ -1168,13 +1246,12 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
1168
1246
|
static VALUE enum_lookup(VALUE self, VALUE number) {
|
1169
1247
|
int32_t num = NUM2INT(number);
|
1170
1248
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
1171
|
-
const
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
return Qnil;
|
1249
|
+
const upb_EnumDef* e = EnumDescriptor_GetEnumDef(desc);
|
1250
|
+
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e, num);
|
1251
|
+
if (ev) {
|
1252
|
+
return ID2SYM(rb_intern(upb_EnumValueDef_Name(ev)));
|
1176
1253
|
} else {
|
1177
|
-
return
|
1254
|
+
return Qnil;
|
1178
1255
|
}
|
1179
1256
|
}
|
1180
1257
|
|
@@ -1188,14 +1265,12 @@ static VALUE enum_lookup(VALUE self, VALUE number) {
|
|
1188
1265
|
static VALUE enum_resolve(VALUE self, VALUE sym) {
|
1189
1266
|
const char* name = rb_id2name(SYM2ID(sym));
|
1190
1267
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
1191
|
-
const
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
if (!found) {
|
1196
|
-
return Qnil;
|
1268
|
+
const upb_EnumDef* e = EnumDescriptor_GetEnumDef(desc);
|
1269
|
+
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByName(e, name);
|
1270
|
+
if (ev) {
|
1271
|
+
return INT2NUM(upb_EnumValueDef_Number(ev));
|
1197
1272
|
} else {
|
1198
|
-
return
|
1273
|
+
return Qnil;
|
1199
1274
|
}
|
1200
1275
|
}
|
1201
1276
|
|
@@ -1211,19 +1286,19 @@ static VALUE enum_descriptor(VALUE self) {
|
|
1211
1286
|
}
|
1212
1287
|
|
1213
1288
|
VALUE build_module_from_enumdesc(VALUE _enumdesc) {
|
1214
|
-
const
|
1215
|
-
VALUE mod = rb_define_module_id(rb_intern(
|
1216
|
-
|
1217
|
-
|
1218
|
-
for (
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
int32_t value = upb_enum_iter_number(&it);
|
1289
|
+
const upb_EnumDef* e = EnumDescriptor_GetEnumDef(_enumdesc);
|
1290
|
+
VALUE mod = rb_define_module_id(rb_intern(upb_EnumDef_FullName(e)));
|
1291
|
+
|
1292
|
+
int n = upb_EnumDef_ValueCount(e);
|
1293
|
+
for (int i = 0; i < n; i++) {
|
1294
|
+
const upb_EnumValueDef* ev = upb_EnumDef_Value(e, i);
|
1295
|
+
const char* name = upb_EnumValueDef_Name(ev);
|
1296
|
+
int32_t value = upb_EnumValueDef_Number(ev);
|
1223
1297
|
if (name[0] < 'A' || name[0] > 'Z') {
|
1224
|
-
rb_warn(
|
1225
|
-
|
1226
|
-
|
1298
|
+
rb_warn(
|
1299
|
+
"Enum value '%s' does not start with an uppercase letter "
|
1300
|
+
"as is required for Ruby constants.",
|
1301
|
+
name);
|
1227
1302
|
}
|
1228
1303
|
rb_define_const(mod, name, INT2NUM(value));
|
1229
1304
|
}
|
@@ -1237,80 +1312,84 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
|
|
1237
1312
|
}
|
1238
1313
|
|
1239
1314
|
// Internal only; used by Google::Protobuf.deep_copy.
|
1240
|
-
|
1241
|
-
|
1315
|
+
upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
|
1316
|
+
upb_Arena* arena) {
|
1242
1317
|
// Serialize and parse.
|
1243
|
-
|
1244
|
-
const
|
1318
|
+
upb_Arena* tmp_arena = upb_Arena_New();
|
1319
|
+
const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
|
1245
1320
|
size_t size;
|
1246
1321
|
|
1247
|
-
char* data =
|
1248
|
-
|
1322
|
+
char* data = upb_Encode(msg, layout, 0, tmp_arena, &size);
|
1323
|
+
upb_Message* new_msg = upb_Message_New(m, arena);
|
1249
1324
|
|
1250
|
-
if (!data ||
|
1251
|
-
|
1325
|
+
if (!data || upb_Decode(data, size, new_msg, layout, NULL, 0, arena) !=
|
1326
|
+
kUpb_DecodeStatus_Ok) {
|
1327
|
+
upb_Arena_Free(tmp_arena);
|
1252
1328
|
rb_raise(cParseError, "Error occurred copying proto");
|
1253
1329
|
}
|
1254
1330
|
|
1255
|
-
|
1331
|
+
upb_Arena_Free(tmp_arena);
|
1256
1332
|
return new_msg;
|
1257
1333
|
}
|
1258
1334
|
|
1259
|
-
const
|
1260
|
-
|
1335
|
+
const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
|
1336
|
+
const char* name, upb_Arena* arena) {
|
1261
1337
|
if (value == Qnil) {
|
1262
1338
|
rb_raise(cTypeError, "nil message not allowed here.");
|
1263
1339
|
}
|
1264
1340
|
|
1265
1341
|
VALUE klass = CLASS_OF(value);
|
1266
1342
|
VALUE desc_rb = rb_ivar_get(klass, descriptor_instancevar_interned);
|
1267
|
-
const
|
1343
|
+
const upb_MessageDef* val_m =
|
1268
1344
|
desc_rb == Qnil ? NULL : Descriptor_GetMsgDef(desc_rb);
|
1269
1345
|
|
1270
1346
|
if (val_m != m) {
|
1271
1347
|
// Check for possible implicit conversions
|
1272
1348
|
// TODO: hash conversion?
|
1273
1349
|
|
1274
|
-
switch (
|
1275
|
-
case
|
1350
|
+
switch (upb_MessageDef_WellKnownType(m)) {
|
1351
|
+
case kUpb_WellKnown_Timestamp: {
|
1276
1352
|
// Time -> Google::Protobuf::Timestamp
|
1277
|
-
|
1278
|
-
|
1353
|
+
upb_Message* msg = upb_Message_New(m, arena);
|
1354
|
+
upb_MessageValue sec, nsec;
|
1279
1355
|
struct timespec time;
|
1280
|
-
const
|
1281
|
-
|
1356
|
+
const upb_FieldDef* sec_f =
|
1357
|
+
upb_MessageDef_FindFieldByNumberWithSize(m, 1);
|
1358
|
+
const upb_FieldDef* nsec_f =
|
1359
|
+
upb_MessageDef_FindFieldByNumberWithSize(m, 2);
|
1282
1360
|
|
1283
1361
|
if (!rb_obj_is_kind_of(value, rb_cTime)) goto badtype;
|
1284
1362
|
|
1285
1363
|
time = rb_time_timespec(value);
|
1286
1364
|
sec.int64_val = time.tv_sec;
|
1287
1365
|
nsec.int32_val = time.tv_nsec;
|
1288
|
-
|
1289
|
-
|
1366
|
+
upb_Message_Set(msg, sec_f, sec, arena);
|
1367
|
+
upb_Message_Set(msg, nsec_f, nsec, arena);
|
1290
1368
|
return msg;
|
1291
1369
|
}
|
1292
|
-
case
|
1370
|
+
case kUpb_WellKnown_Duration: {
|
1293
1371
|
// Numeric -> Google::Protobuf::Duration
|
1294
|
-
|
1295
|
-
|
1296
|
-
const
|
1297
|
-
|
1372
|
+
upb_Message* msg = upb_Message_New(m, arena);
|
1373
|
+
upb_MessageValue sec, nsec;
|
1374
|
+
const upb_FieldDef* sec_f =
|
1375
|
+
upb_MessageDef_FindFieldByNumberWithSize(m, 1);
|
1376
|
+
const upb_FieldDef* nsec_f =
|
1377
|
+
upb_MessageDef_FindFieldByNumberWithSize(m, 2);
|
1298
1378
|
|
1299
1379
|
if (!rb_obj_is_kind_of(value, rb_cNumeric)) goto badtype;
|
1300
1380
|
|
1301
1381
|
sec.int64_val = NUM2LL(value);
|
1302
1382
|
nsec.int32_val = round((NUM2DBL(value) - NUM2LL(value)) * 1000000000);
|
1303
|
-
|
1304
|
-
|
1383
|
+
upb_Message_Set(msg, sec_f, sec, arena);
|
1384
|
+
upb_Message_Set(msg, nsec_f, nsec, arena);
|
1305
1385
|
return msg;
|
1306
1386
|
}
|
1307
1387
|
default:
|
1308
1388
|
badtype:
|
1309
1389
|
rb_raise(cTypeError,
|
1310
1390
|
"Invalid type %s to assign to submessage field '%s'.",
|
1311
|
-
|
1391
|
+
rb_class2name(CLASS_OF(value)), name);
|
1312
1392
|
}
|
1313
|
-
|
1314
1393
|
}
|
1315
1394
|
|
1316
1395
|
Message* self = ruby_to_Message(value);
|