google-protobuf 3.7.1 → 3.17.3
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 +349 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +1555 -1228
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +5 -7
- data/ext/google/protobuf_c/map.c +312 -470
- data/ext/google/protobuf_c/map.h +67 -0
- data/ext/google/protobuf_c/message.c +942 -348
- data/ext/google/protobuf_c/message.h +101 -0
- data/ext/google/protobuf_c/protobuf.c +400 -51
- data/ext/google/protobuf_c/protobuf.h +47 -545
- data/ext/google/protobuf_c/repeated_field.c +313 -308
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +8858 -0
- data/ext/google/protobuf_c/ruby-upb.h +4411 -0
- data/ext/google/protobuf_c/third_party/wyhash/wyhash.h +145 -0
- data/lib/google/protobuf.rb +70 -0
- data/lib/google/protobuf/any_pb.rb +1 -1
- data/lib/google/protobuf/api_pb.rb +3 -3
- 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/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 +8 -8
- data/lib/google/protobuf/well_known_types.rb +8 -2
- data/lib/google/protobuf/wrappers_pb.rb +9 -9
- data/tests/basic.rb +320 -70
- data/tests/generated_code_test.rb +0 -0
- data/tests/stress.rb +0 -0
- metadata +27 -15
- data/ext/google/protobuf_c/encode_decode.c +0 -1614
- data/ext/google/protobuf_c/storage.c +0 -1032
- data/ext/google/protobuf_c/upb.c +0 -17480
- data/ext/google/protobuf_c/upb.h +0 -10642
@@ -0,0 +1,67 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
// https://developers.google.com/protocol-buffers/
|
4
|
+
//
|
5
|
+
// Redistribution and use in source and binary forms, with or without
|
6
|
+
// modification, are permitted provided that the following conditions are
|
7
|
+
// met:
|
8
|
+
//
|
9
|
+
// * Redistributions of source code must retain the above copyright
|
10
|
+
// notice, this list of conditions and the following disclaimer.
|
11
|
+
// * Redistributions in binary form must reproduce the above
|
12
|
+
// copyright notice, this list of conditions and the following disclaimer
|
13
|
+
// in the documentation and/or other materials provided with the
|
14
|
+
// distribution.
|
15
|
+
// * Neither the name of Google Inc. nor the names of its
|
16
|
+
// contributors may be used to endorse or promote products derived from
|
17
|
+
// this software without specific prior written permission.
|
18
|
+
//
|
19
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
#ifndef RUBY_PROTOBUF_MAP_H_
|
32
|
+
#define RUBY_PROTOBUF_MAP_H_
|
33
|
+
|
34
|
+
#include <ruby/ruby.h>
|
35
|
+
|
36
|
+
#include "protobuf.h"
|
37
|
+
#include "ruby-upb.h"
|
38
|
+
|
39
|
+
// Returns a Ruby wrapper object for the given map, which will be created if
|
40
|
+
// one does not exist already.
|
41
|
+
VALUE Map_GetRubyWrapper(upb_map *map, upb_fieldtype_t key_type,
|
42
|
+
TypeInfo value_type, VALUE arena);
|
43
|
+
|
44
|
+
// Gets the underlying upb_map for this Ruby map object, which must have
|
45
|
+
// key/value type that match |field|. If this is not a map or the type doesn't
|
46
|
+
// match, raises an exception.
|
47
|
+
const upb_map *Map_GetUpbMap(VALUE val, const upb_fielddef *field,
|
48
|
+
upb_arena *arena);
|
49
|
+
|
50
|
+
// Implements #inspect for this map by appending its contents to |b|.
|
51
|
+
void Map_Inspect(StringBuilder *b, const upb_map *map, upb_fieldtype_t key_type,
|
52
|
+
TypeInfo val_type);
|
53
|
+
|
54
|
+
// Returns a new Hash object containing the contents of this Map.
|
55
|
+
VALUE Map_CreateHash(const upb_map* map, upb_fieldtype_t key_type,
|
56
|
+
TypeInfo val_info);
|
57
|
+
|
58
|
+
// Returns a deep copy of this Map object.
|
59
|
+
VALUE Map_deep_copy(VALUE obj);
|
60
|
+
|
61
|
+
// Ruby class of Google::Protobuf::Map.
|
62
|
+
extern VALUE cMap;
|
63
|
+
|
64
|
+
// Call at startup to register all types in this module.
|
65
|
+
void Map_register(VALUE module);
|
66
|
+
|
67
|
+
#endif // RUBY_PROTOBUF_MAP_H_
|
@@ -28,148 +28,363 @@
|
|
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"
|
38
|
+
#include "third_party/wyhash/wyhash.h"
|
32
39
|
|
33
|
-
|
34
|
-
|
35
|
-
// -----------------------------------------------------------------------------
|
40
|
+
static VALUE cParseError = Qnil;
|
41
|
+
static ID descriptor_instancevar_interned;
|
36
42
|
|
37
|
-
|
38
|
-
|
43
|
+
static VALUE initialize_rb_class_with_no_args(VALUE klass) {
|
44
|
+
return rb_funcall(klass, rb_intern("new"), 0);
|
39
45
|
}
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
layout_mark(self->descriptor->layout, Message_data(self));
|
47
|
+
VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
|
48
|
+
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
44
49
|
}
|
45
50
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
// -----------------------------------------------------------------------------
|
52
|
+
// Class/module creation from msgdefs and enumdefs, respectively.
|
53
|
+
// -----------------------------------------------------------------------------
|
54
|
+
|
55
|
+
typedef struct {
|
56
|
+
VALUE arena;
|
57
|
+
const upb_msg* msg; // Can get as mutable when non-frozen.
|
58
|
+
const upb_msgdef* 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 = {
|
66
|
+
static rb_data_type_t Message_type = {
|
56
67
|
"Message",
|
57
|
-
{ Message_mark,
|
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
|
+
}
|
76
92
|
|
77
|
-
|
93
|
+
const upb_msg *Message_Get(VALUE msg_rb, const upb_msgdef **m) {
|
94
|
+
Message* msg = ruby_to_Message(msg_rb);
|
95
|
+
if (m) *m = msg->msgdef;
|
96
|
+
return msg->msg;
|
97
|
+
}
|
78
98
|
|
79
|
-
|
99
|
+
upb_msg *Message_GetMutable(VALUE msg_rb, const upb_msgdef **m) {
|
100
|
+
rb_check_frozen(msg_rb);
|
101
|
+
return (upb_msg*)Message_Get(msg_rb, m);
|
80
102
|
}
|
81
103
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
104
|
+
void Message_InitPtr(VALUE self_, upb_msg *msg, VALUE arena) {
|
105
|
+
Message* self = ruby_to_Message(self_);
|
106
|
+
self->msg = msg;
|
107
|
+
self->arena = arena;
|
108
|
+
ObjectCache_Add(msg, self_);
|
109
|
+
}
|
88
110
|
|
89
|
-
|
90
|
-
|
91
|
-
|
111
|
+
VALUE Message_GetArena(VALUE msg_rb) {
|
112
|
+
Message* msg = ruby_to_Message(msg_rb);
|
113
|
+
return msg->arena;
|
114
|
+
}
|
115
|
+
|
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
|
-
// oneof_case field.
|
95
|
-
upb_oneof_begin(&it, o);
|
96
|
-
assert(!upb_oneof_done(&it));
|
97
|
-
first_field = upb_oneof_iter_field(&it);
|
98
|
-
assert(upb_fielddef_containingoneof(first_field) != NULL);
|
121
|
+
}
|
99
122
|
|
100
|
-
|
101
|
-
|
102
|
-
fields[upb_fielddef_index(first_field)].case_offset;
|
103
|
-
oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
|
123
|
+
VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena) {
|
124
|
+
if (msg == NULL) return Qnil;
|
104
125
|
|
105
|
-
|
106
|
-
|
126
|
+
VALUE val = ObjectCache_Get(msg);
|
127
|
+
|
128
|
+
if (val == Qnil) {
|
129
|
+
VALUE klass = Descriptor_DefToClass(m);
|
130
|
+
val = Message_alloc(klass);
|
131
|
+
Message_InitPtr(val, msg, arena);
|
107
132
|
}
|
108
133
|
|
109
|
-
|
110
|
-
|
111
|
-
|
134
|
+
return val;
|
135
|
+
}
|
136
|
+
|
137
|
+
void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
|
138
|
+
const upb_msgdef* m) {
|
139
|
+
bool first = true;
|
140
|
+
int n = upb_msgdef_fieldcount(m);
|
141
|
+
VALUE klass = Descriptor_DefToClass(m);
|
142
|
+
StringBuilder_Printf(b, "<%s: ", rb_class2name(klass));
|
143
|
+
|
144
|
+
for (int i = 0; i < n; i++) {
|
145
|
+
const upb_fielddef* field = upb_msgdef_field(m, i);
|
146
|
+
|
147
|
+
if (upb_fielddef_haspresence(field) && !upb_msg_has(msg, field)) {
|
148
|
+
continue;
|
149
|
+
}
|
150
|
+
|
151
|
+
if (!first) {
|
152
|
+
StringBuilder_Printf(b, ", ");
|
153
|
+
} else {
|
154
|
+
first = false;
|
155
|
+
}
|
156
|
+
|
157
|
+
upb_msgval msgval = upb_msg_get(msg, field);
|
158
|
+
|
159
|
+
StringBuilder_Printf(b, "%s: ", upb_fielddef_name(field));
|
160
|
+
|
161
|
+
if (upb_fielddef_ismap(field)) {
|
162
|
+
const upb_msgdef* entry_m = upb_fielddef_msgsubdef(field);
|
163
|
+
const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
|
164
|
+
const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
|
165
|
+
TypeInfo val_info = TypeInfo_get(val_f);
|
166
|
+
Map_Inspect(b, msgval.map_val, upb_fielddef_type(key_f), val_info);
|
167
|
+
} else if (upb_fielddef_isseq(field)) {
|
168
|
+
RepeatedField_Inspect(b, msgval.array_val, TypeInfo_get(field));
|
169
|
+
} else {
|
170
|
+
StringBuilder_PrintMsgval(b, msgval, TypeInfo_get(field));
|
171
|
+
}
|
172
|
+
}
|
112
173
|
|
113
|
-
|
174
|
+
StringBuilder_Printf(b, ">");
|
114
175
|
}
|
115
176
|
|
177
|
+
// Helper functions for #method_missing ////////////////////////////////////////
|
178
|
+
|
116
179
|
enum {
|
117
180
|
METHOD_UNKNOWN = 0,
|
118
181
|
METHOD_GETTER = 1,
|
119
182
|
METHOD_SETTER = 2,
|
120
183
|
METHOD_CLEAR = 3,
|
121
|
-
METHOD_PRESENCE = 4
|
184
|
+
METHOD_PRESENCE = 4,
|
185
|
+
METHOD_ENUM_GETTER = 5,
|
186
|
+
METHOD_WRAPPER_GETTER = 6,
|
187
|
+
METHOD_WRAPPER_SETTER = 7
|
122
188
|
};
|
123
189
|
|
124
|
-
|
125
|
-
|
190
|
+
// Check if the field is a well known wrapper type
|
191
|
+
static bool IsWrapper(const upb_fielddef* f) {
|
192
|
+
return upb_fielddef_issubmsg(f) &&
|
193
|
+
upb_msgdef_iswrapper(upb_fielddef_msgsubdef(f));
|
194
|
+
}
|
195
|
+
|
196
|
+
static bool Match(const upb_msgdef* m, const char* name, const upb_fielddef** f,
|
197
|
+
const upb_oneofdef** o, const char* prefix,
|
198
|
+
const char* suffix) {
|
199
|
+
size_t sp = strlen(prefix);
|
200
|
+
size_t ss = strlen(suffix);
|
201
|
+
size_t sn = strlen(name);
|
202
|
+
|
203
|
+
if (sn <= sp + ss) return false;
|
204
|
+
|
205
|
+
if (memcmp(name, prefix, sp) != 0 ||
|
206
|
+
memcmp(name + sn - ss, suffix, ss) != 0) {
|
207
|
+
return false;
|
208
|
+
}
|
209
|
+
|
210
|
+
return upb_msgdef_lookupname(m, name + sp, sn - sp - ss, f, o);
|
211
|
+
}
|
212
|
+
|
213
|
+
static int extract_method_call(VALUE method_name, Message* self,
|
214
|
+
const upb_fielddef** f, const upb_oneofdef** o) {
|
215
|
+
const upb_msgdef* m = self->msgdef;
|
216
|
+
const char* name;
|
217
|
+
|
126
218
|
Check_Type(method_name, T_SYMBOL);
|
219
|
+
name = rb_id2name(SYM2ID(method_name));
|
220
|
+
|
221
|
+
if (Match(m, name, f, o, "", "")) return METHOD_GETTER;
|
222
|
+
if (Match(m, name, f, o, "", "=")) return METHOD_SETTER;
|
223
|
+
if (Match(m, name, f, o, "clear_", "")) return METHOD_CLEAR;
|
224
|
+
if (Match(m, name, f, o, "has_", "?") &&
|
225
|
+
(*o || (*f && upb_fielddef_haspresence(*f)))) {
|
226
|
+
// Disallow oneof hazzers for proto3.
|
227
|
+
// TODO(haberman): remove this test when we are enabling oneof hazzers for
|
228
|
+
// proto3.
|
229
|
+
if (*f && !upb_fielddef_issubmsg(*f) &&
|
230
|
+
upb_fielddef_realcontainingoneof(*f) &&
|
231
|
+
upb_msgdef_syntax(upb_fielddef_containingtype(*f)) !=
|
232
|
+
UPB_SYNTAX_PROTO2) {
|
233
|
+
return METHOD_UNKNOWN;
|
234
|
+
}
|
235
|
+
return METHOD_PRESENCE;
|
236
|
+
}
|
237
|
+
if (Match(m, name, f, o, "", "_as_value") && *f && !upb_fielddef_isseq(*f) &&
|
238
|
+
IsWrapper(*f)) {
|
239
|
+
return METHOD_WRAPPER_GETTER;
|
240
|
+
}
|
241
|
+
if (Match(m, name, f, o, "", "_as_value=") && *f && !upb_fielddef_isseq(*f) &&
|
242
|
+
IsWrapper(*f)) {
|
243
|
+
return METHOD_WRAPPER_SETTER;
|
244
|
+
}
|
245
|
+
if (Match(m, name, f, o, "", "_const") && *f &&
|
246
|
+
upb_fielddef_type(*f) == UPB_TYPE_ENUM) {
|
247
|
+
return METHOD_ENUM_GETTER;
|
248
|
+
}
|
127
249
|
|
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;
|
250
|
+
return METHOD_UNKNOWN;
|
251
|
+
}
|
252
|
+
|
253
|
+
static VALUE Message_oneof_accessor(VALUE _self, const upb_oneofdef* o,
|
254
|
+
int accessor_type) {
|
255
|
+
Message* self = ruby_to_Message(_self);
|
256
|
+
const upb_fielddef* oneof_field = upb_msg_whichoneof(self->msg, o);
|
257
|
+
|
258
|
+
switch (accessor_type) {
|
259
|
+
case METHOD_PRESENCE:
|
260
|
+
return oneof_field == NULL ? Qfalse : Qtrue;
|
261
|
+
case METHOD_CLEAR:
|
262
|
+
if (oneof_field != NULL) {
|
263
|
+
upb_msg_clearfield(Message_GetMutable(_self, NULL), oneof_field);
|
264
|
+
}
|
265
|
+
return Qnil;
|
266
|
+
case METHOD_GETTER:
|
267
|
+
return oneof_field == NULL
|
268
|
+
? Qnil
|
269
|
+
: ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
|
270
|
+
case METHOD_SETTER:
|
271
|
+
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
154
272
|
}
|
273
|
+
rb_raise(rb_eRuntimeError, "Invalid access of oneof field.");
|
274
|
+
}
|
155
275
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
276
|
+
static void Message_setfield(upb_msg* msg, const upb_fielddef* f, VALUE val,
|
277
|
+
upb_arena* arena) {
|
278
|
+
upb_msgval msgval;
|
279
|
+
if (upb_fielddef_ismap(f)) {
|
280
|
+
msgval.map_val = Map_GetUpbMap(val, f, arena);
|
281
|
+
} else if (upb_fielddef_isseq(f)) {
|
282
|
+
msgval.array_val = RepeatedField_GetUpbArray(val, f, arena);
|
283
|
+
} else {
|
284
|
+
if (val == Qnil &&
|
285
|
+
(upb_fielddef_issubmsg(f) || upb_fielddef_realcontainingoneof(f))) {
|
286
|
+
upb_msg_clearfield(msg, f);
|
287
|
+
return;
|
288
|
+
}
|
289
|
+
msgval =
|
290
|
+
Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
160
291
|
}
|
292
|
+
upb_msg_set(msg, f, msgval, arena);
|
293
|
+
}
|
161
294
|
|
162
|
-
|
163
|
-
|
164
|
-
//
|
165
|
-
|
166
|
-
|
167
|
-
|
295
|
+
VALUE Message_getfield(VALUE _self, const upb_fielddef* f) {
|
296
|
+
Message* self = ruby_to_Message(_self);
|
297
|
+
// This is a special-case: upb_msg_mutable() for map & array are logically
|
298
|
+
// const (they will not change what is serialized) but physically
|
299
|
+
// non-const, as they do allocate a repeated field or map. The logical
|
300
|
+
// constness means it's ok to do even if the message is frozen.
|
301
|
+
upb_msg *msg = (upb_msg*)self->msg;
|
302
|
+
upb_arena *arena = Arena_get(self->arena);
|
303
|
+
if (upb_fielddef_ismap(f)) {
|
304
|
+
upb_map *map = upb_msg_mutable(msg, f, arena).map;
|
305
|
+
const upb_fielddef *key_f = map_field_key(f);
|
306
|
+
const upb_fielddef *val_f = map_field_value(f);
|
307
|
+
upb_fieldtype_t key_type = upb_fielddef_type(key_f);
|
308
|
+
TypeInfo value_type_info = TypeInfo_get(val_f);
|
309
|
+
return Map_GetRubyWrapper(map, key_type, value_type_info, self->arena);
|
310
|
+
} else if (upb_fielddef_isseq(f)) {
|
311
|
+
upb_array *arr = upb_msg_mutable(msg, f, arena).array;
|
312
|
+
return RepeatedField_GetRubyWrapper(arr, TypeInfo_get(f), self->arena);
|
313
|
+
} else if (upb_fielddef_issubmsg(f)) {
|
314
|
+
if (!upb_msg_has(self->msg, f)) return Qnil;
|
315
|
+
upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
|
316
|
+
const upb_msgdef *m = upb_fielddef_msgsubdef(f);
|
317
|
+
return Message_GetRubyWrapper(submsg, m, self->arena);
|
318
|
+
} else {
|
319
|
+
upb_msgval msgval = upb_msg_get(self->msg, f);
|
320
|
+
return Convert_UpbToRuby(msgval, TypeInfo_get(f), self->arena);
|
168
321
|
}
|
322
|
+
}
|
323
|
+
|
324
|
+
static VALUE Message_field_accessor(VALUE _self, const upb_fielddef* f,
|
325
|
+
int accessor_type, int argc, VALUE* argv) {
|
326
|
+
upb_arena *arena = Arena_get(Message_GetArena(_self));
|
169
327
|
|
170
|
-
|
171
|
-
|
172
|
-
|
328
|
+
switch (accessor_type) {
|
329
|
+
case METHOD_SETTER:
|
330
|
+
Message_setfield(Message_GetMutable(_self, NULL), f, argv[1], arena);
|
331
|
+
return Qnil;
|
332
|
+
case METHOD_CLEAR:
|
333
|
+
upb_msg_clearfield(Message_GetMutable(_self, NULL), f);
|
334
|
+
return Qnil;
|
335
|
+
case METHOD_PRESENCE:
|
336
|
+
if (!upb_fielddef_haspresence(f)) {
|
337
|
+
rb_raise(rb_eRuntimeError, "Field does not have presence.");
|
338
|
+
}
|
339
|
+
return upb_msg_has(Message_Get(_self, NULL), f);
|
340
|
+
case METHOD_WRAPPER_GETTER: {
|
341
|
+
Message* self = ruby_to_Message(_self);
|
342
|
+
if (upb_msg_has(self->msg, f)) {
|
343
|
+
PBRUBY_ASSERT(upb_fielddef_issubmsg(f) && !upb_fielddef_isseq(f));
|
344
|
+
upb_msgval wrapper = upb_msg_get(self->msg, f);
|
345
|
+
const upb_msgdef *wrapper_m = upb_fielddef_msgsubdef(f);
|
346
|
+
const upb_fielddef *value_f = upb_msgdef_itof(wrapper_m, 1);
|
347
|
+
upb_msgval value = upb_msg_get(wrapper.msg_val, value_f);
|
348
|
+
return Convert_UpbToRuby(value, TypeInfo_get(value_f), self->arena);
|
349
|
+
} else {
|
350
|
+
return Qnil;
|
351
|
+
}
|
352
|
+
}
|
353
|
+
case METHOD_WRAPPER_SETTER: {
|
354
|
+
upb_msg *msg = Message_GetMutable(_self, NULL);
|
355
|
+
if (argv[1] == Qnil) {
|
356
|
+
upb_msg_clearfield(msg, f);
|
357
|
+
} else {
|
358
|
+
const upb_fielddef *val_f = upb_msgdef_itof(upb_fielddef_msgsubdef(f), 1);
|
359
|
+
upb_msgval msgval = Convert_RubyToUpb(argv[1], upb_fielddef_name(f),
|
360
|
+
TypeInfo_get(val_f), arena);
|
361
|
+
upb_msg *wrapper = upb_msg_mutable(msg, f, arena).msg;
|
362
|
+
upb_msg_set(wrapper, val_f, msgval, arena);
|
363
|
+
}
|
364
|
+
return Qnil;
|
365
|
+
}
|
366
|
+
case METHOD_ENUM_GETTER: {
|
367
|
+
upb_msgval msgval = upb_msg_get(Message_Get(_self, NULL), f);
|
368
|
+
|
369
|
+
if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
370
|
+
// Map repeated fields to a new type with ints
|
371
|
+
VALUE arr = rb_ary_new();
|
372
|
+
size_t i, n = upb_array_size(msgval.array_val);
|
373
|
+
for (i = 0; i < n; i++) {
|
374
|
+
upb_msgval elem = upb_array_get(msgval.array_val, i);
|
375
|
+
rb_ary_push(arr, INT2NUM(elem.int32_val));
|
376
|
+
}
|
377
|
+
return arr;
|
378
|
+
} else {
|
379
|
+
return INT2NUM(msgval.int32_val);
|
380
|
+
}
|
381
|
+
}
|
382
|
+
case METHOD_GETTER:
|
383
|
+
return Message_getfield(_self, f);
|
384
|
+
default:
|
385
|
+
rb_raise(rb_eRuntimeError, "Internal error, no such accessor: %d",
|
386
|
+
accessor_type);
|
387
|
+
}
|
173
388
|
}
|
174
389
|
|
175
390
|
/*
|
@@ -199,72 +414,56 @@ static int extract_method_call(VALUE method_name, MessageHeader* self,
|
|
199
414
|
* true if the field 'fieldname' is set in the message object, else false. For
|
200
415
|
* 'proto3' syntax, calling this for a basic type field will result in an error.
|
201
416
|
*/
|
202
|
-
VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
203
|
-
|
417
|
+
static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
418
|
+
Message* self = ruby_to_Message(_self);
|
204
419
|
const upb_oneofdef* o;
|
205
420
|
const upb_fielddef* f;
|
421
|
+
int accessor_type;
|
206
422
|
|
207
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
208
423
|
if (argc < 1) {
|
209
424
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
210
425
|
}
|
211
426
|
|
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
|
-
}
|
427
|
+
accessor_type = extract_method_call(argv[0], self, &f, &o);
|
222
428
|
|
223
|
-
|
224
|
-
if (o != NULL) {
|
225
|
-
if (accessor_type == METHOD_SETTER) {
|
226
|
-
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
227
|
-
}
|
429
|
+
if (accessor_type == METHOD_UNKNOWN) return rb_call_super(argc, argv);
|
228
430
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
if (
|
234
|
-
|
431
|
+
// Validate argument count.
|
432
|
+
switch (accessor_type) {
|
433
|
+
case METHOD_SETTER:
|
434
|
+
case METHOD_WRAPPER_SETTER:
|
435
|
+
if (argc != 2) {
|
436
|
+
rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
|
235
437
|
}
|
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);
|
438
|
+
rb_check_frozen(_self);
|
439
|
+
break;
|
440
|
+
default:
|
441
|
+
if (argc != 1) {
|
442
|
+
rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
|
443
|
+
}
|
444
|
+
break;
|
445
|
+
}
|
446
|
+
|
447
|
+
// Dispatch accessor.
|
448
|
+
if (o != NULL) {
|
449
|
+
return Message_oneof_accessor(_self, o, accessor_type);
|
251
450
|
} else {
|
252
|
-
return
|
451
|
+
return Message_field_accessor(_self, f, accessor_type, argc, argv);
|
253
452
|
}
|
254
453
|
}
|
255
454
|
|
256
|
-
|
257
|
-
|
258
|
-
MessageHeader* self;
|
455
|
+
static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
456
|
+
Message* self = ruby_to_Message(_self);
|
259
457
|
const upb_oneofdef* o;
|
260
458
|
const upb_fielddef* f;
|
459
|
+
int accessor_type;
|
261
460
|
|
262
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
263
461
|
if (argc < 1) {
|
264
462
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
265
463
|
}
|
266
464
|
|
267
|
-
|
465
|
+
accessor_type = extract_method_call(argv[0], self, &f, &o);
|
466
|
+
|
268
467
|
if (accessor_type == METHOD_UNKNOWN) {
|
269
468
|
return rb_call_super(argc, argv);
|
270
469
|
} else if (o != NULL) {
|
@@ -274,22 +473,116 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
|
274
473
|
}
|
275
474
|
}
|
276
475
|
|
277
|
-
|
278
|
-
|
279
|
-
|
476
|
+
void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
|
477
|
+
upb_arena* arena);
|
478
|
+
|
479
|
+
typedef struct {
|
480
|
+
upb_map *map;
|
481
|
+
TypeInfo key_type;
|
482
|
+
TypeInfo val_type;
|
483
|
+
upb_arena *arena;
|
484
|
+
} MapInit;
|
485
|
+
|
486
|
+
static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
487
|
+
MapInit *map_init = (MapInit*)_self;
|
488
|
+
upb_msgval k, v;
|
489
|
+
k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
|
490
|
+
|
491
|
+
if (map_init->val_type.type == UPB_TYPE_MESSAGE && TYPE(val) == T_HASH) {
|
492
|
+
upb_msg *msg = upb_msg_new(map_init->val_type.def.msgdef, map_init->arena);
|
493
|
+
Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
|
494
|
+
map_init->arena);
|
495
|
+
v.msg_val = msg;
|
496
|
+
} else {
|
497
|
+
v = Convert_RubyToUpb(val, "", map_init->val_type, map_init->arena);
|
498
|
+
}
|
499
|
+
upb_map_set(map_init->map, k, v, map_init->arena);
|
500
|
+
return ST_CONTINUE;
|
501
|
+
}
|
280
502
|
|
281
|
-
|
282
|
-
|
503
|
+
static void Map_InitFromValue(upb_map* map, const upb_fielddef* f, VALUE val,
|
504
|
+
upb_arena* arena) {
|
505
|
+
const upb_msgdef* entry_m = upb_fielddef_msgsubdef(f);
|
506
|
+
const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
|
507
|
+
const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
|
508
|
+
if (TYPE(val) != T_HASH) {
|
509
|
+
rb_raise(rb_eArgError,
|
510
|
+
"Expected Hash object as initializer value for map field '%s' "
|
511
|
+
"(given %s).",
|
512
|
+
upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
|
513
|
+
}
|
514
|
+
MapInit map_init = {map, TypeInfo_get(key_f), TypeInfo_get(val_f), arena};
|
515
|
+
rb_hash_foreach(val, Map_initialize_kwarg, (VALUE)&map_init);
|
516
|
+
}
|
283
517
|
|
284
|
-
|
285
|
-
|
518
|
+
static upb_msgval MessageValue_FromValue(VALUE val, TypeInfo info,
|
519
|
+
upb_arena* arena) {
|
520
|
+
if (info.type == UPB_TYPE_MESSAGE) {
|
521
|
+
upb_msgval msgval;
|
522
|
+
upb_msg* msg = upb_msg_new(info.def.msgdef, arena);
|
523
|
+
Message_InitFromValue(msg, info.def.msgdef, val, arena);
|
524
|
+
msgval.msg_val = msg;
|
525
|
+
return msgval;
|
526
|
+
} else {
|
527
|
+
return Convert_RubyToUpb(val, "", info, arena);
|
528
|
+
}
|
286
529
|
}
|
287
530
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
531
|
+
static void RepeatedField_InitFromValue(upb_array* arr, const upb_fielddef* f,
|
532
|
+
VALUE val, upb_arena* arena) {
|
533
|
+
TypeInfo type_info = TypeInfo_get(f);
|
534
|
+
|
535
|
+
if (TYPE(val) != T_ARRAY) {
|
536
|
+
rb_raise(rb_eArgError,
|
537
|
+
"Expected array as initializer value for repeated field '%s' (given %s).",
|
538
|
+
upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
|
539
|
+
}
|
540
|
+
|
541
|
+
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
542
|
+
VALUE entry = rb_ary_entry(val, i);
|
543
|
+
upb_msgval msgval;
|
544
|
+
if (upb_fielddef_issubmsg(f) && TYPE(entry) == T_HASH) {
|
545
|
+
msgval = MessageValue_FromValue(entry, type_info, arena);
|
546
|
+
} else {
|
547
|
+
msgval = Convert_RubyToUpb(entry, upb_fielddef_name(f), type_info, arena);
|
548
|
+
}
|
549
|
+
upb_array_append(arr, msgval, arena);
|
550
|
+
}
|
551
|
+
}
|
552
|
+
|
553
|
+
static void Message_InitFieldFromValue(upb_msg* msg, const upb_fielddef* f,
|
554
|
+
VALUE val, upb_arena* arena) {
|
555
|
+
if (TYPE(val) == T_NIL) return;
|
556
|
+
|
557
|
+
if (upb_fielddef_ismap(f)) {
|
558
|
+
upb_map *map = upb_msg_mutable(msg, f, arena).map;
|
559
|
+
Map_InitFromValue(map, f, val, arena);
|
560
|
+
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
561
|
+
upb_array *arr = upb_msg_mutable(msg, f, arena).array;
|
562
|
+
RepeatedField_InitFromValue(arr, f, val, arena);
|
563
|
+
} else if (upb_fielddef_issubmsg(f)) {
|
564
|
+
if (TYPE(val) == T_HASH) {
|
565
|
+
upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
|
566
|
+
Message_InitFromValue(submsg, upb_fielddef_msgsubdef(f), val, arena);
|
567
|
+
} else {
|
568
|
+
Message_setfield(msg, f, val, arena);
|
569
|
+
}
|
570
|
+
} else {
|
571
|
+
upb_msgval msgval =
|
572
|
+
Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
573
|
+
upb_msg_set(msg, f, msgval, arena);
|
574
|
+
}
|
575
|
+
}
|
576
|
+
|
577
|
+
typedef struct {
|
578
|
+
upb_msg *msg;
|
579
|
+
const upb_msgdef *msgdef;
|
580
|
+
upb_arena *arena;
|
581
|
+
} MsgInit;
|
582
|
+
|
583
|
+
static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
584
|
+
MsgInit *msg_init = (MsgInit*)_self;
|
585
|
+
const char *name;
|
293
586
|
|
294
587
|
if (TYPE(key) == T_STRING) {
|
295
588
|
name = RSTRING_PTR(key);
|
@@ -300,51 +593,26 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
300
593
|
"Expected string or symbols as hash keys when initializing proto from hash.");
|
301
594
|
}
|
302
595
|
|
303
|
-
f = upb_msgdef_ntofz(
|
596
|
+
const upb_fielddef* f = upb_msgdef_ntofz(msg_init->msgdef, name);
|
597
|
+
|
304
598
|
if (f == NULL) {
|
305
599
|
rb_raise(rb_eArgError,
|
306
600
|
"Unknown field name '%s' in initialization map entry.", name);
|
307
601
|
}
|
308
602
|
|
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' (given %s).",
|
319
|
-
name, rb_class2name(CLASS_OF(val)));
|
320
|
-
}
|
321
|
-
map = layout_get(self->descriptor->layout, Message_data(self), f);
|
322
|
-
Map_merge_into_self(map, val);
|
323
|
-
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
324
|
-
VALUE ary;
|
325
|
-
|
326
|
-
if (TYPE(val) != T_ARRAY) {
|
327
|
-
rb_raise(rb_eArgError,
|
328
|
-
"Expected array as initializer value for repeated field '%s' (given %s).",
|
329
|
-
name, rb_class2name(CLASS_OF(val)));
|
330
|
-
}
|
331
|
-
ary = layout_get(self->descriptor->layout, Message_data(self), f);
|
332
|
-
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
333
|
-
VALUE entry = rb_ary_entry(val, i);
|
334
|
-
if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
|
335
|
-
entry = create_submsg_from_hash(f, entry);
|
336
|
-
}
|
603
|
+
Message_InitFieldFromValue(msg_init->msg, f, val, msg_init->arena);
|
604
|
+
return ST_CONTINUE;
|
605
|
+
}
|
337
606
|
|
338
|
-
|
339
|
-
|
607
|
+
void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
|
608
|
+
upb_arena* arena) {
|
609
|
+
MsgInit msg_init = {msg, m, arena};
|
610
|
+
if (TYPE(val) == T_HASH) {
|
611
|
+
rb_hash_foreach(val, Message_initialize_kwarg, (VALUE)&msg_init);
|
340
612
|
} else {
|
341
|
-
|
342
|
-
|
343
|
-
}
|
344
|
-
|
345
|
-
layout_set(self->descriptor->layout, Message_data(self), f, val);
|
613
|
+
rb_raise(rb_eArgError, "Expected hash arguments or message, not %s",
|
614
|
+
rb_class2name(CLASS_OF(val)));
|
346
615
|
}
|
347
|
-
return 0;
|
348
616
|
}
|
349
617
|
|
350
618
|
/*
|
@@ -359,8 +627,13 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
359
627
|
* have been added to a pool. The method definitions described here on the
|
360
628
|
* Message class are provided on each concrete message class.
|
361
629
|
*/
|
362
|
-
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
363
|
-
|
630
|
+
static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
631
|
+
Message* self = ruby_to_Message(_self);
|
632
|
+
VALUE arena_rb = Arena_new();
|
633
|
+
upb_arena *arena = Arena_get(arena_rb);
|
634
|
+
upb_msg *msg = upb_msg_new(self->msgdef, arena);
|
635
|
+
|
636
|
+
Message_InitPtr(_self, msg, arena_rb);
|
364
637
|
|
365
638
|
if (argc == 0) {
|
366
639
|
return Qnil;
|
@@ -368,12 +641,7 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
368
641
|
if (argc != 1) {
|
369
642
|
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
370
643
|
}
|
371
|
-
|
372
|
-
if (TYPE(hash_args) != T_HASH) {
|
373
|
-
rb_raise(rb_eArgError, "Expected hash arguments.");
|
374
|
-
}
|
375
|
-
|
376
|
-
rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
|
644
|
+
Message_InitFromValue((upb_msg*)self->msg, self->msgdef, argv[0], arena);
|
377
645
|
return Qnil;
|
378
646
|
}
|
379
647
|
|
@@ -383,37 +651,40 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
383
651
|
*
|
384
652
|
* Performs a shallow copy of this message and returns the new copy.
|
385
653
|
*/
|
386
|
-
VALUE Message_dup(VALUE _self) {
|
387
|
-
|
388
|
-
VALUE new_msg;
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
Message_data(new_msg_self),
|
397
|
-
Message_data(self));
|
398
|
-
|
654
|
+
static VALUE Message_dup(VALUE _self) {
|
655
|
+
Message* self = ruby_to_Message(_self);
|
656
|
+
VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
657
|
+
Message* new_msg_self = ruby_to_Message(new_msg);
|
658
|
+
size_t size = upb_msgdef_layout(self->msgdef)->size;
|
659
|
+
|
660
|
+
// TODO(copy unknown fields?)
|
661
|
+
// TODO(use official upb msg copy function)
|
662
|
+
memcpy((upb_msg*)new_msg_self->msg, self->msg, size);
|
663
|
+
Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
|
399
664
|
return new_msg;
|
400
665
|
}
|
401
666
|
|
402
|
-
//
|
403
|
-
|
404
|
-
|
405
|
-
MessageHeader* new_msg_self;
|
406
|
-
VALUE new_msg;
|
407
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
667
|
+
// Support function for Message_eq, and also used by other #eq functions.
|
668
|
+
bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m) {
|
669
|
+
if (m1 == m2) return true;
|
408
670
|
|
409
|
-
|
410
|
-
|
671
|
+
size_t size1, size2;
|
672
|
+
int encode_opts = UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC;
|
673
|
+
upb_arena *arena_tmp = upb_arena_new();
|
674
|
+
const upb_msglayout *layout = upb_msgdef_layout(m);
|
411
675
|
|
412
|
-
|
413
|
-
|
414
|
-
|
676
|
+
// Compare deterministically serialized payloads with no unknown fields.
|
677
|
+
char *data1 = upb_encode_ex(m1, layout, encode_opts, arena_tmp, &size1);
|
678
|
+
char *data2 = upb_encode_ex(m2, layout, encode_opts, arena_tmp, &size2);
|
415
679
|
|
416
|
-
|
680
|
+
if (data1 && data2) {
|
681
|
+
bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
|
682
|
+
upb_arena_free(arena_tmp);
|
683
|
+
return ret;
|
684
|
+
} else {
|
685
|
+
upb_arena_free(arena_tmp);
|
686
|
+
rb_raise(cParseError, "Error comparing messages");
|
687
|
+
}
|
417
688
|
}
|
418
689
|
|
419
690
|
/*
|
@@ -425,22 +696,34 @@ VALUE Message_deep_copy(VALUE _self) {
|
|
425
696
|
* method's semantics (a more efficient comparison may actually be done if the
|
426
697
|
* field is of a primitive type).
|
427
698
|
*/
|
428
|
-
VALUE Message_eq(VALUE _self, VALUE _other) {
|
429
|
-
|
430
|
-
MessageHeader* other;
|
431
|
-
if (TYPE(_self) != TYPE(_other)) {
|
432
|
-
return Qfalse;
|
433
|
-
}
|
434
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
435
|
-
TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
|
699
|
+
static VALUE Message_eq(VALUE _self, VALUE _other) {
|
700
|
+
if (CLASS_OF(_self) != CLASS_OF(_other)) return Qfalse;
|
436
701
|
|
437
|
-
|
438
|
-
|
439
|
-
|
702
|
+
Message* self = ruby_to_Message(_self);
|
703
|
+
Message* other = ruby_to_Message(_other);
|
704
|
+
assert(self->msgdef == other->msgdef);
|
705
|
+
|
706
|
+
return Message_Equal(self->msg, other->msg, self->msgdef) ? Qtrue : Qfalse;
|
707
|
+
}
|
708
|
+
|
709
|
+
uint64_t Message_Hash(const upb_msg* msg, const upb_msgdef* m, uint64_t seed) {
|
710
|
+
upb_arena *arena = upb_arena_new();
|
711
|
+
const char *data;
|
712
|
+
size_t size;
|
713
|
+
|
714
|
+
// Hash a deterministically serialized payloads with no unknown fields.
|
715
|
+
data = upb_encode_ex(msg, upb_msgdef_layout(m),
|
716
|
+
UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC, arena,
|
717
|
+
&size);
|
440
718
|
|
441
|
-
|
442
|
-
|
443
|
-
|
719
|
+
if (data) {
|
720
|
+
uint64_t ret = wyhash(data, size, seed, _wyp);
|
721
|
+
upb_arena_free(arena);
|
722
|
+
return ret;
|
723
|
+
} else {
|
724
|
+
upb_arena_free(arena);
|
725
|
+
rb_raise(cParseError, "Error calculating hash");
|
726
|
+
}
|
444
727
|
}
|
445
728
|
|
446
729
|
/*
|
@@ -449,11 +732,12 @@ VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
449
732
|
*
|
450
733
|
* Returns a hash value that represents this message's field values.
|
451
734
|
*/
|
452
|
-
VALUE Message_hash(VALUE _self) {
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
735
|
+
static VALUE Message_hash(VALUE _self) {
|
736
|
+
Message* self = ruby_to_Message(_self);
|
737
|
+
uint64_t hash_value = Message_Hash(self->msg, self->msgdef, 0);
|
738
|
+
// RUBY_FIXNUM_MAX should be one less than a power of 2.
|
739
|
+
assert((RUBY_FIXNUM_MAX & (RUBY_FIXNUM_MAX + 1)) == 0);
|
740
|
+
return INT2FIX(hash_value & RUBY_FIXNUM_MAX);
|
457
741
|
}
|
458
742
|
|
459
743
|
/*
|
@@ -464,75 +748,127 @@ VALUE Message_hash(VALUE _self) {
|
|
464
748
|
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
|
465
749
|
* field's value is represented according to its own #inspect method.
|
466
750
|
*/
|
467
|
-
VALUE Message_inspect(VALUE _self) {
|
468
|
-
|
469
|
-
VALUE str;
|
470
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
751
|
+
static VALUE Message_inspect(VALUE _self) {
|
752
|
+
Message* self = ruby_to_Message(_self);
|
471
753
|
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
str = rb_str_cat2(str, ">");
|
478
|
-
return str;
|
754
|
+
StringBuilder* builder = StringBuilder_New();
|
755
|
+
Message_PrintMessage(builder, self->msg, self->msgdef);
|
756
|
+
VALUE ret = StringBuilder_ToRubyString(builder);
|
757
|
+
StringBuilder_Free(builder);
|
758
|
+
return ret;
|
479
759
|
}
|
480
760
|
|
481
|
-
|
482
|
-
|
483
|
-
*
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
761
|
+
// Support functions for Message_to_h //////////////////////////////////////////
|
762
|
+
|
763
|
+
static VALUE RepeatedField_CreateArray(const upb_array* arr,
|
764
|
+
TypeInfo type_info) {
|
765
|
+
int size = arr ? upb_array_size(arr) : 0;
|
766
|
+
VALUE ary = rb_ary_new2(size);
|
767
|
+
|
768
|
+
for (int i = 0; i < size; i++) {
|
769
|
+
upb_msgval msgval = upb_array_get(arr, i);
|
770
|
+
VALUE val = Scalar_CreateHash(msgval, type_info);
|
771
|
+
rb_ary_push(ary, val);
|
772
|
+
}
|
773
|
+
|
774
|
+
return ary;
|
775
|
+
}
|
776
|
+
|
777
|
+
static VALUE Message_CreateHash(const upb_msg *msg, const upb_msgdef *m) {
|
778
|
+
if (!msg) return Qnil;
|
779
|
+
|
780
|
+
VALUE hash = rb_hash_new();
|
781
|
+
int n = upb_msgdef_fieldcount(m);
|
782
|
+
bool is_proto2;
|
783
|
+
|
784
|
+
// We currently have a few behaviors that are specific to proto2.
|
785
|
+
// This is unfortunate, we should key behaviors off field attributes (like
|
786
|
+
// whether a field has presence), not proto2 vs. proto3. We should see if we
|
787
|
+
// can change this without breaking users.
|
788
|
+
is_proto2 = upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2;
|
789
|
+
|
790
|
+
for (int i = 0; i < n; i++) {
|
791
|
+
const upb_fielddef* field = upb_msgdef_field(m, i);
|
792
|
+
TypeInfo type_info = TypeInfo_get(field);
|
793
|
+
upb_msgval msgval;
|
794
|
+
VALUE msg_value;
|
795
|
+
VALUE msg_key;
|
796
|
+
|
797
|
+
if (!is_proto2 && upb_fielddef_issubmsg(field) &&
|
798
|
+
!upb_fielddef_isseq(field) && !upb_msg_has(msg, field)) {
|
799
|
+
// TODO: Legacy behavior, remove when we fix the is_proto2 differences.
|
800
|
+
msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
801
|
+
rb_hash_aset(hash, msg_key, Qnil);
|
504
802
|
continue;
|
505
803
|
}
|
506
804
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
|
513
|
-
msg_value = RepeatedField_to_ary(msg_value);
|
514
|
-
if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
|
515
|
-
RARRAY_LEN(msg_value) == 0) {
|
516
|
-
continue;
|
517
|
-
}
|
805
|
+
// Do not include fields that are not present (oneof or optional fields).
|
806
|
+
if (is_proto2 && upb_fielddef_haspresence(field) &&
|
807
|
+
!upb_msg_has(msg, field)) {
|
808
|
+
continue;
|
809
|
+
}
|
518
810
|
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
}
|
524
|
-
}
|
811
|
+
msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
812
|
+
msgval = upb_msg_get(msg, field);
|
813
|
+
|
814
|
+
// Proto2 omits empty map/repeated filds also.
|
525
815
|
|
526
|
-
|
527
|
-
|
528
|
-
|
816
|
+
if (upb_fielddef_ismap(field)) {
|
817
|
+
const upb_msgdef *entry_m = upb_fielddef_msgsubdef(field);
|
818
|
+
const upb_fielddef *key_f = upb_msgdef_itof(entry_m, 1);
|
819
|
+
const upb_fielddef *val_f = upb_msgdef_itof(entry_m, 2);
|
820
|
+
upb_fieldtype_t key_type = upb_fielddef_type(key_f);
|
821
|
+
msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
|
822
|
+
} else if (upb_fielddef_isseq(field)) {
|
823
|
+
if (is_proto2 &&
|
824
|
+
(!msgval.array_val || upb_array_size(msgval.array_val) == 0)) {
|
825
|
+
continue;
|
826
|
+
}
|
827
|
+
msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
|
828
|
+
} else {
|
829
|
+
msg_value = Scalar_CreateHash(msgval, type_info);
|
529
830
|
}
|
831
|
+
|
530
832
|
rb_hash_aset(hash, msg_key, msg_value);
|
531
833
|
}
|
834
|
+
|
532
835
|
return hash;
|
533
836
|
}
|
534
837
|
|
838
|
+
VALUE Scalar_CreateHash(upb_msgval msgval, TypeInfo type_info) {
|
839
|
+
if (type_info.type == UPB_TYPE_MESSAGE) {
|
840
|
+
return Message_CreateHash(msgval.msg_val, type_info.def.msgdef);
|
841
|
+
} else {
|
842
|
+
return Convert_UpbToRuby(msgval, type_info, Qnil);
|
843
|
+
}
|
844
|
+
}
|
845
|
+
|
846
|
+
/*
|
847
|
+
* call-seq:
|
848
|
+
* Message.to_h => {}
|
849
|
+
*
|
850
|
+
* Returns the message as a Ruby Hash object, with keys as symbols.
|
851
|
+
*/
|
852
|
+
static VALUE Message_to_h(VALUE _self) {
|
853
|
+
Message* self = ruby_to_Message(_self);
|
854
|
+
return Message_CreateHash(self->msg, self->msgdef);
|
855
|
+
}
|
535
856
|
|
857
|
+
/*
|
858
|
+
* call-seq:
|
859
|
+
* Message.freeze => self
|
860
|
+
*
|
861
|
+
* Freezes the message object. We have to intercept this so we can pin the
|
862
|
+
* Ruby object into memory so we don't forget it's frozen.
|
863
|
+
*/
|
864
|
+
static VALUE Message_freeze(VALUE _self) {
|
865
|
+
Message* self = ruby_to_Message(_self);
|
866
|
+
if (!RB_OBJ_FROZEN(_self)) {
|
867
|
+
Arena_Pin(self->arena, _self);
|
868
|
+
RB_OBJ_FREEZE(_self);
|
869
|
+
}
|
870
|
+
return _self;
|
871
|
+
}
|
536
872
|
|
537
873
|
/*
|
538
874
|
* call-seq:
|
@@ -541,16 +877,18 @@ VALUE Message_to_h(VALUE _self) {
|
|
541
877
|
* Accesses a field's value by field name. The provided field name should be a
|
542
878
|
* string.
|
543
879
|
*/
|
544
|
-
VALUE Message_index(VALUE _self, VALUE field_name) {
|
545
|
-
|
880
|
+
static VALUE Message_index(VALUE _self, VALUE field_name) {
|
881
|
+
Message* self = ruby_to_Message(_self);
|
546
882
|
const upb_fielddef* field;
|
547
|
-
|
883
|
+
|
548
884
|
Check_Type(field_name, T_STRING);
|
549
|
-
field = upb_msgdef_ntofz(self->
|
885
|
+
field = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
|
886
|
+
|
550
887
|
if (field == NULL) {
|
551
888
|
return Qnil;
|
552
889
|
}
|
553
|
-
|
890
|
+
|
891
|
+
return Message_getfield(_self, field);
|
554
892
|
}
|
555
893
|
|
556
894
|
/*
|
@@ -560,19 +898,208 @@ VALUE Message_index(VALUE _self, VALUE field_name) {
|
|
560
898
|
* Sets a field's value by field name. The provided field name should be a
|
561
899
|
* string.
|
562
900
|
*/
|
563
|
-
VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
564
|
-
|
565
|
-
const upb_fielddef*
|
566
|
-
|
901
|
+
static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
902
|
+
Message* self = ruby_to_Message(_self);
|
903
|
+
const upb_fielddef* f;
|
904
|
+
upb_msgval val;
|
905
|
+
upb_arena *arena = Arena_get(self->arena);
|
906
|
+
|
567
907
|
Check_Type(field_name, T_STRING);
|
568
|
-
|
569
|
-
|
908
|
+
f = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
|
909
|
+
|
910
|
+
if (f == NULL) {
|
570
911
|
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
571
912
|
}
|
572
|
-
|
913
|
+
|
914
|
+
val = Convert_RubyToUpb(value, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
915
|
+
upb_msg_set(Message_GetMutable(_self, NULL), f, val, arena);
|
916
|
+
|
573
917
|
return Qnil;
|
574
918
|
}
|
575
919
|
|
920
|
+
/*
|
921
|
+
* call-seq:
|
922
|
+
* MessageClass.decode(data) => message
|
923
|
+
*
|
924
|
+
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
925
|
+
* format) under the interpretration given by this message class's definition
|
926
|
+
* and returns a message object with the corresponding field values.
|
927
|
+
*/
|
928
|
+
static VALUE Message_decode(VALUE klass, VALUE data) {
|
929
|
+
if (TYPE(data) != T_STRING) {
|
930
|
+
rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
|
931
|
+
}
|
932
|
+
|
933
|
+
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
934
|
+
Message* msg = ruby_to_Message(msg_rb);
|
935
|
+
|
936
|
+
if (!upb_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
|
937
|
+
upb_msgdef_layout(msg->msgdef),
|
938
|
+
Arena_get(msg->arena))) {
|
939
|
+
rb_raise(cParseError, "Error occurred during parsing");
|
940
|
+
}
|
941
|
+
|
942
|
+
return msg_rb;
|
943
|
+
}
|
944
|
+
|
945
|
+
/*
|
946
|
+
* call-seq:
|
947
|
+
* MessageClass.decode_json(data, options = {}) => message
|
948
|
+
*
|
949
|
+
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
950
|
+
* format) under the interpretration given by this message class's definition
|
951
|
+
* and returns a message object with the corresponding field values.
|
952
|
+
*
|
953
|
+
* @param options [Hash] options for the decoder
|
954
|
+
* ignore_unknown_fields: set true to ignore unknown fields (default is to
|
955
|
+
* raise an error)
|
956
|
+
*/
|
957
|
+
static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
958
|
+
VALUE data = argv[0];
|
959
|
+
int options = 0;
|
960
|
+
upb_status status;
|
961
|
+
|
962
|
+
// TODO(haberman): use this message's pool instead.
|
963
|
+
const upb_symtab *symtab = DescriptorPool_GetSymtab(generated_pool);
|
964
|
+
|
965
|
+
if (argc < 1 || argc > 2) {
|
966
|
+
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
967
|
+
}
|
968
|
+
|
969
|
+
if (argc == 2) {
|
970
|
+
VALUE hash_args = argv[1];
|
971
|
+
if (TYPE(hash_args) != T_HASH) {
|
972
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
973
|
+
}
|
974
|
+
|
975
|
+
if (RTEST(rb_hash_lookup2( hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse))) {
|
976
|
+
options |= UPB_JSONDEC_IGNOREUNKNOWN;
|
977
|
+
}
|
978
|
+
}
|
979
|
+
|
980
|
+
if (TYPE(data) != T_STRING) {
|
981
|
+
rb_raise(rb_eArgError, "Expected string for JSON data.");
|
982
|
+
}
|
983
|
+
|
984
|
+
// TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
|
985
|
+
// convert, because string handlers pass data directly to message string
|
986
|
+
// fields.
|
987
|
+
|
988
|
+
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
989
|
+
Message* msg = ruby_to_Message(msg_rb);
|
990
|
+
|
991
|
+
// We don't allow users to decode a wrapper type directly.
|
992
|
+
if (upb_msgdef_iswrapper(msg->msgdef)) {
|
993
|
+
rb_raise(rb_eRuntimeError, "Cannot parse a wrapper directly.");
|
994
|
+
}
|
995
|
+
|
996
|
+
upb_status_clear(&status);
|
997
|
+
if (!upb_json_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
|
998
|
+
msg->msgdef, symtab, options,
|
999
|
+
Arena_get(msg->arena), &status)) {
|
1000
|
+
rb_raise(cParseError, "Error occurred during parsing: %s",
|
1001
|
+
upb_status_errmsg(&status));
|
1002
|
+
}
|
1003
|
+
|
1004
|
+
return msg_rb;
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
/*
|
1008
|
+
* call-seq:
|
1009
|
+
* MessageClass.encode(msg) => bytes
|
1010
|
+
*
|
1011
|
+
* Encodes the given message object to its serialized form in protocol buffers
|
1012
|
+
* wire format.
|
1013
|
+
*/
|
1014
|
+
static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
1015
|
+
Message* msg = ruby_to_Message(msg_rb);
|
1016
|
+
upb_arena *arena = upb_arena_new();
|
1017
|
+
const char *data;
|
1018
|
+
size_t size;
|
1019
|
+
|
1020
|
+
if (CLASS_OF(msg_rb) != klass) {
|
1021
|
+
rb_raise(rb_eArgError, "Message of wrong type.");
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
data = upb_encode(msg->msg, upb_msgdef_layout(msg->msgdef), arena,
|
1025
|
+
&size);
|
1026
|
+
|
1027
|
+
if (data) {
|
1028
|
+
VALUE ret = rb_str_new(data, size);
|
1029
|
+
rb_enc_associate(ret, rb_ascii8bit_encoding());
|
1030
|
+
upb_arena_free(arena);
|
1031
|
+
return ret;
|
1032
|
+
} else {
|
1033
|
+
upb_arena_free(arena);
|
1034
|
+
rb_raise(rb_eRuntimeError, "Exceeded maximum depth (possibly cycle)");
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
|
1038
|
+
/*
|
1039
|
+
* call-seq:
|
1040
|
+
* MessageClass.encode_json(msg, options = {}) => json_string
|
1041
|
+
*
|
1042
|
+
* Encodes the given message object into its serialized JSON representation.
|
1043
|
+
* @param options [Hash] options for the decoder
|
1044
|
+
* preserve_proto_fieldnames: set true to use original fieldnames (default is to camelCase)
|
1045
|
+
* emit_defaults: set true to emit 0/false values (default is to omit them)
|
1046
|
+
*/
|
1047
|
+
static VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
|
1048
|
+
Message* msg = ruby_to_Message(argv[0]);
|
1049
|
+
int options = 0;
|
1050
|
+
char buf[1024];
|
1051
|
+
size_t size;
|
1052
|
+
upb_status status;
|
1053
|
+
|
1054
|
+
// TODO(haberman): use this message's pool instead.
|
1055
|
+
const upb_symtab *symtab = DescriptorPool_GetSymtab(generated_pool);
|
1056
|
+
|
1057
|
+
if (argc < 1 || argc > 2) {
|
1058
|
+
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
1059
|
+
}
|
1060
|
+
|
1061
|
+
if (argc == 2) {
|
1062
|
+
VALUE hash_args = argv[1];
|
1063
|
+
if (TYPE(hash_args) != T_HASH) {
|
1064
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
1065
|
+
}
|
1066
|
+
|
1067
|
+
if (RTEST(rb_hash_lookup2(hash_args,
|
1068
|
+
ID2SYM(rb_intern("preserve_proto_fieldnames")),
|
1069
|
+
Qfalse))) {
|
1070
|
+
options |= UPB_JSONENC_PROTONAMES;
|
1071
|
+
}
|
1072
|
+
|
1073
|
+
if (RTEST(rb_hash_lookup2(hash_args, ID2SYM(rb_intern("emit_defaults")),
|
1074
|
+
Qfalse))) {
|
1075
|
+
options |= UPB_JSONENC_EMITDEFAULTS;
|
1076
|
+
}
|
1077
|
+
}
|
1078
|
+
|
1079
|
+
upb_status_clear(&status);
|
1080
|
+
size = upb_json_encode(msg->msg, msg->msgdef, symtab, options, buf,
|
1081
|
+
sizeof(buf), &status);
|
1082
|
+
|
1083
|
+
if (!upb_ok(&status)) {
|
1084
|
+
rb_raise(cParseError, "Error occurred during encoding: %s",
|
1085
|
+
upb_status_errmsg(&status));
|
1086
|
+
}
|
1087
|
+
|
1088
|
+
VALUE ret;
|
1089
|
+
if (size >= sizeof(buf)) {
|
1090
|
+
char* buf2 = malloc(size + 1);
|
1091
|
+
upb_json_encode(msg->msg, msg->msgdef, symtab, options, buf2, size + 1,
|
1092
|
+
&status);
|
1093
|
+
ret = rb_str_new(buf2, size);
|
1094
|
+
free(buf2);
|
1095
|
+
} else {
|
1096
|
+
ret = rb_str_new(buf, size);
|
1097
|
+
}
|
1098
|
+
|
1099
|
+
rb_enc_associate(ret, rb_utf8_encoding());
|
1100
|
+
return ret;
|
1101
|
+
}
|
1102
|
+
|
576
1103
|
/*
|
577
1104
|
* call-seq:
|
578
1105
|
* Message.descriptor => descriptor
|
@@ -580,22 +1107,15 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
580
1107
|
* Class method that returns the Descriptor instance corresponding to this
|
581
1108
|
* message class's type.
|
582
1109
|
*/
|
583
|
-
VALUE Message_descriptor(VALUE klass) {
|
1110
|
+
static VALUE Message_descriptor(VALUE klass) {
|
584
1111
|
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
585
1112
|
}
|
586
1113
|
|
587
|
-
VALUE build_class_from_descriptor(
|
1114
|
+
VALUE build_class_from_descriptor(VALUE descriptor) {
|
588
1115
|
const char *name;
|
589
1116
|
VALUE klass;
|
590
1117
|
|
591
|
-
|
592
|
-
desc->layout = create_layout(desc->msgdef);
|
593
|
-
}
|
594
|
-
if (desc->fill_method == NULL) {
|
595
|
-
desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
|
596
|
-
}
|
597
|
-
|
598
|
-
name = upb_msgdef_fullname(desc->msgdef);
|
1118
|
+
name = upb_msgdef_fullname(Descriptor_GetMsgDef(descriptor));
|
599
1119
|
if (name == NULL) {
|
600
1120
|
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
601
1121
|
}
|
@@ -605,8 +1125,7 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
605
1125
|
// their own toplevel constant class name.
|
606
1126
|
rb_intern("Message"),
|
607
1127
|
rb_cObject);
|
608
|
-
rb_ivar_set(klass, descriptor_instancevar_interned,
|
609
|
-
get_def_obj(desc->msgdef));
|
1128
|
+
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
|
610
1129
|
rb_define_alloc_func(klass, Message_alloc);
|
611
1130
|
rb_require("google/protobuf/message_exts");
|
612
1131
|
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
|
@@ -622,10 +1141,12 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
622
1141
|
// Also define #clone so that we don't inherit Object#clone.
|
623
1142
|
rb_define_method(klass, "clone", Message_dup, 0);
|
624
1143
|
rb_define_method(klass, "==", Message_eq, 1);
|
1144
|
+
rb_define_method(klass, "eql?", Message_eq, 1);
|
1145
|
+
rb_define_method(klass, "freeze", Message_freeze, 0);
|
625
1146
|
rb_define_method(klass, "hash", Message_hash, 0);
|
626
1147
|
rb_define_method(klass, "to_h", Message_to_h, 0);
|
627
|
-
rb_define_method(klass, "to_hash", Message_to_h, 0);
|
628
1148
|
rb_define_method(klass, "inspect", Message_inspect, 0);
|
1149
|
+
rb_define_method(klass, "to_s", Message_inspect, 0);
|
629
1150
|
rb_define_method(klass, "[]", Message_index, 1);
|
630
1151
|
rb_define_method(klass, "[]=", Message_index_set, 2);
|
631
1152
|
rb_define_singleton_method(klass, "decode", Message_decode, 1);
|
@@ -644,12 +1165,12 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
644
1165
|
* This module method, provided on each generated enum module, looks up an enum
|
645
1166
|
* value by number and returns its name as a Ruby symbol, or nil if not found.
|
646
1167
|
*/
|
647
|
-
VALUE enum_lookup(VALUE self, VALUE number) {
|
1168
|
+
static VALUE enum_lookup(VALUE self, VALUE number) {
|
648
1169
|
int32_t num = NUM2INT(number);
|
649
1170
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
650
|
-
|
1171
|
+
const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
|
651
1172
|
|
652
|
-
const char* name = upb_enumdef_iton(
|
1173
|
+
const char* name = upb_enumdef_iton(e, num);
|
653
1174
|
if (name == NULL) {
|
654
1175
|
return Qnil;
|
655
1176
|
} else {
|
@@ -664,13 +1185,13 @@ VALUE enum_lookup(VALUE self, VALUE number) {
|
|
664
1185
|
* This module method, provided on each generated enum module, looks up an enum
|
665
1186
|
* value by name (as a Ruby symbol) and returns its name, or nil if not found.
|
666
1187
|
*/
|
667
|
-
VALUE enum_resolve(VALUE self, VALUE sym) {
|
1188
|
+
static VALUE enum_resolve(VALUE self, VALUE sym) {
|
668
1189
|
const char* name = rb_id2name(SYM2ID(sym));
|
669
1190
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
670
|
-
|
1191
|
+
const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
|
671
1192
|
|
672
1193
|
int32_t num = 0;
|
673
|
-
bool found = upb_enumdef_ntoiz(
|
1194
|
+
bool found = upb_enumdef_ntoiz(e, name, &num);
|
674
1195
|
if (!found) {
|
675
1196
|
return Qnil;
|
676
1197
|
} else {
|
@@ -685,16 +1206,16 @@ VALUE enum_resolve(VALUE self, VALUE sym) {
|
|
685
1206
|
* This module method, provided on each generated enum module, returns the
|
686
1207
|
* EnumDescriptor corresponding to this enum type.
|
687
1208
|
*/
|
688
|
-
VALUE enum_descriptor(VALUE self) {
|
1209
|
+
static VALUE enum_descriptor(VALUE self) {
|
689
1210
|
return rb_ivar_get(self, descriptor_instancevar_interned);
|
690
1211
|
}
|
691
1212
|
|
692
|
-
VALUE build_module_from_enumdesc(
|
693
|
-
|
694
|
-
|
1213
|
+
VALUE build_module_from_enumdesc(VALUE _enumdesc) {
|
1214
|
+
const upb_enumdef *e = EnumDescriptor_GetEnumDef(_enumdesc);
|
1215
|
+
VALUE mod = rb_define_module_id(rb_intern(upb_enumdef_fullname(e)));
|
695
1216
|
|
696
1217
|
upb_enum_iter it;
|
697
|
-
for (upb_enum_begin(&it,
|
1218
|
+
for (upb_enum_begin(&it, e);
|
698
1219
|
!upb_enum_done(&it);
|
699
1220
|
upb_enum_next(&it)) {
|
700
1221
|
const char* name = upb_enum_iter_name(&it);
|
@@ -710,26 +1231,99 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
|
710
1231
|
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
711
1232
|
rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
|
712
1233
|
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
|
713
|
-
rb_ivar_set(mod, descriptor_instancevar_interned,
|
714
|
-
get_def_obj(enumdesc->enumdef));
|
1234
|
+
rb_ivar_set(mod, descriptor_instancevar_interned, _enumdesc);
|
715
1235
|
|
716
1236
|
return mod;
|
717
1237
|
}
|
718
1238
|
|
719
|
-
|
720
|
-
*
|
721
|
-
*
|
722
|
-
|
723
|
-
*
|
724
|
-
*
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
1239
|
+
// Internal only; used by Google::Protobuf.deep_copy.
|
1240
|
+
upb_msg* Message_deep_copy(const upb_msg* msg, const upb_msgdef* m,
|
1241
|
+
upb_arena *arena) {
|
1242
|
+
// Serialize and parse.
|
1243
|
+
upb_arena *tmp_arena = upb_arena_new();
|
1244
|
+
const upb_msglayout *layout = upb_msgdef_layout(m);
|
1245
|
+
size_t size;
|
1246
|
+
|
1247
|
+
char* data = upb_encode_ex(msg, layout, 0, tmp_arena, &size);
|
1248
|
+
upb_msg* new_msg = upb_msg_new(m, arena);
|
1249
|
+
|
1250
|
+
if (!data || !upb_decode(data, size, new_msg, layout, arena)) {
|
1251
|
+
upb_arena_free(tmp_arena);
|
1252
|
+
rb_raise(cParseError, "Error occurred copying proto");
|
1253
|
+
}
|
1254
|
+
|
1255
|
+
upb_arena_free(tmp_arena);
|
1256
|
+
return new_msg;
|
1257
|
+
}
|
1258
|
+
|
1259
|
+
const upb_msg* Message_GetUpbMessage(VALUE value, const upb_msgdef* m,
|
1260
|
+
const char* name, upb_arena* arena) {
|
1261
|
+
if (value == Qnil) {
|
1262
|
+
rb_raise(cTypeError, "nil message not allowed here.");
|
734
1263
|
}
|
1264
|
+
|
1265
|
+
VALUE klass = CLASS_OF(value);
|
1266
|
+
VALUE desc_rb = rb_ivar_get(klass, descriptor_instancevar_interned);
|
1267
|
+
const upb_msgdef* val_m =
|
1268
|
+
desc_rb == Qnil ? NULL : Descriptor_GetMsgDef(desc_rb);
|
1269
|
+
|
1270
|
+
if (val_m != m) {
|
1271
|
+
// Check for possible implicit conversions
|
1272
|
+
// TODO: hash conversion?
|
1273
|
+
|
1274
|
+
switch (upb_msgdef_wellknowntype(m)) {
|
1275
|
+
case UPB_WELLKNOWN_TIMESTAMP: {
|
1276
|
+
// Time -> Google::Protobuf::Timestamp
|
1277
|
+
upb_msg *msg = upb_msg_new(m, arena);
|
1278
|
+
upb_msgval sec, nsec;
|
1279
|
+
struct timespec time;
|
1280
|
+
const upb_fielddef *sec_f = upb_msgdef_itof(m, 1);
|
1281
|
+
const upb_fielddef *nsec_f = upb_msgdef_itof(m, 2);
|
1282
|
+
|
1283
|
+
if (!rb_obj_is_kind_of(value, rb_cTime)) goto badtype;
|
1284
|
+
|
1285
|
+
time = rb_time_timespec(value);
|
1286
|
+
sec.int64_val = time.tv_sec;
|
1287
|
+
nsec.int32_val = time.tv_nsec;
|
1288
|
+
upb_msg_set(msg, sec_f, sec, arena);
|
1289
|
+
upb_msg_set(msg, nsec_f, nsec, arena);
|
1290
|
+
return msg;
|
1291
|
+
}
|
1292
|
+
case UPB_WELLKNOWN_DURATION: {
|
1293
|
+
// Numeric -> Google::Protobuf::Duration
|
1294
|
+
upb_msg *msg = upb_msg_new(m, arena);
|
1295
|
+
upb_msgval sec, nsec;
|
1296
|
+
const upb_fielddef *sec_f = upb_msgdef_itof(m, 1);
|
1297
|
+
const upb_fielddef *nsec_f = upb_msgdef_itof(m, 2);
|
1298
|
+
|
1299
|
+
if (!rb_obj_is_kind_of(value, rb_cNumeric)) goto badtype;
|
1300
|
+
|
1301
|
+
sec.int64_val = NUM2LL(value);
|
1302
|
+
nsec.int32_val = round((NUM2DBL(value) - NUM2LL(value)) * 1000000000);
|
1303
|
+
upb_msg_set(msg, sec_f, sec, arena);
|
1304
|
+
upb_msg_set(msg, nsec_f, nsec, arena);
|
1305
|
+
return msg;
|
1306
|
+
}
|
1307
|
+
default:
|
1308
|
+
badtype:
|
1309
|
+
rb_raise(cTypeError,
|
1310
|
+
"Invalid type %s to assign to submessage field '%s'.",
|
1311
|
+
rb_class2name(CLASS_OF(value)), name);
|
1312
|
+
}
|
1313
|
+
|
1314
|
+
}
|
1315
|
+
|
1316
|
+
Message* self = ruby_to_Message(value);
|
1317
|
+
Arena_fuse(self->arena, arena);
|
1318
|
+
|
1319
|
+
return self->msg;
|
1320
|
+
}
|
1321
|
+
|
1322
|
+
void Message_register(VALUE protobuf) {
|
1323
|
+
cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
|
1324
|
+
|
1325
|
+
// Ruby-interned string: "descriptor". We use this identifier to store an
|
1326
|
+
// instance variable on message classes we create in order to link them back
|
1327
|
+
// to their descriptors.
|
1328
|
+
descriptor_instancevar_interned = rb_intern("descriptor");
|
735
1329
|
}
|