google-protobuf 3.8.0 → 3.19.3
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 +348 -0
- data/ext/google/protobuf_c/convert.h +72 -0
- data/ext/google/protobuf_c/defs.c +610 -1583
- data/ext/google/protobuf_c/defs.h +107 -0
- data/ext/google/protobuf_c/extconf.rb +4 -7
- data/ext/google/protobuf_c/map.c +315 -476
- data/ext/google/protobuf_c/map.h +67 -0
- data/ext/google/protobuf_c/message.c +928 -448
- 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 +311 -312
- data/ext/google/protobuf_c/repeated_field.h +63 -0
- data/ext/google/protobuf_c/ruby-upb.c +9171 -0
- data/ext/google/protobuf_c/ruby-upb.h +4704 -0
- 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 +458 -0
- data/lib/google/protobuf/descriptor_pb.rb +268 -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/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 +13 -2
- data/lib/google/protobuf/wrappers_pb.rb +9 -9
- data/lib/google/protobuf.rb +2 -0
- data/tests/basic.rb +329 -70
- data/tests/generated_code_test.rb +0 -0
- data/tests/stress.rb +1 -1
- metadata +21 -28
- data/ext/google/protobuf_c/encode_decode.c +0 -1614
- data/ext/google/protobuf_c/storage.c +0 -1062
- data/ext/google/protobuf_c/upb.c +0 -17480
- data/ext/google/protobuf_c/upb.h +0 -10641
@@ -28,91 +28,153 @@
|
|
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
|
-
|
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_msg* msg; // Can get as mutable when non-frozen.
|
57
|
+
const upb_msgdef* msgdef; // kept alive by self.class.descriptor reference.
|
58
|
+
} Message;
|
59
|
+
|
60
|
+
static void Message_mark(void* _self) {
|
61
|
+
Message* self = (Message *)_self;
|
62
|
+
rb_gc_mark(self->arena);
|
53
63
|
}
|
54
64
|
|
55
|
-
rb_data_type_t Message_type = {
|
65
|
+
static rb_data_type_t Message_type = {
|
56
66
|
"Message",
|
57
|
-
{ Message_mark,
|
67
|
+
{ Message_mark, RUBY_DEFAULT_FREE, NULL },
|
68
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
58
69
|
};
|
59
70
|
|
60
|
-
|
71
|
+
static Message* ruby_to_Message(VALUE msg_rb) {
|
72
|
+
Message* msg;
|
73
|
+
TypedData_Get_Struct(msg_rb, Message, &Message_type, msg);
|
74
|
+
return msg;
|
75
|
+
}
|
76
|
+
|
77
|
+
static VALUE Message_alloc(VALUE klass) {
|
61
78
|
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
|
62
|
-
|
63
|
-
MessageHeader* msg = (MessageHeader*)ALLOC_N(
|
64
|
-
uint8_t, sizeof(MessageHeader) + desc->layout->size);
|
79
|
+
Message* msg = ALLOC(Message);
|
65
80
|
VALUE ret;
|
66
81
|
|
67
|
-
|
82
|
+
msg->msgdef = Descriptor_GetMsgDef(descriptor);
|
83
|
+
msg->arena = Qnil;
|
84
|
+
msg->msg = NULL;
|
68
85
|
|
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
86
|
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
|
72
|
-
msg->descriptor = desc;
|
73
87
|
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
|
74
88
|
|
75
|
-
|
89
|
+
return ret;
|
90
|
+
}
|
91
|
+
|
92
|
+
const upb_msg *Message_Get(VALUE msg_rb, const upb_msgdef **m) {
|
93
|
+
Message* msg = ruby_to_Message(msg_rb);
|
94
|
+
if (m) *m = msg->msgdef;
|
95
|
+
return msg->msg;
|
96
|
+
}
|
76
97
|
|
77
|
-
|
98
|
+
upb_msg *Message_GetMutable(VALUE msg_rb, const upb_msgdef **m) {
|
99
|
+
rb_check_frozen(msg_rb);
|
100
|
+
return (upb_msg*)Message_Get(msg_rb, m);
|
101
|
+
}
|
78
102
|
|
79
|
-
|
103
|
+
void Message_InitPtr(VALUE self_, upb_msg *msg, VALUE arena) {
|
104
|
+
Message* self = ruby_to_Message(self_);
|
105
|
+
self->msg = msg;
|
106
|
+
self->arena = arena;
|
107
|
+
ObjectCache_Add(msg, self_);
|
80
108
|
}
|
81
109
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
const upb_fielddef* first_field;
|
87
|
-
const upb_fielddef* f;
|
110
|
+
VALUE Message_GetArena(VALUE msg_rb) {
|
111
|
+
Message* msg = ruby_to_Message(msg_rb);
|
112
|
+
return msg->arena;
|
113
|
+
}
|
88
114
|
|
89
|
-
|
90
|
-
if (
|
91
|
-
|
115
|
+
void Message_CheckClass(VALUE klass) {
|
116
|
+
if (rb_get_alloc_func(klass) != &Message_alloc) {
|
117
|
+
rb_raise(rb_eArgError,
|
118
|
+
"Message class was not returned by the DescriptorPool.");
|
92
119
|
}
|
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);
|
120
|
+
}
|
99
121
|
|
100
|
-
|
101
|
-
|
102
|
-
fields[upb_fielddef_index(first_field)].case_offset;
|
103
|
-
oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
|
122
|
+
VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena) {
|
123
|
+
if (msg == NULL) return Qnil;
|
104
124
|
|
105
|
-
|
106
|
-
|
125
|
+
VALUE val = ObjectCache_Get(msg);
|
126
|
+
|
127
|
+
if (val == Qnil) {
|
128
|
+
VALUE klass = Descriptor_DefToClass(m);
|
129
|
+
val = Message_alloc(klass);
|
130
|
+
Message_InitPtr(val, msg, arena);
|
107
131
|
}
|
108
132
|
|
109
|
-
|
110
|
-
|
111
|
-
|
133
|
+
return val;
|
134
|
+
}
|
135
|
+
|
136
|
+
void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
|
137
|
+
const upb_msgdef* m) {
|
138
|
+
bool first = true;
|
139
|
+
int n = upb_msgdef_fieldcount(m);
|
140
|
+
VALUE klass = Descriptor_DefToClass(m);
|
141
|
+
StringBuilder_Printf(b, "<%s: ", rb_class2name(klass));
|
142
|
+
|
143
|
+
for (int i = 0; i < n; i++) {
|
144
|
+
const upb_fielddef* field = upb_msgdef_field(m, i);
|
145
|
+
|
146
|
+
if (upb_fielddef_haspresence(field) && !upb_msg_has(msg, field)) {
|
147
|
+
continue;
|
148
|
+
}
|
149
|
+
|
150
|
+
if (!first) {
|
151
|
+
StringBuilder_Printf(b, ", ");
|
152
|
+
} else {
|
153
|
+
first = false;
|
154
|
+
}
|
155
|
+
|
156
|
+
upb_msgval msgval = upb_msg_get(msg, field);
|
157
|
+
|
158
|
+
StringBuilder_Printf(b, "%s: ", upb_fielddef_name(field));
|
112
159
|
|
113
|
-
|
160
|
+
if (upb_fielddef_ismap(field)) {
|
161
|
+
const upb_msgdef* entry_m = upb_fielddef_msgsubdef(field);
|
162
|
+
const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
|
163
|
+
const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
|
164
|
+
TypeInfo val_info = TypeInfo_get(val_f);
|
165
|
+
Map_Inspect(b, msgval.map_val, upb_fielddef_type(key_f), val_info);
|
166
|
+
} else if (upb_fielddef_isseq(field)) {
|
167
|
+
RepeatedField_Inspect(b, msgval.array_val, TypeInfo_get(field));
|
168
|
+
} else {
|
169
|
+
StringBuilder_PrintMsgval(b, msgval, TypeInfo_get(field));
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
StringBuilder_Printf(b, ">");
|
114
174
|
}
|
115
175
|
|
176
|
+
// Helper functions for #method_missing ////////////////////////////////////////
|
177
|
+
|
116
178
|
enum {
|
117
179
|
METHOD_UNKNOWN = 0,
|
118
180
|
METHOD_GETTER = 1,
|
@@ -125,133 +187,203 @@ enum {
|
|
125
187
|
};
|
126
188
|
|
127
189
|
// Check if the field is a well known wrapper type
|
128
|
-
static bool
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
VALUE hash = rb_hash_new();
|
146
|
-
rb_hash_aset(hash, rb_str_new2("value"), value);
|
147
|
-
VALUE args[1] = { hash };
|
148
|
-
return rb_class_new_instance(1, args, field_type_class(field));
|
190
|
+
static bool IsWrapper(const upb_fielddef* f) {
|
191
|
+
return upb_fielddef_issubmsg(f) &&
|
192
|
+
upb_msgdef_iswrapper(upb_fielddef_msgsubdef(f));
|
193
|
+
}
|
194
|
+
|
195
|
+
static bool Match(const upb_msgdef* m, const char* name, const upb_fielddef** f,
|
196
|
+
const upb_oneofdef** o, const char* prefix,
|
197
|
+
const char* suffix) {
|
198
|
+
size_t sp = strlen(prefix);
|
199
|
+
size_t ss = strlen(suffix);
|
200
|
+
size_t sn = strlen(name);
|
201
|
+
|
202
|
+
if (sn <= sp + ss) return false;
|
203
|
+
|
204
|
+
if (memcmp(name, prefix, sp) != 0 ||
|
205
|
+
memcmp(name + sn - ss, suffix, ss) != 0) {
|
206
|
+
return false;
|
149
207
|
}
|
150
|
-
|
208
|
+
|
209
|
+
return upb_msgdef_lookupname(m, name + sp, sn - sp - ss, f, o);
|
151
210
|
}
|
152
211
|
|
153
|
-
static int extract_method_call(VALUE method_name,
|
154
|
-
|
212
|
+
static int extract_method_call(VALUE method_name, Message* self,
|
213
|
+
const upb_fielddef** f, const upb_oneofdef** o) {
|
214
|
+
const upb_msgdef* m = self->msgdef;
|
215
|
+
const char* name;
|
216
|
+
|
155
217
|
Check_Type(method_name, T_SYMBOL);
|
218
|
+
name = rb_id2name(SYM2ID(method_name));
|
219
|
+
|
220
|
+
if (Match(m, name, f, o, "", "")) return METHOD_GETTER;
|
221
|
+
if (Match(m, name, f, o, "", "=")) return METHOD_SETTER;
|
222
|
+
if (Match(m, name, f, o, "clear_", "")) return METHOD_CLEAR;
|
223
|
+
if (Match(m, name, f, o, "has_", "?") &&
|
224
|
+
(*o || (*f && upb_fielddef_haspresence(*f)))) {
|
225
|
+
// Disallow oneof hazzers for proto3.
|
226
|
+
// TODO(haberman): remove this test when we are enabling oneof hazzers for
|
227
|
+
// proto3.
|
228
|
+
if (*f && !upb_fielddef_issubmsg(*f) &&
|
229
|
+
upb_fielddef_realcontainingoneof(*f) &&
|
230
|
+
upb_msgdef_syntax(upb_fielddef_containingtype(*f)) !=
|
231
|
+
UPB_SYNTAX_PROTO2) {
|
232
|
+
return METHOD_UNKNOWN;
|
233
|
+
}
|
234
|
+
return METHOD_PRESENCE;
|
235
|
+
}
|
236
|
+
if (Match(m, name, f, o, "", "_as_value") && *f && !upb_fielddef_isseq(*f) &&
|
237
|
+
IsWrapper(*f)) {
|
238
|
+
return METHOD_WRAPPER_GETTER;
|
239
|
+
}
|
240
|
+
if (Match(m, name, f, o, "", "_as_value=") && *f && !upb_fielddef_isseq(*f) &&
|
241
|
+
IsWrapper(*f)) {
|
242
|
+
return METHOD_WRAPPER_SETTER;
|
243
|
+
}
|
244
|
+
if (Match(m, name, f, o, "", "_const") && *f &&
|
245
|
+
upb_fielddef_type(*f) == UPB_TYPE_ENUM) {
|
246
|
+
return METHOD_ENUM_GETTER;
|
247
|
+
}
|
156
248
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
&test_f, &test_o)) {
|
172
|
-
accessor_type = METHOD_CLEAR;
|
173
|
-
name = name + 6;
|
174
|
-
name_len = name_len - 6;
|
175
|
-
} else if (strncmp("has_", name, 4) == 0 && name[name_len - 1] == '?' &&
|
176
|
-
!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
|
177
|
-
&test_f, &test_o)) {
|
178
|
-
accessor_type = METHOD_PRESENCE;
|
179
|
-
name = name + 4;
|
180
|
-
name_len = name_len - 5;
|
181
|
-
} else {
|
182
|
-
accessor_type = METHOD_GETTER;
|
183
|
-
}
|
184
|
-
|
185
|
-
bool has_field = upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
|
186
|
-
&test_f, &test_o);
|
187
|
-
|
188
|
-
// Look for wrapper type accessor of the form <field_name>_as_value
|
189
|
-
if (!has_field &&
|
190
|
-
(accessor_type == METHOD_GETTER || accessor_type == METHOD_SETTER) &&
|
191
|
-
name_len > 9 && strncmp(name + name_len - 9, "_as_value", 9) == 0) {
|
192
|
-
// Find the field name
|
193
|
-
char wrapper_field_name[name_len - 8];
|
194
|
-
strncpy(wrapper_field_name, name, name_len - 9);
|
195
|
-
wrapper_field_name[name_len - 7] = '\0';
|
196
|
-
|
197
|
-
// Check if field exists and is a wrapper type
|
198
|
-
const upb_oneofdef* test_o_wrapper;
|
199
|
-
const upb_fielddef* test_f_wrapper;
|
200
|
-
if (upb_msgdef_lookupname(self->descriptor->msgdef, wrapper_field_name, name_len - 9,
|
201
|
-
&test_f_wrapper, &test_o_wrapper) &&
|
202
|
-
upb_fielddef_type(test_f_wrapper) == UPB_TYPE_MESSAGE &&
|
203
|
-
is_wrapper_type_field(test_f_wrapper)) {
|
204
|
-
// It does exist!
|
205
|
-
has_field = true;
|
206
|
-
if (accessor_type == METHOD_SETTER) {
|
207
|
-
accessor_type = METHOD_WRAPPER_SETTER;
|
208
|
-
} else {
|
209
|
-
accessor_type = METHOD_WRAPPER_GETTER;
|
249
|
+
return METHOD_UNKNOWN;
|
250
|
+
}
|
251
|
+
|
252
|
+
static VALUE Message_oneof_accessor(VALUE _self, const upb_oneofdef* o,
|
253
|
+
int accessor_type) {
|
254
|
+
Message* self = ruby_to_Message(_self);
|
255
|
+
const upb_fielddef* oneof_field = upb_msg_whichoneof(self->msg, o);
|
256
|
+
|
257
|
+
switch (accessor_type) {
|
258
|
+
case METHOD_PRESENCE:
|
259
|
+
return oneof_field == NULL ? Qfalse : Qtrue;
|
260
|
+
case METHOD_CLEAR:
|
261
|
+
if (oneof_field != NULL) {
|
262
|
+
upb_msg_clearfield(Message_GetMutable(_self, NULL), oneof_field);
|
210
263
|
}
|
211
|
-
|
212
|
-
|
213
|
-
|
264
|
+
return Qnil;
|
265
|
+
case METHOD_GETTER:
|
266
|
+
return oneof_field == NULL
|
267
|
+
? Qnil
|
268
|
+
: ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
|
269
|
+
case METHOD_SETTER:
|
270
|
+
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
214
271
|
}
|
272
|
+
rb_raise(rb_eRuntimeError, "Invalid access of oneof field.");
|
273
|
+
}
|
215
274
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
if (upb_msgdef_lookupname(self->descriptor->msgdef, enum_name, name_len - 6,
|
229
|
-
&test_f_enum, &test_o_enum) &&
|
230
|
-
upb_fielddef_type(test_f_enum) == UPB_TYPE_ENUM) {
|
231
|
-
// It does exist!
|
232
|
-
has_field = true;
|
233
|
-
accessor_type = METHOD_ENUM_GETTER;
|
234
|
-
test_o = test_o_enum;
|
235
|
-
test_f = test_f_enum;
|
275
|
+
static void Message_setfield(upb_msg* msg, const upb_fielddef* f, VALUE val,
|
276
|
+
upb_arena* arena) {
|
277
|
+
upb_msgval msgval;
|
278
|
+
if (upb_fielddef_ismap(f)) {
|
279
|
+
msgval.map_val = Map_GetUpbMap(val, f, arena);
|
280
|
+
} else if (upb_fielddef_isseq(f)) {
|
281
|
+
msgval.array_val = RepeatedField_GetUpbArray(val, f, arena);
|
282
|
+
} else {
|
283
|
+
if (val == Qnil &&
|
284
|
+
(upb_fielddef_issubmsg(f) || upb_fielddef_realcontainingoneof(f))) {
|
285
|
+
upb_msg_clearfield(msg, f);
|
286
|
+
return;
|
236
287
|
}
|
288
|
+
msgval =
|
289
|
+
Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
237
290
|
}
|
291
|
+
upb_msg_set(msg, f, msgval, arena);
|
292
|
+
}
|
238
293
|
|
239
|
-
|
240
|
-
|
241
|
-
|
294
|
+
VALUE Message_getfield(VALUE _self, const upb_fielddef* f) {
|
295
|
+
Message* self = ruby_to_Message(_self);
|
296
|
+
// This is a special-case: upb_msg_mutable() for map & array are logically
|
297
|
+
// const (they will not change what is serialized) but physically
|
298
|
+
// non-const, as they do allocate a repeated field or map. The logical
|
299
|
+
// constness means it's ok to do even if the message is frozen.
|
300
|
+
upb_msg *msg = (upb_msg*)self->msg;
|
301
|
+
upb_arena *arena = Arena_get(self->arena);
|
302
|
+
if (upb_fielddef_ismap(f)) {
|
303
|
+
upb_map *map = upb_msg_mutable(msg, f, arena).map;
|
304
|
+
const upb_fielddef *key_f = map_field_key(f);
|
305
|
+
const upb_fielddef *val_f = map_field_value(f);
|
306
|
+
upb_fieldtype_t key_type = upb_fielddef_type(key_f);
|
307
|
+
TypeInfo value_type_info = TypeInfo_get(val_f);
|
308
|
+
return Map_GetRubyWrapper(map, key_type, value_type_info, self->arena);
|
309
|
+
} else if (upb_fielddef_isseq(f)) {
|
310
|
+
upb_array *arr = upb_msg_mutable(msg, f, arena).array;
|
311
|
+
return RepeatedField_GetRubyWrapper(arr, TypeInfo_get(f), self->arena);
|
312
|
+
} else if (upb_fielddef_issubmsg(f)) {
|
313
|
+
if (!upb_msg_has(self->msg, f)) return Qnil;
|
314
|
+
upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
|
315
|
+
const upb_msgdef *m = upb_fielddef_msgsubdef(f);
|
316
|
+
return Message_GetRubyWrapper(submsg, m, self->arena);
|
317
|
+
} else {
|
318
|
+
upb_msgval msgval = upb_msg_get(self->msg, f);
|
319
|
+
return Convert_UpbToRuby(msgval, TypeInfo_get(f), self->arena);
|
242
320
|
}
|
321
|
+
}
|
243
322
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
if (accessor_type == METHOD_PRESENCE && test_f != NULL &&
|
248
|
-
!upb_fielddef_haspresence(test_f)) {
|
249
|
-
return METHOD_UNKNOWN;
|
250
|
-
}
|
323
|
+
static VALUE Message_field_accessor(VALUE _self, const upb_fielddef* f,
|
324
|
+
int accessor_type, int argc, VALUE* argv) {
|
325
|
+
upb_arena *arena = Arena_get(Message_GetArena(_self));
|
251
326
|
|
252
|
-
|
253
|
-
|
254
|
-
|
327
|
+
switch (accessor_type) {
|
328
|
+
case METHOD_SETTER:
|
329
|
+
Message_setfield(Message_GetMutable(_self, NULL), f, argv[1], arena);
|
330
|
+
return Qnil;
|
331
|
+
case METHOD_CLEAR:
|
332
|
+
upb_msg_clearfield(Message_GetMutable(_self, NULL), f);
|
333
|
+
return Qnil;
|
334
|
+
case METHOD_PRESENCE:
|
335
|
+
if (!upb_fielddef_haspresence(f)) {
|
336
|
+
rb_raise(rb_eRuntimeError, "Field does not have presence.");
|
337
|
+
}
|
338
|
+
return upb_msg_has(Message_Get(_self, NULL), f);
|
339
|
+
case METHOD_WRAPPER_GETTER: {
|
340
|
+
Message* self = ruby_to_Message(_self);
|
341
|
+
if (upb_msg_has(self->msg, f)) {
|
342
|
+
PBRUBY_ASSERT(upb_fielddef_issubmsg(f) && !upb_fielddef_isseq(f));
|
343
|
+
upb_msgval wrapper = upb_msg_get(self->msg, f);
|
344
|
+
const upb_msgdef *wrapper_m = upb_fielddef_msgsubdef(f);
|
345
|
+
const upb_fielddef *value_f = upb_msgdef_itof(wrapper_m, 1);
|
346
|
+
upb_msgval value = upb_msg_get(wrapper.msg_val, value_f);
|
347
|
+
return Convert_UpbToRuby(value, TypeInfo_get(value_f), self->arena);
|
348
|
+
} else {
|
349
|
+
return Qnil;
|
350
|
+
}
|
351
|
+
}
|
352
|
+
case METHOD_WRAPPER_SETTER: {
|
353
|
+
upb_msg *msg = Message_GetMutable(_self, NULL);
|
354
|
+
if (argv[1] == Qnil) {
|
355
|
+
upb_msg_clearfield(msg, f);
|
356
|
+
} else {
|
357
|
+
const upb_fielddef *val_f = upb_msgdef_itof(upb_fielddef_msgsubdef(f), 1);
|
358
|
+
upb_msgval msgval = Convert_RubyToUpb(argv[1], upb_fielddef_name(f),
|
359
|
+
TypeInfo_get(val_f), arena);
|
360
|
+
upb_msg *wrapper = upb_msg_mutable(msg, f, arena).msg;
|
361
|
+
upb_msg_set(wrapper, val_f, msgval, arena);
|
362
|
+
}
|
363
|
+
return Qnil;
|
364
|
+
}
|
365
|
+
case METHOD_ENUM_GETTER: {
|
366
|
+
upb_msgval msgval = upb_msg_get(Message_Get(_self, NULL), f);
|
367
|
+
|
368
|
+
if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
369
|
+
// Map repeated fields to a new type with ints
|
370
|
+
VALUE arr = rb_ary_new();
|
371
|
+
size_t i, n = upb_array_size(msgval.array_val);
|
372
|
+
for (i = 0; i < n; i++) {
|
373
|
+
upb_msgval elem = upb_array_get(msgval.array_val, i);
|
374
|
+
rb_ary_push(arr, INT2NUM(elem.int32_val));
|
375
|
+
}
|
376
|
+
return arr;
|
377
|
+
} else {
|
378
|
+
return INT2NUM(msgval.int32_val);
|
379
|
+
}
|
380
|
+
}
|
381
|
+
case METHOD_GETTER:
|
382
|
+
return Message_getfield(_self, f);
|
383
|
+
default:
|
384
|
+
rb_raise(rb_eRuntimeError, "Internal error, no such accessor: %d",
|
385
|
+
accessor_type);
|
386
|
+
}
|
255
387
|
}
|
256
388
|
|
257
389
|
/*
|
@@ -281,102 +413,56 @@ static int extract_method_call(VALUE method_name, MessageHeader* self,
|
|
281
413
|
* true if the field 'fieldname' is set in the message object, else false. For
|
282
414
|
* 'proto3' syntax, calling this for a basic type field will result in an error.
|
283
415
|
*/
|
284
|
-
VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
285
|
-
|
416
|
+
static VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
417
|
+
Message* self = ruby_to_Message(_self);
|
286
418
|
const upb_oneofdef* o;
|
287
419
|
const upb_fielddef* f;
|
420
|
+
int accessor_type;
|
288
421
|
|
289
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
290
422
|
if (argc < 1) {
|
291
423
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
292
424
|
}
|
293
425
|
|
294
|
-
|
295
|
-
if (accessor_type == METHOD_UNKNOWN || (o == NULL && f == NULL) ) {
|
296
|
-
return rb_call_super(argc, argv);
|
297
|
-
} else if (accessor_type == METHOD_SETTER || accessor_type == METHOD_WRAPPER_SETTER) {
|
298
|
-
if (argc != 2) {
|
299
|
-
rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
|
300
|
-
}
|
301
|
-
rb_check_frozen(_self);
|
302
|
-
} else if (argc != 1) {
|
303
|
-
rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
|
304
|
-
}
|
426
|
+
accessor_type = extract_method_call(argv[0], self, &f, &o);
|
305
427
|
|
306
|
-
|
307
|
-
if (o != NULL) {
|
308
|
-
if (accessor_type == METHOD_SETTER) {
|
309
|
-
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
310
|
-
}
|
428
|
+
if (accessor_type == METHOD_UNKNOWN) return rb_call_super(argc, argv);
|
311
429
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
if (
|
317
|
-
|
430
|
+
// Validate argument count.
|
431
|
+
switch (accessor_type) {
|
432
|
+
case METHOD_SETTER:
|
433
|
+
case METHOD_WRAPPER_SETTER:
|
434
|
+
if (argc != 2) {
|
435
|
+
rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
|
318
436
|
}
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
}
|
325
|
-
// Otherwise we're operating on a single proto field
|
326
|
-
} else if (accessor_type == METHOD_SETTER) {
|
327
|
-
layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
|
328
|
-
return Qnil;
|
329
|
-
} else if (accessor_type == METHOD_CLEAR) {
|
330
|
-
layout_clear(self->descriptor->layout, Message_data(self), f);
|
331
|
-
return Qnil;
|
332
|
-
} else if (accessor_type == METHOD_PRESENCE) {
|
333
|
-
return layout_has(self->descriptor->layout, Message_data(self), f);
|
334
|
-
} else if (accessor_type == METHOD_WRAPPER_GETTER) {
|
335
|
-
VALUE value = layout_get(self->descriptor->layout, Message_data(self), f);
|
336
|
-
if (value != Qnil) {
|
337
|
-
value = rb_funcall(value, rb_intern("value"), 0);
|
338
|
-
}
|
339
|
-
return value;
|
340
|
-
} else if (accessor_type == METHOD_WRAPPER_SETTER) {
|
341
|
-
VALUE wrapper = ruby_wrapper_type(f, argv[1]);
|
342
|
-
layout_set(self->descriptor->layout, Message_data(self), f, wrapper);
|
343
|
-
return Qnil;
|
344
|
-
} else if (accessor_type == METHOD_ENUM_GETTER) {
|
345
|
-
VALUE enum_type = field_type_class(f);
|
346
|
-
VALUE method = rb_intern("const_get");
|
347
|
-
VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f);
|
348
|
-
|
349
|
-
// Map repeated fields to a new type with ints
|
350
|
-
if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
351
|
-
int array_size = FIX2INT(rb_funcall(raw_value, rb_intern("length"), 0));
|
352
|
-
VALUE array_args[1] = { ID2SYM(rb_intern("int64")) };
|
353
|
-
VALUE array = rb_class_new_instance(1, array_args, CLASS_OF(raw_value));
|
354
|
-
for (int i = 0; i < array_size; i++) {
|
355
|
-
VALUE entry = rb_funcall(enum_type, method, 1, rb_funcall(raw_value,
|
356
|
-
rb_intern("at"), 1, INT2NUM(i)));
|
357
|
-
rb_funcall(array, rb_intern("push"), 1, entry);
|
437
|
+
rb_check_frozen(_self);
|
438
|
+
break;
|
439
|
+
default:
|
440
|
+
if (argc != 1) {
|
441
|
+
rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
|
358
442
|
}
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
443
|
+
break;
|
444
|
+
}
|
445
|
+
|
446
|
+
// Dispatch accessor.
|
447
|
+
if (o != NULL) {
|
448
|
+
return Message_oneof_accessor(_self, o, accessor_type);
|
363
449
|
} else {
|
364
|
-
return
|
450
|
+
return Message_field_accessor(_self, f, accessor_type, argc, argv);
|
365
451
|
}
|
366
452
|
}
|
367
453
|
|
368
|
-
|
369
|
-
|
370
|
-
MessageHeader* self;
|
454
|
+
static VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
455
|
+
Message* self = ruby_to_Message(_self);
|
371
456
|
const upb_oneofdef* o;
|
372
457
|
const upb_fielddef* f;
|
458
|
+
int accessor_type;
|
373
459
|
|
374
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
375
460
|
if (argc < 1) {
|
376
461
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
377
462
|
}
|
378
463
|
|
379
|
-
|
464
|
+
accessor_type = extract_method_call(argv[0], self, &f, &o);
|
465
|
+
|
380
466
|
if (accessor_type == METHOD_UNKNOWN) {
|
381
467
|
return rb_call_super(argc, argv);
|
382
468
|
} else if (o != NULL) {
|
@@ -386,22 +472,116 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
|
386
472
|
}
|
387
473
|
}
|
388
474
|
|
389
|
-
|
390
|
-
|
391
|
-
|
475
|
+
void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
|
476
|
+
upb_arena* arena);
|
477
|
+
|
478
|
+
typedef struct {
|
479
|
+
upb_map *map;
|
480
|
+
TypeInfo key_type;
|
481
|
+
TypeInfo val_type;
|
482
|
+
upb_arena *arena;
|
483
|
+
} MapInit;
|
484
|
+
|
485
|
+
static int Map_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
486
|
+
MapInit *map_init = (MapInit*)_self;
|
487
|
+
upb_msgval k, v;
|
488
|
+
k = Convert_RubyToUpb(key, "", map_init->key_type, NULL);
|
489
|
+
|
490
|
+
if (map_init->val_type.type == UPB_TYPE_MESSAGE && TYPE(val) == T_HASH) {
|
491
|
+
upb_msg *msg = upb_msg_new(map_init->val_type.def.msgdef, map_init->arena);
|
492
|
+
Message_InitFromValue(msg, map_init->val_type.def.msgdef, val,
|
493
|
+
map_init->arena);
|
494
|
+
v.msg_val = msg;
|
495
|
+
} else {
|
496
|
+
v = Convert_RubyToUpb(val, "", map_init->val_type, map_init->arena);
|
497
|
+
}
|
498
|
+
upb_map_set(map_init->map, k, v, map_init->arena);
|
499
|
+
return ST_CONTINUE;
|
500
|
+
}
|
392
501
|
|
393
|
-
|
394
|
-
|
502
|
+
static void Map_InitFromValue(upb_map* map, const upb_fielddef* f, VALUE val,
|
503
|
+
upb_arena* arena) {
|
504
|
+
const upb_msgdef* entry_m = upb_fielddef_msgsubdef(f);
|
505
|
+
const upb_fielddef* key_f = upb_msgdef_itof(entry_m, 1);
|
506
|
+
const upb_fielddef* val_f = upb_msgdef_itof(entry_m, 2);
|
507
|
+
if (TYPE(val) != T_HASH) {
|
508
|
+
rb_raise(rb_eArgError,
|
509
|
+
"Expected Hash object as initializer value for map field '%s' "
|
510
|
+
"(given %s).",
|
511
|
+
upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
|
512
|
+
}
|
513
|
+
MapInit map_init = {map, TypeInfo_get(key_f), TypeInfo_get(val_f), arena};
|
514
|
+
rb_hash_foreach(val, Map_initialize_kwarg, (VALUE)&map_init);
|
515
|
+
}
|
395
516
|
|
396
|
-
|
397
|
-
|
517
|
+
static upb_msgval MessageValue_FromValue(VALUE val, TypeInfo info,
|
518
|
+
upb_arena* arena) {
|
519
|
+
if (info.type == UPB_TYPE_MESSAGE) {
|
520
|
+
upb_msgval msgval;
|
521
|
+
upb_msg* msg = upb_msg_new(info.def.msgdef, arena);
|
522
|
+
Message_InitFromValue(msg, info.def.msgdef, val, arena);
|
523
|
+
msgval.msg_val = msg;
|
524
|
+
return msgval;
|
525
|
+
} else {
|
526
|
+
return Convert_RubyToUpb(val, "", info, arena);
|
527
|
+
}
|
398
528
|
}
|
399
529
|
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
530
|
+
static void RepeatedField_InitFromValue(upb_array* arr, const upb_fielddef* f,
|
531
|
+
VALUE val, upb_arena* arena) {
|
532
|
+
TypeInfo type_info = TypeInfo_get(f);
|
533
|
+
|
534
|
+
if (TYPE(val) != T_ARRAY) {
|
535
|
+
rb_raise(rb_eArgError,
|
536
|
+
"Expected array as initializer value for repeated field '%s' (given %s).",
|
537
|
+
upb_fielddef_name(f), rb_class2name(CLASS_OF(val)));
|
538
|
+
}
|
539
|
+
|
540
|
+
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
541
|
+
VALUE entry = rb_ary_entry(val, i);
|
542
|
+
upb_msgval msgval;
|
543
|
+
if (upb_fielddef_issubmsg(f) && TYPE(entry) == T_HASH) {
|
544
|
+
msgval = MessageValue_FromValue(entry, type_info, arena);
|
545
|
+
} else {
|
546
|
+
msgval = Convert_RubyToUpb(entry, upb_fielddef_name(f), type_info, arena);
|
547
|
+
}
|
548
|
+
upb_array_append(arr, msgval, arena);
|
549
|
+
}
|
550
|
+
}
|
551
|
+
|
552
|
+
static void Message_InitFieldFromValue(upb_msg* msg, const upb_fielddef* f,
|
553
|
+
VALUE val, upb_arena* arena) {
|
554
|
+
if (TYPE(val) == T_NIL) return;
|
555
|
+
|
556
|
+
if (upb_fielddef_ismap(f)) {
|
557
|
+
upb_map *map = upb_msg_mutable(msg, f, arena).map;
|
558
|
+
Map_InitFromValue(map, f, val, arena);
|
559
|
+
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
560
|
+
upb_array *arr = upb_msg_mutable(msg, f, arena).array;
|
561
|
+
RepeatedField_InitFromValue(arr, f, val, arena);
|
562
|
+
} else if (upb_fielddef_issubmsg(f)) {
|
563
|
+
if (TYPE(val) == T_HASH) {
|
564
|
+
upb_msg *submsg = upb_msg_mutable(msg, f, arena).msg;
|
565
|
+
Message_InitFromValue(submsg, upb_fielddef_msgsubdef(f), val, arena);
|
566
|
+
} else {
|
567
|
+
Message_setfield(msg, f, val, arena);
|
568
|
+
}
|
569
|
+
} else {
|
570
|
+
upb_msgval msgval =
|
571
|
+
Convert_RubyToUpb(val, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
572
|
+
upb_msg_set(msg, f, msgval, arena);
|
573
|
+
}
|
574
|
+
}
|
575
|
+
|
576
|
+
typedef struct {
|
577
|
+
upb_msg *msg;
|
578
|
+
const upb_msgdef *msgdef;
|
579
|
+
upb_arena *arena;
|
580
|
+
} MsgInit;
|
581
|
+
|
582
|
+
static int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
583
|
+
MsgInit *msg_init = (MsgInit*)_self;
|
584
|
+
const char *name;
|
405
585
|
|
406
586
|
if (TYPE(key) == T_STRING) {
|
407
587
|
name = RSTRING_PTR(key);
|
@@ -412,51 +592,26 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
412
592
|
"Expected string or symbols as hash keys when initializing proto from hash.");
|
413
593
|
}
|
414
594
|
|
415
|
-
f = upb_msgdef_ntofz(
|
595
|
+
const upb_fielddef* f = upb_msgdef_ntofz(msg_init->msgdef, name);
|
596
|
+
|
416
597
|
if (f == NULL) {
|
417
598
|
rb_raise(rb_eArgError,
|
418
599
|
"Unknown field name '%s' in initialization map entry.", name);
|
419
600
|
}
|
420
601
|
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
if (is_map_field(f)) {
|
426
|
-
VALUE map;
|
427
|
-
|
428
|
-
if (TYPE(val) != T_HASH) {
|
429
|
-
rb_raise(rb_eArgError,
|
430
|
-
"Expected Hash object as initializer value for map field '%s' (given %s).",
|
431
|
-
name, rb_class2name(CLASS_OF(val)));
|
432
|
-
}
|
433
|
-
map = layout_get(self->descriptor->layout, Message_data(self), f);
|
434
|
-
Map_merge_into_self(map, val);
|
435
|
-
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
436
|
-
VALUE ary;
|
437
|
-
|
438
|
-
if (TYPE(val) != T_ARRAY) {
|
439
|
-
rb_raise(rb_eArgError,
|
440
|
-
"Expected array as initializer value for repeated field '%s' (given %s).",
|
441
|
-
name, rb_class2name(CLASS_OF(val)));
|
442
|
-
}
|
443
|
-
ary = layout_get(self->descriptor->layout, Message_data(self), f);
|
444
|
-
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
445
|
-
VALUE entry = rb_ary_entry(val, i);
|
446
|
-
if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
|
447
|
-
entry = create_submsg_from_hash(f, entry);
|
448
|
-
}
|
602
|
+
Message_InitFieldFromValue(msg_init->msg, f, val, msg_init->arena);
|
603
|
+
return ST_CONTINUE;
|
604
|
+
}
|
449
605
|
|
450
|
-
|
451
|
-
|
606
|
+
void Message_InitFromValue(upb_msg* msg, const upb_msgdef* m, VALUE val,
|
607
|
+
upb_arena* arena) {
|
608
|
+
MsgInit msg_init = {msg, m, arena};
|
609
|
+
if (TYPE(val) == T_HASH) {
|
610
|
+
rb_hash_foreach(val, Message_initialize_kwarg, (VALUE)&msg_init);
|
452
611
|
} else {
|
453
|
-
|
454
|
-
|
455
|
-
}
|
456
|
-
|
457
|
-
layout_set(self->descriptor->layout, Message_data(self), f, val);
|
612
|
+
rb_raise(rb_eArgError, "Expected hash arguments or message, not %s",
|
613
|
+
rb_class2name(CLASS_OF(val)));
|
458
614
|
}
|
459
|
-
return 0;
|
460
615
|
}
|
461
616
|
|
462
617
|
/*
|
@@ -471,8 +626,13 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
471
626
|
* have been added to a pool. The method definitions described here on the
|
472
627
|
* Message class are provided on each concrete message class.
|
473
628
|
*/
|
474
|
-
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
475
|
-
|
629
|
+
static VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
630
|
+
Message* self = ruby_to_Message(_self);
|
631
|
+
VALUE arena_rb = Arena_new();
|
632
|
+
upb_arena *arena = Arena_get(arena_rb);
|
633
|
+
upb_msg *msg = upb_msg_new(self->msgdef, arena);
|
634
|
+
|
635
|
+
Message_InitPtr(_self, msg, arena_rb);
|
476
636
|
|
477
637
|
if (argc == 0) {
|
478
638
|
return Qnil;
|
@@ -480,12 +640,7 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
480
640
|
if (argc != 1) {
|
481
641
|
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
482
642
|
}
|
483
|
-
|
484
|
-
if (TYPE(hash_args) != T_HASH) {
|
485
|
-
rb_raise(rb_eArgError, "Expected hash arguments.");
|
486
|
-
}
|
487
|
-
|
488
|
-
rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
|
643
|
+
Message_InitFromValue((upb_msg*)self->msg, self->msgdef, argv[0], arena);
|
489
644
|
return Qnil;
|
490
645
|
}
|
491
646
|
|
@@ -495,37 +650,40 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
495
650
|
*
|
496
651
|
* Performs a shallow copy of this message and returns the new copy.
|
497
652
|
*/
|
498
|
-
VALUE Message_dup(VALUE _self) {
|
499
|
-
|
500
|
-
VALUE new_msg;
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
Message_data(new_msg_self),
|
509
|
-
Message_data(self));
|
510
|
-
|
653
|
+
static VALUE Message_dup(VALUE _self) {
|
654
|
+
Message* self = ruby_to_Message(_self);
|
655
|
+
VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
656
|
+
Message* new_msg_self = ruby_to_Message(new_msg);
|
657
|
+
size_t size = upb_msgdef_layout(self->msgdef)->size;
|
658
|
+
|
659
|
+
// TODO(copy unknown fields?)
|
660
|
+
// TODO(use official upb msg copy function)
|
661
|
+
memcpy((upb_msg*)new_msg_self->msg, self->msg, size);
|
662
|
+
Arena_fuse(self->arena, Arena_get(new_msg_self->arena));
|
511
663
|
return new_msg;
|
512
664
|
}
|
513
665
|
|
514
|
-
//
|
515
|
-
|
516
|
-
|
517
|
-
MessageHeader* new_msg_self;
|
518
|
-
VALUE new_msg;
|
519
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
666
|
+
// Support function for Message_eq, and also used by other #eq functions.
|
667
|
+
bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m) {
|
668
|
+
if (m1 == m2) return true;
|
520
669
|
|
521
|
-
|
522
|
-
|
670
|
+
size_t size1, size2;
|
671
|
+
int encode_opts = UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC;
|
672
|
+
upb_arena *arena_tmp = upb_arena_new();
|
673
|
+
const upb_msglayout *layout = upb_msgdef_layout(m);
|
523
674
|
|
524
|
-
|
525
|
-
|
526
|
-
|
675
|
+
// Compare deterministically serialized payloads with no unknown fields.
|
676
|
+
char *data1 = upb_encode_ex(m1, layout, encode_opts, arena_tmp, &size1);
|
677
|
+
char *data2 = upb_encode_ex(m2, layout, encode_opts, arena_tmp, &size2);
|
527
678
|
|
528
|
-
|
679
|
+
if (data1 && data2) {
|
680
|
+
bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
|
681
|
+
upb_arena_free(arena_tmp);
|
682
|
+
return ret;
|
683
|
+
} else {
|
684
|
+
upb_arena_free(arena_tmp);
|
685
|
+
rb_raise(cParseError, "Error comparing messages");
|
686
|
+
}
|
529
687
|
}
|
530
688
|
|
531
689
|
/*
|
@@ -537,22 +695,34 @@ VALUE Message_deep_copy(VALUE _self) {
|
|
537
695
|
* method's semantics (a more efficient comparison may actually be done if the
|
538
696
|
* field is of a primitive type).
|
539
697
|
*/
|
540
|
-
VALUE Message_eq(VALUE _self, VALUE _other) {
|
541
|
-
|
542
|
-
MessageHeader* other;
|
543
|
-
if (TYPE(_self) != TYPE(_other)) {
|
544
|
-
return Qfalse;
|
545
|
-
}
|
546
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
547
|
-
TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
|
698
|
+
static VALUE Message_eq(VALUE _self, VALUE _other) {
|
699
|
+
if (CLASS_OF(_self) != CLASS_OF(_other)) return Qfalse;
|
548
700
|
|
549
|
-
|
550
|
-
|
551
|
-
|
701
|
+
Message* self = ruby_to_Message(_self);
|
702
|
+
Message* other = ruby_to_Message(_other);
|
703
|
+
assert(self->msgdef == other->msgdef);
|
704
|
+
|
705
|
+
return Message_Equal(self->msg, other->msg, self->msgdef) ? Qtrue : Qfalse;
|
706
|
+
}
|
707
|
+
|
708
|
+
uint64_t Message_Hash(const upb_msg* msg, const upb_msgdef* m, uint64_t seed) {
|
709
|
+
upb_arena *arena = upb_arena_new();
|
710
|
+
const char *data;
|
711
|
+
size_t size;
|
552
712
|
|
553
|
-
|
554
|
-
|
555
|
-
|
713
|
+
// Hash a deterministically serialized payloads with no unknown fields.
|
714
|
+
data = upb_encode_ex(msg, upb_msgdef_layout(m),
|
715
|
+
UPB_ENCODE_SKIPUNKNOWN | UPB_ENCODE_DETERMINISTIC, arena,
|
716
|
+
&size);
|
717
|
+
|
718
|
+
if (data) {
|
719
|
+
uint64_t ret = Wyhash(data, size, seed, kWyhashSalt);
|
720
|
+
upb_arena_free(arena);
|
721
|
+
return ret;
|
722
|
+
} else {
|
723
|
+
upb_arena_free(arena);
|
724
|
+
rb_raise(cParseError, "Error calculating hash");
|
725
|
+
}
|
556
726
|
}
|
557
727
|
|
558
728
|
/*
|
@@ -561,11 +731,12 @@ VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
561
731
|
*
|
562
732
|
* Returns a hash value that represents this message's field values.
|
563
733
|
*/
|
564
|
-
VALUE Message_hash(VALUE _self) {
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
734
|
+
static VALUE Message_hash(VALUE _self) {
|
735
|
+
Message* self = ruby_to_Message(_self);
|
736
|
+
uint64_t hash_value = Message_Hash(self->msg, self->msgdef, 0);
|
737
|
+
// RUBY_FIXNUM_MAX should be one less than a power of 2.
|
738
|
+
assert((RUBY_FIXNUM_MAX & (RUBY_FIXNUM_MAX + 1)) == 0);
|
739
|
+
return INT2FIX(hash_value & RUBY_FIXNUM_MAX);
|
569
740
|
}
|
570
741
|
|
571
742
|
/*
|
@@ -576,75 +747,127 @@ VALUE Message_hash(VALUE _self) {
|
|
576
747
|
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
|
577
748
|
* field's value is represented according to its own #inspect method.
|
578
749
|
*/
|
579
|
-
VALUE Message_inspect(VALUE _self) {
|
580
|
-
|
581
|
-
VALUE str;
|
582
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
750
|
+
static VALUE Message_inspect(VALUE _self) {
|
751
|
+
Message* self = ruby_to_Message(_self);
|
583
752
|
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
str = rb_str_cat2(str, ">");
|
590
|
-
return str;
|
753
|
+
StringBuilder* builder = StringBuilder_New();
|
754
|
+
Message_PrintMessage(builder, self->msg, self->msgdef);
|
755
|
+
VALUE ret = StringBuilder_ToRubyString(builder);
|
756
|
+
StringBuilder_Free(builder);
|
757
|
+
return ret;
|
591
758
|
}
|
592
759
|
|
593
|
-
|
594
|
-
|
595
|
-
*
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
760
|
+
// Support functions for Message_to_h //////////////////////////////////////////
|
761
|
+
|
762
|
+
static VALUE RepeatedField_CreateArray(const upb_array* arr,
|
763
|
+
TypeInfo type_info) {
|
764
|
+
int size = arr ? upb_array_size(arr) : 0;
|
765
|
+
VALUE ary = rb_ary_new2(size);
|
766
|
+
|
767
|
+
for (int i = 0; i < size; i++) {
|
768
|
+
upb_msgval msgval = upb_array_get(arr, i);
|
769
|
+
VALUE val = Scalar_CreateHash(msgval, type_info);
|
770
|
+
rb_ary_push(ary, val);
|
771
|
+
}
|
772
|
+
|
773
|
+
return ary;
|
774
|
+
}
|
775
|
+
|
776
|
+
static VALUE Message_CreateHash(const upb_msg *msg, const upb_msgdef *m) {
|
777
|
+
if (!msg) return Qnil;
|
778
|
+
|
779
|
+
VALUE hash = rb_hash_new();
|
780
|
+
int n = upb_msgdef_fieldcount(m);
|
781
|
+
bool is_proto2;
|
782
|
+
|
783
|
+
// We currently have a few behaviors that are specific to proto2.
|
784
|
+
// This is unfortunate, we should key behaviors off field attributes (like
|
785
|
+
// whether a field has presence), not proto2 vs. proto3. We should see if we
|
786
|
+
// can change this without breaking users.
|
787
|
+
is_proto2 = upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2;
|
788
|
+
|
789
|
+
for (int i = 0; i < n; i++) {
|
790
|
+
const upb_fielddef* field = upb_msgdef_field(m, i);
|
791
|
+
TypeInfo type_info = TypeInfo_get(field);
|
792
|
+
upb_msgval msgval;
|
793
|
+
VALUE msg_value;
|
794
|
+
VALUE msg_key;
|
795
|
+
|
796
|
+
if (!is_proto2 && upb_fielddef_issubmsg(field) &&
|
797
|
+
!upb_fielddef_isseq(field) && !upb_msg_has(msg, field)) {
|
798
|
+
// TODO: Legacy behavior, remove when we fix the is_proto2 differences.
|
799
|
+
msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
800
|
+
rb_hash_aset(hash, msg_key, Qnil);
|
616
801
|
continue;
|
617
802
|
}
|
618
803
|
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
|
625
|
-
msg_value = RepeatedField_to_ary(msg_value);
|
626
|
-
if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
|
627
|
-
RARRAY_LEN(msg_value) == 0) {
|
628
|
-
continue;
|
629
|
-
}
|
804
|
+
// Do not include fields that are not present (oneof or optional fields).
|
805
|
+
if (is_proto2 && upb_fielddef_haspresence(field) &&
|
806
|
+
!upb_msg_has(msg, field)) {
|
807
|
+
continue;
|
808
|
+
}
|
630
809
|
|
631
|
-
|
632
|
-
|
633
|
-
VALUE elem = rb_ary_entry(msg_value, i);
|
634
|
-
rb_ary_store(msg_value, i, Message_to_h(elem));
|
635
|
-
}
|
636
|
-
}
|
810
|
+
msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
811
|
+
msgval = upb_msg_get(msg, field);
|
637
812
|
|
638
|
-
|
639
|
-
|
640
|
-
|
813
|
+
// Proto2 omits empty map/repeated filds also.
|
814
|
+
|
815
|
+
if (upb_fielddef_ismap(field)) {
|
816
|
+
const upb_msgdef *entry_m = upb_fielddef_msgsubdef(field);
|
817
|
+
const upb_fielddef *key_f = upb_msgdef_itof(entry_m, 1);
|
818
|
+
const upb_fielddef *val_f = upb_msgdef_itof(entry_m, 2);
|
819
|
+
upb_fieldtype_t key_type = upb_fielddef_type(key_f);
|
820
|
+
msg_value = Map_CreateHash(msgval.map_val, key_type, TypeInfo_get(val_f));
|
821
|
+
} else if (upb_fielddef_isseq(field)) {
|
822
|
+
if (is_proto2 &&
|
823
|
+
(!msgval.array_val || upb_array_size(msgval.array_val) == 0)) {
|
824
|
+
continue;
|
825
|
+
}
|
826
|
+
msg_value = RepeatedField_CreateArray(msgval.array_val, type_info);
|
827
|
+
} else {
|
828
|
+
msg_value = Scalar_CreateHash(msgval, type_info);
|
641
829
|
}
|
830
|
+
|
642
831
|
rb_hash_aset(hash, msg_key, msg_value);
|
643
832
|
}
|
833
|
+
|
644
834
|
return hash;
|
645
835
|
}
|
646
836
|
|
837
|
+
VALUE Scalar_CreateHash(upb_msgval msgval, TypeInfo type_info) {
|
838
|
+
if (type_info.type == UPB_TYPE_MESSAGE) {
|
839
|
+
return Message_CreateHash(msgval.msg_val, type_info.def.msgdef);
|
840
|
+
} else {
|
841
|
+
return Convert_UpbToRuby(msgval, type_info, Qnil);
|
842
|
+
}
|
843
|
+
}
|
647
844
|
|
845
|
+
/*
|
846
|
+
* call-seq:
|
847
|
+
* Message.to_h => {}
|
848
|
+
*
|
849
|
+
* Returns the message as a Ruby Hash object, with keys as symbols.
|
850
|
+
*/
|
851
|
+
static VALUE Message_to_h(VALUE _self) {
|
852
|
+
Message* self = ruby_to_Message(_self);
|
853
|
+
return Message_CreateHash(self->msg, self->msgdef);
|
854
|
+
}
|
855
|
+
|
856
|
+
/*
|
857
|
+
* call-seq:
|
858
|
+
* Message.freeze => self
|
859
|
+
*
|
860
|
+
* Freezes the message object. We have to intercept this so we can pin the
|
861
|
+
* Ruby object into memory so we don't forget it's frozen.
|
862
|
+
*/
|
863
|
+
static VALUE Message_freeze(VALUE _self) {
|
864
|
+
Message* self = ruby_to_Message(_self);
|
865
|
+
if (!RB_OBJ_FROZEN(_self)) {
|
866
|
+
Arena_Pin(self->arena, _self);
|
867
|
+
RB_OBJ_FREEZE(_self);
|
868
|
+
}
|
869
|
+
return _self;
|
870
|
+
}
|
648
871
|
|
649
872
|
/*
|
650
873
|
* call-seq:
|
@@ -653,16 +876,18 @@ VALUE Message_to_h(VALUE _self) {
|
|
653
876
|
* Accesses a field's value by field name. The provided field name should be a
|
654
877
|
* string.
|
655
878
|
*/
|
656
|
-
VALUE Message_index(VALUE _self, VALUE field_name) {
|
657
|
-
|
879
|
+
static VALUE Message_index(VALUE _self, VALUE field_name) {
|
880
|
+
Message* self = ruby_to_Message(_self);
|
658
881
|
const upb_fielddef* field;
|
659
|
-
|
882
|
+
|
660
883
|
Check_Type(field_name, T_STRING);
|
661
|
-
field = upb_msgdef_ntofz(self->
|
884
|
+
field = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
|
885
|
+
|
662
886
|
if (field == NULL) {
|
663
887
|
return Qnil;
|
664
888
|
}
|
665
|
-
|
889
|
+
|
890
|
+
return Message_getfield(_self, field);
|
666
891
|
}
|
667
892
|
|
668
893
|
/*
|
@@ -672,19 +897,209 @@ VALUE Message_index(VALUE _self, VALUE field_name) {
|
|
672
897
|
* Sets a field's value by field name. The provided field name should be a
|
673
898
|
* string.
|
674
899
|
*/
|
675
|
-
VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
676
|
-
|
677
|
-
const upb_fielddef*
|
678
|
-
|
900
|
+
static VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
901
|
+
Message* self = ruby_to_Message(_self);
|
902
|
+
const upb_fielddef* f;
|
903
|
+
upb_msgval val;
|
904
|
+
upb_arena *arena = Arena_get(self->arena);
|
905
|
+
|
679
906
|
Check_Type(field_name, T_STRING);
|
680
|
-
|
681
|
-
|
907
|
+
f = upb_msgdef_ntofz(self->msgdef, RSTRING_PTR(field_name));
|
908
|
+
|
909
|
+
if (f == NULL) {
|
682
910
|
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
683
911
|
}
|
684
|
-
|
912
|
+
|
913
|
+
val = Convert_RubyToUpb(value, upb_fielddef_name(f), TypeInfo_get(f), arena);
|
914
|
+
upb_msg_set(Message_GetMutable(_self, NULL), f, val, arena);
|
915
|
+
|
685
916
|
return Qnil;
|
686
917
|
}
|
687
918
|
|
919
|
+
/*
|
920
|
+
* call-seq:
|
921
|
+
* MessageClass.decode(data) => message
|
922
|
+
*
|
923
|
+
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
924
|
+
* format) under the interpretration given by this message class's definition
|
925
|
+
* and returns a message object with the corresponding field values.
|
926
|
+
*/
|
927
|
+
static VALUE Message_decode(VALUE klass, VALUE data) {
|
928
|
+
if (TYPE(data) != T_STRING) {
|
929
|
+
rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
|
930
|
+
}
|
931
|
+
|
932
|
+
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
933
|
+
Message* msg = ruby_to_Message(msg_rb);
|
934
|
+
|
935
|
+
if (!upb_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
|
936
|
+
upb_msgdef_layout(msg->msgdef),
|
937
|
+
Arena_get(msg->arena))) {
|
938
|
+
rb_raise(cParseError, "Error occurred during parsing");
|
939
|
+
}
|
940
|
+
|
941
|
+
return msg_rb;
|
942
|
+
}
|
943
|
+
|
944
|
+
/*
|
945
|
+
* call-seq:
|
946
|
+
* MessageClass.decode_json(data, options = {}) => message
|
947
|
+
*
|
948
|
+
* Decodes the given data (as a string containing bytes in protocol buffers wire
|
949
|
+
* format) under the interpretration given by this message class's definition
|
950
|
+
* and returns a message object with the corresponding field values.
|
951
|
+
*
|
952
|
+
* @param options [Hash] options for the decoder
|
953
|
+
* ignore_unknown_fields: set true to ignore unknown fields (default is to
|
954
|
+
* raise an error)
|
955
|
+
*/
|
956
|
+
static VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
|
957
|
+
VALUE data = argv[0];
|
958
|
+
int options = 0;
|
959
|
+
upb_status status;
|
960
|
+
|
961
|
+
// TODO(haberman): use this message's pool instead.
|
962
|
+
const upb_symtab *symtab = DescriptorPool_GetSymtab(generated_pool);
|
963
|
+
|
964
|
+
if (argc < 1 || argc > 2) {
|
965
|
+
rb_raise(rb_eArgError, "Expected 1 or 2 arguments.");
|
966
|
+
}
|
967
|
+
|
968
|
+
if (argc == 2) {
|
969
|
+
VALUE hash_args = argv[1];
|
970
|
+
if (TYPE(hash_args) != T_HASH) {
|
971
|
+
rb_raise(rb_eArgError, "Expected hash arguments.");
|
972
|
+
}
|
973
|
+
|
974
|
+
if (RTEST(rb_hash_lookup2( hash_args, ID2SYM(rb_intern("ignore_unknown_fields")), Qfalse))) {
|
975
|
+
options |= UPB_JSONDEC_IGNOREUNKNOWN;
|
976
|
+
}
|
977
|
+
}
|
978
|
+
|
979
|
+
if (TYPE(data) != T_STRING) {
|
980
|
+
rb_raise(rb_eArgError, "Expected string for JSON data.");
|
981
|
+
}
|
982
|
+
|
983
|
+
// TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
|
984
|
+
// convert, because string handlers pass data directly to message string
|
985
|
+
// fields.
|
986
|
+
|
987
|
+
VALUE msg_rb = initialize_rb_class_with_no_args(klass);
|
988
|
+
Message* msg = ruby_to_Message(msg_rb);
|
989
|
+
|
990
|
+
// We don't allow users to decode a wrapper type directly.
|
991
|
+
if (upb_msgdef_iswrapper(msg->msgdef)) {
|
992
|
+
rb_raise(rb_eRuntimeError, "Cannot parse a wrapper directly.");
|
993
|
+
}
|
994
|
+
|
995
|
+
upb_status_clear(&status);
|
996
|
+
if (!upb_json_decode(RSTRING_PTR(data), RSTRING_LEN(data), (upb_msg*)msg->msg,
|
997
|
+
msg->msgdef, symtab, options,
|
998
|
+
Arena_get(msg->arena), &status)) {
|
999
|
+
rb_raise(cParseError, "Error occurred during parsing: %s",
|
1000
|
+
upb_status_errmsg(&status));
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
return msg_rb;
|
1004
|
+
}
|
1005
|
+
|
1006
|
+
/*
|
1007
|
+
* call-seq:
|
1008
|
+
* MessageClass.encode(msg) => bytes
|
1009
|
+
*
|
1010
|
+
* Encodes the given message object to its serialized form in protocol buffers
|
1011
|
+
* wire format.
|
1012
|
+
*/
|
1013
|
+
static VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
1014
|
+
Message* msg = ruby_to_Message(msg_rb);
|
1015
|
+
const char *data;
|
1016
|
+
size_t size;
|
1017
|
+
|
1018
|
+
if (CLASS_OF(msg_rb) != klass) {
|
1019
|
+
rb_raise(rb_eArgError, "Message of wrong type.");
|
1020
|
+
}
|
1021
|
+
|
1022
|
+
upb_arena *arena = upb_arena_new();
|
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
|
+
|
688
1103
|
/*
|
689
1104
|
* call-seq:
|
690
1105
|
* Message.descriptor => descriptor
|
@@ -692,22 +1107,15 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
692
1107
|
* Class method that returns the Descriptor instance corresponding to this
|
693
1108
|
* message class's type.
|
694
1109
|
*/
|
695
|
-
VALUE Message_descriptor(VALUE klass) {
|
1110
|
+
static VALUE Message_descriptor(VALUE klass) {
|
696
1111
|
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
697
1112
|
}
|
698
1113
|
|
699
|
-
VALUE build_class_from_descriptor(
|
1114
|
+
VALUE build_class_from_descriptor(VALUE descriptor) {
|
700
1115
|
const char *name;
|
701
1116
|
VALUE klass;
|
702
1117
|
|
703
|
-
|
704
|
-
desc->layout = create_layout(desc->msgdef);
|
705
|
-
}
|
706
|
-
if (desc->fill_method == NULL) {
|
707
|
-
desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
|
708
|
-
}
|
709
|
-
|
710
|
-
name = upb_msgdef_fullname(desc->msgdef);
|
1118
|
+
name = upb_msgdef_fullname(Descriptor_GetMsgDef(descriptor));
|
711
1119
|
if (name == NULL) {
|
712
1120
|
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
713
1121
|
}
|
@@ -717,8 +1125,7 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
717
1125
|
// their own toplevel constant class name.
|
718
1126
|
rb_intern("Message"),
|
719
1127
|
rb_cObject);
|
720
|
-
rb_ivar_set(klass, descriptor_instancevar_interned,
|
721
|
-
get_def_obj(desc->msgdef));
|
1128
|
+
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
|
722
1129
|
rb_define_alloc_func(klass, Message_alloc);
|
723
1130
|
rb_require("google/protobuf/message_exts");
|
724
1131
|
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
|
@@ -735,9 +1142,9 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
735
1142
|
rb_define_method(klass, "clone", Message_dup, 0);
|
736
1143
|
rb_define_method(klass, "==", Message_eq, 1);
|
737
1144
|
rb_define_method(klass, "eql?", Message_eq, 1);
|
1145
|
+
rb_define_method(klass, "freeze", Message_freeze, 0);
|
738
1146
|
rb_define_method(klass, "hash", Message_hash, 0);
|
739
1147
|
rb_define_method(klass, "to_h", Message_to_h, 0);
|
740
|
-
rb_define_method(klass, "to_hash", Message_to_h, 0);
|
741
1148
|
rb_define_method(klass, "inspect", Message_inspect, 0);
|
742
1149
|
rb_define_method(klass, "to_s", Message_inspect, 0);
|
743
1150
|
rb_define_method(klass, "[]", Message_index, 1);
|
@@ -758,12 +1165,12 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
758
1165
|
* This module method, provided on each generated enum module, looks up an enum
|
759
1166
|
* value by number and returns its name as a Ruby symbol, or nil if not found.
|
760
1167
|
*/
|
761
|
-
VALUE enum_lookup(VALUE self, VALUE number) {
|
1168
|
+
static VALUE enum_lookup(VALUE self, VALUE number) {
|
762
1169
|
int32_t num = NUM2INT(number);
|
763
1170
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
764
|
-
|
1171
|
+
const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
|
765
1172
|
|
766
|
-
const char* name = upb_enumdef_iton(
|
1173
|
+
const char* name = upb_enumdef_iton(e, num);
|
767
1174
|
if (name == NULL) {
|
768
1175
|
return Qnil;
|
769
1176
|
} else {
|
@@ -778,13 +1185,13 @@ VALUE enum_lookup(VALUE self, VALUE number) {
|
|
778
1185
|
* This module method, provided on each generated enum module, looks up an enum
|
779
1186
|
* value by name (as a Ruby symbol) and returns its name, or nil if not found.
|
780
1187
|
*/
|
781
|
-
VALUE enum_resolve(VALUE self, VALUE sym) {
|
1188
|
+
static VALUE enum_resolve(VALUE self, VALUE sym) {
|
782
1189
|
const char* name = rb_id2name(SYM2ID(sym));
|
783
1190
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
784
|
-
|
1191
|
+
const upb_enumdef *e = EnumDescriptor_GetEnumDef(desc);
|
785
1192
|
|
786
1193
|
int32_t num = 0;
|
787
|
-
bool found = upb_enumdef_ntoiz(
|
1194
|
+
bool found = upb_enumdef_ntoiz(e, name, &num);
|
788
1195
|
if (!found) {
|
789
1196
|
return Qnil;
|
790
1197
|
} else {
|
@@ -799,16 +1206,16 @@ VALUE enum_resolve(VALUE self, VALUE sym) {
|
|
799
1206
|
* This module method, provided on each generated enum module, returns the
|
800
1207
|
* EnumDescriptor corresponding to this enum type.
|
801
1208
|
*/
|
802
|
-
VALUE enum_descriptor(VALUE self) {
|
1209
|
+
static VALUE enum_descriptor(VALUE self) {
|
803
1210
|
return rb_ivar_get(self, descriptor_instancevar_interned);
|
804
1211
|
}
|
805
1212
|
|
806
|
-
VALUE build_module_from_enumdesc(
|
807
|
-
|
808
|
-
|
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)));
|
809
1216
|
|
810
1217
|
upb_enum_iter it;
|
811
|
-
for (upb_enum_begin(&it,
|
1218
|
+
for (upb_enum_begin(&it, e);
|
812
1219
|
!upb_enum_done(&it);
|
813
1220
|
upb_enum_next(&it)) {
|
814
1221
|
const char* name = upb_enum_iter_name(&it);
|
@@ -824,26 +1231,99 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
|
824
1231
|
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
825
1232
|
rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
|
826
1233
|
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
|
827
|
-
rb_ivar_set(mod, descriptor_instancevar_interned,
|
828
|
-
get_def_obj(enumdesc->enumdef));
|
1234
|
+
rb_ivar_set(mod, descriptor_instancevar_interned, _enumdesc);
|
829
1235
|
|
830
1236
|
return mod;
|
831
1237
|
}
|
832
1238
|
|
833
|
-
|
834
|
-
*
|
835
|
-
*
|
836
|
-
|
837
|
-
*
|
838
|
-
*
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
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.");
|
848
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");
|
849
1329
|
}
|