Class: Puppet::Pops::Serialization::FromDataConverter

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = EMPTY_HASH) ⇒ FromDataConverter

Creates a new instance of the processor

Parameters:

  • options (Symbol => Object) (defaults to: EMPTY_HASH)

    options hash

Options Hash (options):

  • :loader (Loaders::Loader)

    the loader to use. Can be nil in which case the default is determined by the Types::TypeParser.

  • :allow_unresolved (Boolean)

    true to allow that rich_data hashes are kept “as is” if the designated ‘pcore_type’ cannot be resolved. Defaults to false.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 32

def initialize(options = EMPTY_HASH)
  @allow_unresolved = options[:allow_unresolved]
  @allow_unresolved = false if @allow_unresolved.nil?
  @loader = options[:loader]

  @pcore_type_procs = {
    PCORE_TYPE_HASH => proc do |hash, _|
      value = hash[PCORE_VALUE_KEY]
      build({}) do
        top = value.size
        idx = 0
        while idx < top
          key = without_value { convert(value[idx]) }
          idx += 1
          with(key) { convert(value[idx]) }
          idx += 1
        end
      end
    end,

    PCORE_TYPE_SENSITIVE => proc do |hash, _|
      build(Types::PSensitiveType::Sensitive.new(convert(hash[PCORE_VALUE_KEY])))
    end,

    PCORE_TYPE_DEFAULT => proc do |_, _|
      build(:default)
    end,

    PCORE_TYPE_SYMBOL => proc do |hash, _|
      build(:"#{hash[PCORE_VALUE_KEY]}")
    end,

    PCORE_LOCAL_REF_SYMBOL => proc do |hash, _|
      build(JsonPath::Resolver.singleton.resolve(@root, hash[PCORE_VALUE_KEY]))
    end
  }
  @pcore_type_procs.default = proc do |hash, type_value|
    value = hash.include?(PCORE_VALUE_KEY) ? hash[PCORE_VALUE_KEY] : hash.reject { |key, _| PCORE_TYPE_KEY == key }
    if type_value.is_a?(Hash)
      type = without_value { convert(type_value) }
      if type.is_a?(Hash)
        raise SerializationError, _('Unable to deserialize type from %{type}') % { type: type } unless @allow_unresolved
        hash
      else
        pcore_type_hash_to_value(type, value)
      end
    else
      type = Types::TypeParser.singleton.parse(type_value, @loader)
      if type.is_a?(Types::PTypeReferenceType)
        unless @allow_unresolved
          raise SerializationError, _('No implementation mapping found for Puppet Type %{type_name}') % { type_name: type_value }
        end
        hash
      else
        pcore_type_hash_to_value(type, value)
      end
    end
  end
end

Class Method Details

.convert(value, options = EMPTY_HASH) ⇒ RichData

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

Parameters:

  • value (Data)

    the value to convert

  • options (Symbol => <Boolean,String>) (defaults to: EMPTY_HASH)

    options hash

Options Hash (options):

  • :loader (Loaders::Loader)

    the loader to use. Can be nil in which case the default is determined by the Types::TypeParser.

  • :allow_unresolved (Boolean)

    true to allow that rich_data hashes are kept “as is” if the designated ‘pcore_type’ cannot be resolved. Defaults to false.

Returns:

  • (RichData)

    the processed result.



19
20
21
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 19

def self.convert(value, options = EMPTY_HASH)
  new(options).convert(value)
end

Instance Method Details

#convert(value) ⇒ RichData

Converts the given Data value and returns the resulting RichData

Parameters:

  • value (Data)

    the value to convert

Returns:

  • (RichData)

    the processed result



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 98

def convert(value)
  if value.is_a?(Hash)
    pcore_type = value[PCORE_TYPE_KEY]
    if pcore_type
      @pcore_type_procs[pcore_type].call(value, pcore_type)
    else
      build({}) { value.each_pair { |key, elem| with(key) { convert(elem) }}}
    end
  elsif value.is_a?(Array)
    build([]) { value.each_with_index { |elem, idx| with(idx) { convert(elem)}}}
  else
    build(value)
  end
end