Class: Protobuf::Field::StringField

Inherits:
BytesField show all
Defined in:
lib/protobuf/field/string_field.rb

Constant Summary collapse

ENCODING =

Constants

Encoding::UTF_8

Constants inherited from BytesField

BytesField::BYTES_ENCODING

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 BytesField

#acceptable?, #coerce!, default, #wire_type

Methods inherited from BaseField

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

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

#decode(bytes) ⇒ Object

Public Instance Methods



17
18
19
20
21
# File 'lib/protobuf/field/string_field.rb', line 17

def decode(bytes)
  bytes_to_decode = bytes.dup
  bytes_to_decode.force_encoding(::Protobuf::Field::StringField::ENCODING)
  bytes_to_decode
end

#encode(value) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/protobuf/field/string_field.rb', line 23

def encode(value)
  value_to_encode = value.dup
  value_to_encode.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
  value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)

  "#{::Protobuf::Field::VarintField.encode(value_to_encode.size)}#{value_to_encode}"
end