class Puppet::Pops::Serialization::FromDataConverter

Class that can process the `Data` produced by the {ToDataConverter} class and reassemble the objects that were converted.

@api public

Public Class Methods

convert(value, options = EMPTY_HASH) click to toggle source

Converts the given `Data` value according to the given options and returns the resulting `RichData`.

@param value [Data] the value to convert @param options {Symbol => <Boolean,String>} options hash @option options [Loaders::Loader] :loader the loader to use. Can be `nil` in which case the default is

determined by the {Types::TypeParser}.

@option options [Boolean] :allow_unresolved `true` to allow that rich_data hashes are kept “as is” if the

designated '__ptype' cannot be resolved. Defaults to `false`.

@return [RichData] the processed result.

@api public

   # File lib/puppet/pops/serialization/from_data_converter.rb
71 def self.convert(value, options = EMPTY_HASH)
72   new(options).convert(value)
73 end
new(options = EMPTY_HASH) click to toggle source

Creates a new instance of the processor

@param options {Symbol => Object} options hash @option options [Loaders::Loader] :loader the loader to use. Can be `nil` in which case the default is

determined by the {Types::TypeParser}.

@option options [Boolean] :allow_unresolved `true` to allow that rich_data hashes are kept “as is” if the

designated '__ptype' cannot be resolved. Defaults to `false`.

@api public

    # File lib/puppet/pops/serialization/from_data_converter.rb
 84 def initialize(options = EMPTY_HASH)
 85   @allow_unresolved = options[:allow_unresolved]
 86   @allow_unresolved = false if @allow_unresolved.nil?
 87   @loader = options[:loader]
 88 
 89   @pcore_type_procs = {
 90     PCORE_TYPE_HASH => proc do |hash, _|
 91       value = hash[PCORE_VALUE_KEY]
 92       build({}) do
 93         top = value.size
 94         idx = 0
 95         while idx < top
 96           key = without_value { convert(value[idx]) }
 97           idx += 1
 98           with(key) { convert(value[idx]) }
 99           idx += 1
100         end
101       end
102     end,
103 
104     PCORE_TYPE_SENSITIVE => proc do |hash, _|
105       build(Types::PSensitiveType::Sensitive.new(convert(hash[PCORE_VALUE_KEY])))
106     end,
107 
108     PCORE_TYPE_DEFAULT => proc do |_, _|
109       build(:default)
110     end,
111 
112     PCORE_TYPE_SYMBOL => proc do |hash, _|
113       build(:"#{hash[PCORE_VALUE_KEY]}")
114     end,
115 
116     PCORE_LOCAL_REF_SYMBOL => proc do |hash, _|
117       build(JsonPath::Resolver.singleton.resolve(@root, hash[PCORE_VALUE_KEY]))
118     end
119   }
120   @pcore_type_procs.default = proc do |hash, type_value|
121     value = hash.include?(PCORE_VALUE_KEY) ? hash[PCORE_VALUE_KEY] : hash.reject { |key, _| PCORE_TYPE_KEY == key }
122     if type_value.is_a?(Hash)
123       type = without_value { convert(type_value) }
124       if type.is_a?(Hash)
125         raise SerializationError, _('Unable to deserialize type from %{type}') % { type: type } unless @allow_unresolved
126         hash
127       else
128         pcore_type_hash_to_value(type, value)
129       end
130     else
131       type = Types::TypeParser.singleton.parse(type_value, @loader)
132       if type.is_a?(Types::PTypeReferenceType)
133         unless @allow_unresolved
134           raise SerializationError, _('No implementation mapping found for Puppet Type %{type_name}') % { type_name: type_value }
135         end
136         hash
137       else
138         # not a string
139         pcore_type_hash_to_value(type, value)
140       end
141     end
142   end
143 end

Public Instance Methods

convert(value) click to toggle source

Converts the given `Data` value and returns the resulting `RichData`

@param value [Data] the value to convert @return [RichData] the processed result

@api public

    # File lib/puppet/pops/serialization/from_data_converter.rb
151 def convert(value)
152   if value.is_a?(Hash)
153     pcore_type = value[PCORE_TYPE_KEY]
154     if pcore_type && (pcore_type.is_a?(String) || pcore_type.is_a?(Hash))
155       @pcore_type_procs[pcore_type].call(value, pcore_type)
156     else
157       build({}) { value.each_pair { |key, elem| with(key) { convert(elem) }}}
158     end
159   elsif value.is_a?(Array)
160     build([]) { value.each_with_index { |elem, idx| with(idx) { convert(elem)}}}
161   else
162     build(value)
163   end
164 end

Private Instance Methods

build(value, &block) click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
192 def build(value, &block)
193   vx = Builder.new(value)
194   @current[@key] = vx unless @current.nil?
195   with_value(vx, &block) if block_given?
196   vx.resolve
197 end
build_object(builder, &block) click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
199 def build_object(builder, &block)
200   @current[@key] = builder unless @current.nil?
201   with_value(builder, &block) if block_given?
202   builder.resolve
203 end
pcore_type_hash_to_value(pcore_type, value) click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
205 def pcore_type_hash_to_value(pcore_type, value)
206   if value.is_a?(Hash)
207     # Complex object
208     if value.empty?
209       build(pcore_type.create)
210     elsif pcore_type.implementation_class.respond_to?(:_pcore_init_from_hash)
211       build_object(ObjectHashBuilder.new(pcore_type.allocate)) { value.each_pair { |key, elem| with(key) { convert(elem) } } }
212     else
213       build_object(ObjectArrayBuilder.new(pcore_type.allocate)) { value.each_pair { |key, elem| with(key) { convert(elem) } } }
214     end
215   elsif value.is_a?(String)
216     build(pcore_type.create(value))
217   else
218     raise SerializationError, _('Cannot create a %{type_name} from a %{arg_class}') %
219         { :type_name => pcore_type.name, :arg_class => value.class.name }
220   end
221 end
with(key) { || ... } click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
168 def with(key)
169   parent_key = @key
170   @key = key
171   yield
172   @key = parent_key
173 end
with_value(value) { || ... } click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
175 def with_value(value)
176   @root = value unless instance_variable_defined?(:@root)
177   parent = @current
178   @current = value
179   yield
180   @current = parent
181   value
182 end
without_value() { || ... } click to toggle source
    # File lib/puppet/pops/serialization/from_data_converter.rb
184 def without_value
185   parent = @current
186   @current = nil
187   value = yield
188   @current = parent
189   value
190 end