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


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

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.


11
12
13
# File 'lib/puppet/pops/serialization/deserializer.rb', line 11

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.


11
12
13
# File 'lib/puppet/pops/serialization/deserializer.rb', line 11

def reader
  @reader
end

Instance Method Details

#readObject

Read the next value from the reader.


26
27
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
# File 'lib/puppet/pops/serialization/deserializer.rb', line 26

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.


74
75
76
77
# File 'lib/puppet/pops/serialization/deserializer.rb', line 74

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