Class: Protobuf::Generators::Base

Inherits:
Object
  • Object
show all
Includes:
Printable
Defined in:
lib/protobuf/generators/base.rb

Constant Summary

Constants included from Printable

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Printable

#init_printer

Constructor Details

#initialize(descriptor, indent_level = 0, options = {}) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
# File 'lib/protobuf/generators/base.rb', line 28

def initialize(descriptor, indent_level = 0, options = {})
  @descriptor = descriptor
  @options = options
  @namespace = @options.fetch(:namespace) { [] }
  init_printer(indent_level)
end

Instance Attribute Details

#descriptorObject (readonly)

Returns the value of attribute descriptor.



26
27
28
# File 'lib/protobuf/generators/base.rb', line 26

def descriptor
  @descriptor
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



26
27
28
# File 'lib/protobuf/generators/base.rb', line 26

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/protobuf/generators/base.rb', line 26

def options
  @options
end

Class Method Details

.validate_tags(type_name, tags) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protobuf/generators/base.rb', line 8

def self.validate_tags(type_name, tags)
  return if tags.empty?

  unique_tags = tags.uniq

  if unique_tags.size < tags.size
    ::Protobuf::CodeGenerator.fatal("#{type_name} object has duplicate tags. Expected #{unique_tags.size} tags, but got #{tags.size}. Suppress with PB_NO_TAG_WARNINGS=1.")
  end

  unless ENV.key?('PB_NO_TAG_WARNINGS')
    expected_size = tags.max - tags.min + 1
    if tags.size < expected_size
      ::Protobuf::CodeGenerator.print_tag_warning_suppress
      ::Protobuf::CodeGenerator.warn("#{type_name} object should have #{expected_size} tags (#{tags.min}..#{tags.max}), but found #{tags.size} tags.")
    end
  end
end

Instance Method Details

#fully_qualified_type_namespaceObject



35
36
37
# File 'lib/protobuf/generators/base.rb', line 35

def fully_qualified_type_namespace
  ".#{type_namespace.join('.')}"
end

#run_once(label) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/protobuf/generators/base.rb', line 39

def run_once(label)
  tracker_ivar = "@_#{label}_compiled"
  value_ivar = "@_#{label}_compiled_value"

  if instance_variable_get(tracker_ivar)
    return instance_variable_get(value_ivar)
  end

  return_value = yield
  instance_variable_set(tracker_ivar, true)
  instance_variable_set(value_ivar, return_value)
  return_value
end

#serialize_value(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/protobuf/generators/base.rb', line 62

def serialize_value(value)
  case value
  when Message
    fields = value.each_field.map do |field, inner_value|
      next unless value.field?(field.name)
      serialized_inner_value = serialize_value(inner_value)
      "#{field.fully_qualified_name.inspect} => #{serialized_inner_value}"
    end.compact
    "{ #{fields.join(', ')} }"
  when Enum
    "::#{value.parent_class}::#{value.name}"
  when String
    value.inspect
  when nil
    "nil"
  when Array
    '[' + value.map { |x| serialize_value(x) }.join(', ') + ']'
  else
    value
  end
end

#to_sObject



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

def to_s
  compile
  print_contents # see Printable
end

#type_namespaceObject



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

def type_namespace
  @type_namespace ||= @namespace + [descriptor.name]
end