Class: Puppet::Pops::Serialization::Serializer

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

Overview

The serializer is capable of writing, arrays, maps, and complex objects using an underlying protocol writer. It takes care of tabulating and disassembling complex objects.

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer) ⇒ Serializer

Returns a new instance of Serializer.

Parameters:

  • the writer that is used for writing primitive values

API:

  • public



15
16
17
18
# File 'lib/puppet/pops/serialization/serializer.rb', line 15

def initialize(writer)
  @written = {}
  @writer = writer
end

Instance Attribute Details

#writerObject (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 writer.

API:

  • private



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

def writer
  @writer
end

Instance Method Details

#finishObject

Tell the underlying writer to finish

API:

  • public



22
23
24
# File 'lib/puppet/pops/serialization/serializer.rb', line 22

def finish
  @writer.finish
end

#push_written(value) ⇒ Object

API:

  • public



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

def push_written(value)
  @written[value.object_id] = @written.size
end

#start_array(size) ⇒ 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.

Write the start of an array.

Parameters:

  • the size of the array

API:

  • private



48
49
50
# File 'lib/puppet/pops/serialization/serializer.rb', line 48

def start_array(size)
  @writer.write(Extension::ArrayStart.new(size))
end

#start_map(size) ⇒ 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.

Write the start of a map (hash).

Parameters:

  • the number of entries in the map

API:

  • private



55
56
57
# File 'lib/puppet/pops/serialization/serializer.rb', line 55

def start_map(size)
  @writer.write(Extension::MapStart.new(size))
end

#start_object(attr_count) ⇒ 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.

Write the start of a complex object

Parameters:

  • the number of attributes in the object

API:

  • private



70
71
72
# File 'lib/puppet/pops/serialization/serializer.rb', line 70

def start_object(attr_count)
  @writer.write(Extension::ObjectStart.new(attr_count))
end

#start_pcore_object(type_ref, attr_count) ⇒ 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.

Write the start of a complex pcore object

Parameters:

  • the name of the type

  • the number of attributes in the object

API:

  • private



63
64
65
# File 'lib/puppet/pops/serialization/serializer.rb', line 63

def start_pcore_object(type_ref, attr_count)
  @writer.write(Extension::PcoreObjectStart.new(type_ref, attr_count))
end

#start_sensitiveObject

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.

Write the start of a sensitive object

API:

  • private



80
81
82
# File 'lib/puppet/pops/serialization/serializer.rb', line 80

def start_sensitive
  @writer.write(Extension::SensitiveStart::INSTANCE)
end

#write(value) ⇒ Object

Write an object

Parameters:

  • the object to write

API:

  • public



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/pops/serialization/serializer.rb', line 29

def write(value)
  case value
  when Integer, Float, String, true, false, nil
    @writer.write(value)
  when :default
    @writer.write(Extension::Default::INSTANCE)
  else
    index = @written[value.object_id]
    if index.nil?
      write_tabulated_first_time(value)
    else
      @writer.write(Extension::Tabulation.new(index))
    end
  end
end

#write_tabulated_first_time(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.

First time write of a tabulated object. This means that the object is written and then remembered. Subsequent writes of the same object will yield a write of a tabulation index instead.

Parameters:

  • the value to write

API:

  • private



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puppet/pops/serialization/serializer.rb', line 88

def write_tabulated_first_time(value)
  case value
  when Symbol, Regexp, SemanticPuppet::Version, SemanticPuppet::VersionRange, Time::Timestamp, Time::Timespan, Types::PBinaryType::Binary
    push_written(value)
    @writer.write(value)
  when Array
    push_written(value)
    start_array(value.size)
    value.each { |elem| write(elem) }
  when Hash
    push_written(value)
    start_map(value.size)
    value.each_pair { |key, val| write(key); write(val) }
  when Types::PSensitiveType::Sensitive
    start_sensitive
    write(value.unwrap)
  when Types::PTypeReferenceType
    push_written(value)
    @writer.write(value)
  when Types::PuppetObject
    value._ptype.write(value, self)
  else
    impl_class = value.class
    type = Loaders.implementation_registry.type_for_module(impl_class)
    raise SerializationError, "No Puppet Type found for #{impl_class.name}" unless type.is_a?(Types::PObjectType)
    type.write(value, self)
  end
end