Class: Protobuf::CodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/code_generator.rb

Constant Summary collapse

CodeGeneratorFatalError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_bytes) ⇒ CodeGenerator

Returns a new instance of CodeGenerator.



29
30
31
32
# File 'lib/protobuf/code_generator.rb', line 29

def initialize(request_bytes)
  @request_bytes = request_bytes
  self.request = ::Google::Protobuf::Compiler::CodeGeneratorRequest.decode(request_bytes)
end

Class Method Details

.fatal(message) ⇒ Object



10
11
12
# File 'lib/protobuf/code_generator.rb', line 10

def self.fatal(message)
  fail CodeGeneratorFatalError, message
end

rubocop:disable Lint/DuplicateMethods, Lint/NestedMethodDefinition



16
17
18
19
# File 'lib/protobuf/code_generator.rb', line 16

def self.print_tag_warning_suppress
  STDERR.puts "Suppress tag warning output with PB_NO_TAG_WARNINGS=1."
  def self.print_tag_warning_suppress; end # rubocop:disable Lint/DuplicateMethods, Lint/NestedMethodDefinition
end

.warn(message) ⇒ Object



19
20
21
# File 'lib/protobuf/code_generator.rb', line 19

def self.warn(message)
  STDERR.puts("[WARN] #{message}")
end

Instance Method Details

#eval_unknown_extensions!Object



34
35
36
37
38
39
# File 'lib/protobuf/code_generator.rb', line 34

def eval_unknown_extensions!
  request.proto_file.each do |file_descriptor|
    ::Protobuf::Generators::FileGenerator.new(file_descriptor).eval_unknown_extensions!
  end
  self.request = ::Google::Protobuf::Compiler::CodeGeneratorRequest.decode(@request_bytes)
end

#generate_file(file_descriptor) ⇒ Object



41
42
43
# File 'lib/protobuf/code_generator.rb', line 41

def generate_file(file_descriptor)
  ::Protobuf::Generators::FileGenerator.new(file_descriptor).generate_output_file
end

#response_bytesObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/protobuf/code_generator.rb', line 45

def response_bytes
  generated_files = request.proto_file.map do |file_descriptor|
    generate_file(file_descriptor)
  end

  ::Google::Protobuf::Compiler::CodeGeneratorResponse.encode(
    :file => generated_files,
    :supported_features => supported_features,
  )
end

#supported_featuresObject



56
57
58
59
60
# File 'lib/protobuf/code_generator.rb', line 56

def supported_features
  # The only available feature is proto3 with optional fields.
  # This is backwards compatible with proto2 optional fields.
  ::Google::Protobuf::Compiler::CodeGeneratorResponse::Feature::FEATURE_PROTO3_OPTIONAL.to_i
end