jmespath 0.9.0 → 1.0.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: cd9b114d1de9308e2c28037ebf5664841fa60524
4
- data.tar.gz: 2169a15d13d0ece59efe976b9e1d2da0f5d34aa5
3
+ metadata.gz: 4498ed69dc541e13ec1b682b754419a7ea88aa7d
4
+ data.tar.gz: c98d5347758fe4eb6418b1f15a0b253f4a903e88
5
5
  SHA512:
6
- metadata.gz: 7bfe1d21d8aa9c7e05ba61e600b0f9fce2af250d61058807599bff87ee196ecd62343c288eae5926eb57b6f4456d8916a45adc9c55bf8b25b562c9b0b74909bf
7
- data.tar.gz: 1396c1b380fc0a5fca6130bc2fb2b4b092c2d789948e015d642cbc884b665acd5906e362073c5cd680704ce14645ab0546f0f8f1d83a66539f13890d59374c27
6
+ metadata.gz: a81a6a8994b7fa722a8dc77f70ac5ee43c1799a756046ebd4b44f29a31c0a5e7a269df180163b2c83925ca78593025172a73ad059cc5326ceabaa5ff15d7d9fa
7
+ data.tar.gz: ad285ecab333df630843157cc6c5c93be080152cd84efcb79fe44108bbff17adc2ef437cb9e22aa2ffd7eca36151b69f35c33ba68036dc01f51840af8a952d07
data/lib/jmespath.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'multi_json'
2
+ require 'pathname'
2
3
 
3
4
  module JMESPath
4
5
 
6
+ autoload :CachingParser, 'jmespath/caching_parser'
5
7
  autoload :Errors, 'jmespath/errors'
6
8
  autoload :ExprNode, 'jmespath/expr_node'
7
9
  autoload :Lexer, 'jmespath/lexer'
@@ -0,0 +1,30 @@
1
+ require 'thread'
2
+
3
+ module JMESPath
4
+ class CachingParser
5
+
6
+ def initialize(options = {})
7
+ @parser = options[:parser] || Parser.new(options)
8
+ @mutex = Mutex.new
9
+ @cache = {}
10
+ end
11
+
12
+ def parse(expression)
13
+ if cached = @cache[expression]
14
+ cached
15
+ else
16
+ cache_expression(expression)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def cache_expression(expression)
23
+ @mutex.synchronize do
24
+ @cache.clear if @cache.size > 1000
25
+ @cache[expression] = @parser.parse(expression)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -1,46 +1,71 @@
1
- require 'thread'
2
-
3
1
  module JMESPath
4
2
  # @api private
5
3
  class Runtime
6
4
 
7
5
  # @api private
8
- CACHE = {}
9
-
10
- MUTEX = Mutex.new
6
+ DEFAULT_PARSER = CachingParser.new
11
7
 
12
- # @option options [Parser] :parser
8
+ # Constructs a new runtime object for evaluating JMESPath expressions.
9
+ #
10
+ # runtime = JMESPath::Runtime.new
11
+ # runtime.search(expression, data)
12
+ # #=> ...
13
+ #
14
+ # ## Caching
15
+ #
16
+ # When constructing a {Runtime}, the default parser caches expressions.
17
+ # This significantly speeds up calls to {#search} multiple times
18
+ # with the same expression but different data. To disable caching, pass
19
+ # `:cache_expressions => false` to the constructor or pass a custom
20
+ # `:parser`.
21
+ #
22
+ # @example Re-use a Runtime, caching enabled by default
23
+ #
24
+ # runtime = JMESPath::Runtime.new
25
+ # runtime.parser
26
+ # #=> #<JMESPath::CachingParser ...>
27
+ #
28
+ # @example Disable caching
29
+ #
30
+ # runtime = JMESPath::Runtime.new(cache_expressions: false)
31
+ # runtime.parser
32
+ # #=> #<JMESPath::Parser ...>
33
+ #
34
+ # @option options [Boolean] :cache_expressions (true) When `false`, a non
35
+ # caching parser will be used. When `true`, a shared instance of
36
+ # {CachingParser} is used. Defaults to `true`.
37
+ #
38
+ # @option options [Parser,CachingParser] :parser
39
+ #
13
40
  # @option options [Interpreter] :interpreter
41
+ #
14
42
  def initialize(options = {})
15
- @parser = options[:parser] || Parser.new
43
+ @parser = options[:parser] || default_parser(options)
16
44
  @interpreter = options[:interpreter] || TreeInterpreter.new
17
45
  end
18
46
 
47
+ # @return [Parser, CachingParser]
48
+ attr_reader :parser
49
+
50
+ # @return [Interpreter]
51
+ attr_reader :interpreter
52
+
19
53
  # @param [String<JMESPath>] expression
20
54
  # @param [Hash] data
21
55
  # @return [Mixed,nil]
22
56
  def search(expression, data)
23
- @interpreter.visit(parse(expression), data)
57
+ @interpreter.visit(@parser.parse(expression), data)
24
58
  end
25
59
 
26
60
  private
27
61
 
28
- def parse(expression)
29
- if CACHE[expression]
30
- CACHE[expression]
62
+ def default_parser(options)
63
+ if options[:cache_expressions] == false
64
+ Parser.new(options)
31
65
  else
32
- MUTEX.synchronize { CACHE[expression] = @parser.parse(expression) }
66
+ DEFAULT_PARSER
33
67
  end
34
68
  end
35
69
 
36
- class << self
37
-
38
- # @api private
39
- def clear_cache
40
- MUTEX.synchronize { CACHE.clear }
41
- end
42
-
43
- end
44
-
45
70
  end
46
71
  end
@@ -1,3 +1,3 @@
1
1
  module JMESPath
2
- VERSION = '0.9.0'
2
+ VERSION = '1.0.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.9.0
4
+ version: 1.0.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-27 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -30,6 +30,7 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - lib/jmespath/caching_parser.rb
33
34
  - lib/jmespath/errors.rb
34
35
  - lib/jmespath/expr_node.rb
35
36
  - lib/jmespath/lexer.rb