class Hocon::Impl::SimpleIncluder

Constants

ConfigBugorBrokenError
ConfigIOError
SimpleConfigObject
SimpleConfigOrigin

Public Class Methods

clear_for_include(options) click to toggle source

ConfigIncludeContext does this for us on its options

# File lib/hocon/impl/simple_includer.rb, line 29
def self.clear_for_include(options)
  # the class loader and includer are inherited, but not this other stuff
  options.set_syntax(nil).set_origin_description(nil).set_allow_missing(true)
end
from_basename(source, name, options) click to toggle source

this function is a little tricky because there are three places we’re trying to use it; for ‘include “basename”’ in a .conf file, for loading app.{conf,json,properties} from classpath, and for loading app.{conf,json,properties} from the filesystem.

# File lib/hocon/impl/simple_includer.rb, line 129
def self.from_basename(source, name, options)
  obj = nil
  if name.end_with?(".conf") || name.end_with?(".json") || name.end_with?(".properties")
    p = source.name_to_parseable(name, options)

    obj = p.parse(p.options.set_allow_missing(options.allow_missing?))
  else
    conf_handle = source.name_to_parseable(name + ".conf", options)
    json_handle = source.name_to_parseable(name + ".json", options)
    got_something = false
    fails = []

    syntax = options.syntax

    obj = SimpleConfigObject.empty(SimpleConfigOrigin.new_simple(name))
    if syntax.nil? || (syntax == Hocon::ConfigSyntax::CONF)
      begin
        obj = conf_handle.parse(conf_handle.options.set_allow_missing(false).
                set_syntax(Hocon::ConfigSyntax::CONF))
        got_something = true
      rescue ConfigIOError => e
        fails << e
      end
    end

    if syntax.nil? || (syntax == Hocon::ConfigSyntax::JSON)
      begin
        parsed = json_handle.parse(json_handle.options.set_allow_missing(false).
                                       set_syntax(Hocon::ConfigSyntax::JSON))
        obj = obj.with_fallback(parsed)
        got_something = true
      rescue ConfigIOError => e
        fails << e
      end
    end

    # NOTE: skipping the upstream block here that would attempt to parse
    # a java properties file.

    if (! options.allow_missing?) && (! got_something)
      if Hocon::Impl::ConfigImpl.trace_loads_enabled
        # the individual exceptions should have been logged already
        # with tracing enabled
        Hocon::Impl::ConfigImpl.trace("Did not find '#{name}'" +
          " with any extension (.conf, .json, .properties); " +
          "exceptions should have been logged above.")
      end

      if fails.empty?
        # this should not happen
        raise ConfigBugOrBrokenError, "should not be reached: nothing found but no exceptions thrown"
      else
        sb = StringIO.new
        fails.each do |t|
          sb << t
          sb << ", "
        end
        raise ConfigIOError.new(SimpleConfigOrigin.new_simple(name), sb.string, fails[0])
      end
    elsif !got_something
      if Hocon::Impl::ConfigImpl.trace_loads_enabled
        Hocon::Impl::ConfigImpl.trace("Did not find '#{name}'" +
          " with any extension (.conf, .json, .properties); but '#{name}'" +
          " is allowed to be missing. Exceptions from load attempts should have been logged above.")
      end
    end
  end

  obj
end
include_file_without_fallback(context, file) click to toggle source
# File lib/hocon/impl/simple_includer.rb, line 81
def self.include_file_without_fallback(context, file)
  Hocon::ConfigFactory.parse_file_any_syntax(file, context.parse_options).root
end
include_without_fallback(context, name) click to toggle source

the heuristic includer in static form

# File lib/hocon/impl/simple_includer.rb, line 48
def self.include_without_fallback(context, name)
  # the heuristic is valid URL then URL, else relative to including file;
  # relativeTo in a file falls back to classpath inside relativeTo().

  url = nil
  begin
    url = Hocon::Impl::Url.new(name)
  rescue Hocon::Impl::Url::MalformedUrlError => e
    url = nil
  end

  if !(url.nil?)
    include_url_without_fallback(context, url)
  else
    source = RelativeNameSource.new(context)
    from_basename(source, name, context.parse_options)
  end
end
make_full(includer) click to toggle source
# File lib/hocon/impl/simple_includer.rb, line 207
def self.make_full(includer)
  if includer.is_a?(Hocon::Impl::FullIncluder)
    includer
  else
    Proxy.new(includer)
  end
end
new(fallback) click to toggle source
# File lib/hocon/impl/simple_includer.rb, line 24
def initialize(fallback)
  @fallback = fallback
end

Public Instance Methods

include(context, name) click to toggle source

this is the heuristic includer

# File lib/hocon/impl/simple_includer.rb, line 36
def include(context, name)
  obj = self.class.include_without_fallback(context, name)

  # now use the fallback includer if any and merge its result
  if ! (@fallback.nil?)
    obj.with_fallback(@fallback.include(context, name))
  else
    obj
  end
end
include_file(context, file) click to toggle source

NOTE: not porting ‘include_url` or `include_url_without_fallback` from upstream,

because we probably won't support URL includes for now.
# File lib/hocon/impl/simple_includer.rb, line 70
def include_file(context, file)
  obj = self.class.include_file_without_fallback(context, file)

  # now use the fallback includer if any and merge its result
  if (!@fallback.nil?) && @fallback.is_a?(Hocon::ConfigIncluderFile)
    obj.with_fallback(@fallback).include_file(context, file)
  else
    obj
  end
end
with_fallback(fallback) click to toggle source

NOTE: not porting ‘include_resources` or `include_resources_without_fallback` for now because we’re not going to support looking for things on the ruby load path for now.

# File lib/hocon/impl/simple_includer.rb, line 89
def with_fallback(fallback)
  if self.equal?(fallback)
    raise ConfigBugOrBrokenError, "trying to create includer cycle"
  elsif @fallback.equal?(fallback)
    self
  elsif @fallback.nil?
    self.class.new(@fallback.with_fallback(fallback))
  else
    self.class.new(fallback)
  end
end