diff options
author | Zachary Anker <zanker@squareup.com> | 2017-09-12 12:52:15 -0700 |
---|---|---|
committer | Zachary Anker <zanker@squareup.com> | 2017-09-20 11:39:46 -0700 |
commit | 87714836e328ca3f98c93a451be26bfd35e804b7 (patch) | |
tree | d2c626e4851ab2eb99d1c54dddf16a2de5bb17f5 /ruby/ext/google/protobuf_c/storage.c | |
parent | 06aa8dc9e730cc39c67d0ff9c0c95e645ccf03c9 (diff) | |
download | protobuf-87714836e328ca3f98c93a451be26bfd35e804b7.tar.gz protobuf-87714836e328ca3f98c93a451be26bfd35e804b7.tar.bz2 protobuf-87714836e328ca3f98c93a451be26bfd35e804b7.zip |
Allow initializing a chain of protos using only a hash
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."); } |