Method: Puppet::Pops::Serialization::FromDataConverter#initialize

Defined in:
lib/puppet/pops/serialization/from_data_converter.rb

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



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
144
145
146
147
148
# File 'lib/puppet/pops/serialization/from_data_converter.rb', line 87

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