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