class ProtobufDescriptor

A wrapper for the {FileDescriptorSet} proto. This acts as the root from which name resolution occurs.

Constants

VERSION

protobuf_descriptor version

Attributes

descriptor_set[R]

Raw FileDescriptorSet protocol buffer

file[R]

Set of .proto files that are contained within the descriptor set, as a NamedCollection of {ProtobufDescriptor::FileDescriptor}

files[R]

Set of .proto files that are contained within the descriptor set, as a NamedCollection of {ProtobufDescriptor::FileDescriptor}

Public Class Methods

decode(bytes) click to toggle source

Decode a ProtobufDescriptor from bytes

ProtobufDescriptor.decode(File.read("descriptor.desc"))
# File lib/protobuf_descriptor.rb, line 27
def self.decode(bytes)
  return self.decode_from(::StringIO.new(bytes))
end
decode_from(stream) click to toggle source

Decode a ProtobufDescriptor from a readable stream

ProtobufDescriptor.decode_from(File.open("descriptor.desc"))
# File lib/protobuf_descriptor.rb, line 34
def self.decode_from(stream)
  return self.new(stream)
end
load(path) click to toggle source

Loads a ProtobufDescriptor from a file

ProtobufDescriptor.load("descriptor.desc")
# File lib/protobuf_descriptor.rb, line 41
def self.load(path)
  return self.decode_from(File.open(path))
end
new(stream) click to toggle source
# File lib/protobuf_descriptor.rb, line 52
def initialize(stream)
  @descriptor_set = Google::Protobuf::FileDescriptorSet.new.decode_from(stream)
  @file = ProtobufDescriptor::NamedCollection.new(@descriptor_set.file.map { |f| ProtobufDescriptor::FileDescriptor.new(self, f) }) do |name, member|
    member.name == name || member.name == "#{name}.proto"
  end
end

Public Instance Methods

[](index) click to toggle source

Shorthand for accessing files

# File lib/protobuf_descriptor.rb, line 103
def [](index)
  return files[index]
end
all_descendants() click to toggle source

Returns all the named descendants of this descriptor set, basically every {ProtobufDescriptor::MessageDescriptor}, {ProtobufDescriptor::EnumDescriptor}, and {ProtobufDescriptor::ServiceDescriptor} defined in this set of proto files.

# File lib/protobuf_descriptor.rb, line 65
def all_descendants
  seeds = files.to_a.dup
  children = Set.new
  while !seeds.empty?
    seeds.pop.named_children.each do |child|
      children << child

      seeds << child if child.is_a?(HasChildren)
    end
  end
  children
end
has_source_code_info?() click to toggle source

Returns whether all files have source code info attached

# File lib/protobuf_descriptor.rb, line 108
def has_source_code_info?
  return files.all? { |f| f.has_source_code_info? }
end
resolve_type_name(type_name, relative_to=nil) click to toggle source

Finds the descriptor corresponding to a given type name. type_name can either be a fully qualified name (with a leading “.”), or a relative name, in which case relative_to must either be a descriptor or a fully qualified name that the relative name is resolved relative to.

# File lib/protobuf_descriptor.rb, line 82
def resolve_type_name(type_name, relative_to=nil)
  if type_name.start_with?('.')
    all_descendants.find { |descendant|
      descendant.fully_qualified_name == type_name
    }
  else
    raise "Must provide a relative path!" unless relative_to

    relative_to = relative_to.fully_qualified_name if relative_to.respond_to? :fully_qualified_name
    parents = relative_to.split('.')

    # The first element is the empty string, which is the root.
    while parents.size > 1
      type = resolve_type_name("#{parents.join('.')}.#{type_name}")
      return type if type
      parents.pop
    end
  end
end