diff options
Diffstat (limited to 'ruby/ext/google/protobuf_c/map.c')
-rw-r--r-- | ruby/ext/google/protobuf_c/map.c | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/ruby/ext/google/protobuf_c/map.c b/ruby/ext/google/protobuf_c/map.c index 12f1f9dd..8c2f6424 100644 --- a/ruby/ext/google/protobuf_c/map.c +++ b/ruby/ext/google/protobuf_c/map.c @@ -146,6 +146,7 @@ void Map_mark(void* _self) { Map* self = _self; rb_gc_mark(self->value_type_class); + rb_gc_mark(self->parse_frame); if (self->value_type == UPB_TYPE_STRING || self->value_type == UPB_TYPE_BYTES || @@ -174,6 +175,12 @@ VALUE Map_alloc(VALUE klass) { return TypedData_Wrap_Struct(klass, &Map_type, self); } +VALUE Map_set_frame(VALUE map, VALUE val) { + Map* self = ruby_to_Map(map); + self->parse_frame = val; + return val; +} + static bool needs_typeclass(upb_fieldtype_t type) { switch (type) { case UPB_TYPE_MESSAGE: @@ -227,6 +234,7 @@ VALUE Map_init(int argc, VALUE* argv, VALUE _self) { self->key_type = ruby_to_fieldtype(argv[0]); self->value_type = ruby_to_fieldtype(argv[1]); + self->parse_frame = Qnil; // Check that the key type is an allowed type. switch (self->key_type) { @@ -654,6 +662,35 @@ VALUE Map_hash(VALUE _self) { /* * call-seq: + * Map.to_h => {} + * + * Returns a Ruby Hash object containing all the values within the map + */ +VALUE Map_to_h(VALUE _self) { + Map* self = ruby_to_Map(_self); + VALUE hash = rb_hash_new(); + upb_strtable_iter it; + for (upb_strtable_begin(&it, &self->table); + !upb_strtable_done(&it); + upb_strtable_next(&it)) { + VALUE key = table_key_to_ruby( + self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it)); + upb_value v = upb_strtable_iter_value(&it); + void* mem = value_memory(&v); + VALUE value = native_slot_get(self->value_type, + self->value_type_class, + mem); + + if (self->value_type == UPB_TYPE_MESSAGE) { + value = Message_to_h(value); + } + rb_hash_aset(hash, key, value); + } + return hash; +} + +/* + * call-seq: * Map.inspect => string * * Returns a string representing this map's elements. It will be formatted as @@ -788,8 +825,8 @@ VALUE Map_iter_value(Map_iter* iter) { void Map_register(VALUE module) { VALUE klass = rb_define_class_under(module, "Map", rb_cObject); rb_define_alloc_func(klass, Map_alloc); - cMap = klass; rb_gc_register_address(&cMap); + cMap = klass; rb_define_method(klass, "initialize", Map_init, -1); rb_define_method(klass, "each", Map_each, 0); @@ -804,6 +841,8 @@ void Map_register(VALUE module) { rb_define_method(klass, "dup", Map_dup, 0); rb_define_method(klass, "==", Map_eq, 1); rb_define_method(klass, "hash", Map_hash, 0); + rb_define_method(klass, "to_hash", Map_to_h, 0); + rb_define_method(klass, "to_h", Map_to_h, 0); rb_define_method(klass, "inspect", Map_inspect, 0); rb_define_method(klass, "merge", Map_merge, 1); rb_include_module(klass, rb_mEnumerable); |