File: simple_config_list.rb

package info (click to toggle)
ruby-hocon 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ruby: 7,903; makefile: 4
file content (349 lines) | stat: -rw-r--r-- 8,494 bytes parent folder | download
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
# encoding: utf-8

require_relative '../../hocon/impl'
require_relative '../../hocon/impl/resolve_status'
require_relative '../../hocon/config_value_type'
require_relative '../../hocon/config_error'
require_relative '../../hocon/impl/abstract_config_object'
require 'forwardable'
require_relative '../../hocon/impl/unsupported_operation_error'
require_relative '../../hocon/impl/resolve_result'
require_relative '../../hocon/impl/container'
require_relative '../../hocon/config_list'

class Hocon::Impl::SimpleConfigList
  include Hocon::Impl::Container
  include Hocon::ConfigList
  include Hocon::Impl::AbstractConfigValue
  extend Forwardable

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

  def initialize(origin, value, status = ResolveStatus.from_values(value))
    super(origin)
    @value = value
    @resolved = (status == ResolveStatus::RESOLVED)

    # kind of an expensive debug check (makes this constructor pointless)
    if status != ResolveStatus.from_values(value)
      raise ConfigBugOrBrokenError, "SimpleConfigList created with wrong resolve status: #{self}"
    end
  end

  attr_reader :value

  def_delegators :@value, :[], :include?, :empty?, :size, :index, :rindex, :each, :map

  def value_type
    Hocon::ConfigValueType::LIST
  end

  def unwrapped
    @value.map { |v| v.unwrapped }
  end

  def resolve_status
    ResolveStatus.from_boolean(@resolved)
  end

  def replace_child(child, replacement)
    new_list = replace_child_in_list(@value, child, replacement)
    if new_list.nil?
      nil
    else
      # we use the constructor flavor that will recompute the resolve status
      SimpleConfigList.new(origin, new_list)
    end
  end

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

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

  def modify_may_throw(modifier, new_resolve_status)
    # lazy-create for optimization
    changed = nil
    i = 0
    @value.each { |v|
      modified = modifier.modify_child_may_throw(nil, v)

      # lazy-create the new list if required
      if changed == nil && !modified.equal?(v)
        changed = []
        j = 0
        while j < i
          changed << @value[j]
          j += 1
        end
      end

      # once the new list is created, all elements
      # have to go in it.if modifyChild returned
      # null, we drop that element.
      if changed != nil && modified != nil
        changed << modified
      end

      i += 1
    }

    if changed != nil
      if new_resolve_status != nil
        self.class.new(origin, changed, new_resolve_status)
      else
        self.class.new(origin, changed)
      end
    else
      self
    end
  end

  class ResolveModifier
    attr_reader :context, :source
    def initialize(context, source)
      @context = context
      @source = source
    end

    def modify_child_may_throw(key, v)
      result = @context.resolve(v, source)
      @context = result.context
      result.value
    end
  end

  def resolve_substitutions(context, source)
    if @resolved
      return Hocon::Impl::ResolveResult.make(context, self)
    end

    if context.is_restricted_to_child
      # if a list restricts to a child path, then it has no child paths,
      # so nothing to do.
      Hocon::Impl::ResolveResult.make(context, self)
    else
      begin
        modifier = ResolveModifier.new(context, source.push_parent(self))
        value = modify_may_throw(modifier, context.options.allow_unresolved ? nil : ResolveStatus::RESOLVED)
        Hocon::Impl::ResolveResult.make(modifier.context, value)
      rescue NotPossibleToResolve => e
        raise e
      rescue RuntimeError => e
        raise e
      rescue Exception => e
        raise ConfigBugOrBrokenError.new("unexpected exception", e)
      end
    end
  end

  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, resolve_status)
  end

  def can_equal(other)
    other.is_a?(self.class)
  end

  def ==(other)
    # note that "origin" is deliberately NOT part of equality
    if other.is_a?(self.class)
      # optimization to avoid unwrapped() for two ConfigList
      can_equal(other) &&
          (value.equal?(other.value) || (value == other.value))
    else
      false
    end
  end

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

  def render_value_to_sb(sb, indent_size, at_root, options)
    if @value.empty?
      sb << "[]"
    else
      sb << "["
      if options.formatted?
        sb << "\n"
      end
      @value.each do |v|
        if options.origin_comments?
          lines = v.origin.description.split("\n")
          lines.each do |l|
            Hocon::Impl::AbstractConfigValue.indent(sb, indent_size + 1, options)
            sb << "# "
            sb << l
            sb << "\n"
          end
        end
        if options.comments?
          v.origin.comments.each do |comment|
            sb << "# "
            sb << comment
            sb << "\n"
          end
        end
        Hocon::Impl::AbstractConfigValue.indent(sb, indent_size + 1, options)

        v.render_value_to_sb(sb, indent_size + 1, at_root, options)
        sb << ","
        if options.formatted?
          sb << "\n"
        end
      end

      # 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 - 1 # chop or newline
      if options.formatted?
        sb.pos = sb.pos - 1 # also chop comma
        sb << "\n"
        Hocon::Impl::AbstractConfigValue.indent(sb, indent_size, options)
      end
      sb << "]"
    end
  end

  def contains?(o)
    value.include?(o)
  end

  def include_all?(value_list)
    value_list.all? { |v| @value.include?(v)}
  end

  def contains_all?(c)
    include_all?(c)
  end

  def get(index)
    value[index]
  end

  def index_of(o)
    value.index(o)
  end

  def is_empty
    empty?
  end

  # Skipping upstream definition of "iterator", because that's not really a thing
  # in Ruby.

  def last_index_of(o)
    value.rindex(o)
  end

  # skipping upstream definitions of "wrapListIterator", "listIterator", and
  # "listIterator(int)", because those don't really apply in Ruby.

  def sub_list(from_index, to_index)
    value[from_index..to_index]
  end

  def to_array
    value
  end

  def we_are_immutable(method)
    Hocon::Impl::UnsupportedOperationError.new("ConfigList is immutable, you can't call List. '#{method}'")
  end

  def add(e)
    raise we_are_immutable("add")
  end

  def add_at(index, element)
    raise we_are_immutable("add_at")
  end

  def add_all(c)
    raise we_are_immutable("add_all")
  end

  def add_all_at(index, c)
    raise we_are_immutable("add_all_at")
  end

  def clear
    raise we_are_immutable("clear")
  end

  def remove(o)
    raise we_are_immutable("remove")
  end

  def remove_at(i)
    raise we_are_immutable("remove_at")
  end

  def delete(o)
    raise we_are_immutable("delete")
  end

  def remove_all(c)
    raise we_are_immutable("remove_all")
  end

  def retain_all(c)
    raise we_are_immutable("retain_all")
  end

  def set(index, element)
    raise we_are_immutable("set")
  end

  def []=(index, element)
    raise we_are_immutable("[]=")
  end

  def push(e)
    raise we_are_immutable("push")
  end

  def <<(e)
    raise we_are_immutable("<<")
  end

  def new_copy(origin)
    Hocon::Impl::SimpleConfigList.new(origin, @value)
  end

  def concatenate(other)
    combined_origin = Hocon::Impl::SimpleConfigOrigin.merge_two_origins(origin, other.origin)
    combined = value + other.value
    Hocon::Impl::SimpleConfigList.new(combined_origin, combined)
  end

  # Skipping upstream "writeReplace" until we see that we need it for something

  def with_origin(origin)
    super(origin)
  end

end