Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ruby] unable to read custom options from descriptor #16834

Closed
cocoahero opened this issue May 13, 2024 · 1 comment
Closed

[Ruby] unable to read custom options from descriptor #16834

cocoahero opened this issue May 13, 2024 · 1 comment
Assignees

Comments

@cocoahero
Copy link

It appears that #1198 has been closed as fixed. However, I am unable to figure out how to actually access any custom options on a message's descriptor. Calling to_h on the MessageOptions object returns {}, but to_json returns a promising value.

Below are some things I have tried, but have not found an access pattern that works.

Example

sandbox.proto

syntax = "proto3";

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions {
  string my_custom_option = 50001;
}

message MyMessage {
  option (my_custom_option) = "hello";
}

sandbox_pb.rb (generated using protoc -I . --ruby_out=. sandbox.proto)

# frozen_string_literal: true
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: sandbox.proto

require 'google/protobuf'

require 'google/protobuf/descriptor_pb'


descriptor_data = "\n\rsandbox.proto\x1a google/protobuf/descriptor.proto\"\x16\n\tMyMessage:\t\x8a\xb5\x18\x05hello:;\n\x10my_custom_option\x12\x1f.google.protobuf.MessageOptions\x18\xd1\x86\x03 \x01(\tb\x06proto3"

pool = Google::Protobuf::DescriptorPool.generated_pool
pool.add_serialized_file(descriptor_data)

MyMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("MyMessage").msgclass
require_relative "sandbox_pb" 
# => true

MyMessage.descriptor.options 
# => <Google::Protobuf::MessageOptions: uninterpreted_option: []>

MyMessage.descriptor.options.to_h
# => {}

MyMessage.descriptor.options.to_json
# => "{\"[my_custom_option]\":\"hello\"}"

MyMessage.descriptor.options["my_custom_option"]
# => nil

MyMessage.descriptor.options["[my_custom_option]"]
# => nil

MyMessage.descriptor.options["(my_custom_option)"]
# => nil

MyMessage.descriptor.options.my_custom_option
# `method_missing': undefined method `my_custom_option' for an instance of Google::Protobuf::MessageOptions (NoMethodError)

Versions:

> ruby --version
ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]

> bundle show google-protobuf
~/.gem/ruby/3.3.1/gems/google-protobuf-4.26.1-arm64-darwin

> protoc --version
libprotoc 26.1
@cocoahero cocoahero added the untriaged auto added to all issues by default when created. label May 13, 2024
@esorot esorot added ruby 26.x and removed untriaged auto added to all issues by default when created. labels Jul 8, 2024
@haberman
Copy link
Member

haberman commented Jul 8, 2024

Here is the pattern you are looking for:

my_custom_option = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("my_custom_option")
my_custom_option.get(MyMessage.descriptor.options)

You can see an example in the tests:

assert_instance_of Google::Protobuf::OneofOptions, oneof_descriptor.options
test_top_level_option = Google::Protobuf::DescriptorPool.generated_pool.lookup 'basic_test.test_top_level_option'
assert_instance_of Google::Protobuf::FieldDescriptor, test_top_level_option
assert_equal "Custom option value", test_top_level_option.get(oneof_descriptor.options)
end
def test_nested_extension
descriptor = TestDeprecatedMessage.descriptor
oneof_descriptor = descriptor.lookup_oneof("test_deprecated_message_oneof")
assert_instance_of Google::Protobuf::OneofOptions, oneof_descriptor.options
test_nested_option = Google::Protobuf::DescriptorPool.generated_pool.lookup 'basic_test.TestDeprecatedMessage.test_nested_option'
assert_instance_of Google::Protobuf::FieldDescriptor, test_nested_option
assert_equal "Another custom option value", test_nested_option.get(oneof_descriptor.options)
end

@haberman haberman closed this as completed Jul 8, 2024
@Logofile Logofile self-assigned this Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants