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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ Accessor

Returns a new instance of Accessor.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 33

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

  if @keys.is_a?(Array)
    @last_key = @keys.last
    @dig_keys = @keys[0..-2]
    if @dig_keys.empty?
      @keys = @keys.first
    else
      mcall = method(:call_dig)
      mdelete = method(:delete_nest)
      mset = method(:set_nest)
      singleton_class.module_eval do
        define_method(:call, mcall)
        define_method(:delete, mdelete)
        define_method(:set, mset)
      end
    end
  end
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



31
32
33
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 31

def keys
  @keys
end

Class Method Details

.parse_bracket_notation(param) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 162

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] == "'" || param[0] == '"'
        if i = param.index("']") || param.index('"]')
          raise Fluent::ConfigError, "Mismatched quotes. Invalid syntax: #{orig_param}" unless param[0] == param[i]
          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



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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 102

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



92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 92

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



119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 119

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



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

def call(r)
  r[@keys]
end

#call_dig(r) ⇒ Object

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



60
61
62
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 60

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

#delete(r) ⇒ Object



64
65
66
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 64

def delete(r)
  r.delete(@keys)
end

#delete_nest(r) ⇒ Object



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

def delete_nest(r)
  if target = r.dig(*@dig_keys)
    if target.is_a?(Array)
      target.delete_at(@last_key)
    else
      target.delete(@last_key)
    end
  end
rescue
  nil
end

#set(r, v) ⇒ Object



80
81
82
# File 'lib/fluent/plugin_helper/record_accessor.rb', line 80

def set(r, v)
  r[@keys] = v
end

#set_nest(r, v) ⇒ Object

set_nest doesn’t create intermediate object. If key doesn’t exist, no effect. See also: bugs.ruby-lang.org/issues/11747



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

def set_nest(r, v)
  r.dig(*@dig_keys)&.[]=(@last_key, v)
rescue
  nil
end