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/has_children.rb,
lib/protobuf_descriptor/enum_descriptor.rb,
lib/protobuf_descriptor/file_descriptor.rb,
lib/protobuf_descriptor/field_descriptor.rb,
lib/protobuf_descriptor/named_collection.rb,
lib/protobuf_descriptor/method_descriptor.rb,
lib/protobuf_descriptor/message_descriptor.rb,
lib/protobuf_descriptor/service_descriptor.rb,
lib/protobuf_descriptor/enum_value_descriptor.rb

Overview

A wrapper for the +FileDescriptorSet+[https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto#49] proto. This acts as the root from which name resolution occurs.

Defined Under Namespace

Modules: HasChildren, HasParent, NamedChild Classes: EnumDescriptor, EnumValueDescriptor, FieldDescriptor, FileDescriptor, MessageDescriptor, MethodDescriptor, NamedCollection, ServiceDescriptor

Constant Summary collapse

VERSION =

protobuf_descriptor version

"1.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ ProtobufDescriptor

Returns a new instance of ProtobufDescriptor.



52
53
54
55
56
57
# 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

Instance Attribute Details

#descriptor_setObject (readonly)

Raw FileDescriptorSet protocol buffer



46
47
48
# File 'lib/protobuf_descriptor.rb', line 46

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 FileDescriptor



50
51
52
# File 'lib/protobuf_descriptor.rb', line 50

def file
  @file
end

Class Method Details

.decode(bytes) ⇒ Object

Decode a ProtobufDescriptor from bytes

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



27
28
29
# File 'lib/protobuf_descriptor.rb', line 27

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"))



34
35
36
# File 'lib/protobuf_descriptor.rb', line 34

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

.load(path) ⇒ Object

Loads a ProtobufDescriptor from a file

ProtobufDescriptor.load("descriptor.desc")



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

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

Instance Method Details

#[](index) ⇒ Object

Shorthand for accessing files



103
104
105
# File 'lib/protobuf_descriptor.rb', line 103

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

#all_descendantsObject

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



65
66
67
68
69
70
71
72
73
74
75
76
# 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?Boolean

Returns whether all files have source code info attached

Returns:

  • (Boolean)


108
109
110
# 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) ⇒ 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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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