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