diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2017-09-21 15:08:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-21 15:08:45 -0700 |
commit | 4fc75296c781e851b887da6d05fed722acf3373b (patch) | |
tree | 8e66ed46d842268594e18f6b33468997a9e4249f /ruby/ext/google/protobuf_c/storage.c | |
parent | 2b0ee3fdf62577708eea74db3c31853782ae4efe (diff) | |
parent | 633ef8bde974a236ef1bdec58c246c0a798d2aa9 (diff) |
Merge pull request #3627 from zanker/zanker/add-submsg-hash-init
Allow initializing a chain of protos using only a hash in Ruby
Diffstat (limited to 'ruby/ext/google/protobuf_c/storage.c')
-rw-r--r-- | ruby/ext/google/protobuf_c/storage.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 3ff2bda6..24064dfd 100644 --- a/ruby/ext/google/protobuf_c/storage.c +++ b/ruby/ext/google/protobuf_c/storage.c @@ -176,6 +176,15 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, break; } case UPB_TYPE_STRING: + if (CLASS_OF(value) == rb_cSymbol) { + value = rb_funcall(value, rb_intern("to_s"), 0, NULL); + } else if (CLASS_OF(value) != rb_cString) { + rb_raise(rb_eTypeError, "Invalid argument for string field."); + } + + DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value); + break; + case UPB_TYPE_BYTES: { if (CLASS_OF(value) != rb_cString) { rb_raise(rb_eTypeError, "Invalid argument for string field."); @@ -197,7 +206,9 @@ void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class, } case UPB_TYPE_ENUM: { int32_t int_val = 0; - if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) { + if (TYPE(value) == T_STRING) { + value = rb_funcall(value, rb_intern("to_sym"), 0, NULL); + } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) { rb_raise(rb_eTypeError, "Expected number or symbol type for enum field."); } |