Class: Protobuf::Field::VarintField

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

Constant Summary collapse

INT32_MAX =

Constants

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

Constants inherited from BaseField

BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#message_class, #name, #options, #rule, #tag, #type_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#coerce!, #default, #default_value, #deprecated?, #enum?, #extension?, #getter, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #setter, #to_s, #type, #warn_if_deprecated

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, logger, logger=, #sign_message

Constructor Details

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

Class Method Details

.defaultObject

Class Methods



22
23
24
# File 'lib/protobuf/field/varint_field.rb', line 22

def self.default
  0
end

.encode(value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/protobuf/field/varint_field.rb', line 26

def self.encode(value)
  bytes = []
  until value < 128
    bytes << (0x80 | (value & 0x7f))
    value >>= 7
  end
  (bytes << value).pack('C*')
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/protobuf/field/varint_field.rb', line 39

def acceptable?(val)
  (val > self.class.min || val < self.class.max)
rescue
  false
end

#decode(value) ⇒ Object



45
46
47
# File 'lib/protobuf/field/varint_field.rb', line 45

def decode(value)
  value
end

#encode(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/protobuf/field/varint_field.rb', line 49

def encode(value)
  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

#wire_typeObject



61
62
63
# File 'lib/protobuf/field/varint_field.rb', line 61

def wire_type
  ::Protobuf::WireType::VARINT
end