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

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

Overview

API:

  • public

Direct Known Subclasses

JSON::Reader

Instance Method Summary collapse

Constructor Details

#initialize(unpacker, extension_unpacker = nil) ⇒ AbstractReader

Returns a new instance of AbstractReader.

Parameters:

  • The low lever unpacker that delivers the primitive objects

  • (defaults to: nil)

    Optional unpacker for extensions. Defaults to the unpacker

API:

  • public



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

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.

API:

  • private



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

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:

  • The total count of unique primitive values that has been read

API:

  • private



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

def primitive_count
  @read.size
end

#readObject

Read an object from the underlying unpacker

Returns:

  • the object that was read

API:

  • public



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

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:

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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
174
175
176
177
178
# File 'lib/puppet/pops/serialization/abstract_reader.rb', line 92

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

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