Sha256: 4fd90aa689c7e2798e7dbc85b89b2db8ff89f0a2ff3fd2f29533e7d4715b827e

Contents?: true

Size: 1.2 KB

Versions: 26

Compression:

Stored size: 1.2 KB

Contents

require 'protobuf/field/bytes_field'

module Protobuf
  module Field
    class StringField < BytesField

      ##
      # Constants
      #

      ENCODING = Encoding::UTF_8

      ##
      # Public Instance Methods
      #

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

      def coerce!(value)
        if value.nil?
          nil
        elsif acceptable?(value)
          value.to_s
        else
          fail TypeError, "Unacceptable value #{value} for field #{name} of type #{type_class}"
        end
      end

      def decode(bytes)
        bytes.force_encoding(::Protobuf::Field::StringField::ENCODING)
        bytes
      end

      def encode(value)
        value_to_encode = "" + value # dup is slower
        unless value_to_encode.encoding == ENCODING
          value_to_encode.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
        end
        value_to_encode.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)

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

      def json_encode(value)
        value
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

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