summaryrefslogtreecommitdiff
path: root/lib/chef/node/attribute_cell.rb
blob: 44c518ada3d3b72c76ddf5f9019909500fd344ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
require "chef/node/attribute_constants"
require "chef/node/vivid_mash"

class Chef
  class Node
    class AttributeCell

      #
      # There are dangerous and unpredictable ways to use the internals of this API:
      #
      # 1.  Mutating an interior hash/array into a bare value (particularly nil)
      # 2.  Using individual setters/getters at anything other than the top level (always use
      #     node.default['foo'] not node['foo'].default)
      #

      include AttributeConstants

      attr_accessor :default
      attr_accessor :env_default
      attr_accessor :role_default
      attr_accessor :force_default
      attr_accessor :normal
      attr_accessor :override
      attr_accessor :role_override
      attr_accessor :env_override
      attr_accessor :force_override
      attr_accessor :automatic
      attr_accessor :__node
      attr_accessor :__deep_merge_cache

      def to_json(*opts)
        raise "BUG: should not be called"
      end

      def to_ffi_yajl(*opts)
        raise "BUG: should not be called"
      end

      def to_ary
        as_simple_object.to_ary
      end

      def to_a
        as_simple_object.to_a
      end

      def to_hash
        as_simple_object.to_hash
      end

      def to_h
        as_simple_object.to_h
      end

      def for_json
        as_simple_object.for_json
      end

      def initialize(default: nil, env_default: nil, role_default: nil, force_default: nil,
                     normal: nil,
                     override: nil, role_override: nil, env_override: nil, force_override: nil,
                     automatic: nil,
                     node: nil,
                     deep_merge_cache: nil)
        @__node = node
        @__deep_merge_cache = deep_merge_cache
        self.default        = default
        self.env_default    = env_default
        self.role_default   = role_default
        self.force_default  = force_default
        self.normal         = normal
        self.override       = override
        self.role_override  = role_override
        self.env_override   = env_override
        self.force_override = force_override
        self.automatic      = automatic
      end

      COMPONENTS_AS_SYMBOLS.each do |component|
        define_method :"#{component}=" do |value|
          instance_variable_set(
            :"@#{component}",
            if value.is_a?(Hash) || value.is_a?(Array)
              Chef::Node::VividMash.new(
                wrapped_object: value,
                precedence: component,
                node: __node,
                deep_merge_cache: __deep_merge_cache
              )
            else
              value
            end
          )
        end
      end

      def wrap_automatic_attrs(value)
        @automatic = Chef::Node::VividMash.new(
          wrapped_object: value,
          precedence: :automatic,
          node: __node,
          deep_merge_cache: __deep_merge_cache,
          convert_value: false
        )
      end

      # for performance we delegate Enumerable methods rather than implementing it
      Enumerable.instance_methods.each do |method|
        define_method method do |*args, &block|
          as_simple_object.public_send(method, *args, &block)
        end
      end

      def is_a?(klass)
        highest_precedence.is_a?(klass) || super(klass)
      end

      def kind_of?(klass)
        highest_precedence.kind_of?(klass) || super(klass)
      end

      def eql?(other)
        if is_a?(Hash)
          return false unless other.is_a?(Hash)
          merged_hash.each do |key, value|
            return false unless merged_hash[key].eql?(other[key])
          end
          return true
        elsif is_a?(Array)
          return false unless other.is_a?(Array)
          merged_array.each_with_index do |value, i|
            return false unless value.eql?(other[i])
          end
          return true
        else
          highest_precedence.eql?(other)
        end
      end

      def ==(other)
        if is_a?(Hash)
          return false unless other.is_a?(Hash)
          merged_hash.each do |key, value|
            return false unless merged_hash[key] == other[key]
          end
        elsif is_a?(Array)
          return false unless other.is_a?(Array)
          merged_array.each_with_index do |value, i|
            return false unless value == other[i]
          end
        else
          highest_precedence == other
        end
      end

      def ===(other)
        as_simple_object === other
      end

      def to_s
        as_simple_object.to_s
      end

      # perf
      def key?(key)
        if self.is_a?(Hash)
          merged_hash_has_key?(key)
        else
          as_simple_object.key?(key)
        end
      end

      # perf
      def include?(key)
        if self.is_a?(Hash)
          merged_hash_has_key?(key)
        else
          as_simple_object.include?(key)
        end
      end

      # perf
      def member?(key)
        if self.is_a?(Hash)
          merged_hash_has_key?(key)
        else
          as_simple_object.member?(key)
        end
      end

      # perf
      def has_key?(key)
        if self.is_a?(Hash)
          merged_hash_has_key?(key)
        else
          as_simple_object.has_key?(key)
        end
      end

      def method_missing(method, *args, &block)
        as_simple_object.public_send(method, *args, &block)
      end

      def respond_to?(method, include_private = false)
        as_simple_object.respond_to?(method, include_private) || is_a?(Hash) && key?(method.to_s)
      end

      def [](key)
        if self.is_a?(Hash)
          merged_hash_key(key)
        elsif self.is_a?(Array)
          merged_array_key(key)
        else
          return highest_precedence[key]
        end
      end

      def combined_default
        cell = self.class.allocate
        cell.instance_variable_set(:@default, @default)
        cell.instance_variable_set(:@env_default, @env_default)
        cell.instance_variable_set(:@role_default, @role_default)
        cell.instance_variable_set(:@force_default, @force_default)
        cell
      end

      def combined_override
        cell = self.class.allocate
        cell.instance_variable_set(:@override, @override)
        cell.instance_variable_set(:@role_override, @role_override)
        cell.instance_variable_set(:@env_override, @env_override)
        cell.instance_variable_set(:@force_override, @force_override)
        cell
      end

      def each(&block)
        return enum_for(:each) unless block_given?

        if self.is_a?(Hash)
          merged_hash.each do |key, value|
            yield key, value
          end
        elsif self.is_a?(Array)
          merged_array.each do |value|
            yield value
          end
        else
          yield highest_precedence
        end
      end

      private

      def as_simple_object
        if self.is_a?(Hash)
          merged_hash
        elsif self.is_a?(Array)
          merged_array
        else
          # in normal usage we never wrap non-containers, so this should never happen
          highest_precedence
        end
      end

      def merged_hash_has_key?(key)
        COMPONENTS_AS_SYMBOLS.reverse_each do |component|
          hash = instance_variable_get(:"@#{component}")
          next unless hash.is_a?(Hash)
          return true if hash.key?(key)
        end
        return false
      end

      def merged_array_key(key)
        # this is much faster than merged_array[key]
        automatic_array_key(key) || override_array_key(key) || normal_array_key(key) || default_array_key(key)
      end

      def merged_hash_key(key)
        # this is much faster than merged_hash[key]
        highest_value_found = false
        retval = nil
        COMPONENTS_AS_SYMBOLS.reverse_each do |component|
          hash = instance_variable_get(:"@#{component}")
          next unless hash.is_a?(Hash)
          next unless hash.key?(key)
          value = hash[key]
          unless highest_value_found
            highest_value_found = true
            # this short-circuit is critical for performance
            return value unless value.is_a?(Hash) || value.is_a?(Array)
          end
          retval ||= self.class.allocate
          retval.instance_variable_set(:"@#{component}", value)
        end
        return retval
      end

      def merged_hash
        # this is a one level deep deep_merge
        merged_hash = {}
        highest_value_found = {}
        COMPONENTS_AS_SYMBOLS.each do |component|
          hash = instance_variable_get(:"@#{component}")
          next unless hash.is_a?(Hash)
          hash.each do |key, value|
            merged_hash[key] ||= self.class.allocate
            merged_hash[key].instance_variable_set(:"@#{component}", value)
            highest_value_found[key] = value
          end
        end
        # we need to expose scalars as undecorated scalars (esp. nil, true, false)
        highest_value_found.each do |key, value|
          next if highest_value_found[key].is_a?(Hash) || highest_value_found[key].is_a?(Array)
          merged_hash[key] = highest_value_found[key]
        end
        merged_hash
      end

      def merged_array
        automatic_array || override_array || normal_array || default_array
      end

      def default_array
        return nil unless DEFAULT_COMPONENTS_AS_SYMBOLS.any? do |component|
          send(component).is_a?(Array)
        end
        # this is a one level deep deep_merge
        default_array = []
        DEFAULT_COMPONENTS_AS_SYMBOLS.each do |component|
          array = instance_variable_get(:"@#{component}")
          next unless array.is_a?(Array)
          default_array += array.map do |value|
            if value.is_a?(Hash) || value.is_a?(Array)
              cell = self.class.allocate
              cell.instance_variable_set(:"@#{component}", value)
              cell
            else
              value
            end
          end
        end
        default_array
      end

      def default_array_key(key)
        return nil unless DEFAULT_COMPONENTS_AS_SYMBOLS.any? do |component|
          send(component).is_a?(Array)
        end
        # this is a one level deep deep_merge
        default_array = []
        DEFAULT_COMPONENTS_AS_SYMBOLS.each do |component|
          array = instance_variable_get(:"@#{component}")
          next unless array.is_a?(Array)
          default_array += array
        end
        value = default_array[key]
        if value.is_a?(Hash) || value.is_a?(Array)
          cell = self.class.allocate
          cell.instance_variable_set(:@default, value) # XXX? set to default since we forgot where it came from
          cell
        else
          value
        end
      end

      def normal_array
        return nil unless @normal.is_a?(Array)
        @normal.map do |value|
          if value.is_a?(Hash) || value.is_a?(Array)
            cell = self.class.allocate
            cell.instance_variable_set(:@normal, value)
            cell
          else
            value
          end
        end
      end

      def normal_array_key(key)
        return nil unless @normal.is_a?(Array)
        value = @normal[key]
        if value.is_a?(Hash) || value.is_a?(Array)
          cell = self.class.allocate
          cell.instance_variable_set(:@normal, value)
          cell
        else
          value
        end
      end

      def override_array
        return nil unless OVERRIDE_COMPONENTS_AS_SYMBOLS.any? do |component|
          send(component).is_a?(Array)
        end
        # this is a one level deep deep_merge
        override_array = []
        OVERRIDE_COMPONENTS_AS_SYMBOLS.each do |component|
          array = instance_variable_get(:"@#{component}")
          next unless array.is_a?(Array)
          override_array += array.map do |value|
            if value.is_a?(Hash) || value.is_a?(Array)
              cell = self.class.allocate
              cell.instance_variable_set(:"@#{component}", value)
              cell
            else
              value
            end
          end
        end
        override_array
      end

      def override_array_key(key)
        return nil unless OVERRIDE_COMPONENTS_AS_SYMBOLS.any? do |component|
          send(component).is_a?(Array)
        end
        # this is a one level deep deep_merge
        override_array = []
        OVERRIDE_COMPONENTS_AS_SYMBOLS.each do |component|
          array = instance_variable_get(:"@#{component}")
          next unless array.is_a?(Array)
          override_array += array
        end
        value = override_array[key]
        if value.is_a?(Hash) || value.is_a?(Array)
          cell = self.class.allocate
          cell.instance_variable_set(:@override, value) # XXX? set to override since we forgot where it came from
          cell
        else
          value
        end
      end

      def automatic_array
        return nil unless @automatic.is_a?(Array)
        @automatic.map do |value|
          if value.is_a?(Hash) || value.is_a?(Array)
            cell = self.class.allocate
            cell.instance_variable_set(:@automatic, value)
            cell
          else
            value
          end
        end
      end

      def automatic_array_key(key)
        return nil unless @automatic.is_a?(Array)
        value = @automatic[key]
        if value.is_a?(Hash) || value.is_a?(Array)
          cell = self.class.allocate
          cell.instance_variable_set(:@automatic, value)
          cell
        else
          value
        end
      end

      # @return [Object] value of the highest precedence level
      def highest_precedence
        COMPONENTS.reverse_each do |component|
          value = instance_variable_get(component)
          return value unless value.nil?
        end
        nil
      end
    end
  end
end