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.



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

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.



32
33
34
# File 'lib/protobuf/generators/base.rb', line 32

def descriptor
  @descriptor
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



32
33
34
# File 'lib/protobuf/generators/base.rb', line 32

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/protobuf/generators/base.rb', line 32

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
25
26
27
28
29
30
# 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')
    range = (tags.min)..(tags.max)
    if range.respond_to?(:size)
      expected_size = range.size
    else
      expected_size = range.to_a.size
    end

    if tags.size < expected_size
      ::Protobuf::CodeGenerator.print_tag_warning_suppress
      ::Protobuf::CodeGenerator.warn("#{type_name} object should have #{expected_size} tags (#{range.begin}..#{range.end}), but found #{tags.size} tags.")
    end
  end
end

Instance Method Details

#fully_qualified_type_namespaceObject



41
42
43
# File 'lib/protobuf/generators/base.rb', line 41

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

#run_once(label, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/protobuf/generators/base.rb', line 45

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

  if instance_variable_get(tracker_ivar)
    return instance_variable_get(value_ivar)
  else
    return_value = block.call
    instance_variable_set(tracker_ivar, true)
    instance_variable_set(value_ivar, return_value)
    return return_value
  end
end

#to_sObject



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

def to_s
  compile
  print_contents # see Printable
end

#type_namespaceObject



64
65
66
# File 'lib/protobuf/generators/base.rb', line 64

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