From fd1a3ff11d5854c34ba66c63598cdc5fd234e399 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Tue, 6 Jan 2015 15:44:09 -0800 Subject: Support for maps in the MRI C Ruby extension. This adds the Map container and support for parsing and serializing maps in the protobuf wire format (as defined by the C++ implementation, with MapEntry submessages in a repeated field). JSON map serialization/parsing are not yet supported as these will require some changes to upb as well. --- ruby/ext/google/protobuf_c/message.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'ruby/ext/google/protobuf_c/message.c') diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index 105b7807..de38dd7b 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -139,7 +139,14 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { "Unknown field name in initialization map entry."); } - if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) { + if (is_map_field(f)) { + if (TYPE(val) != T_HASH) { + rb_raise(rb_eArgError, + "Expected hashmap as initializer value for map field."); + } + VALUE map = layout_get(self->descriptor->layout, Message_data(self), f); + Map_merge_into_self(map, val); + } else if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) { if (TYPE(val) != T_ARRAY) { rb_raise(rb_eArgError, "Expected array as initializer value for repeated field."); @@ -450,13 +457,15 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) { * call-seq: * Google::Protobuf.deep_copy(obj) => copy_of_obj * - * Performs a deep copy of either a RepeatedField instance or a message object, - * recursively copying its members. + * 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); } -- cgit v1.2.3