Class: ProtoPlugin::FileDescriptor

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Commentable
Defined in:
lib/proto_plugin/file_descriptor.rb

Overview

A wrapper class around Google::Protobuf::FileDescriptorProto which provides helpers and more idiomatic Ruby access patterns.

Any method not defined directly is delegated to the descriptor the wrapper was initialized with.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commentable

#comments, #leading_comments, #source_location, #trailing_comments

Constructor Details

#initialize(context, descriptor) ⇒ FileDescriptor

Returns a new instance of FileDescriptor.

Parameters:

  • context (Context)
  • descriptor (Google::Protobuf::FileDescriptorProto)


21
22
23
24
25
# File 'lib/proto_plugin/file_descriptor.rb', line 21

def initialize(context, descriptor)
  super(descriptor)
  @context = context
  @descriptor = descriptor
end

Instance Attribute Details

#descriptorGoogle::Protobuf::FileDescriptorProto (readonly)

Returns:

  • (Google::Protobuf::FileDescriptorProto)


17
18
19
# File 'lib/proto_plugin/file_descriptor.rb', line 17

def descriptor
  @descriptor
end

Instance Method Details

#enumsArray<EnumDescriptor>

The enums defined as children of this file.



53
54
55
56
57
# File 'lib/proto_plugin/file_descriptor.rb', line 53

def enums
  @enums ||= @descriptor.enum_type.map do |e|
    EnumDescriptor.new(e, self)
  end
end

#fileFileDescriptor

The file descriptor this element belongs to.

For a FileDescriptor this is the descriptor itself. Defined so that Commentable can resolve comments uniformly across all descriptor types.

Returns:



33
34
35
# File 'lib/proto_plugin/file_descriptor.rb', line 33

def file
  self
end

#importsArray<FileDescriptor>

The files imported by this file, resolved to their descriptors.

Imports that were not included in the request are omitted.



119
120
121
122
123
# File 'lib/proto_plugin/file_descriptor.rb', line 119

def imports
  @imports ||= @descriptor.dependency.filter_map do |name|
    @context.file_by_filename(name)
  end
end

#location_for(proto) ⇒ Google::Protobuf::SourceCodeInfo::Location?

Returns the SourceCodeInfo::Location for a given raw descriptor proto defined within this file, if source info was included in the request.

Parameters:

  • proto (Object)

    a raw descriptor proto contained in this file

Returns:

  • (Google::Protobuf::SourceCodeInfo::Location)
  • (nil)

    if no matching location is available



43
44
45
# File 'lib/proto_plugin/file_descriptor.rb', line 43

def location_for(proto)
  source_locations[proto]
end

#messagesArray<MessageDescriptor>

The messages defined as children of this file.



65
66
67
68
69
# File 'lib/proto_plugin/file_descriptor.rb', line 65

def messages
  @messages ||= @descriptor.message_type.map do |m|
    MessageDescriptor.new(m, self, @context)
  end
end

#namespace(split: false) ⇒ String+

Returns the Ruby namespace (module) for the file.

If the ruby_package option was specified, then that value is returned directly. Otherwise, the package value is transformed to Ruby module notation.

Examples:

Using package my.protobuf.package;

file.namespace #=> "My::Protobuf::Package"

Using option ruby_package = "My::Ruby::Package";

file.namespace #=> "My::Ruby::Package"

Parameters:

  • split (Boolean) (defaults to: false)

    Returns the namespace as an array of module names.

Returns:

  • (String)

    The namespace for the file.

  • (Array<String>)

    If split: true, the namespace as an array of module names.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/proto_plugin/file_descriptor.rb', line 86

def namespace(split: false)
  @namespace ||= begin
    namespace = @descriptor.options&.ruby_package
    if !namespace || namespace.empty?
      namespace = @descriptor.package.split(".")
        .map { |token| Utils.camelize(token) }
        .join("::")
    end
    namespace
  end
  split ? @namespace.split("::") : @namespace
end

#public_importsArray<FileDescriptor>

The subset of #imports that were declared with import public, resolved to their descriptors.



132
133
134
135
136
137
# File 'lib/proto_plugin/file_descriptor.rb', line 132

def public_imports
  @public_imports ||= @descriptor.public_dependency.filter_map do |i|
    name = @descriptor.dependency[i]
    @context.file_by_filename(name) if name
  end
end

#servicesArray<ServiceDescriptor>

The services defined in this file.



105
106
107
108
109
# File 'lib/proto_plugin/file_descriptor.rb', line 105

def services
  @services ||= @descriptor.service.map do |s|
    ServiceDescriptor.new(s, self, @context)
  end
end