Class: Protobug::Field::DoubleField

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

Direct Known Subclasses

FloatField

Constant Summary

Constants inherited from Protobug::Field

PACKABLE_WIRE_TYPES

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

#adder_method_definition, #binary_decode, #binary_encode, #json_decode, #json_encode, #json_key_encode, #method_definitions, #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.



860
861
862
863
864
# File 'lib/protobug/field.rb', line 860

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



870
871
872
873
# File 'lib/protobug/field.rb', line 870

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



866
867
868
# File 'lib/protobug/field.rb', line 866

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

#binary_packObject



852
853
854
# File 'lib/protobug/field.rb', line 852

def binary_pack
  "E"
end

#defaultObject



910
911
912
913
914
# File 'lib/protobug/field.rb', line 910

def default
  return [] if repeated?

  0.0
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/protobug/field.rb', line 875

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+(?:\.\d+)?|\.\d+)(?:[eE][-+]?\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



896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'lib/protobug/field.rb', line 896

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



848
849
850
# File 'lib/protobug/field.rb', line 848

def type
  :double
end

#wire_typeObject



856
857
858
# File 'lib/protobug/field.rb', line 856

def wire_type
  1
end