Class: Hocon::Impl::ConfigDelayedMergeObject

Inherits:
Object
  • Object
show all
Includes:
AbstractConfigObject, ReplaceableMergeStack, Unmergeable
Defined in:
lib/hocon/impl/config_delayed_merge_object.rb

Overview

This is just like ConfigDelayedMerge except we know statically that it will turn out to be an object.

Constant Summary

Constants included from AbstractConfigObject

AbstractConfigObject::ConfigBugOrBrokenError, AbstractConfigObject::ConfigNotResolvedError

Constants included from AbstractConfigValue

AbstractConfigValue::ConfigBugOrBrokenError, AbstractConfigValue::ConfigImplUtil, AbstractConfigValue::ResolveStatus

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_with_status, #peek_assuming_resolved, #peek_path, #peek_path_from_obj, #putAll, #remove, #render_value_to_sb, #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, #render, #render_value_to_sb, replace_child_in_list, #require_not_ignoring_fallbacks, #to_fallback_value, #to_s, #transform_to_string, #with_fallback, #with_fallbacks_ignored, #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

#get, #to_config, #with_fallback, #with_origin

Constructor Details

#initialize(origin, stack) ⇒ ConfigDelayedMergeObject

Returns a new instance of ConfigDelayedMergeObject.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 12

def initialize(origin, stack)
  super(origin)

  @stack = stack

  if stack.empty?
    raise Hocon::ConfigError::ConfigBugOrBrokenError.new("creating empty delayed merge value", nil)
  end

  if !@stack[0].is_a? Hocon::Impl::AbstractConfigObject
    error_message = "created a delayed merge object not guaranteed to be an object"
    raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
  end

  stack.each do |v|
    if v.is_a?(Hocon::Impl::ConfigDelayedMergeObject) || v.is_a?(Hocon::Impl::ConfigDelayedMergeObject)
      error_message = "placed nested DelayedMerge in a ConfigDelayedMerge, should have consolidated stack"
      raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
    end
  end
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



34
35
36
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 34

def stack
  @stack
end

Class Method Details

.not_resolvedObject



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

def self.not_resolved
  error_message = "need to Config#resolve() before using this object, see the API docs for Config#resolve()"
  Hocon::ConfigError::ConfigNotResolvedError.new(error_message, nil)
end

.unmergeable?(object) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 192

def self.unmergeable?(object)
  # Ruby note: This is the best way I could find to simulate
  # else if (layer instanceof Unmergeable) in java since we're including
  # the Unmergeable module instead of extending an Unmergeable class
  object.class.included_modules.include?(Hocon::Impl::Unmergeable)
end

Instance Method Details

#==(other) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 133

def ==(other)
  # note that "origin" is deliberately NOT part of equality
  if other.is_a? Hocon::Impl::ConfigDelayedMergeObject
    can_equal(other) && (@stack == other.stack || @stack.equal?(other.stack))
  else
    false
  end
end

#[](key) ⇒ Object



160
161
162
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 160

def [](key)
  raise self.class.not_resolved
end

#attempt_peek_with_partial_resolve(key) ⇒ Object



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

def attempt_peek_with_partial_resolve(key)
  # a partial resolve of a ConfigDelayedMergeObject always results in a
  # SimpleConfigObject because all the substitutions in the stack get
  # resolved in order to look up the partial.
  # So we know here that we have not been resolved at all even
  # partially.
  # Given that, all this code is probably gratuitous, since the app code
  # is likely broken. But in general we only throw NotResolved if you try
  # to touch the exact key that isn't resolved, so this is in that
  # spirit.

  # we'll be able to return a key if we have a value that ignores
  # fallbacks, prior to any unmergeable values.
  @stack.each do |layer|
    if layer.is_a?(Hocon::Impl::AbstractConfigObject)
      v = layer.attempt_peek_with_partial_resolve(key)

      if !v.nil?
        if v.ignores_fallbacks?
          # we know we won't need to merge anything in to this
          # value
          return v
        else
          # we can't return this value because we know there are
          # unmergeable values later in the stack that may
          # contain values that need to be merged with this
          # value. we'll throw the exception when we get to those
          # unmergeable values, so continue here.
          next
        end
      elsif self.class.unmergeable?(layer)
        error_message = "should not be reached: unmergeable object returned null value"
        raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
      else
        # a non-unmergeable AbstractConfigObject that returned null
        # for the key in question is not relevant, we can keep
        # looking for a value.
        next
      end
    elsif self.class.unmergeable?(layer)
      error_message = "Key '#{key}' is not available at '#{origin.description}'" +
          "because value at '#{layer.origin.description}' has not been resolved" +
          " and may turn out to contain or hide '#{key}'. Be sure to Config#resolve()" +
          " before using a config object"
      raise Hocon::ConfigError::ConfigNotResolvedError.new(error_message, nil)
    elsif layer.resolved_status == ResolveStatus::UNRESOLVED
      # if the layer is not an object, and not a substitution or
      # merge,
      # then it's something that's unresolved because it _contains_
      # an unresolved object... i.e. it's an array
      if !layer.is_a?(Hocon::Impl::ConfigList)
        error_message = "Expecting a list here, not #{layer}"
        raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
      end
      return nil
    else
      # non-object, but resolved, like an integer or something.
      # has no children so the one we're after won't be in it.
      # we would only have this in the stack in case something
      # else "looks back" to it due to a cycle.
      # anyway at this point we know we can't find the key anymore.
      if !layer.ignores_fallbacks?
        error_message = "resolved non-object should ignore fallbacks"
        raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
      end
      return nil
    end
  end

  # If we get here, then we never found anything unresolved which means
  # the ConfigDelayedMergeObject should not have existed. some
  # invariant was violated.
  error_message = "Delayed merge stack does not contain any unmergeable values"
  raise Hocon::ConfigError::ConfigBugOrBrokenError.new(error_message, nil)
end

#can_equal(other) ⇒ Object



129
130
131
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 129

def can_equal(other)
  other.is_a? Hocon::Impl::ConfigDelayedMergeObject
end

#eachObject



172
173
174
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 172

def each
  raise self.class.not_resolved
end

#empty?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 176

def empty?
  raise self.class.not_resolved
end

#has_descendant?(descendant) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 66

def has_descendant?(descendant)
  Hocon::Impl::AbstractConfigValue.has_descendant_in_list?(@stack, descendant)
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 164

def has_key?(key)
  raise self.class.not_resolved
end

#has_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_value?(value)
  raise self.class.not_resolved
end

#hashObject



142
143
144
145
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 142

def hash
  # note that "origin" is deliberately NOT part of equality
  @stack.hash
end

#ignores_fallbacks?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 78

def ignores_fallbacks?
  Hocon::Impl::ConfigDelayedMerge.stack_ignores_fallbacks?(@stack)
end

#keysObject



180
181
182
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 180

def keys
  raise self.class.not_resolved
end

#make_replacement(context, skipping) ⇒ Object



49
50
51
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 49

def make_replacement(context, skipping)
  Hocon::Impl::ConfigDelayedMerge.make_replacement(context, @stack, skipping)
end

#merged_with_non_object(fallback) ⇒ Object



92
93
94
95
96
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 92

def merged_with_non_object(fallback)
  require_not_ignoring_fallbacks

  merged_stack_with_non_object(@stack, fallback)
end

#merged_with_object(fallback) ⇒ Object



88
89
90
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 88

def merged_with_object(fallback)
  merged_with_non_object(fallback)
end

#merged_with_the_unmergeable(fallback) ⇒ Object



82
83
84
85
86
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 82

def merged_with_the_unmergeable(fallback)
  require_not_ignoring_fallbacks

  merged_stack_with_the_unmergeable(@stack, fallback)
end

#new_copy(status, origin) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 36

def new_copy(status, origin)
  if status != resolve_status
    raise Hocon::ConfigError::ConfigBugOrBrokenError.new(
              "attempt to create resolved ConfigDelayedMergeObject")
  end
  Hocon::Impl::ConfigDelayedMergeObject.new(origin, @stack)
end

#relativized(prefix) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 70

def relativized(prefix)
  new_stack = []
  @stack.each { |o|
    new_stack << o.relativized(prefix)
  }
  self.class.new(origin, new_stack)
end

#render_to_sb(sb, indent, at_root, at_key, options) ⇒ Object



147
148
149
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 147

def render_to_sb(sb, indent, at_root, at_key, options)
  Hocon::Impl::ConfigDelayedMerge.render_value_to_sb_from_stack(@stack, sb, indent, at_root, at_key, options)
end

#replace_child(child, replacement) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 57

def replace_child(child, replacement)
  new_stack = Hocon::Impl::AbstractConfigValue.replace_child_in_list(@stack, child, replacement)
  if new_stack == nil
    nil
  else
    self.class.new(origin, new_stack)
  end
end

#resolve_statusObject



53
54
55
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 53

def resolve_status
  Hocon::Impl::ResolveStatus::UNRESOLVED
end

#resolve_substitutions(context, source) ⇒ Object



44
45
46
47
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 44

def resolve_substitutions(context, source)
  merged = Hocon::Impl::ConfigDelayedMerge.resolve_substitutions(self, @stack, context, source)
  merged.as_object_result
end

#sizeObject



188
189
190
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 188

def size
  raise self.class.not_resolved
end

#unmerged_valuesObject



125
126
127
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 125

def unmerged_values
  @stack
end

#unwrappedObject



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

def unwrapped
  raise self.class.not_resolved
end

#valuesObject



184
185
186
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 184

def values
  raise self.class.not_resolved
end

#with_only_key(key) ⇒ Object

No implementation of withFallback here, just use the implementation in the super-class



101
102
103
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 101

def with_only_key(key)
  raise self.class.not_resolved
end

#with_only_path(key) ⇒ Object



113
114
115
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 113

def with_only_path(key)
  raise self.class.not_resolved
end

#with_only_path_or_nil(key) ⇒ Object



109
110
111
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 109

def with_only_path_or_nil(key)
  raise self.class.not_resolved
end

#with_value(key_or_path, value = nil) ⇒ Object



121
122
123
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 121

def with_value(key_or_path, value = nil)
  raise self.class.not_resolved
end

#without_key(key) ⇒ Object



105
106
107
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 105

def without_key(key)
  raise self.class.not_resolved
end

#without_path(key) ⇒ Object



117
118
119
# File 'lib/hocon/impl/config_delayed_merge_object.rb', line 117

def without_path(key)
  raise self.class.not_resolved
end