Skip to content

Commit 15e4167

Browse files
authored
Merge pull request #51 from reevoo/refactor-complex-code
Refactoring
2 parents 1e14ace + 8660590 commit 15e4167

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

lib/fluent/plugin/systemd/entry_mutator.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ def run(entry)
6969
# entry hash.
7070
# entry - hash or `Systemd::Journal:Entry`
7171
def map_fields(entry)
72-
mapped = {}
73-
@map.each do |cstm, sysds|
72+
@map.each_with_object({}) do |(cstm, sysds), mapped|
7473
vals = sysds.collect { |fld| entry[fld] }.compact
7574
next if vals.empty? # systemd field does not exist in source entry
76-
mapped[cstm] = vals.length == 1 ? vals[0] : vals.join(" ")
75+
mapped[cstm] = join_if_needed(vals)
7776
end
78-
mapped
7977
end
8078

8179
# Run field formatting (mutations applied to all non-mapped fields)
@@ -84,20 +82,29 @@ def map_fields(entry)
8482
# mapped - Optional hash that represents a previously mapped entry to
8583
# which the formatted fields will be added
8684
def format_fields(entry, mapped = nil)
87-
mapped ||= {}
88-
entry.each do |fld, val|
85+
entry.each_with_object(mapped || {}) do |(fld, val), formatted_entry|
8986
# don't mess with explicitly mapped fields
9087
next if @map_src_fields.include?(fld)
91-
fld = fld.gsub(/\A_+/, "") if @opts.fields_strip_underscores
92-
fld = fld.downcase if @opts.fields_lowercase
88+
fld = format_field_name(fld)
9389
# account for mapping (appending) to an existing systemd field
94-
mapped[fld] = mapped.key?(fld) ? [val, mapped[fld]].join(" ") : val
90+
formatted_entry[fld] = join_if_needed([val, mapped[fld]])
9591
end
96-
mapped
9792
end
9893

9994
private
10095

96+
def join_if_needed(values)
97+
values.compact!
98+
return values.first if values.length == 1
99+
values.join(" ")
100+
end
101+
102+
def format_field_name(name)
103+
name = name.gsub(/\A_+/, "") if @opts.fields_strip_underscores
104+
name = name.downcase if @opts.fields_lowercase
105+
name
106+
end
107+
101108
# Returns a `SystemdEntryMutator::Options` struct derived from the
102109
# elements in the supplied hash merged with the option defaults
103110
def options_from_hash(opts)
@@ -109,29 +116,22 @@ def options_from_hash(opts)
109116
end
110117

111118
def validate_options(opts)
112-
unless validate_strings_or_empty(opts[:field_map].keys)
113-
err = "`field_map` keys must be strings"
114-
end
115-
unless validate_strings_or_empty(opts[:field_map].values, true)
116-
err = "`field_map` values must be strings or array of strings"
117-
end
119+
validate_all_strings opts[:field_map].keys, "`field_map` keys must be strings"
120+
validate_all_strings opts[:field_map].values, "`field_map` values must be strings or an array of strings", true
118121
%i[field_map_strict fields_strip_underscores fields_lowercase].each do |opt|
119-
err = "`#{opt}` must be boolean" unless [true, false].include?(opts[opt])
122+
validate_boolean opts[opt], opt
120123
end
121-
fail Fluent::ConfigError, err unless err.nil?
122124
end
123125

124-
# Validates that values in array `arr` are strings. If `nested` is true
125-
# also allow and validate that `arr` values can be an array of strings
126-
def validate_strings_or_empty(arr, nested = false)
127-
return true if arr.empty?
128-
arr.each do |v|
129-
return true if v.is_a?(String)
130-
if v.is_a?(Array) && nested
131-
v.each { |nstd| return false unless nstd.is_a?(String) }
132-
end
126+
def validate_all_strings(arr, message, allow_nesting = false)
127+
valid = arr.all? do |value|
128+
value.is_a?(String) || allow_nesting && value.is_a?(Array) && value.all? { |key| key.is_a?(String) }
133129
end
134-
false
130+
fail Fluent::ConfigError, message unless valid
131+
end
132+
133+
def validate_boolean(value, name)
134+
fail Fluent::ConfigError, "`#{name}` must be boolean" unless [true, false].include?(value)
135135
end
136136

137137
# Compute the inverse of a human friendly field map `fm` which is what

lib/fluent/plugin/systemd/pos_writer.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,24 @@ def shutdown
4444

4545
def setup
4646
if @storage.persistent
47-
migrate_to_storage if @path && File.exist?(@path)
47+
migrate_to_storage
4848
elsif @path
49-
@cursor = IO.read(@path).chomp if File.exist?(@path)
49+
@cursor = read_legacy_pos if legacy_file?
5050
@storage = nil
5151
end
5252
end
5353

54+
def legacy_file?
55+
@path && File.exist?(@path)
56+
end
57+
58+
def read_legacy_pos
59+
IO.read(@path).chomp
60+
end
61+
5462
def migrate_to_storage
55-
@storage.put(:journal, IO.read(@path).chomp)
63+
return unless legacy_file?
64+
@storage.put(:journal, read_legacy_pos)
5665
File.delete(@path)
5766
@path = nil
5867
end

0 commit comments

Comments
 (0)