Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/proto_plugin/file_descriptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ def services
end
end

# The files imported by this file, resolved to their descriptors.
#
# Imports that were not included in the request are omitted.
#
# @return [Array<FileDescriptor>]
#
# @see https://github.com/protocolbuffers/protobuf/blob/v28.2/src/google/protobuf/descriptor.proto#L100
# Google::Protobuf::FileDescriptorProto#dependency
def imports
@imports ||= @descriptor.dependency.filter_map do |name|
@context.file_by_filename(name)
end
end

# The subset of {#imports} that were declared with `import public`, resolved
# to their descriptors.
#
# @return [Array<FileDescriptor>]
#
# @see https://github.com/protocolbuffers/protobuf/blob/v28.2/src/google/protobuf/descriptor.proto#L103
# Google::Protobuf::FileDescriptorProto#public_dependency
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

private

# Field numbers of the relevant repeated fields within their parent
Expand Down
Binary file modified test/fixtures/blog.cgr
Binary file not shown.
Binary file modified test/fixtures/blog.fds
Binary file not shown.
2 changes: 1 addition & 1 deletion test/fixtures/blog/article.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package proto_plugin.fixtures;

import "google/protobuf/timestamp.proto";

import "comment.proto";
import public "comment.proto";

// An article published on the blog.
message Article {
Expand Down
27 changes: 27 additions & 0 deletions test/proto_plugin/file_descriptor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ def test_services
assert_instance_of(ServiceDescriptor, service_one)
end

def test_imports
file = @context.file_by_filename("article.proto")

assert_equal(
["google/protobuf/timestamp.proto", "comment.proto"],
file.imports.map(&:name),
)

file.imports.each do |i|
assert_instance_of(FileDescriptor, i)
end
end

def test_public_imports
file = @context.file_by_filename("article.proto")

assert_equal(["comment.proto"], file.public_imports.map(&:name))
assert_instance_of(FileDescriptor, file.public_imports.first)
end

def test_imports_empty_without_dependencies
file = @context.file_by_filename("category.proto")

assert_empty(file.imports)
assert_empty(file.public_imports)
end

def test_namespace_without_package
file = FileDescriptor.new(@context, Google::Protobuf::FileDescriptorProto.new(
name: "sample.proto",
Expand Down
Loading