Sha256: da5216a8b905a9c383100fea0c4d94655eb33e620bcbac7b1de6d8b616e4d8b8

Contents?: true

Size: 925 Bytes

Versions: 7

Compression:

Stored size: 925 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
        else
          raise TypeError, "Cannot set field : #{name} to value : #{val}"
        end
      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.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

7 entries across 7 versions & 1 rubygems

Version Path
protobuf-2.4.3.rc1 lib/protobuf/field/bytes_field.rb
protobuf-2.4.2-java lib/protobuf/field/bytes_field.rb
protobuf-2.4.2 lib/protobuf/field/bytes_field.rb
protobuf-2.4.1-java lib/protobuf/field/bytes_field.rb
protobuf-2.4.1 lib/protobuf/field/bytes_field.rb
protobuf-2.4.0-java lib/protobuf/field/bytes_field.rb
protobuf-2.4.0 lib/protobuf/field/bytes_field.rb