Sha256: 9ac50f233b3c91305b6fb140ccfce3d4174eaee241ce19df6158e5880b115616

Contents?: true

Size: 1.86 KB

Versions: 28

Compression:

Stored size: 1.86 KB

Contents

require_relative 'instance_reader'
require_relative 'instance_writer'

module Puppet::Pops
module Serialization

# Instance reader for objects that implement {Types::PuppetObject}
# @api private
class ObjectReader
  include InstanceReader

  def read(impl_class, value_count, deserializer)
    type = impl_class._ptype
    (names, types, required_count) = type.parameter_info
    max = names.size
    unless value_count >= required_count && value_count <= max
      raise Serialization::SerializationError, "Feature count mismatch for #{impl_class.name}. Expected #{min} - #{max}, actual #{value_count}"
    end
    # Deserializer must know about this instance before we read its attributes
    val = deserializer.remember(impl_class.allocate)
    args = Array.new(value_count) { deserializer.read }
    types.each_with_index do |ptype, index|
      if index < args.size
        arg = args[index]
        Types::TypeAsserter.assert_instance_of(nil, ptype, arg) { "#{type.name}[#{names[index]}]" } unless arg == :default
      else
        attr = type[names[index]]
        raise Serialization::SerializationError, "Missing default value for #{type.name}[#{names[index]}]" unless attr.value?
        args << attr.value
      end
    end
    val.send(:initialize, *args)
    val
  end

  INSTANCE = ObjectReader.new
end

# Instance writer for objects that implement {Types::PuppetObject}
# @api private
class ObjectWriter
  include InstanceWriter

  def write(type, value, serializer)
    impl_class = value.class
    (names, types, required_count) = type.parameter_info(true)
    args = names.map { |name| value.send(name) }

    # Pop optional arguments that are nil
    while args.size > required_count
      break unless args.last.nil?
      args.pop
    end

    serializer.start_object(type.name, args.size)
    args.each { |arg| serializer.write(arg) }
  end

  INSTANCE = ObjectWriter.new
end
end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
puppet-4.8.2 lib/puppet/pops/serialization/object.rb
puppet-4.8.2-x86-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.2-x64-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.2-universal-darwin lib/puppet/pops/serialization/object.rb
puppet-4.7.1 lib/puppet/pops/serialization/object.rb
puppet-4.7.1-x86-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.7.1-x64-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.7.1-universal-darwin lib/puppet/pops/serialization/object.rb
puppet-4.8.1 lib/puppet/pops/serialization/object.rb
puppet-4.8.1-x86-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.1-x64-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.1-universal-darwin lib/puppet/pops/serialization/object.rb
puppet-4.8.0 lib/puppet/pops/serialization/object.rb
puppet-4.8.0-x86-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.0-x64-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.8.0-universal-darwin lib/puppet/pops/serialization/object.rb
puppet-4.7.0 lib/puppet/pops/serialization/object.rb
puppet-4.7.0-x86-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.7.0-x64-mingw32 lib/puppet/pops/serialization/object.rb
puppet-4.7.0-universal-darwin lib/puppet/pops/serialization/object.rb