Class: Protobuf::Generators::FieldGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/generators/field_generator.rb

Constant Summary collapse

PROTO_INFINITY_DEFAULT =

Constants

/^inf$/i
PROTO_NEGATIVE_INFINITY_DEFAULT =
/^-inf$/i
PROTO_NAN_DEFAULT =
/^nan$/i
RUBY_INFINITY_DEFAULT =
'::Float::INFINITY'.freeze
RUBY_NEGATIVE_INFINITY_DEFAULT =
'-::Float::INFINITY'.freeze
RUBY_NAN_DEFAULT =
'::Float::NAN'.freeze

Constants included from Printable

Printable::PARENT_CLASS_ENUM, Printable::PARENT_CLASS_MESSAGE, Printable::PARENT_CLASS_SERVICE

Instance Attribute Summary collapse

Attributes inherited from Base

#descriptor, #namespace, #options

Instance Method Summary collapse

Methods inherited from Base

#fully_qualified_type_namespace, #run_once, #serialize_value, #to_s, #type_namespace, validate_tags

Methods included from Printable

#init_printer

Constructor Details

#initialize(field_descriptor, enclosing_msg_descriptor, indent_level) ⇒ FieldGenerator

Returns a new instance of FieldGenerator.



22
23
24
25
# 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

Instance Attribute Details

#field_optionsObject (readonly)

Attributes



20
21
22
# File 'lib/protobuf/generators/field_generator.rb', line 20

def field_options
  @field_options
end

Instance Method Details

#applicable_optionsObject



27
28
29
30
31
32
33
34
35
# 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

#compileObject



66
67
68
69
70
71
72
73
74
75
# 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_valueObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/protobuf/generators/field_generator.rb', line 54

def defaulted?
  descriptor.respond_to_has_and_present?(:default_value)
end

#deprecated?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/protobuf/generators/field_generator.rb', line 58

def deprecated?
  descriptor.options.try(:deprecated?) { false }
end

#extension?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/protobuf/generators/field_generator.rb', line 62

def extension?
  descriptor.respond_to_has_and_present?(:extendee)
end

#labelObject



77
78
79
# 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?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/protobuf/generators/field_generator.rb', line 123

def map?
  !map_entry.nil?
end

#map_entryObject

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.



119
120
121
# File 'lib/protobuf/generators/field_generator.rb', line 119

def map_entry
  @map_entry ||= determine_map_entry
end

#map_key_type_nameObject



127
128
129
130
# 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_nameObject



132
133
134
135
# 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

#nameObject



81
82
83
# File 'lib/protobuf/generators/field_generator.rb', line 81

def name
  @name ||= descriptor.name.to_sym.inspect
end

#numberObject



85
86
87
# File 'lib/protobuf/generators/field_generator.rb', line 85

def number
  @number ||= descriptor.number
end

#packed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/protobuf/generators/field_generator.rb', line 107

def packed?
  descriptor.options.try(:packed?) { false }
end

#type_nameObject

Determine the field type



112
113
114
# File 'lib/protobuf/generators/field_generator.rb', line 112

def type_name
  @type_name ||= determine_type_name(descriptor)
end