Class: Protobuf::Field::StringField

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

Constant Summary collapse

ENCODING =

Constants

'UTF-8'.freeze

Constants inherited from BytesField

BytesField::BYTES_ENCODING

Constants inherited from BaseField

BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#default, #default_value, #deprecated, #extension, #getter_method_name, #message_class, #name, #optional, #packed, #repeated, #required, #rule, #setter_method_name, #tag, #type_class

Instance Method Summary collapse

Methods inherited from BytesField

#acceptable?, default, #wire_type

Methods inherited from BaseField

#acceptable?, default, #deprecated?, #enum?, #extension?, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #to_s, #type, #warn_if_deprecated

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
30
# 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)

  string_size = ::Protobuf::Field::VarintField.encode(value_to_encode.size)
  string_size << value_to_encode
end