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) ⇒ Loader

Returns a new instance of Loader.



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

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

Instance Method Details

#eval_include(path, parent) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/config/yaml_parser/loader.rb', line 63

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)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/config/yaml_parser/loader.rb', line 40

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

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