class Protobuf::Generators::Base

Attributes

descriptor[R]
namespace[R]
options[R]

Public Class Methods

new(descriptor, indent_level = 0, options = {}) click to toggle source
# File lib/protobuf/generators/base.rb, line 28
def initialize(descriptor, indent_level = 0, options = {})
  @descriptor = descriptor
  @options = options
  @namespace = @options.fetch(:namespace) { [] }
  init_printer(indent_level)
end
validate_tags(type_name, tags) click to toggle source
# File lib/protobuf/generators/base.rb, line 8
def self.validate_tags(type_name, tags)
  return if tags.empty?

  unique_tags = tags.uniq

  if unique_tags.size < tags.size
    ::Protobuf::CodeGenerator.fatal("#{type_name} object has duplicate tags. Expected #{unique_tags.size} tags, but got #{tags.size}. Suppress with PB_NO_TAG_WARNINGS=1.")
  end

  unless ENV.key?('PB_NO_TAG_WARNINGS')
    expected_size = tags.max - tags.min + 1
    if tags.size < expected_size
      ::Protobuf::CodeGenerator.print_tag_warning_suppress
      ::Protobuf::CodeGenerator.warn("#{type_name} object should have #{expected_size} tags (#{tags.min}..#{tags.max}), but found #{tags.size} tags.")
    end
  end
end

Public Instance Methods

fully_qualified_type_namespace() click to toggle source
# File lib/protobuf/generators/base.rb, line 35
def fully_qualified_type_namespace
  ".#{type_namespace.join('.')}"
end
run_once(label) { || ... } click to toggle source
# File lib/protobuf/generators/base.rb, line 39
def run_once(label)
  tracker_ivar = "@_#{label}_compiled"
  value_ivar = "@_#{label}_compiled_value"

  if instance_variable_get(tracker_ivar)
    return instance_variable_get(value_ivar)
  end

  return_value = yield
  instance_variable_set(tracker_ivar, true)
  instance_variable_set(value_ivar, return_value)
  return_value
end
serialize_value(value) click to toggle source
# File lib/protobuf/generators/base.rb, line 62
def serialize_value(value)
  case value
  when Message
    fields = value.each_field.map do |field, inner_value|
      next unless value.field?(field.name)
      serialized_inner_value = serialize_value(inner_value)
      "#{field.fully_qualified_name.inspect} => #{serialized_inner_value}"
    end.compact
    "{ #{fields.join(', ')} }"
  when Enum
    "::#{value.parent_class}::#{value.name}"
  when String
    value.inspect
  when nil
    "nil"
  when Array
    '[' + value.map { |x| serialize_value(x) }.join(', ') + ']'
  else
    value
  end
end
to_s() click to toggle source
# File lib/protobuf/generators/base.rb, line 53
def to_s
  compile
  print_contents # see Printable
end
type_namespace() click to toggle source
# File lib/protobuf/generators/base.rb, line 58
def type_namespace
  @type_namespace ||= @namespace + [descriptor.name]
end