Class: Fluent::PluginHelper::RecordAccessor::Accessor

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin_helper/record_accessor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ Accessor

Returns a new instance of Accessor.



35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 35

def initialize(param)
  @keys = Accessor.parse_parameter(param)

  # Call [] for single key to reduce dig overhead
  m = method(@keys.is_a?(Array) ? :call_dig : :call_index)
  (class << self; self; end).module_eval do
    define_method(:call, m)
  end
end

Class Method Details

.parse_bracket_notation(param) ⇒ Object



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
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 128

def self.parse_bracket_notation(param)
  orig_param = param
  result = []
  param = param[1..-1]
  in_bracket = false

  until param.empty?
    if in_bracket
      if param[0] == "'"
        if i = param.index("']")
          result << param[1..i - 1]
          param = param[i + 2..-1]
          in_bracket = false
        else
          raise Fluent::ConfigError, "Incomplete bracket. Invalid syntax: #{orig_param}"
        end
      else
        if i = param.index(']')
          index_value = param[0..i - 1]
          raise Fluent::ConfigError, "missing array index in '[]'. Invalid syntax: #{param}" if index_value == ']'
          result << Integer(index_value)
          param = param[i + 1..-1]
          in_bracket = false
        else
          raise Fluent::ConfigError, "'[' found but ']' not found. Invalid syntax: #{orig_param}"
        end
      end
    else
      if i = param.index('[')
        param = param[i + 1..-1]
        in_bracket = true
      else
        raise Fluent::ConfigError, "found more characters after ']'. Invalid syntax: #{orig_param}"
      end
    end
  end

  raise Fluent::ConfigError, "empty keys in bracket notation" if result.empty?

  result
end

.parse_dot_array_op(key, param) ⇒ Object



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
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 94

def self.parse_dot_array_op(key, param)
  start = key.index('[')
  result = if start.zero?
             []
           else
             [key[0..start - 1]]
           end
  key = key[start + 1..-1]
  in_bracket = true

  until key.empty?
    if in_bracket
      if i = key.index(']')
        index_value = key[0..i - 1]
        raise Fluent::ConfigError, "missing array index in '[]'. Invalid syntax: #{param}" if index_value == ']'
        result << Integer(index_value)
        key = key[i + 1..-1]
        in_bracket = false
      else
        raise Fluent::ConfigError, "'[' found but ']' not found. Invalid syntax: #{param}"
      end
    else
      if i = key.index('[')
        key = key[i + 1..-1]
        in_bracket = true
      else
        raise Fluent::ConfigError, "found more characters after ']'. Invalid syntax: #{param}"
      end
    end
  end

  result
end

.parse_dot_notation(param) ⇒ Object



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

def self.parse_dot_notation(param)
  result = []
  keys = param[2..-1].split('.')
  keys.each { |key|
    if key.include?('[')
      result.concat(parse_dot_array_op(key, param))
    else
      result << key
    end
  }

  raise Fluent::ConfigError, "empty keys in dot notation" if result.empty?
  validate_dot_keys(result)

  result
end

.parse_parameter(param) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 58

def self.parse_parameter(param)
  if param.start_with?('$.')
    parse_dot_notation(param)
  elsif param.start_with?('$[')
    parse_bracket_notation(param)
  else
    param
  end
end

.validate_dot_keys(keys) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 85

def self.validate_dot_keys(keys)
  keys.each { |key|
    next unless key.is_a?(String)
    if /\s+/.match(key)
      raise Fluent::ConfigError, "whitespace character is not allowed in dot notation. Use bracket notation: #{key}"
    end
  }
end

Instance Method Details

#call(r) ⇒ Object



45
46
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 45

def call(r)
end

#call_dig(r) ⇒ Object

To optimize the performance, use class_eval with pre-expanding @keys See gist.github.com/repeatedly/ab553ed260cd080bd01ec71da9427312



50
51
52
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 50

def call_dig(r)
  r.dig(*@keys)
end

#call_index(r) ⇒ Object



54
55
56
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 54

def call_index(r)
  r[@keys]
end