Class: Protobug::Field::BoolField

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

Instance Method Summary collapse

Methods inherited from UInt64Field

#bit_length, #encoding, #signed, #wire_type

Methods inherited from IntegerField

#json_encode_one

Instance Method Details

#binary_decode_one ⇒ Object



747
748
749
# File 'lib/protobug/field.rb', line 747

def binary_decode_one(*)
  super != 0
end

#binary_encode_one(value, outbuf) ⇒ Object



751
752
753
# File 'lib/protobug/field.rb', line 751

def binary_encode_one(value, outbuf)
  super(value ? 1 : 0, outbuf)
end

#default ⇒ Object



776
777
778
779
780
# File 'lib/protobug/field.rb', line 776

def default
  return [] if repeated?

  false
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/protobug/field.rb', line 755

def json_decode_one(value, _ignore_unknown_fields, _registry)
  case value
  when TrueClass, FalseClass
    value
  when "true"
    true
  when "false"
    false
  when NilClass
    UNSET
  else
    raise DecodeError, "expected boolean, got #{value.inspect}"
  end
end

#validate!(value, message) ⇒ Object

Raises:



770
771
772
773
774
# File 'lib/protobug/field.rb', line 770

def validate!(value, message)
  raise InvalidValueError.new(message, self, value, "expected boolean") unless [true, false].include?(value)

  super(value ? 1 : 0, message)
end