/* * call-seq: * Message.method_missing(*args) * * Provides accessors and setters for message fields according to their field * names. For any field whose name does not conflict with a built-in method, an * accessor is provided with the same name as the field, and a setter is * provided with the name of the field plus the '=' suffix. Thus, given a * message instance 'msg' with field 'foo', the following code is valid: * * msg.foo = 42 * puts msg.foo * * This method also provides read-only accessors for oneofs. If a oneof exists * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to * the name of the field in that oneof that is currently set, or nil if none. */ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { MessageHeader* self; VALUE method_name, method_str; char* name; size_t name_len; bool setter; const upb_oneofdef* o; const upb_fielddef* f; TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); if (argc < 1) { rb_raise(rb_eArgError, "Expected method name as first argument."); } method_name = argv[0]; if (!SYMBOL_P(method_name)) { rb_raise(rb_eArgError, "Expected symbol as method name."); } method_str = rb_id2str(SYM2ID(method_name)); name = RSTRING_PTR(method_str); name_len = RSTRING_LEN(method_str); setter = false; // Setters have names that end in '='. if (name[name_len - 1] == '=') { setter = true; name_len--; } // Check for a oneof name first. o = upb_msgdef_ntoo(self->descriptor->msgdef, name, name_len); if (o != NULL) { if (setter) { rb_raise(rb_eRuntimeError, "Oneof accessors are read-only."); } return which_oneof_field(self, o); } // Otherwise, check for a field with that name. f = upb_msgdef_ntof(self->descriptor->msgdef, name, name_len); if (f == NULL) { return rb_call_super(argc, argv); } if (setter) { if (argc < 2) { rb_raise(rb_eArgError, "No value provided to setter."); } layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); return Qnil; } else { return layout_get(self->descriptor->layout, Message_data(self), f); } }
/* * call-seq: * Message.method_missing(*args) * * Provides accessors and setters and methods to clear and check for presence of * message fields according to their field names. * * For any field whose name does not conflict with a built-in method, an * accessor is provided with the same name as the field, and a setter is * provided with the name of the field plus the '=' suffix. Thus, given a * message instance 'msg' with field 'foo', the following code is valid: * * msg.foo = 42 * puts msg.foo * * This method also provides read-only accessors for oneofs. If a oneof exists * with name 'my_oneof', then msg.my_oneof will return a Ruby symbol equal to * the name of the field in that oneof that is currently set, or nil if none. * * It also provides methods of the form 'clear_fieldname' to clear the value * of the field 'fieldname'. For basic data types, this will set the default * value of the field. * * Additionally, it provides methods of the form 'has_fieldname?', which returns * true if the field 'fieldname' is set in the message object, else false. For * 'proto3' syntax, calling this for a basic type field will result in an error. */ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { MessageHeader* self; const upb_oneofdef* o; const upb_fielddef* f; TypedData_Get_Struct(_self, MessageHeader, &Message_type, self); if (argc < 1) { rb_raise(rb_eArgError, "Expected method name as first argument."); } int accessor_type = extract_method_call(argv[0], self, &f, &o); if (accessor_type == METHOD_UNKNOWN || (o == NULL && f == NULL) ) { return rb_call_super(argc, argv); } else if (accessor_type == METHOD_SETTER) { if (argc != 2) { rb_raise(rb_eArgError, "Expected 2 arguments, received %d", argc); } } else if (argc != 1) { rb_raise(rb_eArgError, "Expected 1 argument, received %d", argc); } // Return which of the oneof fields are set if (o != NULL) { if (accessor_type == METHOD_SETTER) { rb_raise(rb_eRuntimeError, "Oneof accessors are read-only."); } const upb_fielddef* oneof_field = which_oneof_field(self, o); if (accessor_type == METHOD_PRESENCE) { return oneof_field == NULL ? Qfalse : Qtrue; } else if (accessor_type == METHOD_CLEAR) { if (oneof_field != NULL) { layout_clear(self->descriptor->layout, Message_data(self), oneof_field); } return Qnil; } else { // METHOD_ACCESSOR return oneof_field == NULL ? Qnil : ID2SYM(rb_intern(upb_fielddef_name(oneof_field))); } // Otherwise we're operating on a single proto field } else if (accessor_type == METHOD_SETTER) { layout_set(self->descriptor->layout, Message_data(self), f, argv[1]); return Qnil; } else if (accessor_type == METHOD_CLEAR) { layout_clear(self->descriptor->layout, Message_data(self), f); return Qnil; } else if (accessor_type == METHOD_PRESENCE) { return layout_has(self->descriptor->layout, Message_data(self), f); } else { return layout_get(self->descriptor->layout, Message_data(self), f); } }