Sha256: a2137a3fdf359f449689b0c9d45040cd9e81fed7c7a35ff31d9a7f1e382f288a

Contents?: true

Size: 1.36 KB

Versions: 15

Compression:

Stored size: 1.36 KB

Contents

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

module Protobuf

  module Encoder

    module_function

    # Encode +message+ and write to +stream+.
    def encode(stream, message)
      # FIXME make this not as ghetto
      raise NotInitializedError, "Message %s is not initialized (one or more fields is improperly set): %s" % [message.class.name, JSON.parse(message.to_json)] unless message.initialized?
      
      message.each_field do |field, value|
        next unless message.has_field?(field.name)

        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

15 entries across 15 versions & 1 rubygems

Version Path
protobuf-2.0.0.rc3 lib/protobuf/message/encoder.rb
protobuf-2.0.0.rc2 lib/protobuf/message/encoder.rb
protobuf-2.0.0.rc1 lib/protobuf/message/encoder.rb
protobuf-1.4.2 lib/protobuf/message/encoder.rb
protobuf-1.4.1 lib/protobuf/message/encoder.rb
protobuf-1.4.0 lib/protobuf/message/encoder.rb
protobuf-1.3.0 lib/protobuf/message/encoder.rb
protobuf-1.1.3 lib/protobuf/message/encoder.rb
protobuf-1.1.2 lib/protobuf/message/encoder.rb
protobuf-1.1.1 lib/protobuf/message/encoder.rb
protobuf-1.1.0.beta2 lib/protobuf/message/encoder.rb
protobuf-1.1.0.beta1 lib/protobuf/message/encoder.rb
protobuf-1.1.0.beta0 lib/protobuf/message/encoder.rb
protobuf-1.0.1 lib/protobuf/message/encoder.rb
protobuf-1.0.0 lib/protobuf/message/encoder.rb