Class: Protobug::Field::DoubleField

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

Direct Known Subclasses

FloatField

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, #json_decode, #json_encode, #json_key_encode, #optional?, #packed?, #pretty_print, #proto3_optional?, #repeated?, #to_text, #validate!

Constructor Details

#initialize(number, name, cardinality:, json_name: name, oneof: nil, packed: false, proto3_optional: cardinality == :optional) ⇒ DoubleField

Returns a new instance of DoubleField.



676
677
678
679
680
# File 'lib/protobug/field.rb', line 676

def initialize(number, name, cardinality:, json_name: name, oneof: nil, packed: false,
               proto3_optional: cardinality == :optional)
  super(number, name, json_name: json_name, cardinality: cardinality, oneof: oneof,
                      proto3_optional: proto3_optional, packed: packed)
end

Instance Method Details

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



686
687
688
689
# File 'lib/protobug/field.rb', line 686

def binary_decode_one(io, _message, _registry, wire_type)
  value = BinaryEncoding.read_field_value(io, wire_type)
  value.unpack1(binary_pack)
end

#binary_encode_one(value, outbuf) ⇒ Object



682
683
684
# File 'lib/protobug/field.rb', line 682

def binary_encode_one(value, outbuf)
  [value].pack(binary_pack, buffer: outbuf)
end

#binary_packObject



673
# File 'lib/protobug/field.rb', line 673

def binary_pack = "E"

#defaultObject



726
727
728
729
730
# File 'lib/protobug/field.rb', line 726

def default
  return [] if repeated?

  0.0
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/protobug/field.rb', line 691

def json_decode_one(value, _ignore_unknown_fields, _registry)
  case value
  when Float
    value
  when Integer
    value.to_f
  when "Infinity"
    Float::INFINITY
  when "-Infinity"
    -Float::INFINITY
  when "NaN"
    Float::NAN
  when /\A-?\d+\z/
    Float(value)
  when NilClass
    UNSET
  else
    raise DecodeError, "expected float for #{inspect}, got #{value.inspect}"
  end
end

#json_encode_one(value, print_unknown_fields:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/protobug/field.rb', line 712

def json_encode_one(value, print_unknown_fields:) # rubocop:disable Lint/UnusedMethodArgument
  if value.nan?
    "NaN"
  elsif (sign = value.infinite?)
    if sign == -1
      "-Infinity"
    else
      "Infinity"
    end
  else
    value
  end
end

#typeObject



672
# File 'lib/protobug/field.rb', line 672

def type = :double

#wire_typeObject



674
# File 'lib/protobug/field.rb', line 674

def wire_type = 1