Class: Puppet::Pops::Serialization::AbstractReader

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

Direct Known Subclasses

JSON::Reader

Instance Method Summary collapse

Constructor Details

#initialize(unpacker, extension_unpacker = nil) ⇒ AbstractReader

Returns a new instance of AbstractReader.

Parameters:

  • unpacker (MessagePack::Unpacker, JSON::Unpacker)

    The low lever unpacker that delivers the primitive objects

  • extension_unpacker (MessagePack::Unpacker, JSON::Unpacker) (defaults to: nil)

    Optional unpacker for extensions. Defaults to the unpacker



28
29
30
31
32
33
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 28

def initialize(unpacker, extension_unpacker = nil)
  @read = []
  @unpacker = unpacker
  @extension_unpacker = extension_unpacker.nil? ? unpacker : extension_unpacker
  register_types
end

Instance Method Details

#extension_unpackerObject

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.



81
82
83
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 81

def extension_unpacker
  @extension_unpacker
end

#primitive_countInteger

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.

Returns The total count of unique primitive values that has been read.

Returns:

  • (Integer)

    The total count of unique primitive values that has been read



54
55
56
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 54

def primitive_count
  @read.size
end

#readObject

Read an object from the underlying unpacker

Returns:

  • (Object)

    the object that was read



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 38

def read
  obj = @unpacker.read
  case obj
  when Extension::InnerTabulation
    @read[obj.index]
  when Numeric, Symbol, Extension::NotTabulated, true, false, nil
    # not tabulated
    obj
  else
    @read << obj
    obj
  end
end

#read_payload(data) ⇒ 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.

Raises:



59
60
61
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 59

def read_payload(data)
  raise SerializationError, "Internal error: Class #{self.class} does not implement method 'read_payload'"
end

#read_tpl(ep) ⇒ 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.



69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 69

def read_tpl(ep)
  obj = ep.read
  case obj
  when Integer
    @read[obj]
  else
    @read << obj
    obj
  end
end

#read_tpl_qname(ep) ⇒ 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.



64
65
66
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 64

def read_tpl_qname(ep)
  Array.new(ep.read) { read_tpl(ep) }.join('::')
end

#register_type(extension_number, &block) ⇒ 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.



86
87
88
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 86

def register_type(extension_number, &block)
  @unpacker.register_type(extension_number, &block)
end

#register_typesObject

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.



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 91

def register_types
  register_type(Extension::INNER_TABULATION) do |data|
    read_payload(data) { |ep| Extension::InnerTabulation.new(ep.read) }
  end

  register_type(Extension::TABULATION) do |data|
    read_payload(data) { |ep| Extension::Tabulation.new(ep.read) }
  end

  register_type(Extension::ARRAY_START) do |data|
    read_payload(data) { |ep| Extension::ArrayStart.new(ep.read) }
  end

  register_type(Extension::MAP_START) do |data|
    read_payload(data) { |ep| Extension::MapStart.new(ep.read) }
  end

  register_type(Extension::PCORE_OBJECT_START) do |data|
    read_payload(data) { |ep| type_name = read_tpl_qname(ep); Extension::PcoreObjectStart.new(type_name, ep.read) }
  end

  register_type(Extension::OBJECT_START) do |data|
    read_payload(data) { |ep| Extension::ObjectStart.new(ep.read) }
  end

  register_type(Extension::DEFAULT) do |data|
    read_payload(data) { |ep| Extension::Default::INSTANCE }
  end

  register_type(Extension::COMMENT) do |data|
    read_payload(data) { |ep| Extension::Comment.new(ep.read) }
  end

  register_type(Extension::SENSITIVE_START) do |data|
    read_payload(data) { |ep| Extension::SensitiveStart::INSTANCE }
  end

  register_type(Extension::REGEXP) do |data|
    read_payload(data) { |ep| Regexp.new(ep.read) }
  end

  register_type(Extension::TYPE_REFERENCE) do |data|
    read_payload(data) { |ep| Types::PTypeReferenceType.new(ep.read) }
  end

  register_type(Extension::SYMBOL) do |data|
    read_payload(data) { |ep| ep.read.to_sym }
  end

  register_type(Extension::TIME) do |data|
    read_payload(data) do |ep|
      sec = ep.read
      nsec = ep.read
      Time::Timestamp.new(sec * 1000000000 + nsec)
    end
  end

  register_type(Extension::TIMESPAN) do |data|
    read_payload(data) do |ep|
      sec = ep.read
      nsec = ep.read
      Time::Timespan.new(sec * 1000000000 + nsec)
    end
  end

  register_type(Extension::VERSION) do |data|
    read_payload(data) { |ep| SemanticPuppet::Version.parse(ep.read) }
  end

  register_type(Extension::VERSION_RANGE) do |data|
    read_payload(data) { |ep| SemanticPuppet::VersionRange.parse(ep.read) }
  end

  register_type(Extension::BASE64) do |data|
    read_payload(data) { |ep| Types::PBinaryType::Binary.from_base64_strict(ep.read) }
  end

  register_type(Extension::BINARY) do |data|
    # The Ruby MessagePack implementation have special treatment for "ASCII-8BIT" strings. They
    # are written as binary data.
    read_payload(data) { |ep| Types::PBinaryType::Binary.new(ep.read) }
  end
end