Class: ProtocolBuffers::Field::StringField

Inherits:
BytesField show all
Defined in:
lib/protocol_buffers/runtime/field.rb

Constant Summary collapse

HAS_ENCODING =
(''.respond_to?(:valid_encoding?) && ''.respond_to?(:force_encoding))

Instance Attribute Summary

Attributes inherited from ProtocolBuffers::Field

#name, #otype, #tag

Instance Method Summary collapse

Methods inherited from BytesField

#default_value, #valid_type?

Methods included from WireFormats::LENGTH_DELIMITED

#wire_type

Methods inherited from ProtocolBuffers::Field

#add_methods_to, #add_reader_to, #add_writer_to, #check_valid, create, #initialize, #inspect_value, #packed?, #repeated?, #valid_type?

Constructor Details

This class inherits a constructor from ProtocolBuffers::Field

Instance Method Details

#check_value(value) ⇒ Object



306
307
308
309
310
# File 'lib/protocol_buffers/runtime/field.rb', line 306

def check_value(value)
  if HAS_ENCODING
    value.dup.force_encoding(Encoding::UTF_8).valid_encoding? || raise(ArgumentError, "string value is not valid utf-8")
  end
end

#deserialize(value) ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/protocol_buffers/runtime/field.rb', line 321

def deserialize(value)
  read_value = value.read.to_s
  if HAS_ENCODING
    read_value.force_encoding(Encoding::UTF_8)
  else
    read_value
  end
end

#serialize(value) ⇒ Object



312
313
314
315
316
317
318
319
# File 'lib/protocol_buffers/runtime/field.rb', line 312

def serialize(value)
  check_value(value)
  if HAS_ENCODING
    value.dup.force_encoding(Encoding::UTF_8)
  else
    value
  end
end