jgrep 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/jgrep +4 -1
- data/jgrep.gemspec +1 -1
- data/lib/jgrep.rb +30 -3
- data/lib/parser/scanner.rb +1 -2
- metadata +2 -2
data/bin/jgrep
CHANGED
@@ -13,6 +13,9 @@ OptionParser.new do |opts|
|
|
13
13
|
opts.on("-f", "--flat", "Display non pretty json") do
|
14
14
|
options[:flat] = true
|
15
15
|
end
|
16
|
+
opts.on("-v", "--verbose", "Verbose output") do
|
17
|
+
JGrep::verbose_on
|
18
|
+
end
|
16
19
|
|
17
20
|
end.parse!
|
18
21
|
|
@@ -41,7 +44,7 @@ begin
|
|
41
44
|
|
42
45
|
unless options[:field]
|
43
46
|
result = JGrep::jgrep((json), expression)
|
44
|
-
unless result ==
|
47
|
+
unless result == []
|
45
48
|
(options[:flat] == false) ? puts(JSON.pretty_generate(result)) : puts(result.to_json)
|
46
49
|
end
|
47
50
|
else
|
data/jgrep.gemspec
CHANGED
data/lib/jgrep.rb
CHANGED
@@ -4,26 +4,48 @@ require 'parser/parser.rb'
|
|
4
4
|
require 'parser/scanner.rb'
|
5
5
|
require 'rubygems'
|
6
6
|
require 'json'
|
7
|
+
require 'pp'
|
7
8
|
|
8
9
|
module JGrep
|
10
|
+
@verbose = false
|
11
|
+
|
12
|
+
def self.verbose_on
|
13
|
+
@verbose = true
|
14
|
+
end
|
9
15
|
|
10
16
|
#Method parses json and returns documents that match the logical expression
|
11
17
|
def self.jgrep(json, expression)
|
18
|
+
errors = ""
|
12
19
|
begin
|
13
20
|
call_stack = Parser.new(expression).execution_stack
|
14
21
|
result = []
|
15
22
|
json = JSON.parse(json)
|
16
23
|
json.each do |document|
|
17
|
-
|
18
|
-
|
24
|
+
begin
|
25
|
+
if eval_statement(document, call_stack)
|
26
|
+
result << document
|
27
|
+
end
|
28
|
+
rescue Exception => e
|
29
|
+
if @verbose
|
30
|
+
pp document
|
31
|
+
STDERR.puts "Error - #{e} \n\n"
|
32
|
+
else
|
33
|
+
errors = "One or more the json documents could not be parsed. Run jgrep -v for to display documents"
|
34
|
+
end
|
19
35
|
end
|
20
36
|
end
|
37
|
+
|
38
|
+
unless errors == ""
|
39
|
+
puts errors
|
40
|
+
end
|
41
|
+
|
21
42
|
return result
|
22
43
|
|
23
44
|
rescue JSON::ParserError => e
|
24
45
|
STDERR.puts "Error. Invalid JSON given"
|
25
46
|
exit 1
|
26
47
|
end
|
48
|
+
|
27
49
|
end
|
28
50
|
|
29
51
|
#Correctly format values so we can do the correct type of comparison
|
@@ -52,6 +74,9 @@ module JGrep
|
|
52
74
|
|
53
75
|
key.split(".").each_with_index do |item,i|
|
54
76
|
tmp = tmp[item]
|
77
|
+
if tmp.nil?
|
78
|
+
return false
|
79
|
+
end
|
55
80
|
result = false
|
56
81
|
if tmp.is_a? Array
|
57
82
|
return (is_object_in_array?(tmp, "#{key.split(".")[i+1]}#{op}#{value}"))
|
@@ -105,6 +130,9 @@ module JGrep
|
|
105
130
|
|
106
131
|
field.split(".").each_with_index do |item, i|
|
107
132
|
tmp = tmp[item]
|
133
|
+
if tmp.nil?
|
134
|
+
return false
|
135
|
+
end
|
108
136
|
if tmp.is_a? Array
|
109
137
|
tmp.each do |doc|
|
110
138
|
result = []
|
@@ -121,7 +149,6 @@ module JGrep
|
|
121
149
|
left = token[1].split(op[0]).first.split(".").last
|
122
150
|
right = token[1].split(op[0]).last
|
123
151
|
new_statement = left + op[0] + right
|
124
|
-
# new_statement = token[1].split(/<=|>=|=|<|>/).first.split(".").last + token[1].split(/<=|>=|=|<|>/).last
|
125
152
|
result << has_object?(doc, new_statement)
|
126
153
|
end
|
127
154
|
end
|
data/lib/parser/scanner.rb
CHANGED