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::OBJECT_MODULE, BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#default, #define_encode_to_stream!, #define_field_and_present_predicate!, #define_field_predicate!, #define_set_field!, #define_set_method!, #define_to_message_hash!, #define_value_from_values!, #define_value_from_values_for_serialization!, #deprecated?, #encode_to_stream, #enum?, #extension?, #field?, #field_and_present?, #fully_qualified_name_only!, #initialize, #map?, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #set_default_value!, #set_field, #set_map!, #set_repeated_message!, #tag_encoded, #to_message_hash, #to_message_hash_with_string_key, #to_s, #value_from_values, #value_from_values_for_serialization

Methods included from Logging

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

Constructor Details

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

Class Method Details

.defaultObject

Class Methods



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

def self.default
  0
end

.encode(value) ⇒ Object



25
26
27
# File 'lib/protobuf/field/varint_field.rb', line 25

def self.encode(value)
  ::Protobuf::Varint.encode(value)
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/protobuf/field/varint_field.rb', line 32

def acceptable?(val)
  int_val = if val.is_a?(Integer)
              return true if val >= 0 && val < INT32_MAX # return quickly for smallest integer size, hot code path
              val
            elsif val.is_a?(Numeric)
              val.to_i
            else
              Integer(val, 10)
            end

  int_val >= self.class.min && int_val <= self.class.max
rescue
  false
end

#coerce!(val) ⇒ Object



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

def coerce!(val)
  if val.is_a?(Integer) && val >= 0 && val <= INT32_MAX
    val
  else
    fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'" unless acceptable?(val)

    if val.is_a?(Integer) || val.is_a?(Numeric)
      val.to_i
    else
      Integer(val, 10)
    end
  end
rescue ArgumentError
  fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
end

#decode(value) ⇒ Object



63
64
65
# File 'lib/protobuf/field/varint_field.rb', line 63

def decode(value)
  value
end

#encode(value) ⇒ Object



67
68
69
# File 'lib/protobuf/field/varint_field.rb', line 67

def encode(value)
  ::Protobuf::Field::VarintField.encode(value)
end

#wire_typeObject



71
72
73
# File 'lib/protobuf/field/varint_field.rb', line 71

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