google-protobuf 3.14.0 → 4.31.1
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 +4 -4
- 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 +1174 -1608
- data/ext/google/protobuf_c/defs.h +82 -0
- data/ext/google/protobuf_c/extconf.rb +31 -8
- data/ext/google/protobuf_c/glue.c +135 -0
- data/ext/google/protobuf_c/map.c +380 -485
- data/ext/google/protobuf_c/map.h +48 -0
- data/ext/google/protobuf_c/message.c +1098 -531
- data/ext/google/protobuf_c/message.h +82 -0
- data/ext/google/protobuf_c/protobuf.c +313 -92
- data/ext/google/protobuf_c/protobuf.h +59 -624
- data/ext/google/protobuf_c/repeated_field.c +358 -353
- 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 +7 -29
- data/lib/google/protobuf/any_pb.rb +6 -8
- data/lib/google/protobuf/api_pb.rb +7 -26
- data/lib/google/protobuf/descriptor_pb.rb +70 -0
- data/lib/google/protobuf/duration_pb.rb +6 -8
- data/lib/google/protobuf/empty_pb.rb +6 -6
- 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 +6 -7
- data/lib/google/protobuf/internal/object_cache.rb +99 -0
- data/lib/google/protobuf/message_exts.rb +14 -28
- data/lib/google/protobuf/plugin_pb.rb +25 -0
- data/lib/google/protobuf/repeated_field.rb +22 -33
- data/lib/google/protobuf/source_context_pb.rb +6 -7
- data/lib/google/protobuf/struct_pb.rb +6 -23
- data/lib/google/protobuf/timestamp_pb.rb +6 -8
- data/lib/google/protobuf/type_pb.rb +7 -71
- data/lib/google/protobuf/well_known_types.rb +17 -36
- data/lib/google/protobuf/wrappers_pb.rb +6 -31
- data/lib/google/protobuf.rb +32 -118
- 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 +107 -38
- 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
- data/tests/basic.rb +0 -543
- data/tests/generated_code_test.rb +0 -23
- data/tests/stress.rb +0 -38
@@ -1,76 +1,71 @@
|
|
1
1
|
// Protocol Buffers - Google's data interchange format
|
2
2
|
// Copyright 2014 Google Inc. All rights reserved.
|
3
|
-
// https://developers.google.com/protocol-buffers/
|
4
3
|
//
|
5
|
-
//
|
6
|
-
//
|
7
|
-
//
|
8
|
-
//
|
9
|
-
// * Redistributions of source code must retain the above copyright
|
10
|
-
// notice, this list of conditions and the following disclaimer.
|
11
|
-
// * Redistributions in binary form must reproduce the above
|
12
|
-
// copyright notice, this list of conditions and the following disclaimer
|
13
|
-
// in the documentation and/or other materials provided with the
|
14
|
-
// distribution.
|
15
|
-
// * Neither the name of Google Inc. nor the names of its
|
16
|
-
// contributors may be used to endorse or promote products derived from
|
17
|
-
// this software without specific prior written permission.
|
18
|
-
//
|
19
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
-
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
-
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
-
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
-
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
-
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
-
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
-
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
-
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
-
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
-
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
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
|
30
7
|
|
8
|
+
#include "message.h"
|
9
|
+
|
10
|
+
#include "convert.h"
|
11
|
+
#include "defs.h"
|
12
|
+
#include "map.h"
|
31
13
|
#include "protobuf.h"
|
14
|
+
#include "repeated_field.h"
|
15
|
+
#include "shared_message.h"
|
32
16
|
|
33
|
-
|
34
|
-
|
35
|
-
|
17
|
+
static VALUE cParseError = Qnil;
|
18
|
+
static VALUE cAbstractMessage = Qnil;
|
19
|
+
static ID descriptor_instancevar_interned;
|
36
20
|
|
37
|
-
|
38
|
-
return ((
|
21
|
+
static VALUE initialize_rb_class_with_no_args(VALUE klass) {
|
22
|
+
return rb_funcall(klass, rb_intern("new"), 0);
|
39
23
|
}
|
40
24
|
|
41
|
-
|
42
|
-
|
43
|
-
layout_mark(self->descriptor->layout, Message_data(self));
|
25
|
+
VALUE MessageOrEnum_GetDescriptor(VALUE klass) {
|
26
|
+
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
44
27
|
}
|
45
28
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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);
|
53
45
|
}
|
54
46
|
|
55
|
-
|
56
|
-
|
57
|
-
|
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,
|
58
53
|
};
|
59
54
|
|
60
|
-
|
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) {
|
61
62
|
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
|
62
|
-
|
63
|
-
MessageHeader* msg;
|
63
|
+
Message* msg = ALLOC(Message);
|
64
64
|
VALUE ret;
|
65
65
|
|
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);
|
66
|
+
msg->msgdef = Descriptor_GetMsgDef(descriptor);
|
67
|
+
msg->arena = Qnil;
|
68
|
+
msg->msg = NULL;
|
74
69
|
|
75
70
|
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
|
76
71
|
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
|
@@ -78,24 +73,97 @@ VALUE Message_alloc(VALUE klass) {
|
|
78
73
|
return ret;
|
79
74
|
}
|
80
75
|
|
81
|
-
|
82
|
-
|
83
|
-
|
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;
|
84
113
|
|
85
|
-
|
86
|
-
slot_read_oneof_case(self->descriptor->layout, Message_data(self), o);
|
114
|
+
VALUE val = ObjectCache_Get(msg);
|
87
115
|
|
88
|
-
if (
|
89
|
-
|
116
|
+
if (val == Qnil) {
|
117
|
+
VALUE klass = Descriptor_DefToClass(m);
|
118
|
+
val = Message_alloc(klass);
|
119
|
+
Message_InitPtr(val, msg, arena);
|
90
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
|
+
}
|
91
144
|
|
92
|
-
|
93
|
-
f = upb_oneofdef_itof(o, oneof_case);
|
94
|
-
assert(f != NULL);
|
145
|
+
upb_MessageValue msgval = upb_Message_GetFieldByDef(msg, field);
|
95
146
|
|
96
|
-
|
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, ">");
|
97
163
|
}
|
98
164
|
|
165
|
+
// Helper functions for #method_missing ////////////////////////////////////////
|
166
|
+
|
99
167
|
enum {
|
100
168
|
METHOD_UNKNOWN = 0,
|
101
169
|
METHOD_GETTER = 1,
|
@@ -108,153 +176,247 @@ enum {
|
|
108
176
|
};
|
109
177
|
|
110
178
|
// 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:
|
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:
|
127
191
|
return true;
|
128
192
|
default:
|
129
193
|
return false;
|
130
194
|
}
|
131
195
|
}
|
132
196
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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;
|
142
213
|
}
|
143
|
-
|
214
|
+
|
215
|
+
return upb_MessageDef_FindByNameWithSize(m, name + sp, sn - sp - ss, f, o);
|
144
216
|
}
|
145
217
|
|
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;
|
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;
|
155
222
|
|
156
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
|
+
}
|
157
245
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
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;
|
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);
|
208
261
|
}
|
209
|
-
|
210
|
-
|
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;
|
211
285
|
}
|
286
|
+
msgval =
|
287
|
+
Convert_RubyToUpb(val, upb_FieldDef_Name(f), TypeInfo_get(f), arena);
|
212
288
|
}
|
289
|
+
upb_Message_SetFieldByDef(msg, f, msgval, arena);
|
290
|
+
}
|
213
291
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
has_field = true;
|
231
|
-
accessor_type = METHOD_ENUM_GETTER;
|
232
|
-
test_o = test_o_enum;
|
233
|
-
test_f = test_f_enum;
|
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);
|
234
308
|
}
|
309
|
+
return RepeatedField_GetRubyWrapper(msgval.array_val, TypeInfo_get(f),
|
310
|
+
arena);
|
235
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
|
+
}
|
236
321
|
|
237
|
-
|
238
|
-
|
239
|
-
|
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);
|
240
347
|
}
|
348
|
+
}
|
241
349
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
if (accessor_type == METHOD_PRESENCE && test_f != NULL) {
|
246
|
-
if (!upb_fielddef_haspresence(test_f)) return METHOD_UNKNOWN;
|
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));
|
247
353
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
return
|
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
|
+
}
|
252
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);
|
253
419
|
}
|
254
|
-
|
255
|
-
*o = test_o;
|
256
|
-
*f = test_f;
|
257
|
-
return accessor_type;
|
258
420
|
}
|
259
421
|
|
260
422
|
/*
|
@@ -284,111 +446,55 @@ static int extract_method_call(VALUE method_name, MessageHeader* self,
|
|
284
446
|
* true if the field 'fieldname' is set in the message object, else false. For
|
285
447
|
* 'proto3' syntax, calling this for a basic type field will result in an error.
|
286
448
|
*/
|
287
|
-
VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
288
|
-
|
289
|
-
const
|
290
|
-
const
|
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;
|
291
453
|
int accessor_type;
|
292
454
|
|
293
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
294
455
|
if (argc < 1) {
|
295
456
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
296
457
|
}
|
297
458
|
|
298
459
|
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
460
|
|
310
|
-
|
311
|
-
if (o != NULL) {
|
312
|
-
const upb_fielddef* oneof_field = which_oneof_field(self, o);
|
461
|
+
if (accessor_type == METHOD_UNKNOWN) return rb_call_super(argc, argv);
|
313
462
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
} else if (accessor_type == METHOD_CLEAR) {
|
321
|
-
if (oneof_field != NULL) {
|
322
|
-
layout_clear(self->descriptor->layout, Message_data(self), oneof_field);
|
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);
|
323
469
|
}
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
|
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);
|
470
|
+
break;
|
471
|
+
default:
|
472
|
+
if (argc != 1) {
|
473
|
+
rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
|
369
474
|
}
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
475
|
+
break;
|
476
|
+
}
|
477
|
+
|
478
|
+
// Dispatch accessor.
|
479
|
+
if (o != NULL) {
|
480
|
+
return Message_oneof_accessor(_self, o, accessor_type);
|
374
481
|
} else {
|
375
|
-
return
|
482
|
+
return Message_field_accessor(_self, f, accessor_type, argc, argv);
|
376
483
|
}
|
377
484
|
}
|
378
485
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
const
|
383
|
-
const upb_fielddef* f;
|
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;
|
384
490
|
int accessor_type;
|
385
491
|
|
386
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
387
492
|
if (argc < 1) {
|
388
493
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
389
494
|
}
|
390
495
|
|
391
496
|
accessor_type = extract_method_call(argv[0], self, &f, &o);
|
497
|
+
|
392
498
|
if (accessor_type == METHOD_UNKNOWN) {
|
393
499
|
return rb_call_super(argc, argv);
|
394
500
|
} else if (o != NULL) {
|
@@ -398,17 +504,120 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
|
398
504
|
}
|
399
505
|
}
|
400
506
|
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
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);
|
405
549
|
}
|
406
550
|
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
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;
|
412
621
|
|
413
622
|
if (TYPE(key) == T_STRING) {
|
414
623
|
name = RSTRING_PTR(key);
|
@@ -416,55 +625,31 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
416
625
|
name = RSTRING_PTR(rb_id2str(SYM2ID(key)));
|
417
626
|
} else {
|
418
627
|
rb_raise(rb_eArgError,
|
419
|
-
"Expected string or symbols as hash keys when initializing proto
|
628
|
+
"Expected string or symbols as hash keys when initializing proto "
|
629
|
+
"from hash.");
|
420
630
|
}
|
421
631
|
|
422
|
-
f =
|
632
|
+
const upb_FieldDef* f =
|
633
|
+
upb_MessageDef_FindFieldByName(msg_init->msgdef, name);
|
634
|
+
|
423
635
|
if (f == NULL) {
|
424
636
|
rb_raise(rb_eArgError,
|
425
637
|
"Unknown field name '%s' in initialization map entry.", name);
|
426
638
|
}
|
427
639
|
|
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
|
-
}
|
640
|
+
Message_InitFieldFromValue(msg_init->msg, f, val, msg_init->arena);
|
641
|
+
return ST_CONTINUE;
|
642
|
+
}
|
457
643
|
|
458
|
-
|
459
|
-
|
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);
|
460
649
|
} else {
|
461
|
-
|
462
|
-
|
463
|
-
}
|
464
|
-
|
465
|
-
layout_set(self->descriptor->layout, Message_data(self), f, val);
|
650
|
+
rb_raise(rb_eArgError, "Expected hash arguments or message, not %s",
|
651
|
+
rb_class2name(CLASS_OF(val)));
|
466
652
|
}
|
467
|
-
return 0;
|
468
653
|
}
|
469
654
|
|
470
655
|
/*
|
@@ -479,12 +664,14 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
479
664
|
* have been added to a pool. The method definitions described here on the
|
480
665
|
* Message class are provided on each concrete message class.
|
481
666
|
*/
|
482
|
-
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
483
|
-
|
484
|
-
VALUE
|
485
|
-
|
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);
|
486
673
|
|
487
|
-
|
674
|
+
Message_InitPtr(_self, msg, arena_rb);
|
488
675
|
|
489
676
|
if (argc == 0) {
|
490
677
|
return Qnil;
|
@@ -492,12 +679,7 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
492
679
|
if (argc != 1) {
|
493
680
|
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
494
681
|
}
|
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);
|
682
|
+
Message_InitFromValue((upb_Message*)self->msg, self->msgdef, argv[0], arena);
|
501
683
|
return Qnil;
|
502
684
|
}
|
503
685
|
|
@@ -507,36 +689,13 @@ VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
|
507
689
|
*
|
508
690
|
* Performs a shallow copy of this message and returns the new copy.
|
509
691
|
*/
|
510
|
-
VALUE Message_dup(VALUE _self) {
|
511
|
-
|
512
|
-
VALUE new_msg;
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
518
|
-
|
519
|
-
layout_dup(self->descriptor->layout,
|
520
|
-
Message_data(new_msg_self),
|
521
|
-
Message_data(self));
|
522
|
-
|
523
|
-
return new_msg;
|
524
|
-
}
|
525
|
-
|
526
|
-
// Internal only; used by Google::Protobuf.deep_copy.
|
527
|
-
VALUE Message_deep_copy(VALUE _self) {
|
528
|
-
MessageHeader* self;
|
529
|
-
MessageHeader* new_msg_self;
|
530
|
-
VALUE new_msg;
|
531
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
532
|
-
|
533
|
-
new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
534
|
-
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
535
|
-
|
536
|
-
layout_deep_copy(self->descriptor->layout,
|
537
|
-
Message_data(new_msg_self),
|
538
|
-
Message_data(self));
|
539
|
-
|
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));
|
540
699
|
return new_msg;
|
541
700
|
}
|
542
701
|
|
@@ -549,22 +708,30 @@ VALUE Message_deep_copy(VALUE _self) {
|
|
549
708
|
* method's semantics (a more efficient comparison may actually be done if the
|
550
709
|
* field is of a primitive type).
|
551
710
|
*/
|
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);
|
711
|
+
static VALUE Message_eq(VALUE _self, VALUE _other) {
|
712
|
+
if (CLASS_OF(_self) != CLASS_OF(_other)) return Qfalse;
|
560
713
|
|
561
|
-
|
562
|
-
|
563
|
-
|
714
|
+
Message* self = ruby_to_Message(_self);
|
715
|
+
Message* other = ruby_to_Message(_other);
|
716
|
+
assert(self->msgdef == other->msgdef);
|
564
717
|
|
565
|
-
|
566
|
-
|
567
|
-
|
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
|
+
}
|
568
735
|
}
|
569
736
|
|
570
737
|
/*
|
@@ -573,11 +740,12 @@ VALUE Message_eq(VALUE _self, VALUE _other) {
|
|
573
740
|
*
|
574
741
|
* Returns a hash value that represents this message's field values.
|
575
742
|
*/
|
576
|
-
VALUE Message_hash(VALUE _self) {
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
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);
|
581
749
|
}
|
582
750
|
|
583
751
|
/*
|
@@ -588,81 +756,130 @@ VALUE Message_hash(VALUE _self) {
|
|
588
756
|
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
|
589
757
|
* field's value is represented according to its own #inspect method.
|
590
758
|
*/
|
591
|
-
VALUE Message_inspect(VALUE _self) {
|
592
|
-
|
593
|
-
VALUE str;
|
594
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
759
|
+
static VALUE Message_inspect(VALUE _self) {
|
760
|
+
Message* self = ruby_to_Message(_self);
|
595
761
|
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
str = rb_str_cat2(str, ">");
|
602
|
-
return str;
|
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;
|
603
767
|
}
|
604
768
|
|
605
|
-
|
606
|
-
|
607
|
-
*
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
for (upb_msg_field_begin(&it, self->descriptor->msgdef);
|
626
|
-
!upb_msg_field_done(&it);
|
627
|
-
upb_msg_field_next(&it)) {
|
628
|
-
const upb_fielddef* field = upb_msg_iter_field(&it);
|
629
|
-
VALUE msg_value;
|
630
|
-
VALUE msg_key;
|
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;
|
631
788
|
|
632
|
-
|
633
|
-
|
634
|
-
|
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]"
|
635
799
|
continue;
|
636
800
|
}
|
637
801
|
|
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
|
-
}
|
647
|
-
|
648
|
-
if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
|
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
|
-
}
|
802
|
+
TypeInfo type_info = TypeInfo_get(field);
|
803
|
+
VALUE msg_value;
|
655
804
|
|
656
|
-
|
657
|
-
|
658
|
-
|
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);
|
659
815
|
}
|
816
|
+
|
817
|
+
VALUE msg_key = ID2SYM(rb_intern(upb_FieldDef_Name(field)));
|
660
818
|
rb_hash_aset(hash, msg_key, msg_value);
|
661
819
|
}
|
820
|
+
|
662
821
|
return hash;
|
663
822
|
}
|
664
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
|
+
}
|
665
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
|
+
}
|
666
883
|
|
667
884
|
/*
|
668
885
|
* call-seq:
|
@@ -671,16 +888,18 @@ VALUE Message_to_h(VALUE _self) {
|
|
671
888
|
* Accesses a field's value by field name. The provided field name should be a
|
672
889
|
* string.
|
673
890
|
*/
|
674
|
-
VALUE Message_index(VALUE _self, VALUE field_name) {
|
675
|
-
|
676
|
-
const
|
677
|
-
|
891
|
+
static VALUE Message_index(VALUE _self, VALUE field_name) {
|
892
|
+
Message* self = ruby_to_Message(_self);
|
893
|
+
const upb_FieldDef* field;
|
894
|
+
|
678
895
|
Check_Type(field_name, T_STRING);
|
679
|
-
field =
|
896
|
+
field = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
|
897
|
+
|
680
898
|
if (field == NULL) {
|
681
899
|
return Qnil;
|
682
900
|
}
|
683
|
-
|
901
|
+
|
902
|
+
return Message_getfield(_self, field);
|
684
903
|
}
|
685
904
|
|
686
905
|
/*
|
@@ -690,19 +909,281 @@ VALUE Message_index(VALUE _self, VALUE field_name) {
|
|
690
909
|
* Sets a field's value by field name. The provided field name should be a
|
691
910
|
* string.
|
692
911
|
*/
|
693
|
-
VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
694
|
-
|
695
|
-
const
|
696
|
-
|
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
|
+
|
697
918
|
Check_Type(field_name, T_STRING);
|
698
|
-
|
699
|
-
|
919
|
+
f = upb_MessageDef_FindFieldByName(self->msgdef, RSTRING_PTR(field_name));
|
920
|
+
|
921
|
+
if (f == NULL) {
|
700
922
|
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
701
923
|
}
|
702
|
-
|
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
|
+
|
703
928
|
return Qnil;
|
704
929
|
}
|
705
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
|
+
|
706
1187
|
/*
|
707
1188
|
* call-seq:
|
708
1189
|
* Message.descriptor => descriptor
|
@@ -710,16 +1191,15 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
710
1191
|
* Class method that returns the Descriptor instance corresponding to this
|
711
1192
|
* message class's type.
|
712
1193
|
*/
|
713
|
-
VALUE Message_descriptor(VALUE klass) {
|
1194
|
+
static VALUE Message_descriptor(VALUE klass) {
|
714
1195
|
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
715
1196
|
}
|
716
1197
|
|
717
1198
|
VALUE build_class_from_descriptor(VALUE descriptor) {
|
718
|
-
|
719
|
-
const char *name;
|
1199
|
+
const char* name;
|
720
1200
|
VALUE klass;
|
721
1201
|
|
722
|
-
name =
|
1202
|
+
name = upb_MessageDef_FullName(Descriptor_GetMsgDef(descriptor));
|
723
1203
|
if (name == NULL) {
|
724
1204
|
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
725
1205
|
}
|
@@ -727,37 +1207,8 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
727
1207
|
klass = rb_define_class_id(
|
728
1208
|
// Docs say this parameter is ignored. User will assign return value to
|
729
1209
|
// their own toplevel constant class name.
|
730
|
-
rb_intern("Message"),
|
731
|
-
rb_cObject);
|
1210
|
+
rb_intern("Message"), cAbstractMessage);
|
732
1211
|
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor);
|
733
|
-
rb_define_alloc_func(klass, Message_alloc);
|
734
|
-
rb_require("google/protobuf/message_exts");
|
735
|
-
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
|
736
|
-
rb_extend_object(
|
737
|
-
klass, rb_eval_string("::Google::Protobuf::MessageExts::ClassMethods"));
|
738
|
-
|
739
|
-
rb_define_method(klass, "method_missing",
|
740
|
-
Message_method_missing, -1);
|
741
|
-
rb_define_method(klass, "respond_to_missing?",
|
742
|
-
Message_respond_to_missing, -1);
|
743
|
-
rb_define_method(klass, "initialize", Message_initialize, -1);
|
744
|
-
rb_define_method(klass, "dup", Message_dup, 0);
|
745
|
-
// Also define #clone so that we don't inherit Object#clone.
|
746
|
-
rb_define_method(klass, "clone", Message_dup, 0);
|
747
|
-
rb_define_method(klass, "==", Message_eq, 1);
|
748
|
-
rb_define_method(klass, "eql?", Message_eq, 1);
|
749
|
-
rb_define_method(klass, "hash", Message_hash, 0);
|
750
|
-
rb_define_method(klass, "to_h", Message_to_h, 0);
|
751
|
-
rb_define_method(klass, "inspect", Message_inspect, 0);
|
752
|
-
rb_define_method(klass, "to_s", Message_inspect, 0);
|
753
|
-
rb_define_method(klass, "[]", Message_index, 1);
|
754
|
-
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);
|
757
|
-
rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
|
758
|
-
rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
|
759
|
-
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
|
760
|
-
|
761
1212
|
return klass;
|
762
1213
|
}
|
763
1214
|
|
@@ -768,16 +1219,15 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
|
|
768
1219
|
* This module method, provided on each generated enum module, looks up an enum
|
769
1220
|
* value by number and returns its name as a Ruby symbol, or nil if not found.
|
770
1221
|
*/
|
771
|
-
VALUE enum_lookup(VALUE self, VALUE number) {
|
1222
|
+
static VALUE enum_lookup(VALUE self, VALUE number) {
|
772
1223
|
int32_t num = NUM2INT(number);
|
773
1224
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
return Qnil;
|
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)));
|
779
1229
|
} else {
|
780
|
-
return
|
1230
|
+
return Qnil;
|
781
1231
|
}
|
782
1232
|
}
|
783
1233
|
|
@@ -788,17 +1238,15 @@ VALUE enum_lookup(VALUE self, VALUE number) {
|
|
788
1238
|
* This module method, provided on each generated enum module, looks up an enum
|
789
1239
|
* value by name (as a Ruby symbol) and returns its name, or nil if not found.
|
790
1240
|
*/
|
791
|
-
VALUE enum_resolve(VALUE self, VALUE sym) {
|
1241
|
+
static VALUE enum_resolve(VALUE self, VALUE sym) {
|
792
1242
|
const char* name = rb_id2name(SYM2ID(sym));
|
793
1243
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
if (!found) {
|
799
|
-
return Qnil;
|
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));
|
800
1248
|
} else {
|
801
|
-
return
|
1249
|
+
return Qnil;
|
802
1250
|
}
|
803
1251
|
}
|
804
1252
|
|
@@ -809,27 +1257,33 @@ VALUE enum_resolve(VALUE self, VALUE sym) {
|
|
809
1257
|
* This module method, provided on each generated enum module, returns the
|
810
1258
|
* EnumDescriptor corresponding to this enum type.
|
811
1259
|
*/
|
812
|
-
VALUE enum_descriptor(VALUE self) {
|
1260
|
+
static VALUE enum_descriptor(VALUE self) {
|
813
1261
|
return rb_ivar_get(self, descriptor_instancevar_interned);
|
814
1262
|
}
|
815
1263
|
|
816
1264
|
VALUE build_module_from_enumdesc(VALUE _enumdesc) {
|
817
|
-
|
818
|
-
VALUE mod = rb_define_module_id(
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
int32_t value =
|
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);
|
827
1275
|
if (name[0] < 'A' || name[0] > 'Z') {
|
828
|
-
|
829
|
-
|
830
|
-
|
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
|
+
}
|
831
1284
|
}
|
832
1285
|
rb_define_const(mod, name, INT2NUM(value));
|
1286
|
+
upb_Arena_Free(arena);
|
833
1287
|
}
|
834
1288
|
|
835
1289
|
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
@@ -840,20 +1294,133 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
|
|
840
1294
|
return mod;
|
841
1295
|
}
|
842
1296
|
|
843
|
-
|
844
|
-
*
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
*
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
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
|
+
}
|
858
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");
|
859
1426
|
}
|