Class: ProtoPlugin::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_plugin/base.rb

Overview

This class is abstract.

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

Class Method Summary collapse

Instance Method Summary collapse

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

#requestGoogle::Protobuf::Compiler::CodeGeneratorRequest (readonly)

The request message the plugin was initialized with.

Returns:

  • (Google::Protobuf::Compiler::CodeGeneratorRequest)


49
50
51
# File 'lib/proto_plugin/base.rb', line 49

def request
  @request
end

#responseGoogle::Protobuf::Compiler::CodeGeneratorResponse (readonly)

The response message to be sent back to protoc.

Returns:

  • (Google::Protobuf::Compiler::CodeGeneratorResponse)


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:.

Parameters:

  • input (IO) (defaults to: $stdin)

    The stream that the request is decoded from.

  • output (IO) (defaults to: $stdout)

    The stream that the response is encoded to.



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.

Parameters:

  • path (String)

    The relative path to write the file’s content.

  • content (String)

    The content which will be written to the file.



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_generateArray<FileDescriptor>

Returns an array of ProtoPlugin::FileDescriptor representing the files that were passed to protoc to be generated.

Examples:

protoc --myplugin_out=. input_one.proto input_two.proto

[
  <ProtoPlugin::FileDescriptor: name: "input_one.proto">,
  <ProtoPlugin::FileDescriptor: name: "input_two.proto">
]

Returns:



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.

Returns:



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

#parametersHash

Convenience method for accessing the parameters passed to the plugin.

Examples:

protoc --myplugin_opt=key=value --myplugin_opt=bare

{"key" => "value", "bare" => nil}

Returns:

  • (Hash)


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

#runObject

This method is abstract.

The primary entrypoint. Override to provide your plugin’s implementation.



128
129
# File 'lib/proto_plugin/base.rb', line 128

def run
end

#supported_featuresObject

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