Class: Fluent::Config::ConfigureProxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ ConfigureProxy

config_param :desc, :string, :default => ‘.…’ config_set_default :buffer_type, :memory

config_section :default, required: true, multi: false do

config_argument :arg, :string
config_param :required, :bool, default: false
config_param :name, :string
config_param :power, :integer

end

config_section :child, param_name: ‘children’, required: false, multi: true, alias: ‘node’ do

config_param :name, :string
config_param :power, :integer, default: nil
config_section :item do
  config_param :name
end

end



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fluent/config/configure_proxy.rb', line 41

def initialize(name, opts = {})
  @name = name.to_sym
  @final = opts.fetch(:final, false)

  @param_name = (opts[:param_name] || @name).to_sym
  @required = opts[:required]
  @multi = opts[:multi]
  @alias = opts[:alias]

  @argument = nil # nil: ignore argument
  @params = {}
  @defaults = {}
  @descriptions = {}
  @sections = {}
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def alias
  @alias
end

#argumentObject

Returns the value of attribute argument.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def argument
  @argument
end

#defaultsObject

Returns the value of attribute defaults.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def defaults
  @defaults
end

#descriptionsObject

Returns the value of attribute descriptions.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def descriptions
  @descriptions
end

#finalObject

Returns the value of attribute final.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def final
  @final
end

#multiObject

Returns the value of attribute multi.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def multi
  @multi
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def name
  @name
end

#param_nameObject

Returns the value of attribute param_name.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def param_name
  @param_name
end

#paramsObject

Returns the value of attribute params.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def params
  @params
end

#requiredObject

Returns the value of attribute required.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def required
  @required
end

#sectionsObject

Returns the value of attribute sections.



22
23
24
# File 'lib/fluent/config/configure_proxy.rb', line 22

def sections
  @sections
end

Instance Method Details

#config_argument(name, *args, &block) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/fluent/config/configure_proxy.rb', line 191

def config_argument(name, *args, &block)
  if @argument
    raise ArgumentError, "#{self.name}: config_argument called twice"
  end
  name, block, opts = parameter_configuration(name, *args, &block)

  @argument = [name, block, opts]
  name
end

#config_param(name, *args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fluent/config/configure_proxy.rb', line 201

def config_param(name, *args, &block)
  name, block, opts = parameter_configuration(name, *args, &block)

  if @current_description
    config_set_desc(name, @current_description)
    @current_description = nil
  end

  @sections.delete(name)
  @params[name] = [block, opts]
  name
end

#config_section(name, *args, &block) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fluent/config/configure_proxy.rb', line 240

def config_section(name, *args, &block)
  unless block_given?
    raise ArgumentError, "#{name}: config_section requires block parameter"
  end
  name = name.to_sym

  opts = {}
  unless args.empty? || args.size == 1 && args.first.is_a?(Hash)
    raise ArgumentError, "#{self.name}: unknown config_section arguments: #{args.inspect}"
  end

  sub_proxy = ConfigureProxy.new(name, *args)
  sub_proxy.instance_exec(&block)

  @params.delete(name)
  @sections[name] = sub_proxy

  name
end

#config_set_default(name, defval) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/fluent/config/configure_proxy.rb', line 214

def config_set_default(name, defval)
  name = name.to_sym

  if @defaults.has_key?(name)
    raise ArgumentError, "#{self.name}: default value specified twice for #{name}"
  end

  @defaults[name] = defval
  nil
end

#config_set_desc(name, description) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/fluent/config/configure_proxy.rb', line 225

def config_set_desc(name, description)
  name = name.to_sym

  if @descriptions.has_key?(name)
    raise ArgumentError, "#{self.name}: description specified twice for #{name}"
  end

  @descriptions[name] = description
  nil
end

#desc(description) ⇒ Object



236
237
238
# File 'lib/fluent/config/configure_proxy.rb', line 236

def desc(description)
  @current_description = description
end

#dump(level = 0) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/fluent/config/configure_proxy.rb', line 260

def dump(level = 0)
  dumped_config = ""
  indent = " " * level
  @params.each do |name, config|
    dumped_config << "#{indent}#{name}: #{config[1][:type]}: <#{@defaults[name].inspect}>"
    dumped_config << " # #{@descriptions[name]}" if @descriptions[name]
    dumped_config << "\n"
  end
  @sections.each do |section_name, sub_proxy|
    dumped_config << "#{indent}#{section_name}\n#{sub_proxy.dump(level + 1)}"
  end
  dumped_config
end

#final?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/fluent/config/configure_proxy.rb', line 65

def final?
  @final
end

#merge(other) ⇒ Object

self is base class, other is subclass



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/config/configure_proxy.rb', line 69

def merge(other) # self is base class, other is subclass
  return merge_for_finalized(other) if self.final?

  options = {
    param_name: other.param_name,
    required: (other.required.nil? ? self.required : other.required),
    multi: (other.multi.nil? ? self.multi : other.multi)
  }
  merged = self.class.new(other.name, options)

  merged.argument = other.argument || self.argument
  merged.params = self.params.merge(other.params)
  merged.defaults = self.defaults.merge(other.defaults)
  merged.sections = {}
  (self.sections.keys + other.sections.keys).uniq.each do |section_key|
    self_section = self.sections[section_key]
    other_section = other.sections[section_key]
    merged_section = if self_section && other_section
                       self_section.merge(other_section)
                     elsif self_section || other_section
                       self_section || other_section
                     else
                       raise "BUG: both of self and other section are nil"
                     end
    merged.sections[section_key] = merged_section
  end

  merged
end

#merge_for_finalized(other) ⇒ Object



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
127
128
129
130
# File 'lib/fluent/config/configure_proxy.rb', line 99

def merge_for_finalized(other)
  # list what subclass can do for finalized section
  #  * overwrite param_name to escape duplicated name of instance variable
  #  * append params/defaults/sections which are missing in superclass

  options = {
    param_name: other.param_name,
    required: (self.required.nil? ? other.required : self.required),
    multi: (self.multi.nil? ? other.multi : self.multi),
    final: true,
  }
  merged = self.class.new(other.name, options)

  merged.argument = self.argument || other.argument
  merged.params = other.params.merge(self.params)
  merged.defaults = other.defaults.merge(self.defaults)
  merged.sections = {}
  (self.sections.keys + other.sections.keys).uniq.each do |section_key|
    self_section = self.sections[section_key]
    other_section = other.sections[section_key]
    merged_section = if self_section && other_section
                       other_section.merge(self_section)
                     elsif self_section || other_section
                       self_section || other_section
                     else
                       raise "BUG: both of self and other section are nil"
                     end
    merged.sections[section_key] = merged_section
  end

  merged
end

#multi?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fluent/config/configure_proxy.rb', line 61

def multi?
  @multi.nil? ? true : @multi
end

#option_value_type!(name, opts, key, klass) ⇒ Object



132
133
134
135
136
# File 'lib/fluent/config/configure_proxy.rb', line 132

def option_value_type!(name, opts, key, klass)
  if opts.has_key?(key) && !opts[key].is_a?(klass)
    raise ArgumentError, "#{name}: #{key} must be a #{klass}, but #{opts[key].class}"
  end
end

#parameter_configuration(name, *args, &block) ⇒ Object



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fluent/config/configure_proxy.rb', line 138

def parameter_configuration(name, *args, &block)
  name = name.to_sym

  opts = {}
  args.each { |a|
    if a.is_a?(Symbol)
      opts[:type] = a
    elsif a.is_a?(Hash)
      opts.merge!(a)
    else
      raise ArgumentError, "#{self.name}: wrong number of arguments (#{1 + args.length} for #{block ? 2 : 3})"
    end
  }

  type = opts[:type]
  if block && type
    raise ArgumentError, "#{name}: both of block and type cannot be specified"
  end

  begin
    type = :string if type.nil?
    block ||= Configurable.lookup_type(type)
  rescue ConfigError
    # override error message
    raise ArgumentError, "#{name}: unknown config_argument type `#{type}'"
  end

  option_value_type!(name, opts, :desc, String)
  option_value_type!(name, opts, :alias, Symbol)
  option_value_type!(name, opts, :deprecated, String)
  option_value_type!(name, opts, :obsoleted, String)
  if type == :enum
    if !opts.has_key?(:list) || !opts[:list].all?{|v| v.is_a?(Symbol) }
      raise ArgumentError, "#{name}: enum parameter requires :list of Symbols"
    end
  end
  option_value_type!(name, opts, :value_type, Symbol) # hash, array

  if opts.has_key?(:default)
    config_set_default(name, opts[:default])
  end

  if opts.has_key?(:desc)
    config_set_desc(name, opts[:desc])
  end

  if opts[:deprecated] && opts[:obsoleted]
    raise ArgumentError, "#{name}: both of deprecated and obsoleted cannot be specified at once"
  end

  [name, block, opts]
end

#required?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fluent/config/configure_proxy.rb', line 57

def required?
  @required.nil? ? false : @required
end