Skip to content

Commit

Permalink
Merge pull request #3170 from fluent/better-freeze
Browse files Browse the repository at this point in the history
Make better use of freeze
  • Loading branch information
repeatedly authored Nov 16, 2020
2 parents 2088f16 + fa347b0 commit 5e036cf
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/plugin/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def configure(conf)
super
@newline = case newline
when :lf
"\n"
"\n".freeze
when :crlf
"\r\n"
"\r\n".freeze
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/formatter_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CsvFormatter < Formatter
helpers :record_accessor

config_param :delimiter, default: ',' do |val|
['\t', 'TAB'].include?(val) ? "\t" : val
['\t', 'TAB'].include?(val) ? "\t".freeze : val.freeze
end
config_param :force_quotes, :bool, default: true
# "array" looks good for type of :fields, but this implementation removes tailing comma
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/formatter_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HashFormatter < Formatter

def format(tag, time, record)
line = record.to_s
line << @newline.freeze if @add_newline
line << @newline if @add_newline
line
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/fluent/plugin/formatter_ltsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class LabeledTSVFormatter < Formatter

# http://ltsv.org/

config_param :delimiter, :string, default: "\t"
config_param :label_delimiter, :string, default: ":"
config_param :delimiter, :string, default: "\t".freeze
config_param :label_delimiter, :string, default: ":".freeze
config_param :add_newline, :bool, default: true

# TODO: escaping for \t in values
Expand All @@ -36,7 +36,7 @@ def format(tag, time, record)
formatted << @delimiter if formatted.length.nonzero?
formatted << "#{label}#{@label_delimiter}#{value}"
end
formatted << @newline.freeze if @add_newline
formatted << @newline if @add_newline
formatted
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/fluent/plugin/formatter_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class OutFileFormatter < Formatter
config_param :output_tag, :bool, default: true
config_param :delimiter, default: "\t" do |val|
case val
when /SPACE/i then ' '
when /COMMA/i then ','
else "\t"
when /SPACE/i then ' '.freeze
when /COMMA/i then ','.freeze
else "\t".freeze
end
end
config_set_default :time_type, :string
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/plugin/formatter_single_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class SingleValueFormatter < Formatter

Plugin.register_formatter('single_value', self)

config_param :message_key, :string, default: 'message'
config_param :message_key, :string, default: 'message'.freeze
config_param :add_newline, :bool, default: true

def format(tag, time, record)
text = record[@message_key].to_s.dup
text << @newline.freeze if @add_newline
text << @newline if @add_newline
text
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/plugin/formatter_tsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class TSVFormatter < Formatter
desc 'Field names included in each lines'
config_param :keys, :array, value_type: :string
desc 'The delimiter character (or string) of TSV values'
config_param :delimiter, :string, default: "\t"
config_param :delimiter, :string, default: "\t".freeze
desc 'The parameter to enable writing to new lines'
config_param :add_newline, :bool, default: true

def format(tag, time, record)
formatted = @keys.map{|k| record[k].to_s }.join(@delimiter)
formatted << @newline.freeze if @add_newline
formatted << @newline if @add_newline
formatted
end
end
Expand Down

0 comments on commit 5e036cf

Please sign in to comment.