Class: Google::Protobuf::FileDescriptorProto

Inherits:
Object
  • Object
show all
Defined in:
lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dependency_proto_filesArray<Google::Protobuf::FileDescriptorProto>

The ‘FileDescriptorProto` contains a `dependency` array of all of the imported file name strings, in the order in which they appear in the source file.

We want to create a parallel array, but instead of just the file names, we want the references to those proto file descriptors. So, we declare the attribute here. NOTE: We also override ‘CodeGeneratorRequest` `decode` such that it automatically populates this array when the request is decoded.



16
17
18
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 16

def dependency_proto_files
  @dependency_proto_files
end

Instance Method Details

#has_service?Boolean

Returns true if the proto file has at least one ‘service` definition, false otherwise.

Returns:

  • (Boolean)

    true if the proto file has at least one ‘service` definition, false otherwise.



37
38
39
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 37

def has_service?
  !service.empty?
end

#relative_ruby_protobuf_nameString

The file name of the generated ruby protobuf code from protoc, without any path information and missing the “.rb” file extension.

For example, given a ‘name` of e.g. “some/example/hello.proto”, this helper returns “hello_rb”. The ruby output is expected to be located in the same directory as the generated twirp output file.

Returns:

  • (String)

    the file name of the generated ruby protobuf code from protoc, without any path information and missing the “.rb” file extension.

    For example, given a ‘name` of e.g. “some/example/hello.proto”, this helper returns “hello_rb”. The ruby output is expected to be located in the same directory as the generated twirp output file.



31
32
33
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 31

def relative_ruby_protobuf_name
  File.basename(name, File.extname(name)) + "_pb"
end

#ruby_moduleString

Returns the ruby module for this proto file. Gives precedence to the ‘ruby_package` option if specified, then the `package` of the file (converted to UpperCamelCase). Includes a leading top-level namespace qualifier “::”, e.g.: “::MyCompany::Example::Api”. Returns `“”` when neither ruby_package nor package is specified.

Returns:

  • (String)

    the ruby module for this proto file. Gives precedence to the ‘ruby_package` option if specified, then the `package` of the file (converted to UpperCamelCase). Includes a leading top-level namespace qualifier “::”, e.g.: “::MyCompany::Example::Api”. Returns `“”` when neither ruby_package nor package is specified.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 46

def ruby_module
  @ruby_module ||= begin
    pkg = options.ruby_package unless options&.ruby_package.to_s.empty?
    pkg ||= split_to_constants(package).join("::").to_s unless package.to_s.empty?

    if pkg.nil?
      "" # Set to "" instead of nil to properly memoize and avoid re-calculating
    else
      "::" + pkg
    end
  end
end

#ruby_type_for(message_type) ⇒ String

Converts a protobuf message type to a string containing the equivalent Ruby constant, relative to the current proto file’s Ruby module.

Respects the ‘ruby_package` option, both for the current proto file and for all message types that are imported if the imported file also specifies a `ruby_package`.

For example, given …

1) the current file has `package "foo.bar";` and `option ruby_package = "Foo::Bar";`
2) an imported file has `package "other.file.baz";` and `option ruby_package = "Baz";`
3) a third imported file has `package "third.file";` without a `ruby_package` option.

… then:

ruby_type_for(".foo.bar.example_message") => "ExampleMessage"
ruby_type_for(".foo.bar.ExampleMessage.NestedMessage") => "ExampleMessage::NestedMessage"
ruby_type_for(".google.protobuf.Empty") => "::Google::Protobuf::Empty"
ruby_type_for(".other.file.baz.example_message") => "::Baz::ExampleMessage"
ruby_type_for(".third.file.example_message") => "::Third::File::ExampleMessage"

Parameters:

Returns:



81
82
83
84
85
86
87
88
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 81

def ruby_type_for(message_type)
  ruby_type = ruby_type_map[message_type]

  # For types in the same module, remove module and trailing "::"
  ruby_type = ruby_type.delete_prefix(ruby_module + "::") unless ruby_module.empty?

  ruby_type
end

#twirp_output_filenameString

Returns the output filename for the proto file’s generated twirp code. For example, given a ‘name` of e.g. “some/example/hello.proto”, the twirp output filename is “some/example/hello_twirp.rb”.

Returns:

  • (String)

    the output filename for the proto file’s generated twirp code. For example, given a ‘name` of e.g. “some/example/hello.proto”, the twirp output filename is “some/example/hello_twirp.rb”



21
22
23
# File 'lib/twirp/protoc_plugin/descriptor_ext/file_descriptor_proto_ext.rb', line 21

def twirp_output_filename
  File.delete_extension(name) + "_twirp.rb"
end