Class: Protobuf::Field::BytesField

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

Direct Known Subclasses

StringField

Constant Summary collapse

BYTES_ENCODING =

Constants

Encoding::BINARY

Constants inherited from BaseField

Protobuf::Field::BaseField::OBJECT_MODULE, Protobuf::Field::BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#default, #define_encode_to_stream!, #define_field_and_present_predicate!, #define_field_predicate!, #define_set_field!, #define_set_method!, #define_to_message_hash!, #define_value_from_values!, #define_value_from_values_for_serialization!, #deprecated?, #encode_to_stream, #enum?, #extension?, #field?, #field_and_present?, #fully_qualified_name_only!, #initialize, #map?, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #set_default_value!, #set_field, #set_map!, #set_repeated_message!, #tag_encoded, #to_message_hash, #to_message_hash_with_string_key, #to_s, #value_from_values, #value_from_values_for_serialization

Methods included from Logging

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

Constructor Details

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

Class Method Details

.defaultObject

Class Methods



17
18
19
# File 'lib/protobuf/field/bytes_field.rb', line 17

def self.default
  ''
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


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

def acceptable?(val)
  val.is_a?(String) || val.nil? || val.is_a?(Symbol) || val.is_a?(::Protobuf::Message)
end

#coerce!(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/protobuf/field/bytes_field.rb', line 49

def coerce!(value)
  case value
  when String
    if value.encoding == Encoding::ASCII_8BIT
      # This is a "binary" string
      value
    else
      # Assume the value is Base64 encoded (from JSON)
      # Ideally we'd do the Base64 decoding while processing the JSON,
      # but this is tricky to do since we don't know the protobuf field
      # types when we do that.
      Base64.decode64(value)
    end
  when Symbol
    value.to_s
  when NilClass
    nil
  when ::Protobuf::Message
    value.dup
  else
    fail TypeError, "Unacceptable value #{value} for field #{name} of type #{type_class}"
  end
end

#decode(bytes) ⇒ Object



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

def decode(bytes)
  bytes.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
  bytes
end

#encode(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/protobuf/field/bytes_field.rb', line 34

def encode(value)
  value_to_encode = if value.is_a?(::Protobuf::Message)
                      value.encode
                    else
                      "" + value
                    end

  value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
  "#{::Protobuf::Field::VarintField.encode(value_to_encode.bytesize)}#{value_to_encode}"
end

#json_encode(value, options = {}) ⇒ Object



73
74
75
# File 'lib/protobuf/field/bytes_field.rb', line 73

def json_encode(value, options={})
  Base64.strict_encode64(value)
end

#wire_typeObject



45
46
47
# File 'lib/protobuf/field/bytes_field.rb', line 45

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