Skip to content

Instantly share code, notes, and snippets.

@sonots
Created November 21, 2014 15:52
Show Gist options
  • Save sonots/9c802b9e5867a9e96d10 to your computer and use it in GitHub Desktop.
Save sonots/9c802b9e5867a9e96d10 to your computer and use it in GitHub Desktop.
ただのメモ。fluentd v1 config の array, hash 処理は config_param でやればいいのでは? => 改行を扱いたいのであった

https://github.com/fluent/fluentd/blob/8c7bc1c2fb3165b52179e5b9a430c6338d8e53e4/lib/fluent/config/literal_parser.rb#L56-L59

      def parse_literal(string_boundary_charset = LINE_END)
        spacing_without_comment

        value = if skip(/\[/)
                  scan_json(true)
                elsif skip(/\{/)
                  scan_json(false)
                else
                  scan_string(string_boundary_charset)
                end
        value
      end

ここで [ と { を予約語にしてしまっているが、config_param でブロック評価するときに :hash または :array 型だったら、JSON.parse するようにすれば、予約語から外せるのでは?

https://github.com/fluent/fluentd/blob/8c7bc1c2fb3165b52179e5b9a430c6338d8e53e4/lib/fluent/config/section.rb#L104-L114

        proxy.params.each_pair do |name, defval|
          varname = name.to_sym
          block, opts = defval
          if conf.has_key?(name.to_s) || opts[:alias] && conf.has_key?(opts[:alias].to_s)
            val = if conf.has_key?(name.to_s)
                    conf[name.to_s]
                  else
                    conf[opts[:alias].to_s]
                  end
            section_params[varname] = self.instance_exec(val, opts, name, &block)
          end

config_param のブロック評価は、Engine#configure から Plugin の configure の呼び出しがされて、super を辿ってこの辺に来る。

https://github.com/fluent/fluentd/blob/8c7bc1c2fb3165b52179e5b9a430c6338d8e53e4/lib/fluent/config/types.rb#L98

  Configurable.register_type(:array, Proc.new { |val, opts|
    param = val.is_a?(String) ? JSON.load(val) : val
    if param.class != Array
      raise ConfigError, "array required but got #{val.inspect}"
    end
    param
  })

JSON.load がすでに書いてあった。

では、予約語はずせるのでは?と思ったが、

https://github.com/fluent/fluentd/blob/8c7bc1c2fb3165b52179e5b9a430c6338d8e53e4/lib/fluent/config/literal_parser.rb#L56-L59

      def scan_json(is_array)
      ....
          if char == "\n"
            buffer << line_buffer + "\n"
            line_buffer = ""
            next
          end

改行を扱いたいがために、予約語になっているのであった。うーむ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment