Class: Puppet::Pops::Serialization::AbstractWriter

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

Overview

Abstract class for protocol specific writers such as MsgPack or JSON The abstract write is capable of writing the primitive scalars:

  • Boolean

  • Integer

  • Float

  • String

and, by using extensions, also

  • Array start

  • Map start

  • Object start

  • Regexp

  • Version

  • VersionRange

  • Timespan

  • Timestamp

  • Default

Direct Known Subclasses

JSON::Writer

Instance Method Summary collapse

Constructor Details

#initialize(packer, options, extension_packer = nil) ⇒ AbstractWriter

Returns a new instance of AbstractWriter.

Parameters:

  • packer (MessagePack::Packer, JSON::Packer)

    the underlying packer stream

  • options (Hash)
  • extension_packer (DebugPacker, nil) (defaults to: nil)

    Optional specific extension packer. Only used for debug output

Options Hash (options):

  • :tabulate (Boolean)

    ‘true` if tabulation is enabled (which is the default).



33
34
35
36
37
38
39
40
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 33

def initialize(packer, options, extension_packer = nil)
  @tabulate = options[:tabulate]
  @tabulate = true if @tabulate.nil?
  @written = {}
  @packer = packer
  @extension_packer = extension_packer.nil? ? packer : extension_packer
  register_types
end

Instance Method Details

#build_payloadObject

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.

Called from extension callbacks only

Raises:



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

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

#extension_packerObject

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.



85
86
87
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 85

def extension_packer
  @extension_packer
end

#finishObject

Tell the underlying packer to flush.



44
45
46
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 44

def finish
  @packer.flush
end

#inspectObject



210
211
212
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 210

def inspect
  to_s
end

#register_type(extension_number, payload_class, &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.



115
116
117
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 115

def register_type(extension_number, payload_class, &block)
  @packer.register_type(extension_number, payload_class, &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.



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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 120

def register_types
  # 0x00 - 0x0F are reserved for low-level serialization / tabulation extensions

  register_type(Extension::INNER_TABULATION, Extension::InnerTabulation) do |o|
    build_payload { |ep| ep.write(o.index) }
  end

  register_type(Extension::TABULATION, Extension::Tabulation) do |o|
    build_payload { |ep| ep.write(o.index) }
  end

  # 0x10 - 0x1F are reserved for structural extensions

  register_type(Extension::ARRAY_START, Extension::ArrayStart) do |o|
    build_payload { |ep| ep.write(o.size) }
  end

  register_type(Extension::MAP_START, Extension::MapStart) do |o|
    build_payload { |ep| ep.write(o.size) }
  end

  register_type(Extension::PCORE_OBJECT_START, Extension::PcoreObjectStart) do |o|
    build_payload { |ep| write_tpl_qname(ep, o.type_name); ep.write(o.attribute_count) }
  end

  register_type(Extension::OBJECT_START, Extension::ObjectStart) do |o|
    build_payload { |ep| ep.write(o.attribute_count) }
  end

  # 0x20 - 0x2f reserved for special extension objects

  register_type(Extension::DEFAULT, Extension::Default) do |o|
    build_payload { |ep| }
  end

  register_type(Extension::COMMENT, Extension::Comment) do |o|
    build_payload { |ep| ep.write(o.comment) }
  end

  register_type(Extension::SENSITIVE_START, Extension::SensitiveStart) do |o|
    build_payload { |ep| }
  end

  # 0x30 - 0x7f reserved for mapping of specific runtime classes

  register_type(Extension::REGEXP, Regexp) do |o|
    build_payload { |ep| ep.write(o.source) }
  end

  register_type(Extension::TYPE_REFERENCE, Types::PTypeReferenceType) do |o|
    build_payload { |ep| ep.write(o.type_string) }
  end

  register_type(Extension::SYMBOL, Symbol) do |o|
    build_payload { |ep| ep.write(o.to_s) }
  end

  register_type(Extension::TIME, Time::Timestamp) do |o|
    build_payload { |ep| nsecs = o.nsecs; ep.write(nsecs / 1000000000); ep.write(nsecs % 1000000000) }
  end

  register_type(Extension::TIMESPAN, Time::Timespan) do |o|
    build_payload { |ep| nsecs = o.nsecs; ep.write(nsecs / 1000000000); ep.write(nsecs % 1000000000) }
  end

  register_type(Extension::VERSION, SemanticPuppet::Version) do |o|
    build_payload { |ep| ep.write(o.to_s) }
  end

  register_type(Extension::VERSION_RANGE, SemanticPuppet::VersionRange) do |o|
    build_payload { |ep| ep.write(o.to_s) }
  end

  if supports_binary?
    register_type(Extension::BINARY, Types::PBinaryType::Binary) do |o|
      # The Ruby MessagePack implementation has special treatment for "ASCII-8BIT" strings. They
      # are written as binary data.
      build_payload { |ep| ep.write(o.binary_buffer) }
    end
  else
    register_type(Extension::BASE64, Types::PBinaryType::Binary) do |o|
      build_payload { |ep| ep.write(o.to_s) }
    end
  end
end

#supports_binary?Boolean

Returns:

  • (Boolean)


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

def supports_binary?
  false
end

#to_sObject



206
207
208
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 206

def to_s
  "#{self.class.name}"
end

#write(value) ⇒ Object

Write a value on the underlying stream



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 54

def write(value)
  written = false
  case value
  when Integer
    # not tabulated, but integers larger than 64-bit cannot be allowed.
    raise SerializationError, _('Integer out of bounds') if value > MAX_INTEGER || value < MIN_INTEGER
  when Numeric, Symbol, Extension::NotTabulated, true, false, nil
    # not tabulated
  else
    if @tabulate
      index = @written[value]
      if index.nil?
        @packer.write(value)
        written = true
        @written[value] = @written.size
      else
        value = Extension::InnerTabulation.new(index)
      end
    end
  end
  @packer.write(value) unless written
end

#write_tpl(ep, 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.

Called from extension callbacks only

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 101

def write_tpl(ep, value)
  raise ArgumentError, 'Internal error. Integers cannot be tabulated in extension payload' if value.is_a?(Integer)
  if @tabulate
    index = @written[value]
    if index.nil?
      @written[value] = @written.size
    else
      value = index
    end
  end
  ep.write(value)
end

#write_tpl_qname(ep, qname) ⇒ 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.

Called from extension callbacks only



92
93
94
95
96
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 92

def write_tpl_qname(ep, qname)
  names = qname.split('::')
  ep.write(names.size)
  names.each {|n| write_tpl(ep, n)}
end