Class: Protobug::Field::IntegerField

Inherits:
Protobug::Field show all
Defined in:
lib/protobug/field.rb

Instance Attribute Summary

Attributes inherited from Protobug::Field

#adder, #cardinality, #clearer, #haser, #ivar, #json_name, #name, #number, #oneof, #setter

Instance Method Summary collapse

Methods inherited from Protobug::Field

#binary_decode, #binary_encode, #define_adder, #initialize, #json_decode, #json_encode, #json_key_encode, #optional?, #packed?, #pretty_print, #proto3_optional?, #repeated?, #to_text

Constructor Details

This class inherits a constructor from Protobug::Field

Instance Method Details

#binary_decode_one(io, _message, _registry, wire_type) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/protobug/field.rb', line 409

def binary_decode_one(io, _message, _registry, wire_type)
  value = BinaryEncoding.read_field_value(io, wire_type)
  case encoding
  when :zigzag
    BinaryEncoding.decode_zigzag bit_length, value
  when :varint
    length_mask = (2**bit_length) - 1
    negative = signed && value & (2**bit_length.pred) != 0
    # warn negative
    length_mask >> 1 if signed
    if negative
      value &= length_mask # remove sign bit

      # 2's complement
      value ^= length_mask
      value += 1
      # value &= length_mask
      -value
    else
      value & length_mask
    end
  when :fixed
    value.unpack1(binary_pack)
  end
end

#binary_encode_one(value, outbuf) ⇒ Object



435
436
437
438
439
440
441
442
443
444
# File 'lib/protobug/field.rb', line 435

def binary_encode_one(value, outbuf)
  case encoding
  when :zigzag
    BinaryEncoding.encode_zigzag bit_length, value, outbuf
  when :varint
    BinaryEncoding.encode_varint value, outbuf
  when :fixed
    [value].pack(binary_pack, buffer: outbuf)
  end
end

#defaultObject



403
404
405
406
407
# File 'lib/protobug/field.rb', line 403

def default
  return [] if repeated?

  0
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object

Raises:



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/protobug/field.rb', line 446

def json_decode_one(value, _ignore_unknown_fields, _registry)
  return UNSET if value.nil?

  case value
  when Integer
    # nothing
  when /\A-?\d+\z/
    value = Integer(value)
  when Float
    value, remainder = value.divmod(1)
    raise DecodeError, "expected integer for #{inspect}, got #{value.inspect}" unless remainder.zero?
  else
    raise DecodeError, "expected integer for #{inspect}, got #{value.inspect}"
  end
  raise DecodeError, "#{value.inspect} does not fit in 64 bits" if value && value.bit_length > 64

  value
end

#json_encode_one(value, print_unknown_fields:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



465
466
467
468
469
470
471
# File 'lib/protobug/field.rb', line 465

def json_encode_one(value, print_unknown_fields:) # rubocop:disable Lint/UnusedMethodArgument
  if bit_length >= 64
    value.to_s
  else
    value
  end
end

#validate!(value, message) ⇒ Object

Raises:



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/protobug/field.rb', line 473

def validate!(value, message)
  raise InvalidValueError.new(message, self, value, "expected integer") unless value.is_a?(Integer)

  if signed
    min = -2**(bit_length - 1)
    max = 2**(bit_length - 1)
  else
    min = 0
    max = 2**bit_length
  end

  if value < min || value >= max
    raise InvalidValueError.new(message, self, value, "does not fit into [#{min}, #{max})")
  end

  super
end