diff options
Diffstat (limited to 'ruby/ext/google/protobuf_c/storage.c')
-rw-r--r-- | ruby/ext/google/protobuf_c/storage.c | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 3ff2bda6..1437c0b5 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."); } @@ -595,12 +606,20 @@ static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { rb_raise(rb_eTypeError, "Repeated field array has wrong element type"); } - if (self->field_type == UPB_TYPE_MESSAGE || - self->field_type == UPB_TYPE_ENUM) { + if (self->field_type == UPB_TYPE_MESSAGE) { + if (self->field_type_class != + Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) { + rb_raise(rb_eTypeError, + "Repeated field array has wrong message class"); + } + } + + + if (self->field_type == UPB_TYPE_ENUM) { if (self->field_type_class != - get_def_obj(upb_fielddef_subdef(field))) { + EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) { rb_raise(rb_eTypeError, - "Repeated field array has wrong message/enum class"); + "Repeated field array has wrong enum class"); } } } |