Class: Protobug::Field::EnumField

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

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

#binary_decode, #binary_encode, #define_adder, #json_encode, #json_key_encode, #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



610
611
612
613
614
615
# File 'lib/protobug/field.rb', line 610

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.



608
609
610
# File 'lib/protobug/field.rb', line 608

def enum_type
  @enum_type
end

Instance Method Details

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



644
645
646
647
# File 'lib/protobug/field.rb', line 644

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

#binary_encode_one(value, outbuf) ⇒ Object



640
641
642
# File 'lib/protobug/field.rb', line 640

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

#defaultObject



658
659
660
661
662
663
# File 'lib/protobug/field.rb', line 658

def default
  return [] if repeated?

  # TODO: enum_type.default
  0
end

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



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/protobug/field.rb', line 617

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



649
650
651
652
# File 'lib/protobug/field.rb', line 649

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



654
655
656
# File 'lib/protobug/field.rb', line 654

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

#validate!(value, message) ⇒ Object



665
666
667
668
# File 'lib/protobug/field.rb', line 665

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