jmespath 0.2.0 → 0.9.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
2
  SHA1:
3
- metadata.gz: 1dbe0a2b46b3a401846035f1c310a6d9e3e90aeb
4
- data.tar.gz: c25be88050e43b57d065b976cce0fd6b6eebcff0
3
+ metadata.gz: cd9b114d1de9308e2c28037ebf5664841fa60524
4
+ data.tar.gz: 2169a15d13d0ece59efe976b9e1d2da0f5d34aa5
5
5
  SHA512:
6
- metadata.gz: bea4a7c9e7d4013eefb09b186e30c8012d44ebf56fed7611cb5df19ee533e0a0b9b183bc031268e224d36b7c2afb96a4ff092cc8db22ba5d4e295169ee8afe7c
7
- data.tar.gz: f94c1a2a49ee75d6f86a345d0ac2f31d799c407e376697302ed29814d5733b3612ba822b1151b2f19bba9814eb95e9f175e7b75ba399a5ba629d3d5af5eb35d7
6
+ metadata.gz: 7bfe1d21d8aa9c7e05ba61e600b0f9fce2af250d61058807599bff87ee196ecd62343c288eae5926eb57b6f4456d8916a45adc9c55bf8b25b562c9b0b74909bf
7
+ data.tar.gz: 1396c1b380fc0a5fca6130bc2fb2b4b092c2d789948e015d642cbc884b665acd5906e362073c5cd680704ce14645ab0546f0f8f1d83a66539f13890d59374c27
data/lib/jmespath.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'multi_json'
2
+
1
3
  module JMESPath
2
4
 
3
5
  autoload :Errors, 'jmespath/errors'
@@ -12,12 +14,26 @@ module JMESPath
12
14
 
13
15
  class << self
14
16
 
15
- # @param [String<JMESPath>] expression
17
+
18
+ # @param [String] expression A valid
19
+ # [JMESPath](https://github.com/boto/jmespath) expression.
16
20
  # @param [Hash] data
17
- # @return [Mixed,nil]
21
+ # @return [Mixed,nil] Returns the matched values. Returns `nil` if the
22
+ # expression does not resolve inside `data`.
18
23
  def search(expression, data)
24
+ data = case data
25
+ when Hash, Struct then data # check for most common case first
26
+ when Pathname then load_json(data)
27
+ when IO, StringIO then MultiJson.load(data.read)
28
+ else data
29
+ end
19
30
  Runtime.new.search(expression, data)
20
31
  end
21
32
 
33
+ # @api private
34
+ def load_json(path)
35
+ MultiJson.load(File.open(path, 'r', encoding: 'UTF-8') { |f| f.read })
36
+ end
37
+
22
38
  end
23
39
  end
@@ -1,5 +1,3 @@
1
- require 'multi_json'
2
-
3
1
  module JMESPath
4
2
  # @api private
5
3
  class Lexer
@@ -25,7 +25,6 @@ module JMESPath
25
25
  # @param [String<JMESPath>] expression
26
26
  def parse(expression)
27
27
  stream = TokenStream.new(expression, @lexer.tokenize(expression))
28
- #puts "\n" + stream.inspect + "\n\n"
29
28
  result = expr(stream)
30
29
  if stream.token.type != :eof
31
30
  raise Errors::SyntaxError, "expected :eof got #{stream.token.type}"
@@ -48,10 +47,8 @@ module JMESPath
48
47
  # @param [TokenStream] stream
49
48
  # @param [Integer] rbp Right binding power
50
49
  def expr(stream, rbp = 0)
51
- #puts "nud_#{stream.token.type}"
52
50
  left = send("nud_#{stream.token.type}", stream)
53
51
  while rbp < stream.token.binding_power
54
- #puts "#{rbp} #{stream.token.binding_power} led_#{stream.token.type}"
55
52
  left = send("led_#{stream.token.type}", stream, left)
56
53
  end
57
54
  left
@@ -1,7 +1,14 @@
1
+ require 'thread'
2
+
1
3
  module JMESPath
2
4
  # @api private
3
5
  class Runtime
4
6
 
7
+ # @api private
8
+ CACHE = {}
9
+
10
+ MUTEX = Mutex.new
11
+
5
12
  # @option options [Parser] :parser
6
13
  # @option options [Interpreter] :interpreter
7
14
  def initialize(options = {})
@@ -13,7 +20,26 @@ module JMESPath
13
20
  # @param [Hash] data
14
21
  # @return [Mixed,nil]
15
22
  def search(expression, data)
16
- @interpreter.visit(@parser.parse(expression), data)
23
+ @interpreter.visit(parse(expression), data)
24
+ end
25
+
26
+ private
27
+
28
+ def parse(expression)
29
+ if CACHE[expression]
30
+ CACHE[expression]
31
+ else
32
+ MUTEX.synchronize { CACHE[expression] = @parser.parse(expression) }
33
+ end
34
+ end
35
+
36
+ class << self
37
+
38
+ # @api private
39
+ def clear_cache
40
+ MUTEX.synchronize { CACHE.clear }
41
+ end
42
+
17
43
  end
18
44
 
19
45
  end
@@ -21,17 +21,18 @@ module JMESPath
21
21
  case node[:type]
22
22
 
23
23
  when :field
24
- if hash_like?(value)
25
- value[node[:key]]
26
- else
27
- nil
24
+ key = node[:key]
25
+ case value
26
+ when Hash then value.key?(key) ? value[key] : value[key.to_sym]
27
+ when Struct then value[key]
28
+ else nil
28
29
  end
29
30
 
30
31
  when :subexpression
31
32
  dispatch(node[:children][1], dispatch(node[:children][0], value))
32
33
 
33
34
  when :index
34
- if array_like?(value)
35
+ if Array === value
35
36
  value[node[:index]]
36
37
  else
37
38
  nil
@@ -46,7 +47,7 @@ module JMESPath
46
47
  projection(left.values, node)
47
48
  elsif node[:from] == :object && left == []
48
49
  projection(left, node)
49
- elsif node[:from] == :array && array_like?(left)
50
+ elsif node[:from] == :array && Array === left
50
51
  projection(left, node)
51
52
  else
52
53
  nil
@@ -54,9 +55,9 @@ module JMESPath
54
55
 
55
56
  when :flatten
56
57
  value = dispatch(node[:children][0], value)
57
- if array_like?(value)
58
+ if Array === value
58
59
  value.inject([]) do |values, v|
59
- values + (array_like?(v) ? v : [v])
60
+ values + (Array === v ? v : [v])
60
61
  end
61
62
  else
62
63
  nil
@@ -132,10 +133,6 @@ module JMESPath
132
133
  Hash === value || Struct === value
133
134
  end
134
135
 
135
- def array_like?(value)
136
- Array === value
137
- end
138
-
139
136
  def projection(values, node)
140
137
  values.inject([]) do |list, v|
141
138
  list << dispatch(node[:children][1], v)
@@ -292,7 +289,7 @@ module JMESPath
292
289
  value = args.first
293
290
  if hash_like?(value)
294
291
  value.values
295
- elsif array_like?(value)
292
+ elsif Array === value
296
293
  value
297
294
  else
298
295
  raise Errors::InvalidTypeError, "function values() expects an array or a hash"
@@ -308,7 +305,7 @@ module JMESPath
308
305
  values = args[1]
309
306
  if !(String === glue)
310
307
  raise Errors::InvalidTypeError, "function join() expects the first argument to be a string"
311
- elsif array_like?(values) && values.all? { |v| String === v }
308
+ elsif Array === values && values.all? { |v| String === v }
312
309
  values.join(glue)
313
310
  else
314
311
  raise Errors::InvalidTypeError, "function join() expects values to be an array of strings"
@@ -341,7 +338,7 @@ module JMESPath
341
338
  end
342
339
 
343
340
  def function_sum(*args)
344
- if args.count == 1 && array_like?(args.first)
341
+ if args.count == 1 && Array === args.first
345
342
  args.first.inject(0) do |sum,n|
346
343
  if Numeric === n
347
344
  sum + n
@@ -365,7 +362,7 @@ module JMESPath
365
362
  def function_sort(*args)
366
363
  if args.count == 1
367
364
  value = args.first
368
- if array_like?(value)
365
+ if Array === value
369
366
  value.sort do |a, b|
370
367
  a_type = get_type(a)
371
368
  b_type = get_type(b)
@@ -437,7 +434,7 @@ module JMESPath
437
434
  end
438
435
 
439
436
  def function_slice(values, *args)
440
- if String === values || array_like?(values)
437
+ if String === values || Array === values
441
438
  _slice(values, *args)
442
439
  else
443
440
  nil
@@ -514,7 +511,7 @@ module JMESPath
514
511
  when ExprNode === value then 'expression'
515
512
  when String === value then 'string'
516
513
  when hash_like?(value) then 'object'
517
- when array_like?(value) then 'array'
514
+ when Array === value then 'array'
518
515
  when [true, false].include?(value) then 'boolean'
519
516
  when value.nil? then 'null'
520
517
  when Numeric === value then 'number'
@@ -1,3 +1,3 @@
1
1
  module JMESPath
2
- VERSION = '0.2.0'
2
+ VERSION = '0.9.0'
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: 0.2.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Rowe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2014-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json