Sha256: f56b78880629cb3464c52dfd9cdcba5c6f305a71834829370325056510b77af8

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'protobuf/wire_type'
require 'protobuf/exceptions'

module Protobuf

  module Encoder

    module_function

    # Encode +message+ and write to +stream+.
    def encode(stream, message)
      # FIXME make this not as ghetto
      unless message.initialized?
        raise NotInitializedError, "Message #{message.class.name} is not initialized (one or more fields is improperly set): #{JSON.parse(message.to_json)}"
      end

      message.each_field_for_serialization do |field, value|
        if field.repeated?
          if field.packed?
            key = (field.tag << 3) | WireType::LENGTH_DELIMITED
            packed_value = value.map {|val| field.encode(val) }.join
            stream.write(Field::VarintField.encode(key))
            stream.write(Field::VarintField.encode(packed_value.size))
            stream.write(packed_value)
          else
            value.each do |val|
              write_pair(stream, field, val)
            end
          end
        else
          write_pair(stream, field, value)
        end
      end
    end

    # Encode key and value, and write to +stream+.
    def write_pair(stream, field, value)
      key = (field.tag << 3) | field.wire_type
      key_bytes = Field::VarintField.encode(key)
      stream.write(key_bytes)
      bytes = field.encode(value)
      stream.write(bytes)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
protobuf-2.3.2-java lib/protobuf/message/encoder.rb
protobuf-2.3.2 lib/protobuf/message/encoder.rb