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 Instance Methods

applicable_options() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 22
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 61
def compile
  run_once(:compile) do
    field_definition = ["#{label} #{type_name}", name, number, applicable_options]
    puts field_definition.flatten.compact.join(', ')
  end
end
default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 32
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 49
def defaulted?
  descriptor.respond_to_has_and_present?(:default_value)
end
deprecated?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 53
def deprecated?
  descriptor.options.try(:deprecated?) { false }
end
extension?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 57
def extension?
  descriptor.respond_to_has_and_present?(:extendee)
end
label() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 68
def label
  @label ||= descriptor.label.name.to_s.downcase.sub(/label_/, '') # required, optional, repeated
end
name() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 72
def name
  @name ||= descriptor.name.to_sym.inspect
end
number() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 76
def number
  @number ||= descriptor.number
end
packed?() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 98
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 103
def type_name
  @type_name ||= begin
                   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
end

Private Instance Methods

enum_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 117
def enum_default_value
  "#{type_name}::#{verbatim_default_value}"
end
float_double_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 121
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 134
def string_default_value
  %("#{verbatim_default_value.gsub(/'/, '\\\\\'')}")
end
verbatim_default_value() click to toggle source
# File lib/protobuf/generators/field_generator.rb, line 138
def verbatim_default_value
  descriptor.default_value
end