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