aboutsummaryrefslogtreecommitdiff
path: root/ruby/ext/google/protobuf_c/storage.c
diff options
context:
space:
mode:
authorZachary Anker <zanker@squareup.com>2017-09-12 12:52:15 -0700
committerZachary Anker <zanker@squareup.com>2017-09-20 11:39:46 -0700
commit87714836e328ca3f98c93a451be26bfd35e804b7 (patch)
treed2c626e4851ab2eb99d1c54dddf16a2de5bb17f5 /ruby/ext/google/protobuf_c/storage.c
parent06aa8dc9e730cc39c67d0ff9c0c95e645ccf03c9 (diff)
downloadprotobuf-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.c13
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.");
}