Class: Protobuf::Field::BaseField
- Inherits:
-
Object
- Object
- Protobuf::Field::BaseField
- Defined in:
- lib/protobuf/message/field.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#message_class ⇒ Object
readonly
Returns the value of attribute message_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#acceptable?(value) ⇒ Boolean
Is a +value+ acceptable for this field?.
-
#decode(bytes) ⇒ Object
Decode +bytes+ and return a field value.
- #descriptor ⇒ Object
-
#encode(value) ⇒ Object
Encode +value+ and return a byte string.
-
#initialize(message_class, rule, type, name, tag, options) ⇒ BaseField
constructor
A new instance of BaseField.
- #initialized?(message_instance) ⇒ Boolean
-
#max ⇒ Object
Upper limit for this field.
-
#merge(message_instance, value) ⇒ Object
Merge +value+ with +message_instance+.
-
#min ⇒ Object
Lower limit for this field.
-
#optional? ⇒ Boolean
Is this a optional field?.
-
#packed? ⇒ Boolean
Is this a packed repeated field?.
- #ready? ⇒ Boolean
-
#repeated? ⇒ Boolean
Is this a repeated field?.
-
#required? ⇒ Boolean
Is this a required field?.
-
#set(message_instance, bytes) ⇒ Object
Decode +bytes+ and pass to +message_instance+.
- #to_s ⇒ Object
Constructor Details
#initialize(message_class, rule, type, name, tag, options) ⇒ BaseField
Returns a new instance of BaseField.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/protobuf/message/field.rb', line 46 def initialize(, rule, type, name, tag, ) @message_class, @rule, @type, @name, @tag = \ , rule, type, name, tag @default = .delete(:default) @extension = .delete(:extension) @packed = repeated? && .delete(:packed) unless .empty? warn "WARNING: Invalid options: #{.inspect} (in #{@message_class.name.split('::').last}.#{@name})" end if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type) raise "Can't use packed encoding for `#{@type}' type" end @default_value = \ case @rule when :repeated then FieldArray.new(self).freeze when :required then nil when :optional then typed_default_value end define_accessor end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def default @default end |
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
40 41 42 |
# File 'lib/protobuf/message/field.rb', line 40 def default_value @default_value end |
#message_class ⇒ Object (readonly)
Returns the value of attribute message_class.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def @message_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def name @name end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def rule @rule end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
39 40 41 |
# File 'lib/protobuf/message/field.rb', line 39 def type @type end |
Class Method Details
.default ⇒ Object
35 36 37 |
# File 'lib/protobuf/message/field.rb', line 35 def self.default nil end |
.descriptor ⇒ Object
31 32 33 |
# File 'lib/protobuf/message/field.rb', line 31 def self.descriptor @descriptor ||= Descriptor::FieldDescriptor.new end |
Instance Method Details
#acceptable?(value) ⇒ Boolean
Is a +value+ acceptable for this field?
163 164 165 |
# File 'lib/protobuf/message/field.rb', line 163 def acceptable?(value) true end |
#decode(bytes) ⇒ Object
Decode +bytes+ and return a field value.
114 115 116 |
# File 'lib/protobuf/message/field.rb', line 114 def decode(bytes) raise NotImplementedError, "#{self.class.name}\#decode" end |
#descriptor ⇒ Object
42 43 44 |
# File 'lib/protobuf/message/field.rb', line 42 def descriptor @descriptor ||= Descriptor::FieldDescriptor.new(self) end |
#encode(value) ⇒ Object
Encode +value+ and return a byte string.
119 120 121 |
# File 'lib/protobuf/message/field.rb', line 119 def encode(value) raise NotImplementedError, "#{self.class.name}\#encode" end |
#initialized?(message_instance) ⇒ Boolean
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/protobuf/message/field.rb', line 77 def initialized?() value = .__send__(@name) case @rule when :required then ! value.nil? && (! kind_of?(MessageField) || value.initialized?) when :repeated then value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? } when :optional then value.nil? || ! kind_of?(MessageField) || value.initialized? end end |
#max ⇒ Object
Upper limit for this field.
153 154 155 |
# File 'lib/protobuf/message/field.rb', line 153 def max self.class.max end |
#merge(message_instance, value) ⇒ Object
Merge +value+ with +message_instance+.
124 125 126 127 128 129 130 |
# File 'lib/protobuf/message/field.rb', line 124 def merge(, value) if repeated? merge_array(, value) else merge_value(, value) end end |
#min ⇒ Object
Lower limit for this field.
158 159 160 |
# File 'lib/protobuf/message/field.rb', line 158 def min self.class.min end |
#optional? ⇒ Boolean
Is this a optional field?
143 144 145 |
# File 'lib/protobuf/message/field.rb', line 143 def optional? @rule == :optional end |
#packed? ⇒ Boolean
Is this a packed repeated field?
148 149 150 |
# File 'lib/protobuf/message/field.rb', line 148 def packed? !!@packed end |
#ready? ⇒ Boolean
73 74 75 |
# File 'lib/protobuf/message/field.rb', line 73 def ready? true end |
#repeated? ⇒ Boolean
Is this a repeated field?
133 134 135 |
# File 'lib/protobuf/message/field.rb', line 133 def repeated? @rule == :repeated end |
#required? ⇒ Boolean
Is this a required field?
138 139 140 |
# File 'lib/protobuf/message/field.rb', line 138 def required? @rule == :required end |
#set(message_instance, bytes) ⇒ Object
Decode +bytes+ and pass to +message_instance+.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/protobuf/message/field.rb', line 90 def set(, bytes) if packed? array = .__send__(@name) method = \ case wire_type when WireType::FIXED32 then :read_fixed32 when WireType::FIXED64 then :read_fixed64 when WireType::VARINT then :read_varint end stream = StringIO.new(bytes) until stream.eof? array << decode(Decoder.__send__(method, stream)) end else value = decode(bytes) if repeated? .__send__(@name) << value else .__send__("#{@name}=", value) end end end |
#to_s ⇒ Object
167 168 169 |
# File 'lib/protobuf/message/field.rb', line 167 def to_s "#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}" end |