Class: Protobuf::Field::VarintField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/protobuf/message/field.rb

Constant Summary collapse

INT32_MAX =
2**31 - 1
INT32_MIN =
-2**31
INT64_MAX =
2**63 - 1
INT64_MIN =
-2**63
UINT32_MAX =
2**32 - 1
UINT64_MAX =
2**64 - 1

Instance Attribute Summary

Attributes inherited from BaseField

#default, #message_class, #name, #rule, #tag, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#clear, #default_value, #descriptor, descriptor, #error_message, #get, #initialize, #initialized?, #max, #merge, #merge_array, #merge_value, #min, #optional?, #ready?, #repeated?, #required?, #set, #to_s, #typed_default_value

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Class Method Details

.decode(bytes) ⇒ Object



322
323
324
325
326
327
328
# File 'lib/protobuf/message/field.rb', line 322

def decode(bytes)
  value = 0
  bytes.each_with_index do |byte, index|
    value |= byte << (7 * index)
  end
  value
end

.defaultObject



320
# File 'lib/protobuf/message/field.rb', line 320

def default; 0 end

.encode(value) ⇒ Object

Raises:

  • (RangeError)


330
331
332
333
334
335
336
337
338
339
340
# File 'lib/protobuf/message/field.rb', line 330

def encode(value)
  raise RangeError.new(value) if value < 0
  return [value].pack('C') if value < 128
  bytes = []
  until value == 0
    bytes << (0x80 | (value & 0x7f))
    value >>= 7
  end
  bytes[-1] &= 0x7f
  bytes.pack('C*')
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (TypeError)


355
356
357
358
359
# File 'lib/protobuf/message/field.rb', line 355

def acceptable?(val)
  raise TypeError.new(val.class.name) unless val.is_a? Integer
  raise RangeError.new(val) if val < min or max < val
  true
end

#decode(bytes) ⇒ Object



347
348
349
# File 'lib/protobuf/message/field.rb', line 347

def decode(bytes)
  self.class.decode(bytes)
end

#encode(value) ⇒ Object



351
352
353
# File 'lib/protobuf/message/field.rb', line 351

def encode(value)
  self.class.encode(value)
end

#wire_typeObject



343
344
345
# File 'lib/protobuf/message/field.rb', line 343

def wire_type
  Protobuf::WireType::VARINT
end