protobuf_descriptor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +3 -0
- data/.gitignore +21 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.rdoc +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +46 -0
- data/lib/protobuf_descriptor/enum_descriptor.rb +35 -0
- data/lib/protobuf_descriptor/file_descriptor.rb +130 -0
- data/lib/protobuf_descriptor/has_parent.rb +19 -0
- data/lib/protobuf_descriptor/message_descriptor.rb +167 -0
- data/lib/protobuf_descriptor/named_child.rb +21 -0
- data/lib/protobuf_descriptor/named_collection.rb +45 -0
- data/lib/protobuf_descriptor/service_descriptor.rb +94 -0
- data/lib/protobuf_descriptor/version.rb +4 -0
- data/lib/protobuf_descriptor.rb +95 -0
- data/protobuf_descriptor.gemspec +27 -0
- data/spec/enum_descriptor_spec.rb +45 -0
- data/spec/field_descriptor_spec.rb +24 -0
- data/spec/file_descriptor_spec.rb +52 -0
- data/spec/message_descriptor_spec.rb +43 -0
- data/spec/method_descriptor_spec.rb +17 -0
- data/spec/protobuf_descriptor_spec.rb +75 -0
- data/spec/protoc_generator_spec.rb +20 -0
- data/spec/protos/generator_test/aaa.proto +13 -0
- data/spec/protos/generator_test/foo/aab.proto +5 -0
- data/spec/protos/generator_test/java_package.proto +7 -0
- data/spec/protos/generator_test/outer_class_name.proto +7 -0
- data/spec/protos/service_rpc_test/wearabouts_api/multiple_files.proto +12 -0
- data/spec/protos/service_rpc_test/wearabouts_api/outer_class_proto.proto +12 -0
- data/spec/protos/service_rpc_test/wearabouts_api/user.proto +36 -0
- data/spec/protos/service_rpc_test/wearabouts_pb.proto +35 -0
- data/spec/protos/single_file_test/single_file.proto +169 -0
- data/spec/service_descriptor_spec.rb +13 -0
- data/spec/spec_helper.rb +145 -0
- data/spec/wire_generator_spec.rb +23 -0
- metadata +186 -0
| @@ -0,0 +1,94 @@ | |
| 1 | 
            +
            require "protobuf_descriptor/named_collection"
         | 
| 2 | 
            +
            require "protobuf_descriptor/has_parent"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require "active_support"
         | 
| 5 | 
            +
            require "active_support/core_ext/module/delegation"
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            class ProtobufDescriptor
         | 
| 8 | 
            +
              # Describes a service.
         | 
| 9 | 
            +
              #
         | 
| 10 | 
            +
              # See ServiceDescriptorProto[https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto#188]
         | 
| 11 | 
            +
              class ServiceDescriptor
         | 
| 12 | 
            +
                # Describes a method of a service.
         | 
| 13 | 
            +
                #
         | 
| 14 | 
            +
                # See MethodDescriptorProto[https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto#196]
         | 
| 15 | 
            +
                class MethodDescriptor
         | 
| 16 | 
            +
                  # The parent ServiceDescriptor[../ServiceDescriptor.html]
         | 
| 17 | 
            +
                  attr_reader :parent
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  # The +MethodDescriptorProto+ this +MethodDescriptor+ is wrapping
         | 
| 20 | 
            +
                  attr_reader :method_descriptor_proto
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  def initialize(parent, method_descriptor_proto)
         | 
| 23 | 
            +
                    @parent = parent
         | 
| 24 | 
            +
                    @method_descriptor_proto = method_descriptor_proto
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  include ProtobufDescriptor::HasParent
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  # The name of the service method
         | 
| 30 | 
            +
                  def name
         | 
| 31 | 
            +
                    method_descriptor_proto.name
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  # The +MethodOptions+ for the service method
         | 
| 35 | 
            +
                  def options
         | 
| 36 | 
            +
                    method_descriptor_proto.options
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  # Input type name for the service method. This is resolved in the same way
         | 
| 40 | 
            +
                  # as FieldDescriptorProto.type_name, but must refer to a message type.
         | 
| 41 | 
            +
                  def input_type_name
         | 
| 42 | 
            +
                    method_descriptor_proto.input_type
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  # Output type name for the service method. This is resolved in the same way
         | 
| 46 | 
            +
                  # as FieldDescriptorProto.type_name, but must refer to a message type.
         | 
| 47 | 
            +
                  def output_type_name
         | 
| 48 | 
            +
                    method_descriptor_proto.output_type
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                  # Resolves the method's +input_type_name+, returning the
         | 
| 52 | 
            +
                  # MessageDescriptor[link:MessageDescriptor.html] that this method
         | 
| 53 | 
            +
                  # receives.
         | 
| 54 | 
            +
                  def resolve_input_type
         | 
| 55 | 
            +
                    protobuf_descriptor.resolve_type_name(input_type_name, file_descriptor)
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  # Resolves the method's +output_type_name+, returning the
         | 
| 59 | 
            +
                  # MessageDescriptor[link:MessageDescriptor.html] that this method
         | 
| 60 | 
            +
                  # returns.
         | 
| 61 | 
            +
                  def resolve_output_type
         | 
| 62 | 
            +
                    protobuf_descriptor.resolve_type_name(output_type_name, file_descriptor)
         | 
| 63 | 
            +
                  end
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                # The parent FileDescriptor[FileDescriptor.html]
         | 
| 67 | 
            +
                attr_reader :parent
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                # The +ServiceDescriptorProto+ this +ServiceDescriptor+ is wrapping
         | 
| 70 | 
            +
                attr_reader :service_descriptor_proto
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                # Set of methods contained within this service, as a NamedCollection of
         | 
| 73 | 
            +
                # MethodDescriptor[link:ServiceDescriptor/MethodDescriptor.html]
         | 
| 74 | 
            +
                attr_reader :method
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                def initialize(parent, service_descriptor_proto)
         | 
| 77 | 
            +
                  @parent = parent
         | 
| 78 | 
            +
                  @service_descriptor_proto = service_descriptor_proto
         | 
| 79 | 
            +
                  @method = ProtobufDescriptor::NamedCollection.new(
         | 
| 80 | 
            +
                      service_descriptor_proto.method.map { |m|
         | 
| 81 | 
            +
                          ProtobufDescriptor::ServiceDescriptor::MethodDescriptor.new(self, m)
         | 
| 82 | 
            +
                      })
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
             | 
| 85 | 
            +
             | 
| 86 | 
            +
                # The name of the service
         | 
| 87 | 
            +
                def name; service_descriptor_proto.name; end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                # The +ServiceOptions+ for this service.
         | 
| 90 | 
            +
                def options; service_descriptor_proto.options; end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                alias_method :methods, :method
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
            end
         | 
| @@ -0,0 +1,95 @@ | |
| 1 | 
            +
            require "protobuf_descriptor/version"
         | 
| 2 | 
            +
            require "protobuf_descriptor/file_descriptor"
         | 
| 3 | 
            +
            require "protobuf_descriptor/message_descriptor"
         | 
| 4 | 
            +
            require "protobuf_descriptor/named_collection"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            require "stringio"
         | 
| 7 | 
            +
            require "protobuf"
         | 
| 8 | 
            +
            require "protobuf/descriptors/google/protobuf/descriptor.pb.rb"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            # A wrapper for the
         | 
| 11 | 
            +
            # {+FileDescriptorSet+}[https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto#49]
         | 
| 12 | 
            +
            # proto. This acts as the root from which name resolution occurs.
         | 
| 13 | 
            +
            class ProtobufDescriptor
         | 
| 14 | 
            +
              # Decode a ProtobufDescriptor from bytes
         | 
| 15 | 
            +
              #
         | 
| 16 | 
            +
              #   ProtobufDescriptor.decode(File.read("descriptor.desc"))
         | 
| 17 | 
            +
              def self.decode(bytes)
         | 
| 18 | 
            +
                return self.decode_from(::StringIO.new(bytes))
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              # Decode a ProtobufDescriptor from a readable stream
         | 
| 22 | 
            +
              #
         | 
| 23 | 
            +
              #   ProtobufDescriptor.decode_from(File.open("descriptor.desc"))
         | 
| 24 | 
            +
              def self.decode_from(stream)
         | 
| 25 | 
            +
                return self.new(stream)
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              # Loads a ProtobufDescriptor from a file
         | 
| 29 | 
            +
              #
         | 
| 30 | 
            +
              #   ProtobufDescriptor.load("descriptor.desc")
         | 
| 31 | 
            +
              def self.load(path)
         | 
| 32 | 
            +
                return self.decode_from(File.open(path))
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              # Raw FileDescriptorSet protocol buffer
         | 
| 36 | 
            +
              attr_reader :descriptor_set
         | 
| 37 | 
            +
              # Set of .proto files that are contained within the descriptor set, as a NamedCollection of FileDescriptors[link:ProtobufDescriptor/FileDescriptor.html]
         | 
| 38 | 
            +
              attr_reader :file
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              def initialize(stream)
         | 
| 41 | 
            +
                @descriptor_set = Google::Protobuf::FileDescriptorSet.new.decode_from(stream)
         | 
| 42 | 
            +
                @file = ProtobufDescriptor::NamedCollection.new(@descriptor_set.file.map { |f| ProtobufDescriptor::FileDescriptor.new(self, f) }) do |name, member|
         | 
| 43 | 
            +
                  member.name == name || member.name == "#{name}.proto"
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
              alias_method :files, :file
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              # Returns all the named descendants of this descriptor set, basically every
         | 
| 50 | 
            +
              # defined MessageDescriptor[link:ProtobufDescriptor/MessageDescriptor.html],
         | 
| 51 | 
            +
              # EnumDescriptor[link:ProtobufDescriptor/EnumDescriptor.html], and
         | 
| 52 | 
            +
              # ServiceDescriptor[link:ProtobufDescriptor/ServiceDescriptor] referenced
         | 
| 53 | 
            +
              # in this set of proto files.
         | 
| 54 | 
            +
              def all_descendants
         | 
| 55 | 
            +
                seeds = files.to_a.dup
         | 
| 56 | 
            +
                children = Set.new
         | 
| 57 | 
            +
                while !seeds.empty?
         | 
| 58 | 
            +
                  seeds.pop.children.each do |child|
         | 
| 59 | 
            +
                    children << child
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                    seeds << child if child.respond_to?(:children)
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
                end
         | 
| 64 | 
            +
                children
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
              # Finds the descriptor corresponding to a given type name. +type_name+ can
         | 
| 68 | 
            +
              # either be a fully qualified name (with a leading "."), or a relative name,
         | 
| 69 | 
            +
              # in which case +relative_to+ must either be a descriptor or a fully qualified
         | 
| 70 | 
            +
              # name that the relative name is resolved relative to.
         | 
| 71 | 
            +
              def resolve_type_name(type_name, relative_to=nil)
         | 
| 72 | 
            +
                if type_name.start_with?('.')
         | 
| 73 | 
            +
                  all_descendants.find { |descendant|
         | 
| 74 | 
            +
                    descendant.fully_qualified_name == type_name
         | 
| 75 | 
            +
                  }
         | 
| 76 | 
            +
                else
         | 
| 77 | 
            +
                  raise "Must provide a relative path!" unless relative_to
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                  relative_to = relative_to.fully_qualified_name if relative_to.respond_to? :fully_qualified_name
         | 
| 80 | 
            +
                  parents = relative_to.split('.')
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                  # The first element is the empty string, which is the root.
         | 
| 83 | 
            +
                  while parents.size > 1
         | 
| 84 | 
            +
                    type = resolve_type_name("#{parents.join('.')}.#{type_name}")
         | 
| 85 | 
            +
                    return type if type
         | 
| 86 | 
            +
                    parents.pop
         | 
| 87 | 
            +
                  end
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
              end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
              # Shorthand for accessing files
         | 
| 92 | 
            +
              def [](index)
         | 
| 93 | 
            +
                return files[index]
         | 
| 94 | 
            +
              end
         | 
| 95 | 
            +
            end
         | 
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require File.expand_path('../lib/protobuf_descriptor/version', __FILE__)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |gem|
         | 
| 6 | 
            +
              gem.name          = "protobuf_descriptor"
         | 
| 7 | 
            +
              gem.version       = ProtobufDescriptor::VERSION
         | 
| 8 | 
            +
              gem.summary       = %q{Protocol Buffer file descriptor parser}
         | 
| 9 | 
            +
              gem.description   = %q{Wraps the protobuf FileDescriptorSet messages with happy features like type name resolution and the like.}
         | 
| 10 | 
            +
              gem.license       = "MIT"
         | 
| 11 | 
            +
              gem.authors       = ["Hsiu-Fan Wang"]
         | 
| 12 | 
            +
              gem.email         = "hfwang@porkbuns.net"
         | 
| 13 | 
            +
              gem.homepage      = "https://github.com/hfwang/protobuf_descriptor"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              gem.files         = `git ls-files`.split($/)
         | 
| 16 | 
            +
              gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         | 
| 17 | 
            +
              gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
         | 
| 18 | 
            +
              gem.require_paths = ['lib']
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              gem.add_dependency 'protobuf', '~> 3.0.0'
         | 
| 21 | 
            +
              gem.add_dependency 'active_support', '>= 3.0'
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              gem.add_development_dependency 'pry', '~> 0.9.12.6'
         | 
| 24 | 
            +
              gem.add_development_dependency 'rspec', '~> 2.4'
         | 
| 25 | 
            +
              gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
         | 
| 26 | 
            +
              gem.add_development_dependency 'yard', '~> 0.8'
         | 
| 27 | 
            +
            end
         | 
| @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor::EnumDescriptor do
         | 
| 4 | 
            +
              describe "#fully_qualified_name" do
         | 
| 5 | 
            +
                it "handles top-level enums" do
         | 
| 6 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 7 | 
            +
                    expect(descriptor[:single_file].enum_types[:UnnestedEnum].fully_qualified_name).to eq(".porkbuns.UnnestedEnum")
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                it "handles nested enums" do
         | 
| 12 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 13 | 
            +
                    expect(descriptor[:single_file].message_types[:FieldOptions].enum_types[:CType].fully_qualified_name).to eq(".porkbuns.FieldOptions.CType")
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              describe "#fully_qualified_java_name" do
         | 
| 19 | 
            +
                it "handles top-level enums" do
         | 
| 20 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 21 | 
            +
                    expect(descriptor[:single_file].enum_types[:UnnestedEnum].fully_qualified_java_name).to eq("porkbuns.SingleFile.UnnestedEnum")
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                it "handles nested enums" do
         | 
| 26 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 27 | 
            +
                    expect(descriptor[:single_file].message_types[:FieldOptions].enum_types[:CType].fully_qualified_java_name).to eq("porkbuns.SingleFile.FieldOptions.CType")
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                it "handles java_outer_classname option" do
         | 
| 32 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 33 | 
            +
                    name = descriptor["wearabouts_api/outer_class_proto"].enum_types[:IconEnum].fully_qualified_java_name
         | 
| 34 | 
            +
                    expect(name).to eq("us.wearabouts.chatabout.outer.OuterClassName.IconEnum")
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                it "handles java_multiple_files option" do
         | 
| 39 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 40 | 
            +
                    name = descriptor["wearabouts_api/multiple_files"].enum_types[:IconEnum].fully_qualified_java_name
         | 
| 41 | 
            +
                    expect(name).to eq("us.wearabouts.chatabout.multiple.IconEnum")
         | 
| 42 | 
            +
                  end
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
            end
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor::MessageDescriptor::FieldDescriptor do
         | 
| 4 | 
            +
              describe "#type_name" do
         | 
| 5 | 
            +
                it "is sane" do
         | 
| 6 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 7 | 
            +
                    message_descriptor = descriptor[:single_file].messages[:FieldOptions]
         | 
| 8 | 
            +
                    field_descriptor = message_descriptor.fields[:ctype]
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                    expect(field_descriptor.field_type).to eq(Google::Protobuf::FieldDescriptorProto::Type::TYPE_ENUM)
         | 
| 11 | 
            +
                    expect(field_descriptor.type_name).to eq(".porkbuns.FieldOptions.CType")
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              it "#resolve_type resolves a field's type name" do
         | 
| 17 | 
            +
                with_descriptor("single_file_test") do |descriptor|
         | 
| 18 | 
            +
                  message_descriptor = descriptor[:single_file].messages[:FieldOptions]
         | 
| 19 | 
            +
                  field_descriptor = message_descriptor.fields[:ctype]
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  expect(field_descriptor.resolve_type).to eq(message_descriptor.enums[:CType])
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor::FileDescriptor do
         | 
| 4 | 
            +
              describe "#files" do
         | 
| 5 | 
            +
                it "has the right size" do
         | 
| 6 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 7 | 
            +
                    expect(descriptor.files).to have(1).items
         | 
| 8 | 
            +
                    expect(descriptor.files.size).to eq(1)
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                describe "#[]" do
         | 
| 13 | 
            +
                  it "handles numeric index" do
         | 
| 14 | 
            +
                    with_descriptor("service_rpc_test") do |descriptor|
         | 
| 15 | 
            +
                      expect(descriptor.files[0]).to eq(descriptor.files.first)
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  it "handles lookup by basename" do
         | 
| 20 | 
            +
                    with_descriptor("service_rpc_test") do |descriptor|
         | 
| 21 | 
            +
                      expect(descriptor.files["wearabouts_api/user"].name).to eq("wearabouts_api/user.proto")
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  it "handles lookup by filename" do
         | 
| 26 | 
            +
                    with_descriptor("service_rpc_test") do |descriptor|
         | 
| 27 | 
            +
                      expect(descriptor.files["wearabouts_api/user.proto"].name).to eq("wearabouts_api/user.proto")
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  it "returns nil if not found" do
         | 
| 32 | 
            +
                    with_descriptor("service_rpc_test") do |descriptor|
         | 
| 33 | 
            +
                      expect(descriptor.files["wearabouts_api/userblah"]).to be_nil
         | 
| 34 | 
            +
                    end
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              describe "#java_package" do
         | 
| 40 | 
            +
                it "handles java_package option" do
         | 
| 41 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 42 | 
            +
                    expect(descriptor.files[:wearabouts_pb].java_package).to eq("us.wearabouts.chatabout.proto")
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                it "defaults to package if no java_package specified" do
         | 
| 47 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 48 | 
            +
                    expect(descriptor.files[:single_file].java_package).to eq("porkbuns")
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
              end
         | 
| 52 | 
            +
            end
         | 
| @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor::MessageDescriptor do
         | 
| 4 | 
            +
              describe "#fully_qualified_name" do
         | 
| 5 | 
            +
                it "handles top-level messages" do
         | 
| 6 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 7 | 
            +
                    expect(descriptor[:single_file].message_types[:FieldOptions].fully_qualified_name).to eq(".porkbuns.FieldOptions")
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                it "handles nested messages" do
         | 
| 12 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 13 | 
            +
                    expect(descriptor[:wearabouts_pb].message_types[:UserProto].nested_type[:UserDetails].fully_qualified_name).to eq(".WearaboutsPb.UserProto.UserDetails")
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              describe "#fully_qualified_java_name" do
         | 
| 19 | 
            +
                it "handles top-level messages" do
         | 
| 20 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 21 | 
            +
                    expect(descriptor[:single_file].message_types[:FieldOptions].fully_qualified_java_name).to eq("porkbuns.SingleFile.FieldOptions")
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                it "handles nested messages" do
         | 
| 26 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 27 | 
            +
                    expect(descriptor[:wearabouts_pb].message_types[:UserProto].nested_type[:UserDetails].fully_qualified_java_name).to eq("us.wearabouts.chatabout.proto.WearaboutsPb.UserProto.UserDetails")
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                it "handles java_outer_classname option" do
         | 
| 32 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 33 | 
            +
                    expect(descriptor["wearabouts_api/outer_class_proto"].message_types[:Icon].fully_qualified_java_name).to eq("us.wearabouts.chatabout.outer.OuterClassName.Icon")
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                it "handles java_multiple_files option" do
         | 
| 38 | 
            +
                  with_descriptor("service_rpc_test") do |descriptor|
         | 
| 39 | 
            +
                    expect(descriptor["wearabouts_api/multiple_files"].message_types[:Icon].fully_qualified_java_name).to eq("us.wearabouts.chatabout.multiple.Icon")
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor::ServiceDescriptor::MethodDescriptor do
         | 
| 4 | 
            +
              it "is sane" do
         | 
| 5 | 
            +
                with_descriptor("service_rpc_test") do |descriptor|
         | 
| 6 | 
            +
                  file_descriptor = descriptor["wearabouts_api/user"]
         | 
| 7 | 
            +
                  service_descriptor = file_descriptor.services[:UserService]
         | 
| 8 | 
            +
                  method_descriptor = service_descriptor.methods[:Authenticate]
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  expect(method_descriptor.input_type_name).to eq(".WearaboutsApi.User.AuthenticateRequest")
         | 
| 11 | 
            +
                  expect(method_descriptor.output_type_name).to eq(".WearaboutsApi.User.AuthenticateResponse")
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  expect(method_descriptor.resolve_input_type).to eq(file_descriptor.messages[:AuthenticateRequest])
         | 
| 14 | 
            +
                  expect(method_descriptor.resolve_output_type).to eq(file_descriptor.messages[:AuthenticateResponse])
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
            end
         | 
| @@ -0,0 +1,75 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe ProtobufDescriptor do
         | 
| 4 | 
            +
              it "should have a VERSION constant" do
         | 
| 5 | 
            +
                expect(ProtobufDescriptor::VERSION).not_to be_empty
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              describe "#resolve_type_name" do
         | 
| 9 | 
            +
                it "should handle fully qualified names" do
         | 
| 10 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 11 | 
            +
                    expect(descriptor.resolve_type_name(".porkbuns.UnnestedEnum")).to eq(descriptor[:single_file].enums[:UnnestedEnum])
         | 
| 12 | 
            +
                    expect(descriptor.resolve_type_name(".porkbuns.FieldOptions")).to eq(descriptor[:single_file].messages[:FieldOptions])
         | 
| 13 | 
            +
                    expect(descriptor.resolve_type_name(".porkbuns.FieldOptions.CType")).to eq(descriptor[:single_file].messages[:FieldOptions].enums[:CType])
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                it "should handle relative names" do
         | 
| 18 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 19 | 
            +
                    expect(descriptor.resolve_type_name("UnnestedEnum", ".porkbuns")).to eq(descriptor[:single_file].enums[:UnnestedEnum])
         | 
| 20 | 
            +
                    expect(descriptor.resolve_type_name("FieldOptions", ".porkbuns")).to eq(descriptor[:single_file].messages[:FieldOptions])
         | 
| 21 | 
            +
                    expect(descriptor.resolve_type_name("CType", ".porkbuns.FieldOptions")).to eq(descriptor[:single_file].messages[:FieldOptions].enums[:CType])
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                it "should allow the relative_to argument to be a descriptor object" do
         | 
| 26 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 27 | 
            +
                    file_descriptor = descriptor[:single_file]
         | 
| 28 | 
            +
                    expect(descriptor.resolve_type_name("UnnestedEnum", file_descriptor)).to eq(file_descriptor.enums[:UnnestedEnum])
         | 
| 29 | 
            +
                    expect(descriptor.resolve_type_name("FieldOptions", file_descriptor)).to eq(file_descriptor.messages[:FieldOptions])
         | 
| 30 | 
            +
                    expect(descriptor.resolve_type_name("CType", file_descriptor.messages[:FieldOptions])).to eq(file_descriptor.messages[:FieldOptions].enums[:CType])
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                it "should backtrack when using relative names" do
         | 
| 35 | 
            +
                  with_descriptor("single_file_test") do |descriptor|
         | 
| 36 | 
            +
                    expect(descriptor.resolve_type_name("UnnestedEnum", ".porkbuns.FieldOptions.CType")).to eq(descriptor[:single_file].enums[:UnnestedEnum])
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              describe "Deserialization" do
         | 
| 42 | 
            +
                describe "#load" do
         | 
| 43 | 
            +
                  it "should load the file" do
         | 
| 44 | 
            +
                    with_descriptor_file("single_file_test") do |f|
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                      descriptor = ProtobufDescriptor.load(f.path)
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                      expect(descriptor.files).to have(1).item
         | 
| 49 | 
            +
                    end
         | 
| 50 | 
            +
                  end
         | 
| 51 | 
            +
                end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                describe "#decode_from" do
         | 
| 54 | 
            +
                  it "should load the file" do
         | 
| 55 | 
            +
                    with_descriptor_file("single_file_test") do |f|
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                      descriptor = ProtobufDescriptor.decode_from(f)
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                      expect(descriptor.files).to have(1).item
         | 
| 60 | 
            +
                    end
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                describe "#decode" do
         | 
| 65 | 
            +
                  it "should load the file" do
         | 
| 66 | 
            +
                    with_descriptor_file("single_file_test") do |f|
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                      descriptor = ProtobufDescriptor.decode(f.read)
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                      expect(descriptor.files).to have(1).item
         | 
| 71 | 
            +
                    end
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
            end
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
            require "tmpdir"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe "ProtocJavaCompiler" do
         | 
| 5 | 
            +
              # Compile the contents of the generator_tests proto dir, and then assert
         | 
| 6 | 
            +
              # everything is where it should be.
         | 
| 7 | 
            +
              it "computes fully-qualified class names correctly" do
         | 
| 8 | 
            +
                Dir.mktmpdir do |dir|
         | 
| 9 | 
            +
                  with_descriptor("generator_test", plugin: "java", plugin_out: dir) do |descriptor|
         | 
| 10 | 
            +
                    generated_classes = ghetto_parse_java_package(dir)
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                    children = descriptor.all_descendants
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    children.each do |child|
         | 
| 15 | 
            +
                      expect(child.fully_qualified_java_name).to eq(generated_classes[child.name])
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| @@ -0,0 +1,36 @@ | |
| 1 | 
            +
            package WearaboutsApi.User;
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            option java_package = "us.wearabouts.chatabout.proto.user";
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            import "wearabouts_pb.proto";
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            message AuthenticateRequest {
         | 
| 8 | 
            +
              optional string email = 1;
         | 
| 9 | 
            +
              optional string phone = 2;
         | 
| 10 | 
            +
              required string device_id = 3;
         | 
| 11 | 
            +
              optional string name = 4;
         | 
| 12 | 
            +
            }
         | 
| 13 | 
            +
            message AuthenticateResponse {
         | 
| 14 | 
            +
              optional WearaboutsPb.UserProto user = 1;
         | 
| 15 | 
            +
              optional WearaboutsPb.DeviceLinkProto device_link = 2;
         | 
| 16 | 
            +
              optional string auth_token = 3;
         | 
| 17 | 
            +
            }
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            message MeRequest {}
         | 
| 20 | 
            +
            message MeResponse {
         | 
| 21 | 
            +
              optional WearaboutsPb.UserProto user = 1;
         | 
| 22 | 
            +
              optional WearaboutsPb.DeviceLinkProto device_link = 2;
         | 
| 23 | 
            +
            }
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            message UpdateNameRequest {
         | 
| 26 | 
            +
              required string name = 1;
         | 
| 27 | 
            +
            }
         | 
| 28 | 
            +
            message UpdateNameResponse {
         | 
| 29 | 
            +
              optional WearaboutsPb.UserProto user = 1;
         | 
| 30 | 
            +
            }
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            service UserService {
         | 
| 33 | 
            +
              rpc Authenticate (AuthenticateRequest) returns (AuthenticateResponse);
         | 
| 34 | 
            +
              rpc Me (MeRequest) returns (MeResponse);
         | 
| 35 | 
            +
              rpc UpdateName (UpdateNameRequest) returns (UpdateNameResponse);
         | 
| 36 | 
            +
            }
         | 
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            package WearaboutsPb;
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            option java_package = "us.wearabouts.chatabout.proto";
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            message UserProto {
         | 
| 6 | 
            +
              message UserDetails {
         | 
| 7 | 
            +
                optional string email = 2;
         | 
| 8 | 
            +
                optional string phone = 3;
         | 
| 9 | 
            +
                optional string name = 4;
         | 
| 10 | 
            +
              }
         | 
| 11 | 
            +
              required string id = 1;
         | 
| 12 | 
            +
              optional UserDetails user_details = 2;
         | 
| 13 | 
            +
            }
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            message ConversationProto {
         | 
| 16 | 
            +
              required string id = 1;
         | 
| 17 | 
            +
              repeated UserProto participants = 2;
         | 
| 18 | 
            +
            }
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            message PostProto {
         | 
| 21 | 
            +
              enum Kind {
         | 
| 22 | 
            +
                TEXT = 1;
         | 
| 23 | 
            +
                AUDIO = 2;
         | 
| 24 | 
            +
              }
         | 
| 25 | 
            +
              required string id = 1;
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              required Kind kind = 2;
         | 
| 28 | 
            +
              optional string text = 100;
         | 
| 29 | 
            +
              optional string audio_url = 200;
         | 
| 30 | 
            +
            }
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            message DeviceLinkProto {
         | 
| 33 | 
            +
              required string device_id = 1;
         | 
| 34 | 
            +
              optional bool verified = 2;
         | 
| 35 | 
            +
            }
         |