Class: Hocon::Impl::SimpleConfigObject

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AbstractConfigObject
Defined in:
lib/hocon/impl/simple_config_object.rb

Defined Under Namespace

Classes: RenderComparator, ResolveModifier

Constant Summary collapse

ConfigBugOrBrokenError =
Hocon::ConfigError::ConfigBugOrBrokenError
ResolveStatus =
Hocon::Impl::ResolveStatus
ResolveResult =
Hocon::Impl::ResolveResult
SimpleConfigOrigin =
Hocon::Impl::SimpleConfigOrigin
Path =
Hocon::Impl::Path

Constants included from AbstractConfigObject

AbstractConfigObject::ConfigNotResolvedError

Constants included from AbstractConfigValue

AbstractConfigValue::ConfigImplUtil

Instance Attribute Summary collapse

Attributes included from AbstractConfigValue

#origin

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractConfigObject

#[], #[]=, #clear, #construct_delayed_merge, #delete, merge_origins, #new_copy, #peek_assuming_resolved, #peek_path, #peek_path_from_obj, #putAll, #remove, #to_config, #to_fallback_value, #value_type, #we_are_immutable, #with_fallback, #with_origin, #with_path_value

Methods included from AbstractConfigValue

#at_key, #at_key_with_origin, #at_path, #at_path_with_origin, #construct_delayed_merge, #delay_merge, has_descendant_in_list?, indent, #inspect, #merged_stack_with_non_object, #merged_stack_with_object, #merged_stack_with_the_unmergeable, #merged_with_non_object, #merged_with_the_unmergeable, #new_copy, #render, #render_to_sb, replace_child_in_list, #require_not_ignoring_fallbacks, #to_fallback_value, #to_s, #transform_to_string, #with_fallback, #with_origin

Methods included from ConfigValue

#at_key, #at_path, #origin, #render, #value_type, #with_fallback, #with_origin

Methods included from ConfigMergeable

#with_fallback

Methods included from ConfigObject

#to_config, #with_fallback, #with_origin

Constructor Details

#initialize(origin, value, status = Hocon::Impl::ResolveStatus.from_values(value.values), ignores_fallbacks = false) ⇒ SimpleConfigObject

Returns a new instance of SimpleConfigObject.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hocon/impl/simple_config_object.rb', line 25

def initialize(origin,
               value,
               status = Hocon::Impl::ResolveStatus.from_values(value.values),
               ignores_fallbacks = false)
  super(origin)
  if value.nil?
    raise ConfigBugOrBrokenError, "creating config object with null map"
  end
  @value = value
  @resolved = (status == Hocon::Impl::ResolveStatus::RESOLVED)
  @ignores_fallbacks = ignores_fallbacks

  # Kind of an expensive debug check. Comment out?
  if status != Hocon::Impl::ResolveStatus.from_values(value.values)
    raise ConfigBugOrBrokenError, "Wrong resolved status on #{self}"
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



43
44
45
# File 'lib/hocon/impl/simple_config_object.rb', line 43

def value
  @value
end

Class Method Details

.empty(origin = nil) ⇒ Object



567
568
569
570
571
572
573
# File 'lib/hocon/impl/simple_config_object.rb', line 567

def self.empty(origin = nil)
  if origin.nil?
    empty(Hocon::Impl::SimpleConfigOrigin.new_simple("empty config"))
  else
    self.new(origin, {})
  end
end

.empty_missing(base_origin) ⇒ Object



575
576
577
578
579
# File 'lib/hocon/impl/simple_config_object.rb', line 575

def self.empty_missing(base_origin)
  self.new(
      Hocon::Impl::SimpleConfigOrigin.new_simple("#{base_origin.description} (not found)"),
      {})
end

.map_equals(a, b) ⇒ Object



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/hocon/impl/simple_config_object.rb', line 498

def self.map_equals(a, b)
  if a.equal?(b)
    return true
  end

  # Hashes aren't ordered in ruby, so sort first
  if not a.keys.sort == b.keys.sort
    return false
  end

  a.keys.each do |key|
    if a[key] != b[key]
      return false
    end
  end

  true
end

.map_hash(m) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/hocon/impl/simple_config_object.rb', line 521

def self.map_hash(m)
  # the keys have to be sorted, otherwise we could be equal
  # to another map but have a different hashcode.
  keys = m.keys.sort

  value_hash = 0

  keys.each do |key|
    value_hash += m[key].hash
  end

  41 * (41 + keys.hash) + value_hash
end

Instance Method Details

#==(other) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
# File 'lib/hocon/impl/simple_config_object.rb', line 539

def ==(other)
  # note that "origin" is deliberately NOT part of equality.
  # neither are other "extras" like ignoresFallbacks or resolve status.
  if other.is_a? Hocon::ConfigObject
    # optimization to avoid unwrapped() for two ConfigObject,
    # which is what AbstractConfigValue does.
    can_equal(other) && self.class.map_equals(self, other)
  else
    false
  end
end

#attempt_peek_with_partial_resolve(key) ⇒ Object



152
153
154
# File 'lib/hocon/impl/simple_config_object.rb', line 152

def attempt_peek_with_partial_resolve(key)
  @value[key]
end

#can_equal(other) ⇒ Object



535
536
537
# File 'lib/hocon/impl/simple_config_object.rb', line 535

def can_equal(other)
  other.is_a? Hocon::ConfigObject
end

#contains_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


555
556
557
# File 'lib/hocon/impl/simple_config_object.rb', line 555

def contains_key?(key)
  @value.has_key?(key)
end

#contains_value?(v) ⇒ Boolean

Returns:

  • (Boolean)


563
564
565
# File 'lib/hocon/impl/simple_config_object.rb', line 563

def contains_value?(v)
  @value.has_value?(v)
end

#get(key) ⇒ Object



517
518
519
# File 'lib/hocon/impl/simple_config_object.rb', line 517

def get(key)
  @value[key]
end

#has_descendant?(descendant) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/hocon/impl/simple_config_object.rb', line 189

def has_descendant?(descendant)
  value.values.each do |child|
    if child.equal?(descendant)
      return true
    end
  end
  # now do the expensive search
  value.values.each do |child|
    if child.is_a?(Hocon::Impl::Container) && child.has_descendant?(descendant)
      return true
    end
  end

  false
end

#hashObject



551
552
553
# File 'lib/hocon/impl/simple_config_object.rb', line 551

def hash
  self.class.map_hash(@value)
end

#ignores_fallbacks?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/hocon/impl/simple_config_object.rb', line 205

def ignores_fallbacks?
  @ignores_fallbacks
end

#key_setObject



559
560
561
# File 'lib/hocon/impl/simple_config_object.rb', line 559

def key_set
  Set.new(@value.keys)
end

#merged_with_object(abstract_fallback) ⇒ Object



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
# File 'lib/hocon/impl/simple_config_object.rb', line 217

def merged_with_object(abstract_fallback)
  require_not_ignoring_fallbacks

  unless abstract_fallback.is_a?(Hocon::Impl::SimpleConfigObject)
    raise ConfigBugOrBrokenError, "should not be reached (merging non-SimpleConfigObject)"
  end

  fallback = abstract_fallback
  changed = false
  all_resolved = true
  merged = {}
  all_keys = key_set.union(fallback.key_set)
  all_keys.each do |key|
    first = @value[key]
    second = fallback.value[key]
    kept =
        if first.nil?
          second
        elsif second.nil?
          first
        else
          first.with_fallback(second)
        end
    merged[key] = kept

    if first != kept
      changed = true
    end

    if kept.resolve_status == Hocon::Impl::ResolveStatus::UNRESOLVED
      all_resolved = false
    end
  end

  new_resolve_status = Hocon::Impl::ResolveStatus.from_boolean(all_resolved)
  new_ignores_fallbacks = fallback.ignores_fallbacks?

  if changed
    Hocon::Impl::SimpleConfigObject.new(Hocon::Impl::AbstractConfigObject.merge_origins([self, fallback]),
                                        merged, new_resolve_status,
                                        new_ignores_fallbacks)
  elsif (new_resolve_status != resolve_status) || (new_ignores_fallbacks != ignores_fallbacks?)
    new_copy_with_status(new_resolve_status, origin, new_ignores_fallbacks)
  else
    self
  end
end

#modify(modifier) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/hocon/impl/simple_config_object.rb', line 265

def modify(modifier)
  begin
    modify_may_throw(modifier)
  rescue Hocon::ConfigError => e
    raise e
  end
end

#modify_may_throw(modifier) ⇒ Object



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
# File 'lib/hocon/impl/simple_config_object.rb', line 273

def modify_may_throw(modifier)
  changes = nil
  keys.each do |k|
    v = value[k]
    # "modified" may be null, which means remove the child;
    # to do that we put null in the "changes" map.
    modified = modifier.modify_child_may_throw(k, v)
    if ! modified.equal?(v)
      if changes.nil?
        changes = {}
      end
      changes[k] = modified
    end
  end
  if changes.nil?
    self
  else
    modified = {}
    saw_unresolved = false
    keys.each do |k|
      if changes.has_key?(k)
        new_value = changes[k]
        if ! new_value.nil?
          modified[k] = new_value
          if new_value.resolve_status == ResolveStatus::UNRESOLVED
            saw_unresolved = true
          end
        else
          # remove this child; don't put it in the new map
        end
      else
        new_value = value[k]
        modified[k] = new_value
        if new_value.resolve_status == ResolveStatus::UNRESOLVED
          saw_unresolved = true
        end
      end
    end
    self.class.new(origin, modified,
                   saw_unresolved ? ResolveStatus::UNRESOLVED : ResolveStatus::RESOLVED,
                   @ignores_fallbacks)
  end
end

#new_copy_with_status(new_status, new_origin, new_ignores_fallbacks = nil) ⇒ Object



156
157
158
# File 'lib/hocon/impl/simple_config_object.rb', line 156

def new_copy_with_status(new_status, new_origin, new_ignores_fallbacks = nil)
  self.class.new(new_origin, @value, new_status, new_ignores_fallbacks)
end

#relativized(prefix) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/hocon/impl/simple_config_object.rb', line 374

def relativized(prefix)

  modifier = Class.new do
    include Hocon::Impl::AbstractConfigValue::NoExceptionsModifier

    # prefix isn't in scope inside of a def, but it is in scope inside of Class.new
    # so manually define a method that has access to prefix
    # I feel dirty
    define_method(:modify_child) do |key, v|
      v.relativized(prefix)
    end
  end

  modify(modifier.new)
end

#render_value_to_sb(sb, indent, at_root, options) ⇒ Object



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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/hocon/impl/simple_config_object.rb', line 416

def render_value_to_sb(sb, indent, at_root, options)
  if empty?
    sb << "{}"
  else
    outer_braces = options.json? || !at_root

    if outer_braces
      inner_indent = indent + 1
      sb << "{"

      if options.formatted?
        sb << "\n"
      end
    else
      inner_indent = indent
    end

    separator_count = 0
    sorted_keys = RenderComparator.sort(keys)
    sorted_keys.each do |k|
      v = @value[k]

      if options.origin_comments?
        lines = v.origin.description.split("\n")
        lines.each { |l|
          Hocon::Impl::AbstractConfigValue.indent(sb, indent + 1, options)
          sb << '#'
          unless l.empty?
            sb << ' '
          end
          sb << l
          sb << "\n"
        }
      end
      if options.comments?
        v.origin.comments.each do |comment|
          Hocon::Impl::AbstractConfigValue.indent(sb, inner_indent, options)
          sb << "#"
          if !comment.start_with?(" ")
            sb << " "
          end
          sb << comment
          sb << "\n"
        end
      end
      Hocon::Impl::AbstractConfigValue.indent(sb, inner_indent, options)
      v.render_to_sb(sb, inner_indent, false, k.to_s, options)

      if options.formatted?
        if options.json?
          sb << ","
          separator_count = 2
        else
          separator_count = 1
        end
        sb << "\n"
      else
        sb << ","
        separator_count = 1
      end
    end
    # chop last commas/newlines
    # couldn't figure out a better way to chop characters off of the end of
    # the StringIO.  This relies on making sure that, prior to returning the
    # final string, we take a substring that ends at sb.pos.
    sb.pos = sb.pos - separator_count

    if outer_braces
      if options.formatted?
        sb << "\n" # put a newline back
        if outer_braces
          Hocon::Impl::AbstractConfigValue.indent(sb, indent, options)
        end
      end
      sb << "}"
    end
  end
  if at_root && options.formatted?
    sb << "\n"
  end
end

#replace_child(child, replacement) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hocon/impl/simple_config_object.rb', line 172

def replace_child(child, replacement)
  new_children = @value.clone
  new_children.each do |old, old_value|
    if old_value.equal?(child)
      if replacement != nil
        new_children[old] = replacement
      else
        new_children.delete(old)
      end

      return self.class.new(origin, new_children, ResolveStatus.from_values(new_children.values),
                     @ignores_fallbacks)
    end
  end
  raise ConfigBugOrBrokenError, "SimpleConfigObject.replaceChild did not find #{child} in #{self}"
end

#resolve_statusObject



168
169
170
# File 'lib/hocon/impl/simple_config_object.rb', line 168

def resolve_status
  ResolveStatus.from_boolean(@resolved)
end

#resolve_substitutions(context, source) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/hocon/impl/simple_config_object.rb', line 354

def resolve_substitutions(context, source)
  if resolve_status == ResolveStatus::RESOLVED
    return ResolveResult.make(context, self)
  end

  source_with_parent = source.push_parent(self)

  begin
    modifier = ResolveModifier.new(context, source_with_parent)

    value = modify_may_throw(modifier)
    ResolveResult.make(modifier.context, value)

  rescue NotPossibleToResolve => e
    raise e
  rescue Hocon::ConfigError => e
    raise e
  end
end

#unwrappedObject



209
210
211
212
213
214
215
# File 'lib/hocon/impl/simple_config_object.rb', line 209

def unwrapped
  m = {}
  @value.each do |k,v|
    m[k] = v.unwrapped
  end
  m
end

#with_fallbacks_ignoredObject



160
161
162
163
164
165
166
# File 'lib/hocon/impl/simple_config_object.rb', line 160

def with_fallbacks_ignored()
  if @ignores_fallbacks
    self
  else
    new_copy_with_status(resolve_status, origin, true)
  end
end

#with_key_value(key, v) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hocon/impl/simple_config_object.rb', line 137

def with_key_value(key, v)
  if v.nil?
    raise ConfigBugOrBrokenError.new("Trying to store null ConfigValue in a ConfigObject")
  end

  new_map = Hash.new
  if @value.empty?
    new_map[key] = v
  else
    new_map = @value.clone
    new_map[key] = v
  end
  self.class.new(origin, new_map, ResolveStatus.from_values(new_map.values), @ignores_fallbacks)
end

#with_only_key(key) ⇒ Object



48
49
50
# File 'lib/hocon/impl/simple_config_object.rb', line 48

def with_only_key(key)
  with_only_path(Path.new_key(key))
end

#with_only_path(path) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/hocon/impl/simple_config_object.rb', line 83

def with_only_path(path)
  o = with_only_path_or_nil(path)
  if o.nil?
    self.class.new(origin, {}, ResolveStatus::RESOLVED, @ignores_fallbacks)
  else
    o
  end
end

#with_only_path_or_nil(path) ⇒ Object

gets the object with only the path if the path exists, otherwise null if it doesn’t. this ensures that if we have { a : { b : 42 } } and do withOnlyPath(“a.b.c”) that we don’t keep an empty “a” object.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hocon/impl/simple_config_object.rb', line 61

def with_only_path_or_nil(path)
  key = path.first
  path_next = path.remainder
  v = value[key]

  if ! path_next.nil?
    if (!v.nil?) && (v.is_a?(Hocon::Impl::AbstractConfigObject))
      v = v.with_only_path_or_nil(path_next)
    else
      # if the path has more elements but we don't have an object,
      # then the rest of the path does not exist.
      v = nil
    end
  end

  if v.nil?
    nil
  else
    self.class.new(origin, {key => v}, v.resolve_status, @ignores_fallbacks)
  end
end

#with_value(path, v) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hocon/impl/simple_config_object.rb', line 119

def with_value(path, v)
  key = path.first
  remainder = path.remainder

  if remainder.nil?
    with_key_value(key, v)
  else
    child = @value[key]
    if (not child.nil?) && child.is_a?(Hocon::Impl::AbstractConfigObject)
      return with_key_value(key, child.with_value(remainder, v))
    else
      subtree = v.at_path_with_origin(
          SimpleConfigOrigin.new_simple("with_value(#{remainder.render})"), remainder)
      with_key_value(key, subtree.root)
    end
  end
end

#without_key(key) ⇒ Object



52
53
54
# File 'lib/hocon/impl/simple_config_object.rb', line 52

def without_key(key)
  without_path(Path.new_key(key))
end

#without_path(path) ⇒ Object



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
# File 'lib/hocon/impl/simple_config_object.rb', line 92

def without_path(path)
  key = path.first
  remainder = path.remainder
  v = @value[key]

  if (not v.nil?) && (not remainder.nil?) && v.is_a?(Hocon::Impl::AbstractConfigObject)
    v = v.without_path(remainder)
    updated = @value.clone
    updated[key] = v
    self.class.new(origin,
                   updated,
                   ResolveStatus.from_values(updated.values), @ignores_fallbacks)
  elsif (not remainder.nil?) || v.nil?
    return self
  else
    smaller = Hash.new
    @value.each do |old_key, old_value|
      unless old_key == key
        smaller[old_key] = old_value
      end
    end
    self.class.new(origin,
                   smaller,
                   ResolveStatus.from_values(smaller.values), @ignores_fallbacks)
  end
end