burtpath 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bfa4c5a420e8defde2a70bdbf2de5a8b4376576
4
- data.tar.gz: 2ddf695b4cccd57b2e0b93735f89fdeec5878509
3
+ metadata.gz: 1f0fad9f7110671a046e39df22effa1b65ac8644
4
+ data.tar.gz: 0855479643813db91c37eadc33bb43cd16789c65
5
5
  SHA512:
6
- metadata.gz: b4611855c33f0104c78aa2cebef3a0ef308091f86ba63c04ca66406fa38bbccd513ed9382f551a10c33a192f00d11fb2966e2997b1ba3221ebbbc961924e7996
7
- data.tar.gz: 4bfd2c5588811ad86348e80ec2fde8ef200eebce105ba58c1b7dcf5566fbedd4dc618e259f84405800ca6b6bcc43d71a893253631273776926dfd3ec4f89567d
6
+ metadata.gz: d165fe70830ff20c9f453bbce8f6a1af849bd010fe5ce2421c9650b715f4db97a038ff8e1279549ea7b30db54e9f852d73eee1a3b73ccd6fb23840715fc5c9f3
7
+ data.tar.gz: b2647b7e414c3a9f32b6444a2663feb802f0819769de492ddf83710f93a46a16f6a390b57c22546b814b1d337d5507d99e45fe0d268fc2ca5a7fa0a3a4336c86
@@ -3,10 +3,10 @@ module JMESPath
3
3
 
4
4
  class Error < StandardError; end
5
5
 
6
- class RuntimeError < Error; end
7
-
8
6
  class SyntaxError < Error; end
9
7
 
8
+ class InvalidValueError < Error; end
9
+
10
10
  class InvalidTypeError < Error; end
11
11
 
12
12
  class InvalidArityError < Error; end
@@ -4,13 +4,15 @@ module JMESPath
4
4
  class Function < Node
5
5
  FUNCTIONS = {}
6
6
 
7
- def initialize(children)
7
+ def initialize(children, options = {})
8
8
  @children = children
9
+ @options = options
10
+ @disable_visit_errors = @options[:disable_visit_errors]
9
11
  end
10
12
 
11
- def self.create(name, children)
13
+ def self.create(name, children, options = {})
12
14
  if (type = FUNCTIONS[name])
13
- type.new(children)
15
+ type.new(children, options)
14
16
  else
15
17
  raise Errors::UnknownFunctionError, "unknown function #{name}()"
16
18
  end
@@ -21,7 +23,7 @@ module JMESPath
21
23
  end
22
24
 
23
25
  def optimize
24
- self.class.new(@children.map(&:optimize))
26
+ self.class.new(@children.map(&:optimize), @options)
25
27
  end
26
28
 
27
29
  class FunctionName
@@ -34,6 +36,12 @@ module JMESPath
34
36
 
35
37
  private
36
38
 
39
+ def maybe_raise(error_type, message)
40
+ unless @disable_visit_errors
41
+ raise error_type, message
42
+ end
43
+ end
44
+
37
45
  def call(args)
38
46
  nil
39
47
  end
@@ -78,12 +86,12 @@ module JMESPath
78
86
  if args.count == 1
79
87
  value = args.first
80
88
  else
81
- raise Errors::InvalidArityError, "function abs() expects one argument"
89
+ return maybe_raise Errors::InvalidArityError, "function abs() expects one argument"
82
90
  end
83
91
  if Numeric === value
84
92
  value.abs
85
93
  else
86
- raise Errors::InvalidTypeError, "function abs() expects a number"
94
+ return maybe_raise Errors::InvalidTypeError, "function abs() expects a number"
87
95
  end
88
96
  end
89
97
  end
@@ -95,18 +103,18 @@ module JMESPath
95
103
  if args.count == 1
96
104
  values = args.first
97
105
  else
98
- raise Errors::InvalidArityError, "function avg() expects one argument"
106
+ return maybe_raise Errors::InvalidArityError, "function avg() expects one argument"
99
107
  end
100
108
  if Array === values
101
109
  values.inject(0) do |total,n|
102
110
  if Numeric === n
103
111
  total + n
104
112
  else
105
- raise Errors::InvalidTypeError, "function avg() expects numeric values"
113
+ return maybe_raise Errors::InvalidTypeError, "function avg() expects numeric values"
106
114
  end
107
115
  end / values.size.to_f
108
116
  else
109
- raise Errors::InvalidTypeError, "function avg() expects a number"
117
+ return maybe_raise Errors::InvalidTypeError, "function avg() expects a number"
110
118
  end
111
119
  end
112
120
  end
@@ -118,12 +126,12 @@ module JMESPath
118
126
  if args.count == 1
119
127
  value = args.first
120
128
  else
121
- raise Errors::InvalidArityError, "function ceil() expects one argument"
129
+ return maybe_raise Errors::InvalidArityError, "function ceil() expects one argument"
122
130
  end
123
131
  if Numeric === value
124
132
  value.ceil
125
133
  else
126
- raise Errors::InvalidTypeError, "function ceil() expects a numeric value"
134
+ return maybe_raise Errors::InvalidTypeError, "function ceil() expects a numeric value"
127
135
  end
128
136
  end
129
137
  end
@@ -138,10 +146,10 @@ module JMESPath
138
146
  if String === haystack || Array === haystack
139
147
  haystack.include?(needle)
140
148
  else
141
- raise Errors::InvalidTypeError, "contains expects 2nd arg to be a list"
149
+ return maybe_raise Errors::InvalidTypeError, "contains expects 2nd arg to be a list"
142
150
  end
143
151
  else
144
- raise Errors::InvalidArityError, "function contains() expects 2 arguments"
152
+ return maybe_raise Errors::InvalidArityError, "function contains() expects 2 arguments"
145
153
  end
146
154
  end
147
155
  end
@@ -153,12 +161,12 @@ module JMESPath
153
161
  if args.count == 1
154
162
  value = args.first
155
163
  else
156
- raise Errors::InvalidArityError, "function floor() expects one argument"
164
+ return maybe_raise Errors::InvalidArityError, "function floor() expects one argument"
157
165
  end
158
166
  if Numeric === value
159
167
  value.floor
160
168
  else
161
- raise Errors::InvalidTypeError, "function floor() expects a numeric value"
169
+ return maybe_raise Errors::InvalidTypeError, "function floor() expects a numeric value"
162
170
  end
163
171
  end
164
172
  end
@@ -170,11 +178,11 @@ module JMESPath
170
178
  if args.count == 1
171
179
  value = args.first
172
180
  else
173
- raise Errors::InvalidArityError, "function length() expects one argument"
181
+ return maybe_raise Errors::InvalidArityError, "function length() expects one argument"
174
182
  end
175
183
  case value
176
184
  when Hash, Array, String then value.size
177
- else raise Errors::InvalidTypeError, "function length() expects string, array or object"
185
+ else return maybe_raise Errors::InvalidTypeError, "function length() expects string, array or object"
178
186
  end
179
187
  end
180
188
  end
@@ -186,18 +194,18 @@ module JMESPath
186
194
  if args.count == 1
187
195
  values = args.first
188
196
  else
189
- raise Errors::InvalidArityError, "function max() expects one argument"
197
+ return maybe_raise Errors::InvalidArityError, "function max() expects one argument"
190
198
  end
191
199
  if Array === values
192
200
  values.inject(values.first) do |max, v|
193
201
  if Numeric === v
194
202
  v > max ? v : max
195
203
  else
196
- raise Errors::InvalidTypeError, "function max() expects numeric values"
204
+ return maybe_raise Errors::InvalidTypeError, "function max() expects numeric values"
197
205
  end
198
206
  end
199
207
  else
200
- raise Errors::InvalidTypeError, "function max() expects an array"
208
+ return maybe_raise Errors::InvalidTypeError, "function max() expects an array"
201
209
  end
202
210
  end
203
211
  end
@@ -209,18 +217,18 @@ module JMESPath
209
217
  if args.count == 1
210
218
  values = args.first
211
219
  else
212
- raise Errors::InvalidArityError, "function min() expects one argument"
220
+ return maybe_raise Errors::InvalidArityError, "function min() expects one argument"
213
221
  end
214
222
  if Array === values
215
223
  values.inject(values.first) do |min, v|
216
224
  if Numeric === v
217
225
  v < min ? v : min
218
226
  else
219
- raise Errors::InvalidTypeError, "function min() expects numeric values"
227
+ return maybe_raise Errors::InvalidTypeError, "function min() expects numeric values"
220
228
  end
221
229
  end
222
230
  else
223
- raise Errors::InvalidTypeError, "function min() expects an array"
231
+ return maybe_raise Errors::InvalidTypeError, "function min() expects an array"
224
232
  end
225
233
  end
226
234
  end
@@ -234,7 +242,7 @@ module JMESPath
234
242
  if args.count == 1
235
243
  TYPE_NAMES[get_type(args.first)]
236
244
  else
237
- raise Errors::InvalidArityError, "function type() expects one argument"
245
+ return maybe_raise Errors::InvalidArityError, "function type() expects one argument"
238
246
  end
239
247
  end
240
248
  end
@@ -252,10 +260,10 @@ module JMESPath
252
260
  else raise NotImplementedError
253
261
  end
254
262
  else
255
- raise Errors::InvalidTypeError, "function keys() expects a hash"
263
+ return maybe_raise Errors::InvalidTypeError, "function keys() expects a hash"
256
264
  end
257
265
  else
258
- raise Errors::InvalidArityError, "function keys() expects one argument"
266
+ return maybe_raise Errors::InvalidArityError, "function keys() expects one argument"
259
267
  end
260
268
  end
261
269
  end
@@ -271,10 +279,10 @@ module JMESPath
271
279
  elsif Array === value
272
280
  value
273
281
  else
274
- raise Errors::InvalidTypeError, "function values() expects an array or a hash"
282
+ return maybe_raise Errors::InvalidTypeError, "function values() expects an array or a hash"
275
283
  end
276
284
  else
277
- raise Errors::InvalidArityError, "function values() expects one argument"
285
+ return maybe_raise Errors::InvalidArityError, "function values() expects one argument"
278
286
  end
279
287
  end
280
288
  end
@@ -287,14 +295,14 @@ module JMESPath
287
295
  glue = args[0]
288
296
  values = args[1]
289
297
  if !(String === glue)
290
- raise Errors::InvalidTypeError, "function join() expects the first argument to be a string"
298
+ return maybe_raise Errors::InvalidTypeError, "function join() expects the first argument to be a string"
291
299
  elsif Array === values && values.all? { |v| String === v }
292
300
  values.join(glue)
293
301
  else
294
- raise Errors::InvalidTypeError, "function join() expects values to be an array of strings"
302
+ return maybe_raise Errors::InvalidTypeError, "function join() expects values to be an array of strings"
295
303
  end
296
304
  else
297
- raise Errors::InvalidArityError, "function join() expects an array of strings"
305
+ return maybe_raise Errors::InvalidArityError, "function join() expects an array of strings"
298
306
  end
299
307
  end
300
308
  end
@@ -305,9 +313,9 @@ module JMESPath
305
313
  def call(args)
306
314
  if args.count == 1
307
315
  value = args.first
308
- String === value ? value : MultiJson.dump(value)
316
+ String === value ? value : JSON.dump(value)
309
317
  else
310
- raise Errors::InvalidArityError, "function to_string() expects one argument"
318
+ return maybe_raise Errors::InvalidArityError, "function to_string() expects one argument"
311
319
  end
312
320
  end
313
321
  end
@@ -324,7 +332,7 @@ module JMESPath
324
332
  nil
325
333
  end
326
334
  else
327
- raise Errors::InvalidArityError, "function to_number() expects one argument"
335
+ return maybe_raise Errors::InvalidArityError, "function to_number() expects one argument"
328
336
  end
329
337
  end
330
338
  end
@@ -338,11 +346,11 @@ module JMESPath
338
346
  if Numeric === n
339
347
  sum + n
340
348
  else
341
- raise Errors::InvalidTypeError, "function sum() expects values to be numeric"
349
+ return maybe_raise Errors::InvalidTypeError, "function sum() expects values to be numeric"
342
350
  end
343
351
  end
344
352
  else
345
- raise Errors::InvalidArityError, "function sum() expects one argument"
353
+ return maybe_raise Errors::InvalidArityError, "function sum() expects one argument"
346
354
  end
347
355
  end
348
356
  end
@@ -354,7 +362,7 @@ module JMESPath
354
362
  if args.count > 0
355
363
  args.find { |value| !value.nil? }
356
364
  else
357
- raise Errors::InvalidArityError, "function not_null() expects one or more arguments"
365
+ return maybe_raise Errors::InvalidArityError, "function not_null() expects one or more arguments"
358
366
  end
359
367
  end
360
368
  end
@@ -374,14 +382,14 @@ module JMESPath
374
382
  if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
375
383
  a <=> b
376
384
  else
377
- raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
385
+ return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
378
386
  end
379
387
  end
380
388
  else
381
- raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
389
+ return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
382
390
  end
383
391
  else
384
- raise Errors::InvalidArityError, "function sort() expects one argument"
392
+ return maybe_raise Errors::InvalidArityError, "function sort() expects one argument"
385
393
  end
386
394
  end
387
395
  end
@@ -404,14 +412,14 @@ module JMESPath
404
412
  if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
405
413
  a_value <=> b_value
406
414
  else
407
- raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
415
+ return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
408
416
  end
409
417
  end
410
418
  else
411
- raise Errors::InvalidTypeError, "function sort_by() expects an array and an expression"
419
+ return maybe_raise Errors::InvalidTypeError, "function sort_by() expects an array and an expression"
412
420
  end
413
421
  else
414
- raise Errors::InvalidArityError, "function sort_by() expects two arguments"
422
+ return maybe_raise Errors::InvalidArityError, "function sort_by() expects two arguments"
415
423
  end
416
424
  end
417
425
  end
@@ -429,14 +437,14 @@ module JMESPath
429
437
  if get_type(value) == NUMBER_TYPE
430
438
  value
431
439
  else
432
- raise Errors::InvalidTypeError, "function #{mode}() expects values to be an numbers"
440
+ return maybe_raise Errors::InvalidTypeError, "function #{mode}() expects values to be an numbers"
433
441
  end
434
442
  end
435
443
  else
436
- raise Errors::InvalidTypeError, "function #{mode}() expects an array and an expression"
444
+ return maybe_raise Errors::InvalidTypeError, "function #{mode}() expects an array and an expression"
437
445
  end
438
446
  else
439
- raise Errors::InvalidArityError, "function #{mode}() expects two arguments"
447
+ return maybe_raise Errors::InvalidArityError, "function #{mode}() expects two arguments"
440
448
  end
441
449
  end
442
450
  end
@@ -6,11 +6,13 @@ module JMESPath
6
6
  @start = start
7
7
  @stop = stop
8
8
  @step = step
9
+ raise Errors::InvalidValueError.new('slice step cannot be 0') if @step == 0
9
10
  end
10
11
 
11
12
  def visit(value)
12
13
  if String === value || Array === value
13
14
  start, stop, step = adjust_slice(value.size, @start, @stop, @step)
15
+ return unless start && stop && step
14
16
  result = []
15
17
  if step > 0
16
18
  i = start
@@ -44,8 +46,6 @@ module JMESPath
44
46
  def adjust_slice(length, start, stop, step)
45
47
  if step.nil?
46
48
  step = 1
47
- elsif step == 0
48
- raise Errors::RuntimeError, 'slice step cannot be 0'
49
49
  end
50
50
 
51
51
  if start.nil?
@@ -20,6 +20,7 @@ module JMESPath
20
20
  # @option options [Lexer] :lexer
21
21
  def initialize(options = {})
22
22
  @lexer = options[:lexer] || Lexer.new()
23
+ @disable_visit_errors = options[:disable_visit_errors]
23
24
  end
24
25
 
25
26
  # @param [String<JMESPath>] expression
@@ -188,7 +189,7 @@ module JMESPath
188
189
  end
189
190
  end
190
191
  stream.next
191
- Nodes::Function.create(name, args)
192
+ Nodes::Function.create(name, args, :disable_visit_errors => @disable_visit_errors)
192
193
  end
193
194
 
194
195
  def led_or(stream, left)
@@ -49,6 +49,11 @@ module JMESPath
49
49
  # searches, so it's highly recommended if you're using the same expression
50
50
  # multiple times and have not disabled caching. Defaults to `true`.
51
51
  #
52
+ # @option options [Boolean] :disable_visit_errors (false) When `true`,
53
+ # no errors will be raised during runtime processing. Parse errors
54
+ # will still be raised, but unexpected data sent to visit will
55
+ # result in nil being returned.
56
+ #
52
57
  # @option options [Parser] :parser
53
58
  #
54
59
  def initialize(options = {})
@@ -68,7 +73,7 @@ module JMESPath
68
73
  private
69
74
 
70
75
  def create_parser(options)
71
- parser = Parser.new
76
+ parser = Parser.new(:disable_visit_errors => !!options[:disable_visit_errors])
72
77
  unless options[:optimize_expression] == false
73
78
  parser = OptimizingParser.new(parser)
74
79
  end
@@ -1,3 +1,3 @@
1
1
  module JMESPath
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'
3
3
  end
data/lib/jmespath.rb CHANGED
@@ -23,14 +23,14 @@ module JMESPath
23
23
  # @param [Hash] data
24
24
  # @return [Mixed,nil] Returns the matched values. Returns `nil` if the
25
25
  # expression does not resolve inside `data`.
26
- def search(expression, data)
26
+ def search(expression, data, runtime_options = {})
27
27
  data = case data
28
28
  when Hash, Struct then data # check for most common case first
29
29
  when Pathname then load_json(data)
30
30
  when IO, StringIO then JSON.load(data.read)
31
31
  else data
32
32
  end
33
- Runtime.new.search(expression, data)
33
+ Runtime.new(runtime_options).search(expression, data)
34
34
  end
35
35
 
36
36
  # @api private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: burtpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Rowe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-09 00:00:00.000000000 Z
12
+ date: 2015-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json