Class: Protobuf::Field::BaseField
- Inherits:
-
Object
- Object
- Protobuf::Field::BaseField
- 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
-
#default ⇒ Object
readonly
Attributes.
-
#default_value ⇒ Object
readonly
Attributes.
-
#deprecated ⇒ Object
readonly
Attributes.
-
#extension ⇒ Object
readonly
Attributes.
-
#getter_method_name ⇒ Object
readonly
Attributes.
-
#message_class ⇒ Object
readonly
Attributes.
-
#name ⇒ Object
readonly
Attributes.
-
#optional ⇒ Object
readonly
Attributes.
-
#packed ⇒ Object
readonly
Attributes.
-
#repeated ⇒ Object
readonly
Attributes.
-
#required ⇒ Object
readonly
Attributes.
-
#rule ⇒ Object
readonly
Attributes.
-
#setter_method_name ⇒ 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.
-
#decode(bytes) ⇒ Object
Decode +bytes+ and return a field value.
-
#deprecated? ⇒ Boolean
Is this a deprecated field?.
-
#encode(value) ⇒ Object
Encode +value+ and return a byte string.
- #enum? ⇒ Boolean
- #extension? ⇒ Boolean
-
#initialize(message_class, rule, type_class, name, tag, options) ⇒ BaseField
constructor
Constructor.
- #message? ⇒ Boolean
-
#optional? ⇒ Boolean
Is this a optional field?.
-
#packed? ⇒ Boolean
Is this a packed repeated field?.
-
#repeated? ⇒ Boolean
Is this a repeated field?.
-
#repeated_message? ⇒ Boolean
Is this a repeated message field?.
-
#required? ⇒ Boolean
Is this a required field?.
-
#set(message_instance, bytes) ⇒ Object
Decode +bytes+ and pass to +message_instance+.
- #to_s ⇒ Object
- #type ⇒ Object
- #warn_if_deprecated ⇒ Object
Constructor Details
#initialize(message_class, rule, type_class, name, tag, options) ⇒ BaseField
Constructor
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/protobuf/field/base_field.rb', line 39 def initialize(, rule, type_class, name, tag, ) @message_class, @rule, @type_class, @name, @tag = \ , rule, type_class, name, tag set_rule_predicates @getter_method_name = name @setter_method_name = "#{name}=" @default = .delete(:default) @extension = .delete(:extension) @packed = repeated? && .delete(:packed) @deprecated = .delete(:deprecated) set_default_value () unless .empty? validate_packed_field if packed? define_accessor end |
Instance Attribute Details
#default ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def default @default end |
#default_value ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def default_value @default_value end |
#deprecated ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def deprecated @deprecated end |
#extension ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def extension @extension end |
#getter_method_name ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def getter_method_name @getter_method_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 |
#optional ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def optional @optional end |
#packed ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def packed @packed end |
#repeated ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def repeated @repeated end |
#required ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def required @required end |
#rule ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def rule @rule end |
#setter_method_name ⇒ Object (readonly)
Attributes
22 23 24 |
# File 'lib/protobuf/field/base_field.rb', line 22 def setter_method_name @setter_method_name 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
31 32 33 |
# File 'lib/protobuf/field/base_field.rb', line 31 def self.default nil end |
Instance Method Details
#acceptable?(value) ⇒ Boolean
Public Instance Methods
62 63 64 |
# File 'lib/protobuf/field/base_field.rb', line 62 def acceptable?(value) true end |
#decode(bytes) ⇒ Object
Decode +bytes+ and return a field value.
100 101 102 |
# File 'lib/protobuf/field/base_field.rb', line 100 def decode(bytes) raise NotImplementedError, "#{self.class.name}\#decode" end |
#deprecated? ⇒ Boolean
Is this a deprecated field?
134 135 136 |
# File 'lib/protobuf/field/base_field.rb', line 134 def deprecated? !! deprecated end |
#encode(value) ⇒ Object
Encode +value+ and return a byte string.
105 106 107 |
# File 'lib/protobuf/field/base_field.rb', line 105 def encode(value) raise NotImplementedError, "#{self.class.name}\#encode" end |
#enum? ⇒ Boolean
66 67 68 |
# File 'lib/protobuf/field/base_field.rb', line 66 def enum? false end |
#extension? ⇒ Boolean
109 110 111 |
# File 'lib/protobuf/field/base_field.rb', line 109 def extension? !! extension end |
#message? ⇒ Boolean
70 71 72 |
# File 'lib/protobuf/field/base_field.rb', line 70 def false end |
#optional? ⇒ Boolean
Is this a optional field?
129 130 131 |
# File 'lib/protobuf/field/base_field.rb', line 129 def optional? !! optional end |
#packed? ⇒ Boolean
Is this a packed repeated field?
139 140 141 |
# File 'lib/protobuf/field/base_field.rb', line 139 def packed? !! packed end |
#repeated? ⇒ Boolean
Is this a repeated field?
114 115 116 |
# File 'lib/protobuf/field/base_field.rb', line 114 def repeated? !! repeated end |
#repeated_message? ⇒ Boolean
Is this a repeated message field?
119 120 121 |
# File 'lib/protobuf/field/base_field.rb', line 119 def repeated? && end |
#required? ⇒ Boolean
Is this a required field?
124 125 126 |
# File 'lib/protobuf/field/base_field.rb', line 124 def required? !! required end |
#set(message_instance, bytes) ⇒ Object
Decode +bytes+ and pass to +message_instance+.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/protobuf/field/base_field.rb', line 75 def set(, bytes) if packed? array = .__send__(getter_method_name) method = \ case wire_type when ::Protobuf::WireType::FIXED32 then :read_fixed32 when ::Protobuf::WireType::FIXED64 then :read_fixed64 when ::Protobuf::WireType::VARINT then :read_varint end stream = StringIO.new(bytes) until stream.eof? array << decode(::Protobuf::Decoder.__send__(method, stream)) end else value = decode(bytes) if repeated? .__send__(getter_method_name) << value else .__send__(setter_method_name, value) end end end |
#to_s ⇒ Object
143 144 145 |
# File 'lib/protobuf/field/base_field.rb', line 143 def to_s "#{rule} #{type_class} #{name} = #{tag} #{default ? "[default=#{default.inspect}]" : ''}" end |
#type ⇒ Object
147 148 149 150 |
# File 'lib/protobuf/field/base_field.rb', line 147 def type $stderr.puts("[DEPRECATED] #{self.class.name}#type usage is deprecated.\nPlease use #type_class instead.") type_class end |
#warn_if_deprecated ⇒ Object
152 153 154 155 156 |
# File 'lib/protobuf/field/base_field.rb', line 152 def warn_if_deprecated if ::Protobuf.print_deprecation_warnings? && deprecated? $stderr.puts("[WARNING] #{.name}##{name} field usage is deprecated.") end end |