class Protobuf::Generators::FieldGenerator

Constants

PROTO_INFINITY_DEFAULT

Constants

PROTO_NAN_DEFAULT
PROTO_NEGATIVE_INFINITY_DEFAULT
RUBY_INFINITY_DEFAULT
RUBY_NAN_DEFAULT
RUBY_NEGATIVE_INFINITY_DEFAULT

Attributes

field_options[R]

Attributes

Public Class Methods

new(field_descriptor, enclosing_msg_descriptor, indent_level) click to toggle source
Calls superclass method Protobuf::Generators::Base.new
# File lib/protobuf/generators/field_generator.rb, line 22
def initialize(field_descriptor, enclosing_msg_descriptor, indent_level)
  super(field_descriptor, indent_level)
  @enclosing_msg_descriptor = enclosing_msg_descriptor
end

Public Instance Methods

applicable_options() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 27
def applicable_options
  # Note on the strange use of `#inspect`:
  #   :boom.inspect #=> ":boom"
  #   :".boom.foo".inspect #=> ":\".boom.foo\""
  # An alternative to `#inspect` would be always adding double quotes,
  # but the generatated code looks un-idiomatic:
  #   ":\"#{:boom}\"" #=> ":\"boom\"" <-- Note the unnecessary double quotes
  @applicable_options ||= field_options.map { |k, v| "#{k.inspect} => #{v}" }
end
compile() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 66
def compile
  run_once(:compile) do
    field_definition = if map?
                         ["map #{map_key_type_name}", map_value_type_name, name, number, applicable_options]
                       else
                         ["#{label} #{type_name}", name, number, applicable_options]
                       end
    puts field_definition.flatten.compact.join(', ')
  end
end
default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 37
def default_value
  @default_value ||= begin
                       if defaulted?
                         case descriptor.type.name
                         when :TYPE_ENUM
                           enum_default_value
                         when :TYPE_STRING, :TYPE_BYTES
                           string_default_value
                         when :TYPE_FLOAT, :TYPE_DOUBLE
                           float_double_default_value
                         else
                           verbatim_default_value
                         end
                       end
                     end
end
defaulted?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 54
def defaulted?
  descriptor.respond_to_has_and_present?(:default_value)
end
deprecated?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 58
def deprecated?
  descriptor.options.try(:deprecated?) { false }
end
extension?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 62
def extension?
  descriptor.respond_to_has_and_present?(:extendee)
end
label() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 77
def label
  @label ||= descriptor.label.name.to_s.downcase.sub(/label_/, '') # required, optional, repeated
end
map?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 123
def map?
  !map_entry.nil?
end
map_entry() click to toggle source

If this field is a map field, this returns a message descriptor that represents the entries in the map. Returns nil if this field is not a map field.

# File lib/protobuf/generators/field_generator.rb, line 119
def map_entry
  @map_entry ||= determine_map_entry
end
map_key_type_name() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 127
def map_key_type_name
  return nil if map_entry.nil?
  determine_type_name(map_entry.field.find { |v| v.name == 'key' && v.number == 1 })
end
map_value_type_name() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 132
def map_value_type_name
  return nil if map_entry.nil?
  determine_type_name(map_entry.field.find { |v| v.name == 'value' && v.number == 2 })
end
name() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 81
def name
  @name ||= descriptor.name.to_sym.inspect
end
number() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 85
def number
  @number ||= descriptor.number
end
packed?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 107
def packed?
  descriptor.options.try(:packed?) { false }
end
type_name() click to toggle source

Determine the field type

# File lib/protobuf/generators/field_generator.rb, line 112
def type_name
  @type_name ||= determine_type_name(descriptor)
end

Private Instance Methods

determine_map_entry() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 180
def determine_map_entry
  return nil if @enclosing_msg_descriptor.nil?
  return nil unless descriptor.label.name == :LABEL_REPEATED && descriptor.type.name == :TYPE_MESSAGE
  # find nested message type
  name_parts = descriptor.type_name.split(".")
  return nil if name_parts.size < 2 || name_parts[-2] != @enclosing_msg_descriptor.name
  nested = @enclosing_msg_descriptor.nested_type.find { |e| e.name == name_parts[-1] }
  return nested if !nested.nil? && nested.options.try(:map_entry?)
  nil
end
determine_type_name(descriptor) click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 170
def determine_type_name(descriptor)
  case descriptor.type.name
  when :TYPE_MESSAGE, :TYPE_ENUM, :TYPE_GROUP then
    modulize(descriptor.type_name)
  else
    type_name = descriptor.type.name.to_s.downcase.sub(/^type_/, '')
    ":#{type_name}"
  end
end
enum_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 139
def enum_default_value
  optionally_upcased_default =
    if ENV.key?('PB_UPCASE_ENUMS')
      verbatim_default_value.upcase
    else
      verbatim_default_value
    end
  "#{type_name}::#{optionally_upcased_default}"
end
float_double_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 149
def float_double_default_value
  case verbatim_default_value
  when PROTO_INFINITY_DEFAULT then
    RUBY_INFINITY_DEFAULT
  when PROTO_NEGATIVE_INFINITY_DEFAULT then
    RUBY_NEGATIVE_INFINITY_DEFAULT
  when PROTO_NAN_DEFAULT then
    RUBY_NAN_DEFAULT
  else
    verbatim_default_value
  end
end
string_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 162
def string_default_value
  %("#{verbatim_default_value.gsub(/'/, '\\\\\'')}")
end
verbatim_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 166
def verbatim_default_value
  descriptor.default_value
end