Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files_to_generate, lookup_file helpers #1

Merged
merged 2 commits into from
Oct 2, 2024
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
6 changes: 3 additions & 3 deletions exe/protoc-gen-proto-plugin-demo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ require "proto_plugin"

class Demo < ProtoPlugin::Base
def run
request.file_to_generate.each do |f|
name = File.basename(f, ".proto")
files_to_generate.each do |f|
name = File.basename(f.name, ".proto")
add_file(path: "#{name}.txt", content: <<~TXT)
Parameters: #{parameters}

This file was generated from #{name}.proto!
This file was generated from #{f.name}!
TXT
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/proto_plugin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def parameters
end
end

# Returns an array of `Google::Protobuf::FileDescriptorProto` representing the files that
# were passed to `protoc` to be generated.
#
# @example `protoc --myplugin_out=. input_one.proto input_two.proto`
# [
# <Google::Protobuf::FileDescriptorProto: name: "input_one.proto">,
# <Google::Protobuf::FileDescriptorProto: name: "input_two.proto">
# ]
#
# @return [Array]
def files_to_generate
@files_to_generate ||= request.file_to_generate.filter_map do |filename|
lookup_file(name: filename)
end
end

# Finds a `Google::Protobuf::FileDescriptorProto` with the given `name` attribute.
#
# @return [Google::Protobuf::FileDescriptorProto]
# @return [nil] if the file was not found
def lookup_file(name:)
@index_by_filename ||= @request.proto_file.each_with_object({}) do |fd, hash|
hash[fd.name] = fd
end

@index_by_filename[name]
end

# Returns the list of supported `CodeGeneratorResponse::Feature` values by the plugin. The returned
# values are bitwise or-ed together and set on `response`.
#
Expand Down
13 changes: 13 additions & 0 deletions test/proto_plugin/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def run
assert_equal("Hello World, Again!", @plugin.response.file.last.content)
end

test "files_to_generate" do
files = @plugin.files_to_generate

assert_equal(1, files.count)
assert_instance_of(Google::Protobuf::FileDescriptorProto, files.first)
assert_equal("test/fixtures/simple/simple.proto", files.first.name)
end

test "lookup_file" do
refute_nil(@plugin.lookup_file(name: "test/fixtures/simple/simple.proto"))
assert_nil(@plugin.lookup_file(name: "test/fixtures/simple/missing.proto"))
end

test "parameters" do
plugin = TestPlugin.new(request: Google::Protobuf::Compiler::CodeGeneratorRequest.new(
parameter: "foo=bar,bare",
Expand Down