2014-11-18 22:19:58 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
// https://developers.google.com/protocol-buffers/
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "protobuf.h"
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Class/module creation from msgdefs and enumdefs, respectively.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void* Message_data(void* msg) {
|
|
|
|
return ((uint8_t *)msg) + sizeof(MessageHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message_mark(void* _self) {
|
|
|
|
MessageHeader* self = (MessageHeader *)_self;
|
|
|
|
layout_mark(self->descriptor->layout, Message_data(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message_free(void* self) {
|
2017-10-26 21:41:43 +00:00
|
|
|
stringsink* unknown = ((MessageHeader *)self)->unknown_fields;
|
|
|
|
if (unknown != NULL) {
|
|
|
|
stringsink_uninit(unknown);
|
|
|
|
free(unknown);
|
|
|
|
}
|
2014-11-18 22:19:58 +00:00
|
|
|
xfree(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_data_type_t Message_type = {
|
|
|
|
"Message",
|
|
|
|
{ Message_mark, Message_free, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
VALUE Message_alloc(VALUE klass) {
|
2015-05-19 22:33:48 +00:00
|
|
|
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
|
2014-11-18 22:19:58 +00:00
|
|
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
|
|
|
MessageHeader* msg = (MessageHeader*)ALLOC_N(
|
|
|
|
uint8_t, sizeof(MessageHeader) + desc->layout->size);
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE ret;
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
memset(Message_data(msg), 0, desc->layout->size);
|
|
|
|
|
|
|
|
// We wrap first so that everything in the message object is GC-rooted in case
|
|
|
|
// a collection happens during object creation in layout_init().
|
2015-07-10 18:56:06 +00:00
|
|
|
ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
|
2014-11-18 22:19:58 +00:00
|
|
|
msg->descriptor = desc;
|
2015-05-19 22:33:48 +00:00
|
|
|
rb_ivar_set(ret, descriptor_instancevar_interned, descriptor);
|
2014-11-18 22:19:58 +00:00
|
|
|
|
2017-10-26 21:41:43 +00:00
|
|
|
msg->unknown_fields = NULL;
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
layout_init(desc->layout, Message_data(msg));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
static const upb_fielddef* which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
|
2015-07-10 18:56:06 +00:00
|
|
|
upb_oneof_iter it;
|
|
|
|
size_t case_ofs;
|
|
|
|
uint32_t oneof_case;
|
|
|
|
const upb_fielddef* first_field;
|
|
|
|
const upb_fielddef* f;
|
|
|
|
|
2015-01-15 02:02:27 +00:00
|
|
|
// If no fields in the oneof, always nil.
|
|
|
|
if (upb_oneofdef_numfields(o) == 0) {
|
2017-08-25 19:11:15 +00:00
|
|
|
return NULL;
|
2015-01-15 02:02:27 +00:00
|
|
|
}
|
|
|
|
// Grab the first field in the oneof so we can get its layout info to find the
|
|
|
|
// oneof_case field.
|
|
|
|
upb_oneof_begin(&it, o);
|
|
|
|
assert(!upb_oneof_done(&it));
|
2015-07-10 18:56:06 +00:00
|
|
|
first_field = upb_oneof_iter_field(&it);
|
2015-01-15 02:02:27 +00:00
|
|
|
assert(upb_fielddef_containingoneof(first_field) != NULL);
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
case_ofs =
|
2015-01-15 02:02:27 +00:00
|
|
|
self->descriptor->layout->
|
|
|
|
fields[upb_fielddef_index(first_field)].case_offset;
|
2015-07-10 18:56:06 +00:00
|
|
|
oneof_case = *((uint32_t*)((char*)Message_data(self) + case_ofs));
|
2015-01-15 02:02:27 +00:00
|
|
|
|
2015-02-02 21:16:57 +00:00
|
|
|
if (oneof_case == ONEOF_CASE_NONE) {
|
2017-08-25 19:11:15 +00:00
|
|
|
return NULL;
|
2015-01-15 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// oneof_case is a field index, so find that field.
|
2015-07-10 18:56:06 +00:00
|
|
|
f = upb_oneofdef_itof(o, oneof_case);
|
2015-01-15 02:02:27 +00:00
|
|
|
assert(f != NULL);
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
METHOD_UNKNOWN = 0,
|
|
|
|
METHOD_GETTER = 1,
|
|
|
|
METHOD_SETTER = 2,
|
|
|
|
METHOD_CLEAR = 3,
|
2019-03-02 18:36:28 +00:00
|
|
|
METHOD_PRESENCE = 4,
|
2019-03-27 16:44:43 +00:00
|
|
|
METHOD_ENUM_GETTER = 5,
|
|
|
|
METHOD_WRAPPER_GETTER = 6,
|
|
|
|
METHOD_WRAPPER_SETTER = 7
|
2017-08-25 19:11:15 +00:00
|
|
|
};
|
|
|
|
|
2019-03-27 16:44:43 +00:00
|
|
|
// Check if the field is a well known wrapper type
|
|
|
|
static bool is_wrapper_type_field(const upb_fielddef* field) {
|
|
|
|
char* field_type_name = rb_class2name(field_type_class(field));
|
|
|
|
|
|
|
|
return strcmp(field_type_name, "Google::Protobuf::DoubleValue") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::FloatValue") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::Int32Value") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::Int64Value") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::UInt32Value") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::UInt64Value") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::BoolValue") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::StringValue") == 0 ||
|
|
|
|
strcmp(field_type_name, "Google::Protobuf::BytesValue") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new Ruby wrapper type and set the initial value
|
|
|
|
static VALUE ruby_wrapper_type(const upb_fielddef* field, const VALUE* value) {
|
|
|
|
if (is_wrapper_type_field(field) && value != Qnil) {
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
rb_hash_aset(hash, rb_str_new2("value"), value);
|
|
|
|
VALUE args[1] = { hash };
|
|
|
|
return rb_class_new_instance(1, args, field_type_class(field));
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
static int extract_method_call(VALUE method_name, MessageHeader* self,
|
|
|
|
const upb_fielddef **f, const upb_oneofdef **o) {
|
|
|
|
Check_Type(method_name, T_SYMBOL);
|
|
|
|
|
|
|
|
VALUE method_str = rb_id2str(SYM2ID(method_name));
|
|
|
|
char* name = RSTRING_PTR(method_str);
|
|
|
|
size_t name_len = RSTRING_LEN(method_str);
|
|
|
|
int accessor_type;
|
|
|
|
const upb_oneofdef* test_o;
|
|
|
|
const upb_fielddef* test_f;
|
|
|
|
|
|
|
|
if (name[name_len - 1] == '=') {
|
|
|
|
accessor_type = METHOD_SETTER;
|
|
|
|
name_len--;
|
|
|
|
// We want to ensure if the proto has something named clear_foo or has_foo?,
|
|
|
|
// we don't strip the prefix.
|
|
|
|
} else if (strncmp("clear_", name, 6) == 0 &&
|
|
|
|
!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
|
|
|
|
&test_f, &test_o)) {
|
|
|
|
accessor_type = METHOD_CLEAR;
|
|
|
|
name = name + 6;
|
|
|
|
name_len = name_len - 6;
|
|
|
|
} else if (strncmp("has_", name, 4) == 0 && name[name_len - 1] == '?' &&
|
|
|
|
!upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
|
|
|
|
&test_f, &test_o)) {
|
|
|
|
accessor_type = METHOD_PRESENCE;
|
|
|
|
name = name + 4;
|
|
|
|
name_len = name_len - 5;
|
|
|
|
} else {
|
|
|
|
accessor_type = METHOD_GETTER;
|
|
|
|
}
|
|
|
|
|
2019-03-02 18:36:28 +00:00
|
|
|
bool has_field = upb_msgdef_lookupname(self->descriptor->msgdef, name, name_len,
|
|
|
|
&test_f, &test_o);
|
|
|
|
|
2019-03-27 16:44:43 +00:00
|
|
|
// Look for wrapper type accessor of the form <field_name>_as_value
|
|
|
|
if (!has_field &&
|
|
|
|
(accessor_type == METHOD_GETTER || accessor_type == METHOD_SETTER) &&
|
|
|
|
name_len > 9 && strncmp(name + name_len - 9, "_as_value", 9) == 0) {
|
|
|
|
// Find the field name
|
|
|
|
char wrapper_field_name[name_len - 8];
|
|
|
|
strncpy(wrapper_field_name, name, name_len - 9);
|
2019-07-23 00:11:58 +00:00
|
|
|
wrapper_field_name[name_len - 9] = '\0';
|
2019-03-27 16:44:43 +00:00
|
|
|
|
|
|
|
// Check if field exists and is a wrapper type
|
|
|
|
const upb_oneofdef* test_o_wrapper;
|
|
|
|
const upb_fielddef* test_f_wrapper;
|
|
|
|
if (upb_msgdef_lookupname(self->descriptor->msgdef, wrapper_field_name, name_len - 9,
|
|
|
|
&test_f_wrapper, &test_o_wrapper) &&
|
|
|
|
upb_fielddef_type(test_f_wrapper) == UPB_TYPE_MESSAGE &&
|
|
|
|
is_wrapper_type_field(test_f_wrapper)) {
|
|
|
|
// It does exist!
|
|
|
|
has_field = true;
|
|
|
|
if (accessor_type == METHOD_SETTER) {
|
|
|
|
accessor_type = METHOD_WRAPPER_SETTER;
|
|
|
|
} else {
|
|
|
|
accessor_type = METHOD_WRAPPER_GETTER;
|
|
|
|
}
|
|
|
|
test_o = test_o_wrapper;
|
|
|
|
test_f = test_f_wrapper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 18:36:28 +00:00
|
|
|
// Look for enum accessor of the form <enum_name>_const
|
|
|
|
if (!has_field && accessor_type == METHOD_GETTER &&
|
|
|
|
name_len > 6 && strncmp(name + name_len - 6, "_const", 6) == 0) {
|
|
|
|
|
|
|
|
// Find enum field name
|
|
|
|
char enum_name[name_len - 5];
|
|
|
|
strncpy(enum_name, name, name_len - 6);
|
2019-07-23 00:11:58 +00:00
|
|
|
enum_name[name_len - 6] = '\0';
|
2019-03-02 18:36:28 +00:00
|
|
|
|
|
|
|
// Check if enum field exists
|
|
|
|
const upb_oneofdef* test_o_enum;
|
|
|
|
const upb_fielddef* test_f_enum;
|
|
|
|
if (upb_msgdef_lookupname(self->descriptor->msgdef, enum_name, name_len - 6,
|
|
|
|
&test_f_enum, &test_o_enum) &&
|
|
|
|
upb_fielddef_type(test_f_enum) == UPB_TYPE_ENUM) {
|
|
|
|
// It does exist!
|
|
|
|
has_field = true;
|
|
|
|
accessor_type = METHOD_ENUM_GETTER;
|
|
|
|
test_o = test_o_enum;
|
|
|
|
test_f = test_f_enum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
// Verify the name corresponds to a oneof or field in this message.
|
2019-03-02 18:36:28 +00:00
|
|
|
if (!has_field) {
|
2017-08-25 19:11:15 +00:00
|
|
|
return METHOD_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method calls like 'has_foo?' are not allowed if field "foo" does not have
|
|
|
|
// a hasbit (e.g. repeated fields or non-message type fields for proto3
|
|
|
|
// syntax).
|
|
|
|
if (accessor_type == METHOD_PRESENCE && test_f != NULL &&
|
|
|
|
!upb_fielddef_haspresence(test_f)) {
|
|
|
|
return METHOD_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
*o = test_o;
|
|
|
|
*f = test_f;
|
|
|
|
return accessor_type;
|
2015-01-15 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.method_missing(*args)
|
|
|
|
*
|
2017-08-25 19:11:15 +00:00
|
|
|
* Provides accessors and setters and methods to clear and check for presence of
|
|
|
|
* message fields according to their field names.
|
|
|
|
*
|
|
|
|
* For any field whose name does not conflict with a built-in method, an
|
2014-11-18 22:19:58 +00:00
|
|
|
* accessor is provided with the same name as the field, and a setter is
|
|
|
|
* provided with the name of the field plus the '=' suffix. Thus, given a
|
|
|
|
* message instance 'msg' with field 'foo', the following code is valid:
|
|
|
|
*
|
|
|
|
* msg.foo = 42
|
|
|
|
* puts msg.foo
|
2015-01-15 02:02:27 +00:00
|
|
|
*
|
|
|
|
* This method also provides read-only accessors for oneofs. If a oneof exists
|
|
|
|
* with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to
|
|
|
|
* the name of the field in that oneof that is currently set, or nil if none.
|
2017-08-25 19:11:15 +00:00
|
|
|
*
|
|
|
|
* It also provides methods of the form 'clear_fieldname' to clear the value
|
|
|
|
* of the field 'fieldname'. For basic data types, this will set the default
|
|
|
|
* value of the field.
|
|
|
|
*
|
|
|
|
* Additionally, it provides methods of the form 'has_fieldname?', which returns
|
|
|
|
* true if the field 'fieldname' is set in the message object, else false. For
|
|
|
|
* 'proto3' syntax, calling this for a basic type field will result in an error.
|
2014-11-18 22:19:58 +00:00
|
|
|
*/
|
|
|
|
VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
const upb_oneofdef* o;
|
|
|
|
const upb_fielddef* f;
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
if (argc < 1) {
|
|
|
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
int accessor_type = extract_method_call(argv[0], self, &f, &o);
|
|
|
|
if (accessor_type == METHOD_UNKNOWN || (o == NULL && f == NULL) ) {
|
2016-04-22 21:11:39 +00:00
|
|
|
return rb_call_super(argc, argv);
|
2019-03-27 16:44:43 +00:00
|
|
|
} else if (accessor_type == METHOD_SETTER || accessor_type == METHOD_WRAPPER_SETTER) {
|
2017-08-25 19:11:15 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc);
|
|
|
|
}
|
2019-03-02 18:37:37 +00:00
|
|
|
rb_check_frozen(_self);
|
2017-08-25 19:11:15 +00:00
|
|
|
} else if (argc != 1) {
|
|
|
|
rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc);
|
2016-04-22 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
// Return which of the oneof fields are set
|
2015-01-15 02:02:27 +00:00
|
|
|
if (o != NULL) {
|
2017-08-25 19:11:15 +00:00
|
|
|
if (accessor_type == METHOD_SETTER) {
|
2015-01-15 02:02:27 +00:00
|
|
|
rb_raise(rb_eRuntimeError, "Oneof accessors are read-only.");
|
|
|
|
}
|
2017-08-25 19:11:15 +00:00
|
|
|
|
|
|
|
const upb_fielddef* oneof_field = which_oneof_field(self, o);
|
|
|
|
if (accessor_type == METHOD_PRESENCE) {
|
|
|
|
return oneof_field == NULL ? Qfalse : Qtrue;
|
|
|
|
} else if (accessor_type == METHOD_CLEAR) {
|
|
|
|
if (oneof_field != NULL) {
|
2019-03-02 18:36:28 +00:00
|
|
|
layout_clear(self->descriptor->layout, Message_data(self), oneof_field);
|
2016-04-22 21:11:39 +00:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
} else {
|
2017-08-25 19:11:15 +00:00
|
|
|
// METHOD_ACCESSOR
|
|
|
|
return oneof_field == NULL ? Qnil :
|
2019-03-02 18:36:28 +00:00
|
|
|
ID2SYM(rb_intern(upb_fielddef_name(oneof_field)));
|
2016-04-22 21:11:39 +00:00
|
|
|
}
|
2017-08-25 19:11:15 +00:00
|
|
|
// Otherwise we're operating on a single proto field
|
|
|
|
} else if (accessor_type == METHOD_SETTER) {
|
|
|
|
layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
|
|
|
|
return Qnil;
|
|
|
|
} else if (accessor_type == METHOD_CLEAR) {
|
|
|
|
layout_clear(self->descriptor->layout, Message_data(self), f);
|
|
|
|
return Qnil;
|
|
|
|
} else if (accessor_type == METHOD_PRESENCE) {
|
|
|
|
return layout_has(self->descriptor->layout, Message_data(self), f);
|
2019-03-27 16:44:43 +00:00
|
|
|
} else if (accessor_type == METHOD_WRAPPER_GETTER) {
|
|
|
|
VALUE value = layout_get(self->descriptor->layout, Message_data(self), f);
|
|
|
|
if (value != Qnil) {
|
|
|
|
value = rb_funcall(value, rb_intern("value"), 0);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
} else if (accessor_type == METHOD_WRAPPER_SETTER) {
|
|
|
|
VALUE wrapper = ruby_wrapper_type(f, argv[1]);
|
|
|
|
layout_set(self->descriptor->layout, Message_data(self), f, wrapper);
|
|
|
|
return Qnil;
|
2019-03-02 18:36:28 +00:00
|
|
|
} else if (accessor_type == METHOD_ENUM_GETTER) {
|
2019-03-06 23:44:38 +00:00
|
|
|
VALUE enum_type = field_type_class(f);
|
2019-03-02 18:36:28 +00:00
|
|
|
VALUE method = rb_intern("const_get");
|
|
|
|
VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f);
|
|
|
|
|
|
|
|
// Map repeated fields to a new type with ints
|
|
|
|
if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
|
|
|
int array_size = FIX2INT(rb_funcall(raw_value, rb_intern("length"), 0));
|
|
|
|
VALUE array_args[1] = { ID2SYM(rb_intern("int64")) };
|
|
|
|
VALUE array = rb_class_new_instance(1, array_args, CLASS_OF(raw_value));
|
|
|
|
for (int i = 0; i < array_size; i++) {
|
|
|
|
VALUE entry = rb_funcall(enum_type, method, 1, rb_funcall(raw_value,
|
|
|
|
rb_intern("at"), 1, INT2NUM(i)));
|
|
|
|
rb_funcall(array, rb_intern("push"), 1, entry);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
// Convert the value for singular fields
|
|
|
|
return rb_funcall(enum_type, method, 1, raw_value);
|
2017-08-25 19:11:15 +00:00
|
|
|
} else {
|
|
|
|
return layout_get(self->descriptor->layout, Message_data(self), f);
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
|
2016-11-03 13:18:28 +00:00
|
|
|
VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
|
|
|
|
MessageHeader* self;
|
|
|
|
const upb_oneofdef* o;
|
|
|
|
const upb_fielddef* f;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
if (argc < 1) {
|
|
|
|
rb_raise(rb_eArgError, "Expected method name as first argument.");
|
|
|
|
}
|
|
|
|
|
2017-08-25 19:11:15 +00:00
|
|
|
int accessor_type = extract_method_call(argv[0], self, &f, &o);
|
|
|
|
if (accessor_type == METHOD_UNKNOWN) {
|
2016-11-03 13:18:28 +00:00
|
|
|
return rb_call_super(argc, argv);
|
2017-08-25 19:11:15 +00:00
|
|
|
} else if (o != NULL) {
|
|
|
|
return accessor_type == METHOD_SETTER ? Qfalse : Qtrue;
|
|
|
|
} else {
|
|
|
|
return Qtrue;
|
2016-11-03 13:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 23:44:38 +00:00
|
|
|
VALUE create_submsg_from_hash(const upb_fielddef *f, VALUE hash) {
|
|
|
|
const upb_def *d = upb_fielddef_subdef(f);
|
2017-09-12 19:52:15 +00:00
|
|
|
assert(d != NULL);
|
|
|
|
|
2019-03-06 23:44:38 +00:00
|
|
|
VALUE descriptor = get_def_obj(d);
|
|
|
|
VALUE msgclass = rb_funcall(descriptor, rb_intern("msgclass"), 0, NULL);
|
|
|
|
|
2017-09-12 19:52:15 +00:00
|
|
|
VALUE args[1] = { hash };
|
2019-03-06 23:44:38 +00:00
|
|
|
return rb_class_new_instance(1, args, msgclass);
|
2017-09-12 19:52:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
|
|
|
|
MessageHeader* self;
|
2017-09-12 19:52:15 +00:00
|
|
|
char *name;
|
2015-07-10 18:56:06 +00:00
|
|
|
const upb_fielddef* f;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
2017-09-12 19:52:15 +00:00
|
|
|
if (TYPE(key) == T_STRING) {
|
|
|
|
name = RSTRING_PTR(key);
|
|
|
|
} else if (TYPE(key) == T_SYMBOL) {
|
|
|
|
name = RSTRING_PTR(rb_id2str(SYM2ID(key)));
|
|
|
|
} else {
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_raise(rb_eArgError,
|
2017-09-12 19:52:15 +00:00
|
|
|
"Expected string or symbols as hash keys when initializing proto from hash.");
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
|
2014-11-18 22:19:58 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
rb_raise(rb_eArgError,
|
2015-11-20 20:57:28 +00:00
|
|
|
"Unknown field name '%s' in initialization map entry.", name);
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 03:27:24 +00:00
|
|
|
if (TYPE(val) == T_NIL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-06 23:44:09 +00:00
|
|
|
if (is_map_field(f)) {
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE map;
|
|
|
|
|
2015-01-06 23:44:09 +00:00
|
|
|
if (TYPE(val) != T_HASH) {
|
|
|
|
rb_raise(rb_eArgError,
|
2019-02-06 15:57:13 +00:00
|
|
|
"Expected Hash object as initializer value for map field '%s' (given %s).",
|
|
|
|
name, rb_class2name(CLASS_OF(val)));
|
2015-01-06 23:44:09 +00:00
|
|
|
}
|
2015-07-10 18:56:06 +00:00
|
|
|
map = layout_get(self->descriptor->layout, Message_data(self), f);
|
2015-01-06 23:44:09 +00:00
|
|
|
Map_merge_into_self(map, val);
|
|
|
|
} else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE ary;
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
if (TYPE(val) != T_ARRAY) {
|
|
|
|
rb_raise(rb_eArgError,
|
2019-02-06 15:57:13 +00:00
|
|
|
"Expected array as initializer value for repeated field '%s' (given %s).",
|
|
|
|
name, rb_class2name(CLASS_OF(val)));
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
2015-07-10 18:56:06 +00:00
|
|
|
ary = layout_get(self->descriptor->layout, Message_data(self), f);
|
2014-11-18 22:19:58 +00:00
|
|
|
for (int i = 0; i < RARRAY_LEN(val); i++) {
|
2017-09-21 16:26:54 +00:00
|
|
|
VALUE entry = rb_ary_entry(val, i);
|
2017-09-12 19:52:15 +00:00
|
|
|
if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
|
2019-03-06 23:44:38 +00:00
|
|
|
entry = create_submsg_from_hash(f, entry);
|
2017-09-12 19:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RepeatedField_push(ary, entry);
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-12 19:52:15 +00:00
|
|
|
if (TYPE(val) == T_HASH && upb_fielddef_issubmsg(f)) {
|
2019-03-06 23:44:38 +00:00
|
|
|
val = create_submsg_from_hash(f, val);
|
2017-09-12 19:52:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
layout_set(self->descriptor->layout, Message_data(self), f, val);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.new(kwargs) => new_message
|
|
|
|
*
|
|
|
|
* Creates a new instance of the given message class. Keyword arguments may be
|
|
|
|
* provided with keywords corresponding to field names.
|
|
|
|
*
|
|
|
|
* Note that no literal Message class exists. Only concrete classes per message
|
|
|
|
* type exist, as provided by the #msgclass method on Descriptors after they
|
|
|
|
* have been added to a pool. The method definitions described here on the
|
|
|
|
* Message class are provided on each concrete message class.
|
|
|
|
*/
|
|
|
|
VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE hash_args;
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
if (argc != 1) {
|
|
|
|
rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
|
|
|
|
}
|
2015-07-10 18:56:06 +00:00
|
|
|
hash_args = argv[0];
|
2014-11-18 22:19:58 +00:00
|
|
|
if (TYPE(hash_args) != T_HASH) {
|
|
|
|
rb_raise(rb_eArgError, "Expected hash arguments.");
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.dup => new_message
|
|
|
|
*
|
|
|
|
* Performs a shallow copy of this message and returns the new copy.
|
|
|
|
*/
|
|
|
|
VALUE Message_dup(VALUE _self) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE new_msg;
|
|
|
|
MessageHeader* new_msg_self;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
|
|
|
|
|
|
|
layout_dup(self->descriptor->layout,
|
|
|
|
Message_data(new_msg_self),
|
|
|
|
Message_data(self));
|
|
|
|
|
|
|
|
return new_msg;
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:25:11 +00:00
|
|
|
// Internal only; used by Google::Protobuf.deep_copy.
|
|
|
|
VALUE Message_deep_copy(VALUE _self) {
|
2014-11-18 22:19:58 +00:00
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
MessageHeader* new_msg_self;
|
|
|
|
VALUE new_msg;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
|
|
|
|
|
|
|
|
layout_deep_copy(self->descriptor->layout,
|
|
|
|
Message_data(new_msg_self),
|
|
|
|
Message_data(self));
|
|
|
|
|
|
|
|
return new_msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.==(other) => boolean
|
|
|
|
*
|
|
|
|
* Performs a deep comparison of this message with another. Messages are equal
|
|
|
|
* if they have the same type and if each field is equal according to the :==
|
|
|
|
* method's semantics (a more efficient comparison may actually be done if the
|
|
|
|
* field is of a primitive type).
|
|
|
|
*/
|
|
|
|
VALUE Message_eq(VALUE _self, VALUE _other) {
|
2016-11-14 20:45:37 +00:00
|
|
|
MessageHeader* self;
|
|
|
|
MessageHeader* other;
|
2016-11-03 13:18:28 +00:00
|
|
|
if (TYPE(_self) != TYPE(_other)) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2015-07-10 18:56:06 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
|
|
|
|
|
|
|
|
if (self->descriptor != other->descriptor) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layout_eq(self->descriptor->layout,
|
|
|
|
Message_data(self),
|
|
|
|
Message_data(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.hash => hash_value
|
|
|
|
*
|
|
|
|
* Returns a hash value that represents this message's field values.
|
|
|
|
*/
|
|
|
|
VALUE Message_hash(VALUE _self) {
|
|
|
|
MessageHeader* self;
|
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
|
|
|
return layout_hash(self->descriptor->layout, Message_data(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.inspect => string
|
|
|
|
*
|
|
|
|
* Returns a human-readable string representing this message. It will be
|
|
|
|
* formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
|
|
|
|
* field's value is represented according to its own #inspect method.
|
|
|
|
*/
|
|
|
|
VALUE Message_inspect(VALUE _self) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE str;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
str = rb_str_new2("<");
|
2014-11-18 22:19:58 +00:00
|
|
|
str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
|
|
|
|
str = rb_str_cat2(str, ": ");
|
|
|
|
str = rb_str_append(str, layout_inspect(
|
|
|
|
self->descriptor->layout, Message_data(self)));
|
|
|
|
str = rb_str_cat2(str, ">");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:27:16 +00:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.to_h => {}
|
|
|
|
*
|
|
|
|
* Returns the message as a Ruby Hash object, with keys as symbols.
|
|
|
|
*/
|
2015-05-03 17:55:10 +00:00
|
|
|
VALUE Message_to_h(VALUE _self) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
VALUE hash;
|
|
|
|
upb_msg_field_iter it;
|
2015-05-03 17:55:10 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
hash = rb_hash_new();
|
2015-05-03 17:55:10 +00:00
|
|
|
|
|
|
|
for (upb_msg_field_begin(&it, self->descriptor->msgdef);
|
|
|
|
!upb_msg_field_done(&it);
|
|
|
|
upb_msg_field_next(&it)) {
|
|
|
|
const upb_fielddef* field = upb_msg_iter_field(&it);
|
2017-08-25 19:11:15 +00:00
|
|
|
|
|
|
|
// For proto2, do not include fields which are not set.
|
|
|
|
if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
|
|
|
|
field_contains_hasbit(self->descriptor->layout, field) &&
|
|
|
|
!layout_has(self->descriptor->layout, Message_data(self), field)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-19 22:33:48 +00:00
|
|
|
VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self),
|
|
|
|
field);
|
2015-05-03 17:55:10 +00:00
|
|
|
VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
2017-08-25 19:11:15 +00:00
|
|
|
if (is_map_field(field)) {
|
2017-03-14 21:27:16 +00:00
|
|
|
msg_value = Map_to_h(msg_value);
|
|
|
|
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
|
2015-05-03 17:55:10 +00:00
|
|
|
msg_value = RepeatedField_to_ary(msg_value);
|
2017-08-25 19:11:15 +00:00
|
|
|
if (upb_msgdef_syntax(self->descriptor->msgdef) == UPB_SYNTAX_PROTO2 &&
|
|
|
|
RARRAY_LEN(msg_value) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-14 15:35:13 +00:00
|
|
|
|
2017-09-20 19:45:42 +00:00
|
|
|
if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
|
2017-09-14 15:35:13 +00:00
|
|
|
for (int i = 0; i < RARRAY_LEN(msg_value); i++) {
|
|
|
|
VALUE elem = rb_ary_entry(msg_value, i);
|
|
|
|
rb_ary_store(msg_value, i, Message_to_h(elem));
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 19:11:15 +00:00
|
|
|
|
2017-03-14 21:27:16 +00:00
|
|
|
} else if (msg_value != Qnil &&
|
|
|
|
upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
|
|
|
|
msg_value = Message_to_h(msg_value);
|
2015-05-03 17:55:10 +00:00
|
|
|
}
|
|
|
|
rb_hash_aset(hash, msg_key, msg_value);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.[](index) => value
|
|
|
|
*
|
|
|
|
* Accesses a field's value by field name. The provided field name should be a
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
VALUE Message_index(VALUE _self, VALUE field_name) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
const upb_fielddef* field;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
Check_Type(field_name, T_STRING);
|
2015-07-10 18:56:06 +00:00
|
|
|
field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
|
2014-11-18 22:19:58 +00:00
|
|
|
if (field == NULL) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return layout_get(self->descriptor->layout, Message_data(self), field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.[]=(index, value)
|
|
|
|
*
|
|
|
|
* Sets a field's value by field name. The provided field name should be a
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
|
|
MessageHeader* self;
|
2015-07-10 18:56:06 +00:00
|
|
|
const upb_fielddef* field;
|
2014-11-18 22:19:58 +00:00
|
|
|
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
|
|
|
Check_Type(field_name, T_STRING);
|
2015-07-10 18:56:06 +00:00
|
|
|
field = upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
|
2014-11-18 22:19:58 +00:00
|
|
|
if (field == NULL) {
|
|
|
|
rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
|
|
|
|
}
|
|
|
|
layout_set(self->descriptor->layout, Message_data(self), field, value);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Message.descriptor => descriptor
|
|
|
|
*
|
|
|
|
* Class method that returns the Descriptor instance corresponding to this
|
|
|
|
* message class's type.
|
|
|
|
*/
|
|
|
|
VALUE Message_descriptor(VALUE klass) {
|
2015-05-19 22:33:48 +00:00
|
|
|
return rb_ivar_get(klass, descriptor_instancevar_interned);
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 23:44:38 +00:00
|
|
|
VALUE build_class_from_descriptor(Descriptor* desc) {
|
2015-07-10 18:56:06 +00:00
|
|
|
const char *name;
|
|
|
|
VALUE klass;
|
|
|
|
|
2019-03-06 23:44:38 +00:00
|
|
|
if (desc->layout == NULL) {
|
|
|
|
desc->layout = create_layout(desc->msgdef);
|
|
|
|
}
|
|
|
|
if (desc->fill_method == NULL) {
|
|
|
|
desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
|
|
|
|
}
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
name = upb_msgdef_fullname(desc->msgdef);
|
2014-11-18 22:19:58 +00:00
|
|
|
if (name == NULL) {
|
|
|
|
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
|
|
|
|
}
|
|
|
|
|
2015-07-10 18:56:06 +00:00
|
|
|
klass = rb_define_class_id(
|
2014-11-18 22:19:58 +00:00
|
|
|
// Docs say this parameter is ignored. User will assign return value to
|
|
|
|
// their own toplevel constant class name.
|
|
|
|
rb_intern("Message"),
|
|
|
|
rb_cObject);
|
2019-03-06 23:44:38 +00:00
|
|
|
rb_ivar_set(klass, descriptor_instancevar_interned,
|
|
|
|
get_def_obj(desc->msgdef));
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_alloc_func(klass, Message_alloc);
|
2015-05-03 17:55:10 +00:00
|
|
|
rb_require("google/protobuf/message_exts");
|
2018-01-12 20:16:16 +00:00
|
|
|
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
|
2015-05-19 22:33:48 +00:00
|
|
|
rb_extend_object(
|
2018-01-12 20:16:16 +00:00
|
|
|
klass, rb_eval_string("::Google::Protobuf::MessageExts::ClassMethods"));
|
2015-05-03 17:55:10 +00:00
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_method(klass, "method_missing",
|
|
|
|
Message_method_missing, -1);
|
2016-11-03 13:18:28 +00:00
|
|
|
rb_define_method(klass, "respond_to_missing?",
|
|
|
|
Message_respond_to_missing, -1);
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_method(klass, "initialize", Message_initialize, -1);
|
|
|
|
rb_define_method(klass, "dup", Message_dup, 0);
|
|
|
|
// Also define #clone so that we don't inherit Object#clone.
|
|
|
|
rb_define_method(klass, "clone", Message_dup, 0);
|
|
|
|
rb_define_method(klass, "==", Message_eq, 1);
|
2019-02-24 17:53:24 +00:00
|
|
|
rb_define_method(klass, "eql?", Message_eq, 1);
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_method(klass, "hash", Message_hash, 0);
|
2015-05-03 17:55:10 +00:00
|
|
|
rb_define_method(klass, "to_h", Message_to_h, 0);
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_method(klass, "inspect", Message_inspect, 0);
|
2019-02-12 17:50:57 +00:00
|
|
|
rb_define_method(klass, "to_s", Message_inspect, 0);
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_method(klass, "[]", Message_index, 1);
|
|
|
|
rb_define_method(klass, "[]=", Message_index_set, 2);
|
|
|
|
rb_define_singleton_method(klass, "decode", Message_decode, 1);
|
|
|
|
rb_define_singleton_method(klass, "encode", Message_encode, 1);
|
2019-01-08 18:53:34 +00:00
|
|
|
rb_define_singleton_method(klass, "decode_json", Message_decode_json, -1);
|
2016-04-15 01:33:17 +00:00
|
|
|
rb_define_singleton_method(klass, "encode_json", Message_encode_json, -1);
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
|
2015-05-03 17:55:10 +00:00
|
|
|
|
2014-11-18 22:19:58 +00:00
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Enum.lookup(number) => name
|
|
|
|
*
|
|
|
|
* This module method, provided on each generated enum module, looks up an enum
|
|
|
|
* value by number and returns its name as a Ruby symbol, or nil if not found.
|
|
|
|
*/
|
|
|
|
VALUE enum_lookup(VALUE self, VALUE number) {
|
|
|
|
int32_t num = NUM2INT(number);
|
2015-05-19 22:33:48 +00:00
|
|
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
2014-11-18 22:19:58 +00:00
|
|
|
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
|
|
|
|
|
|
|
const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
|
|
|
|
if (name == NULL) {
|
|
|
|
return Qnil;
|
|
|
|
} else {
|
|
|
|
return ID2SYM(rb_intern(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Enum.resolve(name) => number
|
|
|
|
*
|
|
|
|
* This module method, provided on each generated enum module, looks up an enum
|
|
|
|
* value by name (as a Ruby symbol) and returns its name, or nil if not found.
|
|
|
|
*/
|
|
|
|
VALUE enum_resolve(VALUE self, VALUE sym) {
|
|
|
|
const char* name = rb_id2name(SYM2ID(sym));
|
2015-05-19 22:33:48 +00:00
|
|
|
VALUE desc = rb_ivar_get(self, descriptor_instancevar_interned);
|
2014-11-18 22:19:58 +00:00
|
|
|
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
|
|
|
|
|
|
|
int32_t num = 0;
|
|
|
|
bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
|
|
|
|
if (!found) {
|
|
|
|
return Qnil;
|
|
|
|
} else {
|
|
|
|
return INT2NUM(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Enum.descriptor
|
|
|
|
*
|
|
|
|
* This module method, provided on each generated enum module, returns the
|
|
|
|
* EnumDescriptor corresponding to this enum type.
|
|
|
|
*/
|
|
|
|
VALUE enum_descriptor(VALUE self) {
|
2015-05-19 22:33:48 +00:00
|
|
|
return rb_ivar_get(self, descriptor_instancevar_interned);
|
2014-11-18 22:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 23:44:38 +00:00
|
|
|
VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
2014-11-18 22:19:58 +00:00
|
|
|
VALUE mod = rb_define_module_id(
|
|
|
|
rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
|
|
|
|
|
|
|
|
upb_enum_iter it;
|
|
|
|
for (upb_enum_begin(&it, enumdesc->enumdef);
|
|
|
|
!upb_enum_done(&it);
|
|
|
|
upb_enum_next(&it)) {
|
|
|
|
const char* name = upb_enum_iter_name(&it);
|
|
|
|
int32_t value = upb_enum_iter_number(&it);
|
2018-12-19 19:25:53 +00:00
|
|
|
if (name[0] < 'A' || name[0] > 'Z') {
|
|
|
|
rb_warn("Enum value '%s' does not start with an uppercase letter "
|
|
|
|
"as is required for Ruby constants.",
|
|
|
|
name);
|
|
|
|
}
|
2014-11-18 22:19:58 +00:00
|
|
|
rb_define_const(mod, name, INT2NUM(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
|
|
|
rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
|
|
|
|
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
|
2019-03-06 23:44:38 +00:00
|
|
|
rb_ivar_set(mod, descriptor_instancevar_interned,
|
|
|
|
get_def_obj(enumdesc->enumdef));
|
2014-11-18 22:19:58 +00:00
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
2019-05-14 21:25:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Google::Protobuf.deep_copy(obj) => copy_of_obj
|
|
|
|
*
|
|
|
|
* Performs a deep copy of a RepeatedField instance, a Map instance, or a
|
|
|
|
* message object, recursively copying its members.
|
|
|
|
*/
|
|
|
|
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
|
|
|
|
VALUE klass = CLASS_OF(obj);
|
|
|
|
if (klass == cRepeatedField) {
|
|
|
|
return RepeatedField_deep_copy(obj);
|
|
|
|
} else if (klass == cMap) {
|
|
|
|
return Map_deep_copy(obj);
|
|
|
|
} else {
|
|
|
|
return Message_deep_copy(obj);
|
|
|
|
}
|
|
|
|
}
|