Skip to content

Commit

Permalink
Remove redundant freezing on frozen string literals
Browse files Browse the repository at this point in the history
Signed-off-by: Watson <[email protected]>
  • Loading branch information
Watson1978 committed Aug 16, 2024
1 parent bbd85d1 commit af624d2
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def event(level, args)
}

map.each_pair {|k,v|
if k == "error".freeze && v.is_a?(Exception) && !map.has_key?("error_class")
if k == "error" && v.is_a?(Exception) && !map.has_key?("error_class")
message << " error_class=#{v.class.to_s} error=#{v.to_s.inspect}"
else
message << " #{k}=#{v.inspect}"
Expand Down Expand Up @@ -600,7 +600,7 @@ def caller_line(type, time, depth, level)
worker_id_part = if type == :default && (@process_type == :worker0 || @process_type == :workers)
@worker_id_part
else
"".freeze
""
end
log_msg = "#{format_time(time)} [#{LEVEL_TEXT[level]}]: #{worker_id_part}"
if @debug_mode
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 @@ -29,7 +29,7 @@ class CsvFormatter < Formatter
helpers :record_accessor

config_param :delimiter, default: ',' do |val|
['\t', 'TAB'].include?(val) ? "\t".freeze : val.freeze
['\t', 'TAB'].include?(val) ? "\t" : 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
6 changes: 3 additions & 3 deletions lib/fluent/plugin/formatter_ltsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class LabeledTSVFormatter < Formatter

# http://ltsv.org/

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

def format(tag, time, record)
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 @@ -31,9 +31,9 @@ class OutFileFormatter < Formatter
config_param :output_tag, :bool, default: true
config_param :delimiter, default: "\t" do |val|
case val
when /SPACE/i then ' '.freeze
when /COMMA/i then ','.freeze
else "\t".freeze
when /SPACE/i then ' '
when /COMMA/i then ','
else "\t"
end
end
config_set_default :time_type, :string
Expand Down
36 changes: 18 additions & 18 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ def close
end

RES_TEXT_HEADER = {'Content-Type' => 'text/plain'}.freeze
RESPONSE_200 = ["200 OK".freeze, RES_TEXT_HEADER, "".freeze].freeze
RESPONSE_204 = ["204 No Content".freeze, {}.freeze].freeze
RESPONSE_IMG = ["200 OK".freeze, {'Content-Type'=>'image/gif; charset=utf-8'}.freeze, EMPTY_GIF_IMAGE].freeze
RES_400_STATUS = "400 Bad Request".freeze
RES_500_STATUS = "500 Internal Server Error".freeze
RESPONSE_200 = ["200 OK", RES_TEXT_HEADER, ""].freeze
RESPONSE_204 = ["204 No Content", {}.freeze].freeze
RESPONSE_IMG = ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}.freeze, EMPTY_GIF_IMAGE].freeze
RES_400_STATUS = "400 Bad Request"
RES_500_STATUS = "500 Internal Server Error"

def on_request(path_info, params)
begin
Expand Down Expand Up @@ -306,15 +306,15 @@ def parse_params_with_parser(params)
def add_params_to_record(record, params)
if @add_http_headers
params.each_pair { |k, v|
if k.start_with?("HTTP_".freeze)
if k.start_with?("HTTP_")
record[k] = v
end
}
end

if @add_query_params
params.each_pair { |k, v|
if k.start_with?("QUERY_".freeze)
if k.start_with?("QUERY_")
record[k] = v
end
}
Expand Down Expand Up @@ -422,7 +422,7 @@ def on_headers_complete(headers)
end
}
if expect
if expect == '100-continue'.freeze
if expect == '100-continue'
if !size || size < @body_size_limit
send_response_nobody("100 Continue", {})
else
Expand All @@ -444,8 +444,8 @@ def on_body(chunk)
@body << chunk
end

RES_200_STATUS = "200 OK".freeze
RES_403_STATUS = "403 Forbidden".freeze
RES_200_STATUS = "200 OK"
RES_403_STATUS = "403 Forbidden"

# Azure App Service sends GET requests for health checking purpose.
# Respond with `200 OK` to accommodate it.
Expand Down Expand Up @@ -489,11 +489,11 @@ def handle_options_request
def on_message_complete
return if closing?

if @parser.http_method == 'GET'.freeze
if @parser.http_method == 'GET'
return handle_get_request()
end

if @parser.http_method == 'OPTIONS'.freeze
if @parser.http_method == 'OPTIONS'
return handle_options_request()
end

Expand All @@ -513,9 +513,9 @@ def on_message_complete
# Decode payload according to the "Content-Encoding" header.
# For now, we only support 'gzip' and 'deflate'.
begin
if @content_encoding == 'gzip'.freeze
if @content_encoding == 'gzip'
@body = Zlib::GzipReader.new(StringIO.new(@body)).read
elsif @content_encoding == 'deflate'.freeze
elsif @content_encoding == 'deflate'
@body = Zlib::Inflate.inflate(@body)
end
rescue
Expand Down Expand Up @@ -576,7 +576,7 @@ def on_message_complete
end

if @keep_alive
header['Connection'] = 'Keep-Alive'.freeze
header['Connection'] = 'Keep-Alive'
send_response(code, header, body)
else
send_response_and_close(code, header, body)
Expand All @@ -602,13 +602,13 @@ def closing?

def send_response(code, header, body)
header['Content-Length'] ||= body.bytesize
header['Content-Type'] ||= 'text/plain'.freeze
header['Content-Type'] ||= 'text/plain'

data = +"HTTP/1.1 #{code}\r\n"
header.each_pair {|k,v|
data << "#{k}: #{v}\r\n"
}
data << "\r\n".freeze
data << "\r\n"
@io.write(data)

@io.write(body)
Expand All @@ -619,7 +619,7 @@ def send_response_nobody(code, header)
header.each_pair {|k,v|
data << "#{k}: #{v}\r\n"
}
data << "\r\n".freeze
data << "\r\n"
@io.write(data)
end

Expand Down
22 changes: 11 additions & 11 deletions lib/fluent/plugin/in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,21 @@ def render_ltsv(obj, code: 200)

def build_object(opts)
qs = opts[:query]
if tag = qs['tag'.freeze].first
if tag = qs['tag'].first
# ?tag= to search an output plugin by match pattern
if obj = @agent.plugin_info_by_tag(tag, opts)
list = [obj]
else
list = []
end
elsif plugin_id = (qs['@id'.freeze].first || qs['id'.freeze].first)
elsif plugin_id = (qs['@id'].first || qs['id'].first)
# ?@id= to search a plugin by 'id <plugin_id>' config param
if obj = @agent.plugin_info_by_id(plugin_id, opts)
list = [obj]
else
list = []
end
elsif plugin_type = (qs['@type'.freeze].first || qs['type'.freeze].first)
elsif plugin_type = (qs['@type'].first || qs['type'].first)
# ?@type= to search plugins by 'type <type>' config param
list = @agent.plugins_info_by_type(plugin_type, opts)
else
Expand All @@ -160,22 +160,22 @@ def build_option(req)
# if ?debug=1 is set, set :with_debug_info for get_monitor_info
# and :pretty_json for render_json_error
opts = { query: qs }
if qs['debug'.freeze].first
if qs['debug'].first
opts[:with_debug_info] = true
opts[:pretty_json] = true
end

if ivars = qs['with_ivars'.freeze].first
if ivars = qs['with_ivars'].first
opts[:ivars] = ivars.split(',')
end

if with_config = qs['with_config'.freeze].first
if with_config = qs['with_config'].first
opts[:with_config] = Fluent::Config.bool_value(with_config)
else
opts[:with_config] = @agent.include_config
end

if with_retry = qs['with_retry'.freeze].first
if with_retry = qs['with_retry'].first
opts[:with_retry] = Fluent::Config.bool_value(with_retry)
else
opts[:with_retry] = @agent.include_retry
Expand Down Expand Up @@ -388,13 +388,13 @@ def get_retry_info(pe_retry)
def plugin_category(pe)
case pe
when Fluent::Plugin::Input
'input'.freeze
'input'
when Fluent::Plugin::Output, Fluent::Plugin::MultiOutput, Fluent::Plugin::BareOutput
'output'.freeze
'output'
when Fluent::Plugin::Filter
'filter'.freeze
'filter'
else
'unknown'.freeze
'unknown'
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_syslog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SyslogInput < Input
desc 'If true, add source host to event record.'
config_param :include_source_host, :bool, default: false, deprecated: 'use "source_hostname_key" or "source_address_key" instead.'
desc 'Specify key of source host when include_source_host is true.'
config_param :source_host_key, :string, default: 'source_host'.freeze
config_param :source_host_key, :string, default: 'source_host'
desc 'Enable the option to emit unmatched lines.'
config_param :emit_unmatched_lines, :bool, default: false

Expand Down
2 changes: 1 addition & 1 deletion test/config/test_configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class TestConfigurable < ::Test::Unit::TestCase

test 'can accept frozen string' do
b2 = ConfigurableSpec::Base2.new
assert_instance_of(ConfigurableSpec::Base2, b2.configure(config_element("", "", {"name1" => "t1".freeze, "name5" => "t5", "opt3" => "a"})))
assert_instance_of(ConfigurableSpec::Base2, b2.configure(config_element("", "", {"name1" => "t1", "name5" => "t5", "opt3" => "a"})))
end

test 'raise errors without any specifications for param without defaults' do
Expand Down
11 changes: 0 additions & 11 deletions test/plugin_helper/test_http_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,6 @@ def start_https_request(addr, port, verify: true, cert_path: nil, selfsigned: tr
end
end

test 'mount frozen path' do
on_driver do |driver|
driver.http_server_create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: @port, logger: NULL_LOGGER) do |s|
s.get('/example/hello'.freeze) { [200, { 'Content-Type' => 'text/plain' }, 'hello get'] }
end

resp = get("http://127.0.0.1:#{@port}/example/hello/")
assert_equal('200', resp.code)
end
end

test 'when path does not start with `/` or ends with `/`' do
on_driver do |driver|
driver.http_server_create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: @port, logger: NULL_LOGGER) do |s|
Expand Down

0 comments on commit af624d2

Please sign in to comment.