Class: ProtobufDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf_descriptor.rb,
lib/protobuf_descriptor/version.rb,
lib/protobuf_descriptor/has_parent.rb,
lib/protobuf_descriptor/named_child.rb,
lib/protobuf_descriptor/enum_descriptor.rb,
lib/protobuf_descriptor/file_descriptor.rb,
lib/protobuf_descriptor/named_collection.rb,
lib/protobuf_descriptor/message_descriptor.rb,
lib/protobuf_descriptor/service_descriptor.rb

Overview

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

Defined Under Namespace

Modules: HasParent, NamedChild Classes: EnumDescriptor, FileDescriptor, MessageDescriptor, NamedCollection, ServiceDescriptor

Constant Summary collapse

VERSION =

protobuf_descriptor version

"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ ProtobufDescriptor

Returns a new instance of ProtobufDescriptor.



40
41
42
43
44
45
# File 'lib/protobuf_descriptor.rb', line 40

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

Instance Attribute Details

#descriptor_setObject (readonly)

Raw FileDescriptorSet protocol buffer



36
37
38
# File 'lib/protobuf_descriptor.rb', line 36

def descriptor_set
  @descriptor_set
end

#fileObject (readonly) Also known as: files

Set of .proto files that are contained within the descriptor set, as a NamedCollection of FileDescriptors



38
39
40
# File 'lib/protobuf_descriptor.rb', line 38

def file
  @file
end

Class Method Details

.decode(bytes) ⇒ Object

Decode a ProtobufDescriptor from bytes

ProtobufDescriptor.decode(File.read("descriptor.desc"))


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

def self.decode(bytes)
  return self.decode_from(::StringIO.new(bytes))
end

.decode_from(stream) ⇒ Object

Decode a ProtobufDescriptor from a readable stream

ProtobufDescriptor.decode_from(File.open("descriptor.desc"))


24
25
26
# File 'lib/protobuf_descriptor.rb', line 24

def self.decode_from(stream)
  return self.new(stream)
end

.load(path) ⇒ Object

Loads a ProtobufDescriptor from a file

ProtobufDescriptor.load("descriptor.desc")


31
32
33
# File 'lib/protobuf_descriptor.rb', line 31

def self.load(path)
  return self.decode_from(File.open(path))
end

Instance Method Details

#[](index) ⇒ Object

Shorthand for accessing files



92
93
94
# File 'lib/protobuf_descriptor.rb', line 92

def [](index)
  return files[index]
end

#all_descendantsObject

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/protobuf_descriptor.rb', line 54

def all_descendants
  seeds = files.to_a.dup
  children = Set.new
  while !seeds.empty?
    seeds.pop.children.each do |child|
      children << child

      seeds << child if child.respond_to?(:children)
    end
  end
  children
end

#resolve_type_name(type_name, relative_to = nil) ⇒ Object

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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/protobuf_descriptor.rb', line 71

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