"Fossies" - the Fresh Open Source Software Archive

Member "puppet-7.11.0/lib/puppet/pops/serialization/abstract_reader.rb" (13 Sep 2021, 4871 Bytes) of package /linux/misc/puppet-7.11.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 require_relative 'extension'
    2 require_relative 'time_factory'
    3 
    4 module Puppet::Pops
    5 module Serialization
    6 # Abstract class for protocol specific readers such as MsgPack or JSON
    7 # The abstract reader is capable of reading the primitive scalars:
    8 # - Boolean
    9 # - Integer
   10 # - Float
   11 # - String
   12 # and, by using extensions, also
   13 # - Array start
   14 # - Map start
   15 # - Object start
   16 # - Regexp
   17 # - Version
   18 # - VersionRange
   19 # - Timespan
   20 # - Timestamp
   21 # - Default
   22 #
   23 # @api public
   24 class AbstractReader
   25   # @param [MessagePack::Unpacker,JSON::Unpacker] unpacker The low lever unpacker that delivers the primitive objects
   26   # @param [MessagePack::Unpacker,JSON::Unpacker] extension_unpacker Optional unpacker for extensions. Defaults to the unpacker
   27   # @api public
   28   def initialize(unpacker, extension_unpacker = nil)
   29     @read = []
   30     @unpacker = unpacker
   31     @extension_unpacker = extension_unpacker.nil? ? unpacker : extension_unpacker
   32     register_types
   33   end
   34 
   35   # Read an object from the underlying unpacker
   36   # @return [Object] the object that was read
   37   # @api public
   38   def read
   39     obj = @unpacker.read
   40     case obj
   41     when Extension::InnerTabulation
   42       @read[obj.index]
   43     when Numeric, Symbol, Extension::NotTabulated, true, false, nil
   44       # not tabulated
   45       obj
   46     else
   47       @read << obj
   48       obj
   49     end
   50   end
   51 
   52   # @return [Integer] The total count of unique primitive values that has been read
   53   # @api private
   54   def primitive_count
   55     @read.size
   56   end
   57 
   58   # @api private
   59   def read_payload(data)
   60     raise SerializationError, "Internal error: Class #{self.class} does not implement method 'read_payload'"
   61   end
   62 
   63   # @api private
   64   def read_tpl_qname(ep)
   65     Array.new(ep.read) { read_tpl(ep) }.join('::')
   66   end
   67 
   68   # @api private
   69   def read_tpl(ep)
   70     obj = ep.read
   71     case obj
   72     when Integer
   73       @read[obj]
   74     else
   75       @read << obj
   76       obj
   77     end
   78   end
   79 
   80   # @api private
   81   def extension_unpacker
   82     @extension_unpacker
   83   end
   84 
   85   # @api private
   86   def register_type(extension_number, &block)
   87     @unpacker.register_type(extension_number, &block)
   88   end
   89 
   90   # @api private
   91   def register_types
   92     register_type(Extension::INNER_TABULATION) do |data|
   93       read_payload(data) { |ep| Extension::InnerTabulation.new(ep.read) }
   94     end
   95 
   96     register_type(Extension::TABULATION) do |data|
   97       read_payload(data) { |ep| Extension::Tabulation.new(ep.read) }
   98     end
   99 
  100     register_type(Extension::ARRAY_START) do |data|
  101       read_payload(data) { |ep| Extension::ArrayStart.new(ep.read) }
  102     end
  103 
  104     register_type(Extension::MAP_START) do |data|
  105       read_payload(data) { |ep| Extension::MapStart.new(ep.read) }
  106     end
  107 
  108     register_type(Extension::PCORE_OBJECT_START) do |data|
  109       read_payload(data) { |ep| type_name = read_tpl_qname(ep); Extension::PcoreObjectStart.new(type_name, ep.read) }
  110     end
  111 
  112     register_type(Extension::OBJECT_START) do |data|
  113       read_payload(data) { |ep| Extension::ObjectStart.new(ep.read) }
  114     end
  115 
  116     register_type(Extension::DEFAULT) do |data|
  117       read_payload(data) { |ep| Extension::Default::INSTANCE }
  118     end
  119 
  120     register_type(Extension::COMMENT) do |data|
  121       read_payload(data) { |ep| Extension::Comment.new(ep.read) }
  122     end
  123 
  124     register_type(Extension::SENSITIVE_START) do |data|
  125       read_payload(data) { |ep| Extension::SensitiveStart::INSTANCE }
  126     end
  127 
  128     register_type(Extension::REGEXP) do |data|
  129       read_payload(data) { |ep| Regexp.new(ep.read) }
  130     end
  131 
  132     register_type(Extension::TYPE_REFERENCE) do |data|
  133       read_payload(data) { |ep| Types::PTypeReferenceType.new(ep.read) }
  134     end
  135 
  136     register_type(Extension::SYMBOL) do |data|
  137       read_payload(data) { |ep| ep.read.to_sym }
  138     end
  139 
  140     register_type(Extension::TIME) do |data|
  141       read_payload(data) do |ep|
  142         sec = ep.read
  143         nsec = ep.read
  144         Time::Timestamp.new(sec * 1000000000 + nsec)
  145       end
  146     end
  147 
  148     register_type(Extension::TIMESPAN) do |data|
  149       read_payload(data) do |ep|
  150         sec = ep.read
  151         nsec = ep.read
  152         Time::Timespan.new(sec * 1000000000 + nsec)
  153       end
  154     end
  155 
  156     register_type(Extension::VERSION) do |data|
  157       read_payload(data) { |ep| SemanticPuppet::Version.parse(ep.read) }
  158     end
  159 
  160     register_type(Extension::VERSION_RANGE) do |data|
  161       read_payload(data) { |ep| SemanticPuppet::VersionRange.parse(ep.read) }
  162     end
  163 
  164     register_type(Extension::BASE64) do |data|
  165       read_payload(data) { |ep| Types::PBinaryType::Binary.from_base64_strict(ep.read) }
  166     end
  167 
  168     register_type(Extension::BINARY) do |data|
  169       # The Ruby MessagePack implementation have special treatment for "ASCII-8BIT" strings. They
  170       # are written as binary data.
  171       read_payload(data) { |ep| Types::PBinaryType::Binary.new(ep.read) }
  172     end
  173 
  174     register_type(Extension::URI) do |data|
  175       read_payload(data) { |ep| URI(ep.read) }
  176     end
  177   end
  178 end
  179 end
  180 end