Sha256: 1222debd23f2c9d26add652e9ee2630f5702bfaf3fc18a25ea692fae3e2c602b

Contents?: true

Size: 1.81 KB

Versions: 29

Compression:

Stored size: 1.81 KB

Contents

require 'stringio'
require 'protobuf/decoder'
require 'protobuf/encoder'

module Protobuf
  class Message
    module Serialization

      module ClassMethods
        def decode(bytes)
          new.decode(bytes)
        end

        def decode_from(stream)
          new.decode_from(stream)
        end

        # Create a new object with the given values and return the encoded bytes.
        def encode(fields = {})
          new(fields).encode
        end
      end

      def self.included(other)
        other.extend(ClassMethods)
      end

      ##
      # Instance Methods
      #

      # Decode the given non-stream bytes into this message.
      #
      def decode(bytes)
        decode_from(::StringIO.new(bytes))
      end

      # Decode the given stream into this message.
      #
      def decode_from(stream)
        ::Protobuf::Decoder.decode_each_field(stream) do |tag, bytes|
          set_field_bytes(tag, bytes)
        end

        self
      end

      # Encode this message
      #
      def encode
        stream = ::StringIO.new
        stream.set_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
        encode_to(stream)
        stream.string
      end

      # Encode this message to the given stream.
      #
      def encode_to(stream)
        ::Protobuf::Encoder.encode(self, stream)
      end

      ##
      # Instance Aliases
      #
      alias :parse_from_string decode
      alias :deserialize decode
      alias :parse_from decode_from
      alias :deserialize_from decode_from
      alias :to_s encode
      alias :bytes encode
      alias :serialize encode
      alias :serialize_to_string encode
      alias :serialize_to encode_to

      private

      def set_field_bytes(tag, bytes)
        field = _protobuf_message_field[tag]
        field.set(self, bytes) if field
      end

    end
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

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