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 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
# encoding: utf-8
require_relative '../../hocon/impl'
require_relative '../../hocon/impl/simple_config_origin'
require_relative '../../hocon/impl/abstract_config_object'
require_relative '../../hocon/impl/resolve_status'
require_relative '../../hocon/impl/resolve_result'
require_relative '../../hocon/impl/path'
require_relative '../../hocon/config_error'
require 'set'
require 'forwardable'
class Hocon::Impl::SimpleConfigObject
include Hocon::Impl::AbstractConfigObject
extend Forwardable
ConfigBugOrBrokenError = Hocon::ConfigError::ConfigBugOrBrokenError
ResolveStatus = Hocon::Impl::ResolveStatus
ResolveResult = Hocon::Impl::ResolveResult
SimpleConfigOrigin = Hocon::Impl::SimpleConfigOrigin
Path = Hocon::Impl::Path
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
attr_reader :value
# To support accessing ConfigObjects like a hash
def_delegators :@value, :[], :has_key?, :has_value?, :empty?, :size, :keys, :values, :each, :map
def with_only_key(key)
with_only_path(Path.new_key(key))
end
def without_key(key)
without_path(Path.new_key(key))
end
# 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.
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
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
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
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
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
def attempt_peek_with_partial_resolve(key)
@value[key]
end
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
def with_fallbacks_ignored()
if @ignores_fallbacks
self
else
new_copy_with_status(resolve_status, origin, true)
end
end
def resolve_status
ResolveStatus.from_boolean(@resolved)
end
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
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
def ignores_fallbacks?
@ignores_fallbacks
end
def unwrapped
m = {}
@value.each do |k,v|
m[k] = v.unwrapped
end
m
end
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
def modify(modifier)
begin
modify_may_throw(modifier)
rescue Hocon::ConfigError => e
raise e
end
end
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
class ResolveModifier
attr_accessor :context
attr_reader :source
def initialize(context, source)
@context = context
@source = source
@original_restrict = context.restrict_to_child
end
def modify_child_may_throw(key, v)
if @context.is_restricted_to_child
if key == @context.restrict_to_child.first
remainder = @context.restrict_to_child.remainder
if remainder != nil
result = @context.restrict(remainder).resolve(v, @source)
@context = result.context.unrestricted.restrict(@original_restrict)
return result.value
else
# we don't want to resolve the leaf child
return v
end
else
# not in the restrictToChild path
return v
end
else
# no restrictToChild, resolve everything
result = @context.unrestricted.resolve(v, @source)
@context = result.context.unrestricted.restrict(@original_restrict)
result.value
end
end
end
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
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
class RenderComparator
def self.all_digits?(s)
s =~ /^\d+$/
end
# This is supposed to sort numbers before strings,
# and sort the numbers numerically. The point is
# to make objects which are really list-like
# (numeric indices) appear in order.
def self.sort(arr)
arr.sort do |a, b|
a_digits = all_digits?(a)
b_digits = all_digits?(b)
if a_digits && b_digits
Integer(a) <=> Integer(b)
elsif a_digits
-1
elsif b_digits
1
else
a <=> b
end
end
end
end
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
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
def get(key)
@value[key]
end
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
def can_equal(other)
other.is_a? Hocon::ConfigObject
end
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
def hash
self.class.map_hash(@value)
end
def contains_key?(key)
@value.has_key?(key)
end
def key_set
Set.new(@value.keys)
end
def contains_value?(v)
@value.has_value?(v)
end
def self.empty(origin = nil)
if origin.nil?
empty(Hocon::Impl::SimpleConfigOrigin.new_simple("empty config"))
else
self.new(origin, {})
end
end
def self.empty_missing(base_origin)
self.new(
Hocon::Impl::SimpleConfigOrigin.new_simple("#{base_origin.description} (not found)"),
{})
end
end
|