Class: Protobuf::Field::MessageField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/protobuf/field/message_field.rb

Constant Summary

Constants inherited from BaseField

BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#fully_qualified_name, #message_class, #name, #options, #rule, #tag, #type_class

Instance Method Summary collapse

Methods inherited from BaseField

default, #default, #default_value, #deprecated?, #enum?, #extension?, #fully_qualified_name_only!, #initialize, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #tag_encoded, #to_s

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, #sign_message

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


11
12
13
# File 'lib/protobuf/field/message_field.rb', line 11

def acceptable?(val)
  val.is_a?(type_class) || val.respond_to?(:to_hash) || val.respond_to?(:to_proto)
end

#coerce!(value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/protobuf/field/message_field.rb', line 33

def coerce!(value)
  return nil if value.nil?

  coerced_value = if value.respond_to?(:to_proto)
                    value.to_proto
                  elsif value.respond_to?(:to_hash)
                    type_class.new(value.to_hash)
                  else
                    value
                  end

  return coerced_value if coerced_value.is_a?(type_class)

  fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{value.class}'"
end

#decode(bytes) ⇒ Object



15
16
17
# File 'lib/protobuf/field/message_field.rb', line 15

def decode(bytes)
  type_class.decode(bytes)
end

#encode(value) ⇒ Object



19
20
21
22
23
# File 'lib/protobuf/field/message_field.rb', line 19

def encode(value)
  bytes = value.encode
  result = ::Protobuf::Field::VarintField.encode(bytes.size)
  result << bytes
end

#message?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/protobuf/field/message_field.rb', line 25

def message?
  true
end

#wire_typeObject



29
30
31
# File 'lib/protobuf/field/message_field.rb', line 29

def wire_type
  ::Protobuf::WireType::LENGTH_DELIMITED
end