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

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 '__ptype' cannot be resolved. Defaults to `false`.


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 84

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
        # not a string
        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`.

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 '__ptype' cannot be resolved. Defaults to `false`.


71
72
73
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 71

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`


151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 151

def convert(value)
  if value.is_a?(Hash)
    pcore_type = value[PCORE_TYPE_KEY]
    if pcore_type && (pcore_type.is_a?(String) || pcore_type.is_a?(Hash))
      @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