Class: Protobuf::Generators::Base
- Inherits:
-
Object
- Object
- Protobuf::Generators::Base
- Includes:
- Printable
- Defined in:
- lib/protobuf/generators/base.rb
Direct Known Subclasses
EnumGenerator, ExtensionGenerator, FieldGenerator, FileGenerator, MessageGenerator, OptionGenerator, ServiceGenerator
Constant Summary
Constants included from Printable
Printable::PARENT_CLASS_ENUM, Printable::PARENT_CLASS_MESSAGE, Printable::PARENT_CLASS_SERVICE
Instance Attribute Summary collapse
-
#descriptor ⇒ Object
readonly
Returns the value of attribute descriptor.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #fully_qualified_type_namespace ⇒ Object
-
#initialize(descriptor, indent_level = 0, options = {}) ⇒ Base
constructor
A new instance of Base.
- #run_once(label) ⇒ Object
- #serialize_value(value) ⇒ Object
- #to_s ⇒ Object
- #type_namespace ⇒ Object
Methods included from Printable
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, = {}) @descriptor = descriptor @options = @namespace = @options.fetch(:namespace) { [] } init_printer(indent_level) end |
Instance Attribute Details
#descriptor ⇒ Object (readonly)
Returns the value of attribute descriptor.
26 27 28 |
# File 'lib/protobuf/generators/base.rb', line 26 def descriptor @descriptor end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
26 27 28 |
# File 'lib/protobuf/generators/base.rb', line 26 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
26 27 28 |
# File 'lib/protobuf/generators/base.rb', line 26 def @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.(type_name, ) return if .empty? = .uniq if .size < .size ::Protobuf::CodeGenerator.fatal("#{type_name} object has duplicate tags. Expected #{.size} tags, but got #{.size}. Suppress with PB_NO_TAG_WARNINGS=1.") end unless ENV.key?('PB_NO_TAG_WARNINGS') expected_size = .max - .min + 1 if .size < expected_size ::Protobuf::CodeGenerator.print_tag_warning_suppress ::Protobuf::CodeGenerator.warn("#{type_name} object should have #{expected_size} tags (#{.min}..#{.max}), but found #{.size} tags.") end end end |
Instance Method Details
#fully_qualified_type_namespace ⇒ Object
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_s ⇒ Object
53 54 55 56 |
# File 'lib/protobuf/generators/base.rb', line 53 def to_s compile print_contents # see Printable end |
#type_namespace ⇒ Object
58 59 60 |
# File 'lib/protobuf/generators/base.rb', line 58 def type_namespace @type_namespace ||= @namespace + [descriptor.name] end |