Class: Protobug::Field::IntegerField
- Inherits:
-
Protobug::Field
- Object
- Protobug::Field
- Protobug::Field::IntegerField
- Defined in:
- lib/protobug/field.rb
Direct Known Subclasses
Fixed32Field, Fixed64Field, Int32Field, Int64Field, SFixed32Field, SFixed64Field, SInt32Field, SInt64Field, UInt32Field, UInt64Field
Constant Summary
Constants inherited from Protobug::Field
Instance Attribute Summary
Attributes inherited from Protobug::Field
#adder, #cardinality, #clearer, #haser, #ivar, #json_name, #name, #number, #oneof, #setter
Instance Method Summary collapse
- #binary_decode_one(io, _message, _registry, wire_type) ⇒ Object
- #binary_encode_one(value, outbuf) ⇒ Object
- #default ⇒ Object
- #json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object
-
#json_encode_one(value, print_unknown_fields:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #validate!(value, message) ⇒ Object
Methods inherited from Protobug::Field
#adder_method_definition, #binary_decode, #binary_encode, #initialize, #json_decode, #json_encode, #json_key_encode, #method_definitions, #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
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/protobug/field.rb', line 464 def binary_decode_one(io, , _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 if negative value &= length_mask # remove sign bit # 2's complement value ^= length_mask value += 1 -value else value & length_mask end when :fixed value.unpack1(binary_pack) end end |
#binary_encode_one(value, outbuf) ⇒ Object
487 488 489 490 491 492 493 494 495 496 |
# File 'lib/protobug/field.rb', line 487 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 BinaryEncoding.pack([value], binary_pack, buffer: outbuf) end end |
#default ⇒ Object
458 459 460 461 462 |
# File 'lib/protobug/field.rb', line 458 def default return [] if repeated? 0 end |
#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/protobug/field.rb', line 498 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 int, remainder = value.divmod(1) raise DecodeError, "expected integer for #{inspect}, got #{value.inspect}" unless remainder == 0 value = int 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
519 520 521 522 523 524 525 |
# File 'lib/protobug/field.rb', line 519 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
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/protobug/field.rb', line 527 def validate!(value, ) raise InvalidValueError.new(, 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(, self, value, "does not fit into [#{min}, #{max})") end super end |