Class: Puppet::Pops::Serialization::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/serialization/deserializer.rb

Overview

The deserializer is capable of reading, arrays, maps, and complex objects using an underlying protocol reader. It takes care of resolving tabulations and assembling complex objects. The type of the complex objects are resolved using a loader.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, loader) ⇒ Deserializer

Returns a new instance of Deserializer.

Parameters:

  • reader (AbstractReader)

    the reader used when reading primitive objects from a stream

  • loader (Loader::Loader)

    the loader used when resolving names of types



18
19
20
21
22
# File 'lib/puppet/pops/serialization/deserializer.rb', line 18

def initialize(reader, loader)
  @read = []
  @reader = reader
  @loader = loader
end

Instance Attribute Details

#loaderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides access to the reader.



13
14
15
# File 'lib/puppet/pops/serialization/deserializer.rb', line 13

def loader
  @loader
end

#readerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides access to the reader.



13
14
15
# File 'lib/puppet/pops/serialization/deserializer.rb', line 13

def reader
  @reader
end

Instance Method Details

#readObject

Read the next value from the reader.

Returns:

  • (Object)

    the value that was read



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet/pops/serialization/deserializer.rb', line 28

def read
  val = @reader.read
  case val
  when Extension::Tabulation
    @read[val.index]
  when Extension::Default
    :default
  when Extension::ArrayStart
    result = remember([])
    val.size.times { result << read }
    result
  when Extension::MapStart
    result = remember({})
    val.size.times { key = read; result[key] = read }
    result
  when Extension::SensitiveStart
    Types::PSensitiveType::Sensitive.new(read)
  when Extension::PcoreObjectStart
    type_name = val.type_name
    type = Types::TypeParser.singleton.parse(type_name, @loader)
    raise SerializationError, _("No implementation mapping found for Puppet Type %{type_name}") % { type_name: type_name } if type.is_a?(Types::PTypeReferenceType)

    result = type.read(val.attribute_count, self)
    if result.is_a?(Types::PObjectType)
      existing_type = loader.load(:type, result.name)
      if result.eql?(existing_type)
        result = existing_type
      else
        # Add result to the loader unless it is equal to the existing_type. The add
        # will only succeed when the existing_type is nil.
        loader.add_entry(:type, result.name, result, nil)
      end
    end
    result
  when Extension::ObjectStart
    type = read
    type.read(val.attribute_count - 1, self)
  when Numeric, String, true, false, nil
    val
  else
    remember(val)
  end
end

#remember(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remember that a value has been read. This means that the value is given an index and that subsequent reads of a tabulation with that index should return the value.

Parameters:

  • value (Object)

    The value to remember

Returns:



77
78
79
80
# File 'lib/puppet/pops/serialization/deserializer.rb', line 77

def remember(value)
  @read << value
  value
end