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:



76
77
78
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 76

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.



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

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

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



111
112
113
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 111

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.



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
179
180
181
182
183
184
185
186
187
188
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 116

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::OBJECT_START, Extension::ObjectStart) do |o|
    build_payload { |ep| write_tpl_qname(ep, o.type_name); 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

  # 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| write_tpl_qname(ep, 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, Semantic::Version) do |o|
    build_payload { |ep| ep.write(o.to_s) }
  end

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

  register_type(Extension::SENSITIVE, Types::PSensitiveType::Sensitive) do |o|
    build_payload { |ep| ep.write(o.unwrap) }
  end

  register_type(Extension::BINARY, Types::PBinaryType::Binary) do |o|
    build_payload { |ep| ep.write(o.to_s) }
  end
end

#write(value) ⇒ Object

Write a value on the underlying stream



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 50

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, Types::PSensitiveType::Sensitive, 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)


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 97

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



88
89
90
91
92
# File 'lib/puppet/pops/serialization/abstract_writer.rb', line 88

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