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, #default_value, #message_class, #name, #rule, #tag, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

descriptor, #descriptor, #initialize, #initialized?, #max, #merge, #min, #optional?, #packed?, #ready?, #repeated?, #required?, #set, #to_s

Constructor Details

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

Class Method Details

.decode(bytes) ⇒ Object



373
374
375
376
377
378
379
# File 'lib/protobuf/message/field.rb', line 373

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

.defaultObject



369
370
371
# File 'lib/protobuf/message/field.rb', line 369

def default
  0
end

.encode(value) ⇒ Object

Raises:

  • (RangeError)


381
382
383
384
385
386
387
388
389
390
391
# File 'lib/protobuf/message/field.rb', line 381

def encode(value)
  raise RangeError, "#{value} is negative" 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)


406
407
408
409
410
# File 'lib/protobuf/message/field.rb', line 406

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

#decode(value) ⇒ Object



398
399
400
# File 'lib/protobuf/message/field.rb', line 398

def decode(value)
  value
end

#encode(value) ⇒ Object



402
403
404
# File 'lib/protobuf/message/field.rb', line 402

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

#wire_typeObject



394
395
396
# File 'lib/protobuf/message/field.rb', line 394

def wire_type
  WireType::VARINT
end