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, #initialize, #run_once, #serialize_value, #to_s, #type_namespace, validate_tags

Methods included from Printable

#init_printer

Constructor Details

This class inherits a constructor from Protobuf::Generators::Base

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



22
23
24
25
26
27
28
29
30
# 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

#compileObject



61
62
63
64
65
66
# 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_valueObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/protobuf/generators/field_generator.rb', line 49

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

#deprecated?Boolean

Returns:

  • (Boolean)


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

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

#extension?Boolean

Returns:

  • (Boolean)


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

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

#labelObject



68
69
70
# File 'lib/protobuf/generators/field_generator.rb', line 68

def label
  @label ||= descriptor.label.name.to_s.downcase.sub(/label_/, '') # required, optional, repeated
end

#nameObject



72
73
74
# File 'lib/protobuf/generators/field_generator.rb', line 72

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

#numberObject



76
77
78
# File 'lib/protobuf/generators/field_generator.rb', line 76

def number
  @number ||= descriptor.number
end

#packed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/protobuf/generators/field_generator.rb', line 98

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

#type_nameObject

Determine the field type



103
104
105
106
107
108
109
110
111
112
113
# 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