Sha256: 57022e21aabed0ff27c8e5524f76a0e950084ce7e85c36f3d157758c6be5af2c

Contents?: true

Size: 939 Bytes

Versions: 4

Compression:

Stored size: 939 Bytes

Contents

module Protobuf
  module Field
    class BytesField < BaseField
      def self.default
        ''
      end

      def wire_type
        WireType::LENGTH_DELIMITED
      end

      def acceptable?(val)
        if val.nil? || val.is_a?(::Protobuf::Message) || val.instance_of?(String)
          return true
        end

        raise TypeError, "Cannot set field : #{name} to value : #{val}"
      end

      def decode(bytes)
        bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
        bytes
      end

      def encode(value)
        if value.is_a?(::Protobuf::Message)
          value = value.serialize_to_string
        else
          value = value.dup
          value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
        end

        string_size = VarintField.encode(value.size)
        string_size << value
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protobuf-2.3.2-java lib/protobuf/field/bytes_field.rb
protobuf-2.3.2 lib/protobuf/field/bytes_field.rb
protobuf-2.3.1-java lib/protobuf/field/bytes_field.rb
protobuf-2.3.1 lib/protobuf/field/bytes_field.rb