Sha256: 981ba7b277ea5c561d4b4e63d92666c33c60303e92b0056068672b73ecc40181

Contents?: true

Size: 1.44 KB

Versions: 24

Compression:

Stored size: 1.44 KB

Contents

require 'protobuf/wire_type'

module Protobuf
  module Field
    class BytesField < BaseField

      ##
      # Constants
      #

      BYTES_ENCODING = Encoding::BINARY

      ##
      # Class Methods
      #

      def self.default
        ''
      end

      ##
      # Public Instance Methods
      #

      def acceptable?(val)
        val.is_a?(String) || val.nil? || val.is_a?(Symbol) || val.is_a?(::Protobuf::Message)
      end

      def decode(bytes)
        bytes.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
        bytes
      end

      def encode(value)
        value_to_encode = if value.is_a?(::Protobuf::Message)
                            value.encode
                          else
                            "" + value
                          end

        value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
        "#{::Protobuf::Field::VarintField.encode(value_to_encode.bytesize)}#{value_to_encode}"
      end

      def wire_type
        ::Protobuf::WireType::LENGTH_DELIMITED
      end

      def coerce!(value)
        case value
        when String, Symbol
          value.to_s
        when NilClass
          nil
        when ::Protobuf::Message
          value.dup
        else
          fail TypeError, "Unacceptable value #{value} for field #{name} of type #{type_class}"
        end
      end

      def json_encode(value)
        Base64.strict_encode64(value)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
protobuf-3.10.9 lib/protobuf/field/bytes_field.rb
protobuf-3.10.8 lib/protobuf/field/bytes_field.rb
contrast-agent-6.7.0 lib/protobuf/field/bytes_field.rb
protobuf-3.10.7 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.5 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.4 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.3 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.2 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.1 lib/protobuf/field/bytes_field.rb
contrast-agent-6.6.0 lib/protobuf/field/bytes_field.rb
contrast-agent-6.5.1 lib/protobuf/field/bytes_field.rb
contrast-agent-6.5.0 lib/protobuf/field/bytes_field.rb
contrast-agent-6.4.0 lib/protobuf/field/bytes_field.rb
protobuf-3.10.6 lib/protobuf/field/bytes_field.rb
protobuf-3.10.5 lib/protobuf/field/bytes_field.rb
protobuf-3.10.4 lib/protobuf/field/bytes_field.rb
protobuf-3.10.3 lib/protobuf/field/bytes_field.rb
protobuf-3.10.2 lib/protobuf/field/bytes_field.rb
protobuf-3.10.1 lib/protobuf/field/bytes_field.rb
protobuf-3.10.0 lib/protobuf/field/bytes_field.rb