jmespath 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of jmespath might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f2d08f64e74ad4927d6323f98b91486ad7d8f79c
4
- data.tar.gz: ce7796a88dab9241efacdeac1a3b36c9083ff6ca
2
+ SHA256:
3
+ metadata.gz: 802d17af3f269cfbd38879ff753082257effba66f383f415667b1018d9d68c5c
4
+ data.tar.gz: 3b9ec8bf65b3dbcf00273667510385a406fa2c23cd64890557a76263944a8b0f
5
5
  SHA512:
6
- metadata.gz: ca3d9a98c00cedf8b5bafdc70d3cdb8b2ee754ef85dfa270807650fa9517d50db68e513d3713d431ac8ac1b9bdb9757ca76b2758bc8ddfde1add3eedb08973e4
7
- data.tar.gz: 7c09a5ee313e2f7feba1334c240fdc2a820eb9127d5e2db3392ac1dad3b29b2b8b4c55aee4f6c4309417d857df84f8854201a79ba5fe405d724f91a44db331d3
6
+ metadata.gz: c95fe346be9e1e36a67869cad050a03e74ae414ceedb09049210419a9d4c5455df38e49403bcedf1434b56e8b45d6142527bdb3d2af86919cc833da021251e16
7
+ data.tar.gz: 73e3c073ccf6b57c65c2246d12fd4a3c5d5f3beb603772966c4ec92371f9ed10f4e1a10b66a1619f50fdd78137a1a6b0998f65b4e7f966a928e5fc2a8d7393be
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.5.0
@@ -42,13 +42,13 @@ module JMESPath
42
42
 
43
43
  class Eq < Comparator
44
44
  def check(left_value, right_value)
45
- left_value == right_value
45
+ Util.as_json(left_value) == Util.as_json(right_value)
46
46
  end
47
47
  end
48
48
 
49
49
  class Neq < Comparator
50
50
  def check(left_value, right_value)
51
- left_value != right_value
51
+ Util.as_json(left_value) != Util.as_json(right_value)
52
52
  end
53
53
  end
54
54
 
@@ -43,7 +43,7 @@ module JMESPath
43
43
  COMPARATOR_TO_CONDITION[Comparators::Eq] = self
44
44
 
45
45
  def visit(value)
46
- @left.visit(value) == @right.visit(value) ? @child.visit(value) : nil
46
+ Util.as_json(@left.visit(value)) == Util.as_json(@right.visit(value)) ? @child.visit(value) : nil
47
47
  end
48
48
 
49
49
  def optimize
@@ -62,7 +62,7 @@ module JMESPath
62
62
  end
63
63
 
64
64
  def visit(value)
65
- @left.visit(value) == @right ? @child.visit(value) : nil
65
+ Util.as_json(@left.visit(value)) == @right ? @child.visit(value) : nil
66
66
  end
67
67
  end
68
68
 
@@ -70,7 +70,7 @@ module JMESPath
70
70
  COMPARATOR_TO_CONDITION[Comparators::Neq] = self
71
71
 
72
72
  def visit(value)
73
- @left.visit(value) != @right.visit(value) ? @child.visit(value) : nil
73
+ Util.as_json(@left.visit(value)) != Util.as_json(@right.visit(value)) ? @child.visit(value) : nil
74
74
  end
75
75
 
76
76
  def optimize
@@ -89,7 +89,7 @@ module JMESPath
89
89
  end
90
90
 
91
91
  def visit(value)
92
- @left.visit(value) != @right ? @child.visit(value) : nil
92
+ Util.as_json(@left.visit(value)) != @right ? @child.visit(value) : nil
93
93
  end
94
94
  end
95
95
 
@@ -8,9 +8,10 @@ module JMESPath
8
8
  end
9
9
 
10
10
  def visit(value)
11
- if value.is_a?(Array) && @key.is_a?(Integer)
12
- value[@key]
13
- elsif value.is_a?(Hash)
11
+ if value.respond_to?(:to_ary) && @key.is_a?(Integer)
12
+ value.to_ary[@key]
13
+ elsif value.respond_to?(:to_hash)
14
+ value = value.to_hash
14
15
  if !(v = value[@key]).nil?
15
16
  v
16
17
  elsif @key_sym && !(v = value[@key_sym]).nil?
@@ -48,9 +49,10 @@ module JMESPath
48
49
 
49
50
  def visit(obj)
50
51
  @keys.reduce(obj) do |value, key|
51
- if value.is_a?(Array) && key.is_a?(Integer)
52
- value[key]
53
- elsif value.is_a?(Hash)
52
+ if value.respond_to?(:to_ary) && key.is_a?(Integer)
53
+ value.to_ary[key]
54
+ elsif value.respond_to?(:to_hash)
55
+ value = value.to_hash
54
56
  if !(v = value[key]).nil?
55
57
  v
56
58
  elsif (sym = @key_syms[key]) && !(v = value[sym]).nil?
@@ -8,10 +8,10 @@ module JMESPath
8
8
 
9
9
  def visit(value)
10
10
  value = @child.visit(value)
11
- if Array === value
12
- value.each_with_object([]) do |v, values|
13
- if Array === v
14
- values.concat(v)
11
+ if value.respond_to?(:to_ary)
12
+ value.to_ary.each_with_object([]) do |v, values|
13
+ if v.respond_to?(:to_ary)
14
+ values.concat(v.to_ary)
15
15
  else
16
16
  values.push(v)
17
17
  end
@@ -50,14 +50,20 @@ module JMESPath
50
50
 
51
51
  module TypeChecker
52
52
  def get_type(value)
53
- case value
54
- when String then STRING_TYPE
55
- when true, false then BOOLEAN_TYPE
56
- when nil then NULL_TYPE
57
- when Numeric then NUMBER_TYPE
58
- when Hash, Struct then OBJECT_TYPE
59
- when Array then ARRAY_TYPE
60
- when Expression then EXPRESSION_TYPE
53
+ if value.respond_to?(:to_str)
54
+ STRING_TYPE
55
+ elsif value == true || value == false
56
+ BOOLEAN_TYPE
57
+ elsif value == nil
58
+ NULL_TYPE
59
+ elsif value.is_a?(Numeric)
60
+ NUMBER_TYPE
61
+ elsif value.respond_to?(:to_hash) || value.is_a?(Struct)
62
+ OBJECT_TYPE
63
+ elsif value.respond_to?(:to_ary)
64
+ ARRAY_TYPE
65
+ elsif value.is_a?(Expression)
66
+ EXPRESSION_TYPE
61
67
  end
62
68
  end
63
69
 
@@ -106,7 +112,8 @@ module JMESPath
106
112
  else
107
113
  return maybe_raise Errors::InvalidArityError, "function avg() expects one argument"
108
114
  end
109
- if Array === values
115
+ if values.respond_to?(:to_ary)
116
+ values = values.to_ary
110
117
  return nil if values.empty?
111
118
  values.inject(0) do |total,n|
112
119
  if Numeric === n
@@ -144,9 +151,11 @@ module JMESPath
144
151
  def call(args)
145
152
  if args.count == 2
146
153
  haystack = args[0]
147
- needle = args[1]
148
- if String === haystack || Array === haystack
149
- haystack.include?(needle)
154
+ needle = Util.as_json(args[1])
155
+ if haystack.respond_to?(:to_str)
156
+ haystack.to_str.include?(needle)
157
+ elsif haystack.respond_to?(:to_ary)
158
+ haystack.to_ary.any? { |e| Util.as_json(e) == needle }
150
159
  else
151
160
  return maybe_raise Errors::InvalidTypeError, "contains expects 2nd arg to be a list"
152
161
  end
@@ -182,9 +191,14 @@ module JMESPath
182
191
  else
183
192
  return maybe_raise Errors::InvalidArityError, "function length() expects one argument"
184
193
  end
185
- case value
186
- when Hash, Array, String then value.size
187
- else return maybe_raise Errors::InvalidTypeError, "function length() expects string, array or object"
194
+ if value.respond_to?(:to_hash)
195
+ value.to_hash.size
196
+ elsif value.respond_to?(:to_ary)
197
+ value.to_ary.size
198
+ elsif value.respond_to?(:to_str)
199
+ value.to_str.size
200
+ else
201
+ return maybe_raise Errors::InvalidTypeError, "function length() expects string, array or object"
188
202
  end
189
203
  end
190
204
  end
@@ -202,8 +216,8 @@ module JMESPath
202
216
  else
203
217
  return maybe_raise Errors::InvalidTypeError, "function map() expects the first argument to be an expression"
204
218
  end
205
- if Array === args[1]
206
- list = args[1]
219
+ if args[1].respond_to?(:to_ary)
220
+ list = args[1].to_ary
207
221
  else
208
222
  return maybe_raise Errors::InvalidTypeError, "function map() expects the second argument to be an list"
209
223
  end
@@ -223,7 +237,8 @@ module JMESPath
223
237
  else
224
238
  return maybe_raise Errors::InvalidArityError, "function max() expects one argument"
225
239
  end
226
- if Array === values
240
+ if values.respond_to?(:to_ary)
241
+ values = values.to_ary
227
242
  return nil if values.empty?
228
243
  first = values.first
229
244
  first_type = get_type(first)
@@ -258,7 +273,8 @@ module JMESPath
258
273
  else
259
274
  return maybe_raise Errors::InvalidArityError, "function min() expects one argument"
260
275
  end
261
- if Array === values
276
+ if values.respond_to?(:to_ary)
277
+ values = values.to_ary
262
278
  return nil if values.empty?
263
279
  first = values.first
264
280
  first_type = get_type(first)
@@ -302,12 +318,10 @@ module JMESPath
302
318
  def call(args)
303
319
  if args.count == 1
304
320
  value = args.first
305
- if hash_like?(value)
306
- case value
307
- when Hash then value.keys.map(&:to_s)
308
- when Struct then value.members.map(&:to_s)
309
- else raise NotImplementedError
310
- end
321
+ if value.respond_to?(:to_hash)
322
+ value.to_hash.keys.map(&:to_s)
323
+ elsif value.is_a?(Struct)
324
+ value.members.map(&:to_s)
311
325
  else
312
326
  return maybe_raise Errors::InvalidTypeError, "function keys() expects a hash"
313
327
  end
@@ -323,10 +337,12 @@ module JMESPath
323
337
  def call(args)
324
338
  if args.count == 1
325
339
  value = args.first
326
- if hash_like?(value)
340
+ if value.respond_to?(:to_hash)
341
+ value.to_hash.values
342
+ elsif value.is_a?(Struct)
327
343
  value.values
328
- elsif Array === value
329
- value
344
+ elsif value.respond_to?(:to_ary)
345
+ value.to_ary
330
346
  else
331
347
  return maybe_raise Errors::InvalidTypeError, "function values() expects an array or a hash"
332
348
  end
@@ -343,10 +359,10 @@ module JMESPath
343
359
  if args.count == 2
344
360
  glue = args[0]
345
361
  values = args[1]
346
- if !(String === glue)
362
+ if !glue.respond_to?(:to_str)
347
363
  return maybe_raise Errors::InvalidTypeError, "function join() expects the first argument to be a string"
348
- elsif Array === values && values.all? { |v| String === v }
349
- values.join(glue)
364
+ elsif values.respond_to?(:to_ary) && values.to_ary.all? { |v| v.respond_to?(:to_str) }
365
+ values.to_ary.join(glue)
350
366
  else
351
367
  return maybe_raise Errors::InvalidTypeError, "function join() expects values to be an array of strings"
352
368
  end
@@ -362,7 +378,7 @@ module JMESPath
362
378
  def call(args)
363
379
  if args.count == 1
364
380
  value = args.first
365
- String === value ? value : value.to_json
381
+ value.respond_to?(:to_str) ? value.to_str : value.to_json
366
382
  else
367
383
  return maybe_raise Errors::InvalidArityError, "function to_string() expects one argument"
368
384
  end
@@ -390,8 +406,8 @@ module JMESPath
390
406
  FUNCTIONS['sum'] = self
391
407
 
392
408
  def call(args)
393
- if args.count == 1 && Array === args.first
394
- args.first.inject(0) do |sum,n|
409
+ if args.count == 1 && args.first.respond_to?(:to_ary)
410
+ args.first.to_ary.inject(0) do |sum,n|
395
411
  if Numeric === n
396
412
  sum + n
397
413
  else
@@ -424,7 +440,8 @@ module JMESPath
424
440
  def call(args)
425
441
  if args.count == 1
426
442
  value = args.first
427
- if Array === value
443
+ if value.respond_to?(:to_ary)
444
+ value = value.to_ary
428
445
  # every element in the list must be of the same type
429
446
  array_type = get_type(value[0])
430
447
  if array_type == STRING_TYPE || array_type == NUMBER_TYPE || value.size == 0
@@ -459,7 +476,7 @@ module JMESPath
459
476
  def call(args)
460
477
  if args.count == 2
461
478
  if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
462
- values = args[0]
479
+ values = args[0].to_ary
463
480
  expression = args[1]
464
481
  array_type = get_type(expression.eval(values[0]))
465
482
  if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.size == 0
@@ -495,6 +512,7 @@ module JMESPath
495
512
  values = args[0]
496
513
  expression = args[1]
497
514
  if get_type(values) == ARRAY_TYPE && get_type(expression) == EXPRESSION_TYPE
515
+ values = values.to_ary
498
516
  type = get_type(expression.eval(values.first))
499
517
  if type != NUMBER_TYPE && type != STRING_TYPE
500
518
  msg = "function #{mode}() expects values to be strings or numbers"
@@ -616,8 +634,10 @@ module JMESPath
616
634
  return maybe_raise Errors::InvalidArityError, msg
617
635
  end
618
636
  value = args.first
619
- if Array === value || String === value
620
- value.reverse
637
+ if value.respond_to?(:to_ary)
638
+ value.to_ary.reverse
639
+ elsif value.respond_to?(:to_str)
640
+ value.to_str.reverse
621
641
  else
622
642
  msg = "function reverse() expects an array or string"
623
643
  return maybe_raise Errors::InvalidTypeError, msg
@@ -630,7 +650,7 @@ module JMESPath
630
650
 
631
651
  def call(args)
632
652
  value = args.first
633
- Array === value ? value : [value]
653
+ value.respond_to?(:to_ary) ? value.to_ary : [value]
634
654
  end
635
655
  end
636
656
  end
@@ -45,8 +45,8 @@ module JMESPath
45
45
 
46
46
  class ArrayProjection < Projection
47
47
  def extract_targets(target)
48
- if Array === target
49
- target
48
+ if target.respond_to?(:to_ary)
49
+ target.to_ary
50
50
  else
51
51
  nil
52
52
  end
@@ -63,7 +63,9 @@ module JMESPath
63
63
 
64
64
  class ObjectProjection < Projection
65
65
  def extract_targets(target)
66
- if hash_like?(target)
66
+ if target.respond_to?(:to_hash)
67
+ target.to_hash.values
68
+ elsif target.is_a?(Struct)
67
69
  target.values
68
70
  else
69
71
  nil
@@ -10,7 +10,7 @@ module JMESPath
10
10
  end
11
11
 
12
12
  def visit(value)
13
- if String === value || Array === value
13
+ if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
14
14
  start, stop, step = adjust_slice(value.size, @start, @stop, @step)
15
15
  result = []
16
16
  if step > 0
@@ -26,7 +26,7 @@ module JMESPath
26
26
  i += step
27
27
  end
28
28
  end
29
- String === value ? result.join : result
29
+ value.respond_to?(:to_str) ? result.join : result
30
30
  else
31
31
  nil
32
32
  end
@@ -80,7 +80,7 @@ module JMESPath
80
80
  end
81
81
 
82
82
  def visit(value)
83
- if String === value || Array === value
83
+ if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
84
84
  value[@start, @stop - @start]
85
85
  else
86
86
  nil
@@ -5,10 +5,6 @@ module JMESPath
5
5
  def visit(value)
6
6
  end
7
7
 
8
- def hash_like?(value)
9
- Hash === value || Struct === value
10
- end
11
-
12
8
  def optimize
13
9
  self
14
10
  end
data/lib/jmespath/util.rb CHANGED
@@ -9,10 +9,26 @@ module JMESPath
9
9
  #
10
10
  def falsey?(value)
11
11
  !value ||
12
- (value.respond_to?(:empty?) && value.empty?) ||
12
+ (value.respond_to?(:to_ary) && value.to_ary.empty?) ||
13
+ (value.respond_to?(:to_hash) && value.to_hash.empty?) ||
14
+ (value.respond_to?(:to_str) && value.to_str.empty?) ||
13
15
  (value.respond_to?(:entries) && !value.entries.any?)
14
16
  # final case necessary to support Enumerable and Struct
15
17
  end
18
+
19
+ def as_json(value)
20
+ if value.respond_to?(:to_ary)
21
+ value.to_ary.map { |e| as_json(e) }
22
+ elsif value.respond_to?(:to_hash)
23
+ hash = {}
24
+ value.to_hash.each_pair { |k, v| hash[k] = as_json(v) }
25
+ hash
26
+ elsif value.respond_to?(:to_str)
27
+ value.to_str
28
+ else
29
+ value
30
+ end
31
+ end
16
32
  end
17
33
  end
18
34
  end
@@ -1,3 +1,3 @@
1
1
  module JMESPath
2
- VERSION = '1.4.0'
2
+ VERSION = File.read(File.expand_path('../../../VERSION', __FILE__)).strip
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jmespath
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Rowe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-04 00:00:00.000000000 Z
11
+ date: 2022-01-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Implements JMESPath for Ruby
14
14
  email: trevorrowe@gmail.com
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - LICENSE.txt
20
+ - VERSION
20
21
  - lib/jmespath.rb
21
22
  - lib/jmespath/caching_parser.rb
22
23
  - lib/jmespath/errors.rb
@@ -50,7 +51,7 @@ homepage: http://github.com/trevorrowe/jmespath.rb
50
51
  licenses:
51
52
  - Apache-2.0
52
53
  metadata: {}
53
- post_install_message:
54
+ post_install_message:
54
55
  rdoc_options: []
55
56
  require_paths:
56
57
  - lib
@@ -65,9 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  - !ruby/object:Gem::Version
66
67
  version: '0'
67
68
  requirements: []
68
- rubyforge_project:
69
- rubygems_version: 2.5.1
70
- signing_key:
69
+ rubygems_version: 3.2.7
70
+ signing_key:
71
71
  specification_version: 4
72
72
  summary: JMESPath - Ruby Edition
73
73
  test_files: []