Class: Protobug::Field::EnumField

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

Constant Summary

Constants inherited from Protobug::Field

PACKABLE_WIRE_TYPES

Instance Attribute Summary collapse

Attributes inherited from Protobug::Field

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

Instance Method Summary collapse

Methods inherited from Int32Field

#bit_length, #encoding, #signed, #wire_type

Methods inherited from Protobug::Field

#adder_method_definition, #binary_decode, #binary_encode, #json_encode, #json_key_encode, #method_definitions, #optional?, #packed?, #pretty_print, #proto3_optional?, #repeated?, #to_text

Constructor Details

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

Returns a new instance of EnumField.



786
787
788
789
790
791
# File 'lib/protobug/field.rb', line 786

def initialize(number, name, cardinality:, enum_type:, 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)
  @enum_type = enum_type
end

Instance Attribute Details

#enum_typeObject (readonly)

Returns the value of attribute enum_type.



784
785
786
# File 'lib/protobug/field.rb', line 784

def enum_type
  @enum_type
end

Instance Method Details

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



820
821
822
823
# File 'lib/protobug/field.rb', line 820

def binary_decode_one(io, _message, registry, wire_type)
  value = super
  registry.fetch(enum_type).decode(value)
end

#binary_encode_one(value, outbuf) ⇒ Object



816
817
818
# File 'lib/protobug/field.rb', line 816

def binary_encode_one(value, outbuf)
  super(value.value, outbuf)
end

#defaultObject



834
835
836
837
838
839
# File 'lib/protobug/field.rb', line 834

def default
  return [] if repeated?

  # TODO: enum_type.default
  0
end

#json_decode(value, message, ignore_unknown_fields, registry) ⇒ Object



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/protobug/field.rb', line 793

def json_decode(value, message, ignore_unknown_fields, registry)
  return super unless ignore_unknown_fields

  if repeated?
    return if value.nil?

    unless value.is_a?(Array)
      raise DecodeError,
            "expected Array for #{inspect}, got #{value.inspect}"
    end

    value.map do |v|
      v = json_decode_one(v, ignore_unknown_fields, registry)
      next if UNSET == v

      message.send(adder, v)
    end.tap(&:compact!)
  else
    value = json_decode_one(value, ignore_unknown_fields, registry)
    message.send(setter, value) unless UNSET == value
  end
end

#json_decode_one(value, ignore_unknown_fields, registry) ⇒ Object



825
826
827
828
# File 'lib/protobug/field.rb', line 825

def json_decode_one(value, ignore_unknown_fields, registry)
  klass = registry.fetch(enum_type)
  klass.decode_json_hash(value, registry: registry, ignore_unknown_fields: ignore_unknown_fields)
end

#json_encode_one(value, print_unknown_fields:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



830
831
832
# File 'lib/protobug/field.rb', line 830

def json_encode_one(value, print_unknown_fields:) # rubocop:disable Lint/UnusedMethodArgument
  value.as_json
end

#validate!(value, message) ⇒ Object



841
842
843
844
# File 'lib/protobug/field.rb', line 841

def validate!(value, message)
  value = value.value if value.is_a?(Enum::InstanceMethods)
  super
end