Class: Protobug::Field::EnumField
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#binary_decode_one(io, _message, registry, wire_type) ⇒ Object
-
#binary_encode_one(value, outbuf) ⇒ Object
-
#default ⇒ Object
-
#initialize(number, name, cardinality:, enum_type:, json_name: name, oneof: nil, packed: false, proto3_optional: cardinality == :optional) ⇒ EnumField
constructor
A new instance of EnumField.
-
#json_decode(value, message, ignore_unknown_fields, registry) ⇒ Object
-
#json_decode_one(value, ignore_unknown_fields, registry) ⇒ Object
-
#json_encode_one(value, print_unknown_fields:) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#validate!(value, message) ⇒ Object
Methods inherited from Int32Field
#bit_length, #encoding, #signed, #wire_type
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_type ⇒ Object
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
|
#default ⇒ Object
834
835
836
837
838
839
|
# File 'lib/protobug/field.rb', line 834
def default
return [] if repeated?
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:) 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
|