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.freeze
PROTO_NEGATIVE_INFINITY_DEFAULT =
/^-inf$/i.freeze
PROTO_NAN_DEFAULT =
/^nan$/i.freeze
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, #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
# File 'lib/protobuf/generators/field_generator.rb', line 22

def applicable_options
  @applicable_options ||= field_options.map { |k, v| ":#{k} => #{v}" }
end

#compileObject



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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/protobuf/generators/field_generator.rb', line 26

def default_value
  @default_value ||= begin
                       if defaulted?
                         case descriptor.type.name
                         when :TYPE_ENUM then
                           enum_default_value
                         when :TYPE_STRING, :TYPE_BYTES then
                           string_default_value
                         when :TYPE_FLOAT, :TYPE_DOUBLE then
                           float_double_default_value
                         else
                           verbatim_default_value
                         end
                       end
                     end
end

#defaulted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/protobuf/generators/field_generator.rb', line 43

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

#deprecated?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/protobuf/generators/field_generator.rb', line 47

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

#extension?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/protobuf/generators/field_generator.rb', line 51

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

#labelObject



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

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

#nameObject



66
67
68
# File 'lib/protobuf/generators/field_generator.rb', line 66

def name
  @name ||= ":#{descriptor.name}"
end

#numberObject



70
71
72
# File 'lib/protobuf/generators/field_generator.rb', line 70

def number
  @number ||= descriptor.number
end

#packed?Boolean

Returns:

  • (Boolean)


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

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

#type_nameObject

Determine the field type



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/protobuf/generators/field_generator.rb', line 90

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