Skip to content

Commit

Permalink
Moved Interpolation logic to logical_parser
Browse files Browse the repository at this point in the history
Signed-off-by: Athish Pranav D <[email protected]>
  • Loading branch information
Athishpranav2003 committed Aug 6, 2024
1 parent 0841d0c commit ad7b83d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
14 changes: 10 additions & 4 deletions lib/fluent/config/literal_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ def scan_nonquoted_string(boundary_charset = LINE_END)
end

def scan_embedded_code
src = '"#{'+@ss.rest+"\n=begin\n=end\n}"
src = '#{'+@ss.rest+"\n=begin\n=end\n}"

seek = -1
while (seek = src.index('}', seek + 1))
unless Ripper.sexp(src[0..seek] + '"').nil? # eager parsing until valid expression
unless Ripper.sexp(src[0..seek]).nil? # eager parsing until valid expression
break
end
end
Expand All @@ -168,7 +168,7 @@ def scan_embedded_code
raise Fluent::ConfigParseError, @ss.rest
end

code = src[3, seek-3]
code = src[2, seek-2]

if @ss.rest.length < code.length
@ss.pos += @ss.rest.length
Expand Down Expand Up @@ -255,7 +255,12 @@ def scan_json(is_array)
line_buffer = ""
else
# '#' is a char in json string
line_buffer << char
if skip(/\{/)
line_buffer << eval_embedded_code(scan_embedded_code)
skip(/\}/)
else
line_buffer << char
end
end

next # This char '#' MUST NOT terminate json object.
Expand All @@ -280,6 +285,7 @@ def scan_json(is_array)
end

JSON.dump(result)

end
end
end
Expand Down
13 changes: 0 additions & 13 deletions lib/fluent/config/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def self.hash_value(val, opts = {}, name = nil)
return nil if val.nil?

param = if val.is_a?(String)
val = Config.process_env_vars(val)
val.start_with?('{') ? JSON.parse(val) : Hash[val.strip.split(/\s*,\s*/).map{|v| v.split(':', 2)}]
else
val
Expand Down Expand Up @@ -229,7 +228,6 @@ def self.array_value(val, opts = {}, name = nil)
return nil if val.nil?

param = if val.is_a?(String)
val = Config.process_env_vars(val)
val.start_with?('[') ? JSON.parse(val) : val.strip.split(/\s*,\s*/)
else
val
Expand All @@ -247,16 +245,5 @@ def self.array_value(val, opts = {}, name = nil)
ARRAY_TYPE = Proc.new { |val, opts = {}, name = nil|
Config.array_value(val, opts, name)
}

def self.process_env_vars(val)
pattern = /#\{ENV\[[^}]+\]\}/
env_vars = val.scan(pattern)
env_vars.each do |env_var|
env_value = ENV[env_var[7..-4]]
raise ConfigError, "Environment variable #{env_var} not found" unless env_value
val.gsub!(env_var, env_value)
end
val
end
end
end

0 comments on commit ad7b83d

Please sign in to comment.