Class: ProtoPlugin::Base Abstract
- Inherits:
-
Object
- Object
- ProtoPlugin::Base
- Defined in:
- lib/proto_plugin/base.rb
Overview
The primary base class to inherit from when implementing a plugin.
“‘ruby require ’proto_plugin’
class MyCoolPlugin < ProtoPlugin::Base def run # override to provide your implementation end end
MyCoolPlugin.run! ““
Instance Attribute Summary collapse
-
#request ⇒ Google::Protobuf::Compiler::CodeGeneratorRequest
readonly
The request message the plugin was initialized with.
-
#response ⇒ Google::Protobuf::Compiler::CodeGeneratorResponse
readonly
The response message to be sent back to
protoc
.
Class Method Summary collapse
-
.run!(input: $stdin, output: $stdout) ⇒ Object
The preferred way of invoking a plugin.
Instance Method Summary collapse
-
#add_file(path:, content:) ⇒ Object
Convenience method for appending a
CodeGeneratorResponse::File
message toresponse
. -
#files_to_generate ⇒ Array<FileDescriptor>
Returns an array of
ProtoPlugin::FileDescriptor
representing the files that were passed toprotoc
to be generated. -
#initialize(request:) ⇒ Base
constructor
Initializes a new instance of the plugin with a given
Google::Protobuf::Compiler::CodeGeneratorRequest
. -
#lookup_file(name:) ⇒ ProtoPlugin::FileDescriptor?
Finds an imported file descriptor with the given
name
attribute. -
#parameters ⇒ Hash
Convenience method for accessing the parameters passed to the plugin.
-
#run ⇒ Object
abstract
The primary entrypoint.
-
#supported_features ⇒ Object
Returns the list of supported
CodeGeneratorResponse::Feature
values by the plugin.
Constructor Details
#initialize(request:) ⇒ Base
Initializes a new instance of the plugin with a given Google::Protobuf::Compiler::CodeGeneratorRequest
.
57 58 59 60 61 62 |
# File 'lib/proto_plugin/base.rb', line 57 def initialize(request:) @request = request @response = Google::Protobuf::Compiler::CodeGeneratorResponse.new( supported_features: supported_features.reduce(&:|), ) end |
Instance Attribute Details
#request ⇒ Google::Protobuf::Compiler::CodeGeneratorRequest (readonly)
The request message the plugin was initialized with.
49 50 51 |
# File 'lib/proto_plugin/base.rb', line 49 def request @request end |
#response ⇒ Google::Protobuf::Compiler::CodeGeneratorResponse (readonly)
The response message to be sent back to protoc
.
53 54 55 |
# File 'lib/proto_plugin/base.rb', line 53 def response @response end |
Class Method Details
.run!(input: $stdin, output: $stdout) ⇒ Object
The preferred way of invoking a plugin.
Decodes a Google::Protobuf::Compiler::CodeGeneratorRequest
message from input:
, invokes the plugin by calling #run
, and then encodes response
to the stream specified by output:
.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/proto_plugin/base.rb', line 32 def run!(input: $stdin, output: $stdout) plugin = new( request: Google::Protobuf::Compiler::CodeGeneratorRequest.decode( input.read, ), ) plugin.run result = plugin.response output.write(result.to_proto) result end |
Instance Method Details
#add_file(path:, content:) ⇒ Object
Convenience method for appending a CodeGeneratorResponse::File
message to response
.
The path is relative to the directory specified when invoking protoc
. For example, specifiying --myplugin_out=gen
will result in gen/:path
.
120 121 122 123 124 |
# File 'lib/proto_plugin/base.rb', line 120 def add_file(path:, content:) @response.file << Google::Protobuf::Compiler::CodeGeneratorResponse::File.new( name: path, content: content, ) end |
#files_to_generate ⇒ Array<FileDescriptor>
Returns an array of ProtoPlugin::FileDescriptor
representing the files that were passed to protoc
to be generated.
87 88 89 90 91 |
# File 'lib/proto_plugin/base.rb', line 87 def files_to_generate @files_to_generate ||= request.file_to_generate.filter_map do |filename| lookup_file(name: filename) end end |
#lookup_file(name:) ⇒ ProtoPlugin::FileDescriptor?
Finds an imported file descriptor with the given name
attribute.
97 98 99 100 101 102 103 |
# File 'lib/proto_plugin/base.rb', line 97 def lookup_file(name:) @index_by_filename ||= @request.proto_file.each_with_object({}) do |fd, hash| hash[fd.name] = FileDescriptor.new(fd) end @index_by_filename[name] end |
#parameters ⇒ Hash
Convenience method for accessing the parameters passed to the plugin.
70 71 72 73 74 75 |
# File 'lib/proto_plugin/base.rb', line 70 def parameters @parameters ||= request.parameter&.split(",")&.each_with_object({}) do |param, hash| key, value = param.split("=") hash[key] = value end end |
#run ⇒ Object
The primary entrypoint. Override to provide your plugin’s implementation.
128 129 |
# File 'lib/proto_plugin/base.rb', line 128 def run end |
#supported_features ⇒ Object
Returns the list of supported CodeGeneratorResponse::Feature
values by the plugin. The returned values are bitwise or-ed together and set on response
.
Defaults to CodeGeneratorResponse::Feature::FEATURE_NONE
.
109 110 111 |
# File 'lib/proto_plugin/base.rb', line 109 def supported_features [Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_NONE] end |