Class: Hocon::Impl::SimpleConfigOrigin

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/simple_config_origin.rb

Constant Summary collapse

OriginType =
Hocon::Impl::OriginType
MERGE_OF_PREFIX =
"merge of "

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, line_number, end_line_number, origin_type, url_or_nil, resource_or_nil, comments_or_nil) ⇒ SimpleConfigOrigin

Returns a new instance of SimpleConfigOrigin.



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

def initialize(description, line_number, end_line_number,
               origin_type, url_or_nil, resource_or_nil, comments_or_nil)
  if !description
    raise ArgumentError, "description may not be nil"
  end

  # HACK: naming this variable with an underscore, because the upstream library
  # has both a member var and a no-arg method by the name "description", and
  # I don't think Ruby can handle that.
  @_description = description
  @line_number = line_number
  @end_line_number = end_line_number
  @origin_type = origin_type

  # TODO: Currently ruby hocon does not support URLs, and so this variable
  # is not actually a URL/URI, but a string
  @url_or_nil = url_or_nil
  @resource_or_nil = resource_or_nil
  @comments_or_nil = comments_or_nil
end

Instance Attribute Details

#_descriptionObject (readonly)

Returns the value of attribute _description.



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

def _description
  @_description
end

#comments_or_nilObject (readonly)

Returns the value of attribute comments_or_nil.



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

def comments_or_nil
  @comments_or_nil
end

#end_line_numberObject (readonly)

Returns the value of attribute end_line_number.



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

def end_line_number
  @end_line_number
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#origin_typeObject (readonly)

Returns the value of attribute origin_type.



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

def origin_type
  @origin_type
end

#resource_or_nilObject (readonly)

Returns the value of attribute resource_or_nil.



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

def resource_or_nil
  @resource_or_nil
end

#url_or_nilObject (readonly)

Returns the value of attribute url_or_nil.



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

def url_or_nil
  @url_or_nil
end

Class Method Details

.merge_origins(stack) ⇒ Object

see also ‘merge_two_origins’ and ‘merge_three_origins’



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

def self.merge_origins(stack)
  # stack is an array of ConfigOrigin
  if stack.empty?
    raise Hocon::ConfigError::ConfigBugOrBrokenError, "can't merge empty list of origins"
  elsif stack.length == 1
    stack[0]
  elsif stack.length == 2
    merge_two(stack[0], stack[1])
  else
    remaining = []
    stack.each do |o|
      remaining << o
    end
    while remaining.size > 2
      c = remaining.last
      remaining.delete_at(remaining.size - 1)
      b = remaining.last
      remaining.delete_at(remaining.size - 1)
      a = remaining.last
      remaining.delete_at(remaining.size - 1)

      merged = merge_three(a, b, c)

      remaining << merged
    end

    # should be down to either 1 or 2
    self.merge_origins(remaining)
  end
end

.merge_three(a, b, c) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/hocon/impl/simple_config_origin.rb', line 317

def self.merge_three(a, b, c)
  if similarity(a, b) >= similarity(b, c)
    merge_two(merge_two(a, b), c)
  else
    merge_two(a, merge_two(b, c))
  end
end

.merge_two(a, b) ⇒ Object



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

def self.merge_two(a, b)
  merged_desc = nil
  merged_start_line = nil
  merged_end_line = nil
  merged_comments = nil

  merged_type =
      if a.origin_type == b.origin_type
        a.origin_type
      else
        Hocon::Impl::OriginType::GENERIC
      end

  # first use the "description" field which has no line numbers
  # cluttering it.
  a_desc = remove_merge_of_prefix(a._description)
  b_desc = remove_merge_of_prefix(b._description)

  if a_desc == b_desc
    merged_desc = a_desc
    if a.line_number < 0
      merged_start_line = b.line_number
    elsif b.line_number < 0
      merged_start_line = a.line_number
    else
      merged_start_line = [a.line_number, b.line_number].min
    end

    merged_end_line = [a.end_line_number, b.end_line_number].max
  else
    # this whole merge song-and-dance was intended to avoid this case
    # whenever possible, but we've lost. Now we have to lose some
    # structured information and cram into a string.
    #
    # description() method includes line numbers, so use it instead
    # of description field.
    a_full = remove_merge_of_prefix(a._description)
    b_full = remove_merge_of_prefix(b._description)

    merged_desc = "#{MERGE_OF_PREFIX}#{a_full},#{b_full}"
    merged_start_line = -1
    merged_end_line = -1
  end

  merged_url =
      if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(a.url_or_nil, b.url_or_nil)
        a.url_or_nil
      else
        nil
      end

  merged_resource =
      if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(a.resource_or_nil, b.resource_or_nil)
        a.resource_or_nil
      else
        nil
      end

  if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(a.comments_or_nil, b.comments_or_nil)
    merged_comments = a.comments_or_nil
  else
    merged_comments = []
    if a.comments_or_nil
      merged_comments.concat(a.comments_or_nil)
    end
    if b.comments_or_nil
      merged_comments.concat(b.comments_or_nil)
    end
  end

  Hocon::Impl::SimpleConfigOrigin.new(
      merged_desc, merged_start_line, merged_end_line,
      merged_type, merged_url, merged_resource, merged_comments)
end

.merge_two_origins(a, b) ⇒ Object



325
326
327
328
# File 'lib/hocon/impl/simple_config_origin.rb', line 325

def self.merge_two_origins(a, b)
  # a, b are ConfigOrigins
  merge_two(a, b)
end

.merge_value_origins(stack) ⇒ Object



330
331
332
333
334
# File 'lib/hocon/impl/simple_config_origin.rb', line 330

def self.merge_value_origins(stack)
  # stack is an array of AbstractConfigValue
  origins = stack.map { |v| v.origin}
  merge_origins(origins)
end

.new_file(file_path) ⇒ Object



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

def self.new_file(file_path)
  self.new(file_path, -1, -1,
           OriginType::FILE,
           file_path, nil, nil)
end

.new_resource(resource, url = nil) ⇒ Object

NOTE: not porting ‘new_url` because we’re not going to support URLs for now



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hocon/impl/simple_config_origin.rb', line 52

def self.new_resource(resource, url = nil)
  desc = nil
  if ! url.nil?
    desc = resource + " @ " + url.to_external_form
  else
    desc = resource
  end
  Hocon::Impl::SimpleConfigOrigin.new(desc, -1, -1, OriginType::RESOURCE,
                         url.nil? ? nil : url.to_external_form,
                         resource, nil)
end

.new_simple(description) ⇒ Object



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

def self.new_simple(description)
  self.new(description, -1, -1,
           OriginType::GENERIC,
           nil, nil, nil)
end

.remove_merge_of_prefix(desc) ⇒ Object



204
205
206
207
208
209
# File 'lib/hocon/impl/simple_config_origin.rb', line 204

def self.remove_merge_of_prefix(desc)
  if desc.start_with?(MERGE_OF_PREFIX)
    desc = desc[MERGE_OF_PREFIX.length, desc.length - 1]
  end
  desc
end

.similarity(a, b) ⇒ Object



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_origin.rb', line 286

def self.similarity(a, b)
  count = 0
  if a.origin_type == b.origin_type
    count += 1
  end

  if a._description == b._description
    count += 1

    # only count these if the description field (which is the file
    # or resource name) also matches.
    if a.line_number == b.line_number
      count += 1
    end

    if a.end_line_number == b.end_line_number
      count += 1
    end

    if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(a.url_or_nil, b.url_or_nil)
      count += 1
    end

    if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(a.resource_or_nil, b.resource_or_nil)
      count += 1
    end
  end

  count
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hocon/impl/simple_config_origin.rb', line 123

def ==(other)
  if other.is_a? Hocon::Impl::SimpleConfigOrigin
    @_description == other._description &&
        @line_number == other.line_number &&
        @end_line_number == other.end_line_number &&
        @origin_type == other.origin_type &&
        Hocon::Impl::ConfigImplUtil.equals_handling_nil?(@url_or_nil, other.url_or_nil) &&
        Hocon::Impl::ConfigImplUtil.equals_handling_nil?(@resource_or_nil, other.resource_or_nil)
  else
    false
  end
end

#add_url(url) ⇒ Object



75
76
77
78
79
# File 'lib/hocon/impl/simple_config_origin.rb', line 75

def add_url(url)
  SimpleConfigOrigin.new(@_description, line_number, end_line_number, origin_type,
                         url.nil? ? nil : url.to_s, resource_or_nil,
                         comments_or_nil)
end

#append_comments(comments) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/hocon/impl/simple_config_origin.rb', line 102

def append_comments(comments)
  if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(comments, @comments_or_nil)
    self
  elsif comments_or_nil.nil?
    with_comments(comments)
  else
    merged = comments_or_nil + comments
    with_comments(merged)
  end
end

#commentsObject



198
199
200
# File 'lib/hocon/impl/simple_config_origin.rb', line 198

def comments
  @comments_or_nil || []
end

#descriptionObject



113
114
115
116
117
118
119
120
121
# File 'lib/hocon/impl/simple_config_origin.rb', line 113

def description
  if @line_number < 0
    _description
  elsif end_line_number == line_number
    "#{_description}: #{line_number}"
  else
    "#{_description}: #{line_number}-#{end_line_number}"
  end
end

#filenameObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hocon/impl/simple_config_origin.rb', line 157

def filename
  # TODO because we don't support URLs, this function's URL handling
  # is completely pointless. It will only return _description (a string that
  # is the file path) or nil.
  # It should be cleaned up
  if origin_type == OriginType::FILE
    _description
  elsif ! url_or_nil.nil?
    url = nil
    begin
      url = Hocon::Impl::Url.new(url_or_nil)
    rescue Hocon::Impl::Url::MalformedUrlError => e
      return nil
    end

    if url.get_protocol == "file"
      url.get_file
    else
      nil
    end
  else
    nil
  end
end

#hashObject



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

def hash
  h = 41 * (41 + @_description.hash)
  h = 41 * (h + @line_number)
  h = 41 * (h + @end_line_number)
  h = 41 * (h + @origin_type.hash)

  unless @url_or_nil.nil?
    h = 41 * (h + @url_or_nil.hash)
  end

  unless @resource_or_nil.nil?
    h = 41 * (h + @resource_or_nil.hash)
  end

  h
end

#prepend_comments(comments) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/hocon/impl/simple_config_origin.rb', line 91

def prepend_comments(comments)
  if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(comments, @comments_or_nil)
    self
  elsif @comments_or_nil.nil?
    with_comments(comments)
  else
    merged = comments + @comments_or_nil
    with_comments(merged)
  end
end

#resourceObject



194
195
196
# File 'lib/hocon/impl/simple_config_origin.rb', line 194

def resource
  resource_or_nil
end

#to_sObject



153
154
155
# File 'lib/hocon/impl/simple_config_origin.rb', line 153

def to_s
  "ConfigOrigin(#{_description})"
end

#urlObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hocon/impl/simple_config_origin.rb', line 182

def url
  if url_or_nil.nil?
    nil
  else
    begin
      Hocon::Impl::Url.new(url_or_nil)
    rescue Hocon::Impl::Url::MalformedUrlError => e
      nil
    end
  end
end

#with_comments(comments) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/hocon/impl/simple_config_origin.rb', line 81

def with_comments(comments)
  if Hocon::Impl::ConfigImplUtil.equals_handling_nil?(comments, @comments_or_nil)
    self
  else
    Hocon::Impl::SimpleConfigOrigin.new(
        @_description, @line_number, @end_line_number,
        @origin_type, @url_or_nil, @resource_or_nil, comments)
  end
end

#with_line_number(line_number) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/hocon/impl/simple_config_origin.rb', line 64

def with_line_number(line_number)
  if (line_number == @line_number) and
      (line_number == @end_line_number)
    self
  else
    Hocon::Impl::SimpleConfigOrigin.new(
        @_description, line_number, line_number,
        @origin_type, @url_or_nil, @resource_or_nil, @comments_or_nil)
  end
end