Class: Fluent::Config::YamlParser::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/config/yaml_parser/loader.rb

Defined Under Namespace

Classes: Visitor

Constant Summary collapse

INCLUDE_TAG =
'tag:include'.freeze
FLUENT_JSON_TAG =
'tag:fluent/json'.freeze
FLUENT_STR_TAG =
'tag:fluent/s'.freeze
SHOVEL =
'<<'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(context = Kernel.binding, on_file_parsed: nil) ⇒ Loader

Returns a new instance of Loader.



33
34
35
36
37
# File 'lib/fluent/config/yaml_parser/loader.rb', line 33

def initialize(context = Kernel.binding, on_file_parsed: nil)
  @context = context
  @current_path = nil
  @on_file_parsed = on_file_parsed
end

Instance Method Details

#eval_include(path, parent) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/config/yaml_parser/loader.rb', line 68

def eval_include(path, parent)
  if path.relative?
    pattern = parent.join(path)
  else
    pattern = path
  end
  result = []
  Dir.glob(pattern).sort.each do |path|
    result.concat(load(Pathname.new(path)))
  end
  result
rescue SystemCallError => e
  parse_error = ConfigParseError.new("include error #{path} - #{e}")
  parse_error.set_backtrace(e.backtrace)
  raise parse_error
end

#load(path) ⇒ Hash

Parameters:

  • path (String)

Returns:

  • (Hash)


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
# File 'lib/fluent/config/yaml_parser/loader.rb', line 41

def load(path)
  class_loader = Psych::ClassLoader.new
  scanner = Psych::ScalarScanner.new(class_loader)

  visitor = Visitor.new(scanner, class_loader)

  visitor._register_domain(INCLUDE_TAG) do |_, val|
    eval_include(Pathname.new(val), path.parent)
  end

  visitor._register_domain(FLUENT_JSON_TAG) do |_, val|
    Fluent::Config::YamlParser::FluentValue::JsonValue.new(val)
  end

  visitor._register_domain(FLUENT_STR_TAG) do |_, val|
    Fluent::Config::YamlParser::FluentValue::StringValue.new(val, @context)
  end

  config = path.open do |f|
    visitor.accept(Psych.parse(f))
  end

  @on_file_parsed&.call(File.expand_path(path.to_s))

  config
end