/* * call-seq: * EnumDescriptor.lookup_name(name) => value * * Returns the numeric value corresponding to the given key name (as a Ruby * symbol), or nil if none. */ VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) { DEFINE_SELF(EnumDescriptor, self, _self); const char* name_str= rb_id2name(SYM2ID(name)); int32_t val = 0; if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) { return INT2NUM(val); } else { return Qnil; } }
/* * call-seq: * Enum.resolve(name) => number * * This module method, provided on each generated enum module, looks up an enum * value by name (as a Ruby symbol) and returns its name, or nil if not found. */ VALUE enum_resolve(VALUE self, VALUE sym) { const char* name = rb_id2name(SYM2ID(sym)); VALUE desc = rb_iv_get(self, kDescriptorInstanceVar); EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc); int32_t num = 0; bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num); if (!found) { return Qnil; } else { return INT2NUM(num); } }