Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F #2505: Add nested filters #3083

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/cli/cli_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ module CLIHelper
:description => "Filter data. An array is specified with\n"<<
" "*31<<"column=value pairs."
}
OPERATOR = {
:name => "operator",
:large => "--operator operator",
:format => String,
:description => "Logical operator used on filters: AND, OR."<<
" Default: AND."
}

#
#HEADER = {
# :name => "header",
Expand Down Expand Up @@ -82,7 +90,7 @@ module CLIHelper
}

#OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY]
OPTIONS = [LIST, LISTCONF, DELAY, FILTER, CSV_OPT, NO_PAGER]
OPTIONS = [LIST, LISTCONF, DELAY, FILTER, OPERATOR, CSV_OPT, NO_PAGER]

# Sets bold font
def CLIHelper.scr_bold
Expand Down Expand Up @@ -330,7 +338,7 @@ def data_array(data, options)
}

if options
filter_data!(res_data, options[:filter]) if options[:filter]
filter_data!(res_data, options) if options[:filter]
sort_data!(res_data, options[:order]) if options[:order]
end

Expand Down Expand Up @@ -393,10 +401,16 @@ def header_str
}.compact.join(' ')
end

def filter_data!(data, filter)
def filter_data!(data, options)
# TBD: add more operators
# operators=/(==|=|!=|<|<=|>|>=)/
operators=/(=|!=)/
filter = options[:filter]
if options.key?(:operator)
log_operator = options[:operator].upcase
else
log_operator = "AND"
end

stems=filter.map do |s|
m=s.match(/^(.*?)#{operators}(.*?)$/)
Expand Down Expand Up @@ -427,12 +441,19 @@ def filter_data!(data, filter)
stems.each do |s|

if d[s[:index]].public_send(s[:operator] == "=" ? "==" : s[:operator], s[:right])
pass=false
break
if log_operator == "OR"
pass = true
break
end
else
pass = false
if log_operator == "AND"
break
end
end
end

!pass
pass
end
end

Expand Down