Skip to content

Commit

Permalink
fix #711 Object#=~ is called on TrueClass
Browse files Browse the repository at this point in the history
  • Loading branch information
lbetz committed Oct 14, 2022
1 parent a35381c commit 3d1e680
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions lib/puppet_x/icinga2/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,37 +182,39 @@ def self.attribute_types(attr)
def self.parse(row)
result = ''

# parser is disabled
if row =~ %r{^-:(.*)$}m
return Regexp.last_match(1)
end

if row =~ %r{^\{{2}(.+)\}{2}$}m
# scan function
result += '{{%{expr}}}' % { expr: Regexp.last_match(1) }
elsif row =~ %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s\{{2}(.+)\}{2}$}m
# scan expression + function (function should contain expressions, but we donno parse it)
result += '%{expr} %{op} {{%{fct}}}' % { expr: parse(Regexp.last_match(1)), op: Regexp.last_match(2), fct: Regexp.last_match(3) }
elsif row =~ %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s(.+)$}
# scan expression
result += '%{expr1} %{op} %{expr2}' % { expr1: parse(Regexp.last_match(1)), op: Regexp.last_match(2), expr2: parse(Regexp.last_match(3)) }
elsif row =~ %r{^(.+)\((.*)$}
result += '%{fct}(%{param}' % { fct: Regexp.last_match(1), param: Regexp.last_match(2).split(',').map { |x| parse(x.lstrip) }.join(', ') }
elsif row =~ %r{^(.*)\)(.+)?$}
# closing bracket ) with optional access of an attribute e.g. '.arguments'
result += '%{param})%{expr}' % { param: Regexp.last_match(1).split(',').map { |x| parse(x.lstrip) }.join(', '), expr: Regexp.last_match(2) }
elsif row =~ %r{^\((.*)$}
result += '(%{expr}' % { expr: parse(Regexp.last_match(1)) }
elsif row =~ %r{^\s*\[\s*(.*)\s*\]\s?(.+)?$}
# parse array
result += '[ %{lst}]' % { lst: process_array(Regexp.last_match(1).split(',')) }
result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
elsif row =~ %r{^\s*\{\s*(.*)\s*\}\s?(.+)?$}
# parse hash
result += "{\n%{expr}}" % { expr: process_hash(Hash[Regexp.last_match(1).gsub(%r{\s*=>\s*|\s*,\s*}, ',').split(',').each_slice(2).to_a]) }
result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
else
result += value_types(row.to_s.strip)
if row.is_a?(String)
# parser is disabled
if row =~ %r{^-:(.*)$}m
return Regexp.last_match(1)
end

if row =~ %r{^\{{2}(.+)\}{2}$}m
# scan function
result += '{{%{expr}}}' % { expr: Regexp.last_match(1) }
elsif row =~ %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s\{{2}(.+)\}{2}$}m
# scan expression + function (function should contain expressions, but we donno parse it)
result += '%{expr} %{op} {{%{fct}}}' % { expr: parse(Regexp.last_match(1)), op: Regexp.last_match(2), fct: Regexp.last_match(3) }
elsif row =~ %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s(.+)$}
# scan expression
result += '%{expr1} %{op} %{expr2}' % { expr1: parse(Regexp.last_match(1)), op: Regexp.last_match(2), expr2: parse(Regexp.last_match(3)) }
elsif row =~ %r{^(.+)\((.*)$}
result += '%{fct}(%{param}' % { fct: Regexp.last_match(1), param: Regexp.last_match(2).split(',').map { |x| parse(x.lstrip) }.join(', ') }
elsif row =~ %r{^(.*)\)(.+)?$}
# closing bracket ) with optional access of an attribute e.g. '.arguments'
result += '%{param})%{expr}' % { param: Regexp.last_match(1).split(',').map { |x| parse(x.lstrip) }.join(', '), expr: Regexp.last_match(2) }
elsif row =~ %r{^\((.*)$}
result += '(%{expr}' % { expr: parse(Regexp.last_match(1)) }
elsif row =~ %r{^\s*\[\s*(.*)\s*\]\s?(.+)?$}
# parse array
result += '[ %{lst}]' % { lst: process_array(Regexp.last_match(1).split(',')) }
result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
elsif row =~ %r{^\s*\{\s*(.*)\s*\}\s?(.+)?$}
# parse hash
result += "{\n%{expr}}" % { expr: process_hash(Hash[Regexp.last_match(1).gsub(%r{\s*=>\s*|\s*,\s*}, ',').split(',').each_slice(2).to_a]) }
result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
else
result += value_types(row.to_s.strip)
end
end
result.gsub(%r{" in "}, ' in ')
end
Expand Down

0 comments on commit 3d1e680

Please sign in to comment.