Class: Protobuf::Field::BaseField
- Inherits:
-
Object
- Object
- Protobuf::Field::BaseField
- Includes:
- Logging
- Defined in:
- lib/protobuf/field/base_field.rb
Direct Known Subclasses
Constant Summary collapse
- PACKED_TYPES =
Constants
[ ::Protobuf::WireType::VARINT, ::Protobuf::WireType::FIXED32, ::Protobuf::WireType::FIXED64, ].freeze
Instance Attribute Summary collapse
-
#fully_qualified_name ⇒ Object
readonly
Attributes.
-
#message_class ⇒ Object
readonly
Attributes.
-
#name ⇒ Object
readonly
Attributes.
-
#options ⇒ Object
readonly
Attributes.
-
#rule ⇒ Object
readonly
Attributes.
-
#tag ⇒ Object
readonly
Attributes.
-
#type_class ⇒ Object
readonly
Attributes.
Class Method Summary collapse
-
.default ⇒ Object
Class Methods.
Instance Method Summary collapse
-
#acceptable?(_value) ⇒ Boolean
Public Instance Methods.
- #coerce!(value) ⇒ Object
- #decode(_bytes) ⇒ Object
- #default ⇒ Object
- #default_value ⇒ Object
- #deprecated? ⇒ Boolean
- #encode(_value) ⇒ Object
- #enum? ⇒ Boolean
- #extension? ⇒ Boolean
- #fully_qualified_name_only! ⇒ Object
-
#initialize(message_class, rule, type_class, fully_qualified_name, tag, simple_name, options) ⇒ BaseField
constructor
Constructor.
- #message? ⇒ Boolean
- #optional? ⇒ Boolean
- #packed? ⇒ Boolean
- #repeated? ⇒ Boolean
- #repeated_message? ⇒ Boolean
- #required? ⇒ Boolean
-
#set(message_instance, bytes) ⇒ Object
FIXME: need to cleanup (rename) this warthog of a method.
- #tag_encoded ⇒ Object
-
#to_s ⇒ Object
FIXME: add packed, deprecated, extension options to to_s output.
- #wire_type ⇒ Object
Methods included from Logging
initialize_logger, #log_exception, #log_signature, #logger, #sign_message
Constructor Details
#initialize(message_class, rule, type_class, fully_qualified_name, tag, simple_name, options) ⇒ BaseField
Constructor
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/protobuf/field/base_field.rb', line 36 def initialize(, rule, type_class, fully_qualified_name, tag, simple_name, ) @message_class = @name = simple_name || fully_qualified_name @fully_qualified_name = fully_qualified_name @rule = rule @tag = tag @type_class = type_class # Populate the option hash with all the original default field options, for backwards compatibility. # However, both default and custom options should ideally be accessed through the Optionable .{get,get!}_option functions. @options = .slice(:ctype, :packed, :deprecated, :lazy, :jstype, :weak, :uninterpreted_option, :default, :extension) .each do |option_name, value| set_option(option_name, value) end validate_packed_field if packed? define_accessor(simple_name, fully_qualified_name) if simple_name tag_encoded end |
Instance Attribute Details
#fully_qualified_name ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def fully_qualified_name @fully_qualified_name end |
#message_class ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def @message_class end |
#name ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def name @name end |
#options ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def @options end |
#rule ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def rule @rule end |
#tag ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def tag @tag end |
#type_class ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def type_class @type_class end |
Class Method Details
.default ⇒ Object
Class Methods
28 29 30 |
# File 'lib/protobuf/field/base_field.rb', line 28 def self.default nil end |
Instance Method Details
#acceptable?(_value) ⇒ Boolean
Public Instance Methods
59 60 61 |
# File 'lib/protobuf/field/base_field.rb', line 59 def acceptable?(_value) true end |
#coerce!(value) ⇒ Object
63 64 65 |
# File 'lib/protobuf/field/base_field.rb', line 63 def coerce!(value) value end |
#decode(_bytes) ⇒ Object
67 68 69 |
# File 'lib/protobuf/field/base_field.rb', line 67 def decode(_bytes) fail NotImplementedError, "#{self.class.name}##{__method__}" end |
#default ⇒ Object
71 72 73 |
# File 'lib/protobuf/field/base_field.rb', line 71 def default [:default] end |
#default_value ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/protobuf/field/base_field.rb', line 75 def default_value @default_value ||= if optional? || required? typed_default_value elsif repeated? ::Protobuf::Field::FieldArray.new(self).freeze else fail "Unknown field label -- something went very wrong" end end |
#deprecated? ⇒ Boolean
85 86 87 |
# File 'lib/protobuf/field/base_field.rb', line 85 def deprecated? .key?(:deprecated) end |
#encode(_value) ⇒ Object
89 90 91 |
# File 'lib/protobuf/field/base_field.rb', line 89 def encode(_value) fail NotImplementedError, "#{self.class.name}##{__method__}" end |
#enum? ⇒ Boolean
97 98 99 |
# File 'lib/protobuf/field/base_field.rb', line 97 def enum? false end |
#extension? ⇒ Boolean
93 94 95 |
# File 'lib/protobuf/field/base_field.rb', line 93 def extension? .key?(:extension) end |
#fully_qualified_name_only! ⇒ Object
164 165 166 |
# File 'lib/protobuf/field/base_field.rb', line 164 def fully_qualified_name_only! @name = @fully_qualified_name end |
#message? ⇒ Boolean
101 102 103 |
# File 'lib/protobuf/field/base_field.rb', line 101 def false end |
#optional? ⇒ Boolean
105 106 107 |
# File 'lib/protobuf/field/base_field.rb', line 105 def optional? rule == :optional end |
#packed? ⇒ Boolean
109 110 111 |
# File 'lib/protobuf/field/base_field.rb', line 109 def packed? repeated? && .key?(:packed) end |
#repeated? ⇒ Boolean
113 114 115 |
# File 'lib/protobuf/field/base_field.rb', line 113 def repeated? rule == :repeated end |
#repeated_message? ⇒ Boolean
117 118 119 |
# File 'lib/protobuf/field/base_field.rb', line 117 def repeated? && end |
#required? ⇒ Boolean
121 122 123 |
# File 'lib/protobuf/field/base_field.rb', line 121 def required? rule == :required end |
#set(message_instance, bytes) ⇒ Object
FIXME: need to cleanup (rename) this warthog of a method.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/protobuf/field/base_field.rb', line 126 def set(, bytes) return [name] = decode(bytes) unless repeated? return [name] << decode(bytes) unless packed? array = [name] stream = StringIO.new(bytes) if wire_type == ::Protobuf::WireType::VARINT array << decode(Varint.decode(stream)) until stream.eof? elsif wire_type == ::Protobuf::WireType::FIXED64 array << decode(stream.read(8)) until stream.eof? elsif wire_type == ::Protobuf::WireType::FIXED32 array << decode(stream.read(4)) until stream.eof? end end |
#tag_encoded ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/protobuf/field/base_field.rb', line 142 def tag_encoded @tag_encoded ||= begin case when repeated? && packed? ::Protobuf::Field::VarintField.encode((tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED) else ::Protobuf::Field::VarintField.encode((tag << 3) | wire_type) end end end |
#to_s ⇒ Object
FIXME: add packed, deprecated, extension options to to_s output
154 155 156 |
# File 'lib/protobuf/field/base_field.rb', line 154 def to_s "#{rule} #{type_class} #{name} = #{tag} #{default ? "[default=#{default.inspect}]" : ''}" end |