jmespath 1.4.0 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  module Nodes
@@ -12,9 +13,7 @@ module JMESPath
12
13
  list = []
13
14
  targets.each do |v|
14
15
  vv = @projection.visit(v)
15
- unless vv.nil?
16
- list << vv
17
- end
16
+ list << vv unless vv.nil?
18
17
  end
19
18
  list
20
19
  end
@@ -30,7 +29,7 @@ module JMESPath
30
29
 
31
30
  private
32
31
 
33
- def extract_targets(left_value)
32
+ def extract_targets(_left_value)
34
33
  nil
35
34
  end
36
35
  end
@@ -45,11 +44,7 @@ module JMESPath
45
44
 
46
45
  class ArrayProjection < Projection
47
46
  def extract_targets(target)
48
- if Array === target
49
- target
50
- else
51
- nil
52
- end
47
+ target.to_ary if target.respond_to?(:to_ary)
53
48
  end
54
49
 
55
50
  def fast_instance
@@ -63,10 +58,10 @@ module JMESPath
63
58
 
64
59
  class ObjectProjection < Projection
65
60
  def extract_targets(target)
66
- if hash_like?(target)
61
+ if target.respond_to?(:to_hash)
62
+ target.to_hash.values
63
+ elsif target.is_a?(Struct)
67
64
  target.values
68
- else
69
- nil
70
65
  end
71
66
  end
72
67
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  module Nodes
@@ -6,11 +7,11 @@ module JMESPath
6
7
  @start = start
7
8
  @stop = stop
8
9
  @step = step
9
- raise Errors::InvalidValueError.new('slice step cannot be 0') if @step == 0
10
+ raise Errors::InvalidValueError, 'slice step cannot be 0' if @step == 0
10
11
  end
11
12
 
12
13
  def visit(value)
13
- if String === value || Array === value
14
+ if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
14
15
  start, stop, step = adjust_slice(value.size, @start, @stop, @step)
15
16
  result = []
16
17
  if step > 0
@@ -26,9 +27,7 @@ module JMESPath
26
27
  i += step
27
28
  end
28
29
  end
29
- String === value ? result.join : result
30
- else
31
- nil
30
+ value.respond_to?(:to_str) ? result.join : result
32
31
  end
33
32
  end
34
33
 
@@ -43,21 +42,19 @@ module JMESPath
43
42
  private
44
43
 
45
44
  def adjust_slice(length, start, stop, step)
46
- if step.nil?
47
- step = 1
48
- end
45
+ step = 1 if step.nil?
49
46
 
50
- if start.nil?
51
- start = step < 0 ? length - 1 : 0
52
- else
53
- start = adjust_endpoint(length, start, step)
54
- end
47
+ start = if start.nil?
48
+ step < 0 ? length - 1 : 0
49
+ else
50
+ adjust_endpoint(length, start, step)
51
+ end
55
52
 
56
- if stop.nil?
57
- stop = step < 0 ? -1 : length
58
- else
59
- stop = adjust_endpoint(length, stop, step)
60
- end
53
+ stop = if stop.nil?
54
+ step < 0 ? -1 : length
55
+ else
56
+ adjust_endpoint(length, stop, step)
57
+ end
61
58
  [start, stop, step]
62
59
  end
63
60
 
@@ -80,10 +77,8 @@ module JMESPath
80
77
  end
81
78
 
82
79
  def visit(value)
83
- if String === value || Array === value
80
+ if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil)
84
81
  value[@start, @stop - @start]
85
- else
86
- nil
87
82
  end
88
83
  end
89
84
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  module Nodes
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  module Nodes
@@ -5,19 +6,15 @@ module JMESPath
5
6
  def visit(value)
6
7
  end
7
8
 
8
- def hash_like?(value)
9
- Hash === value || Struct === value
10
- end
11
-
12
9
  def optimize
13
10
  self
14
11
  end
15
12
 
16
- def chains_with?(other)
13
+ def chains_with?(_other)
17
14
  false
18
15
  end
19
16
  end
20
-
17
+
21
18
  require 'jmespath/nodes/subexpression'
22
19
  require 'jmespath/nodes/and'
23
20
  require 'jmespath/nodes/comparator'
@@ -39,7 +36,5 @@ module JMESPath
39
36
  require 'jmespath/nodes/projection'
40
37
  require 'jmespath/nodes/projection'
41
38
  require 'jmespath/nodes/slice'
42
-
43
-
44
39
  end
45
40
  end
@@ -1,28 +1,28 @@
1
+ # frozen_string_literal: true
1
2
  require 'set'
2
3
 
3
4
  module JMESPath
4
5
  # @api private
5
6
  class Parser
6
-
7
7
  AFTER_DOT = Set.new([
8
- Lexer::T_IDENTIFIER, # foo.bar
9
- Lexer::T_QUOTED_IDENTIFIER, # foo."bar"
10
- Lexer::T_STAR, # foo.*
11
- Lexer::T_LBRACE, # foo{a: 0}
12
- Lexer::T_LBRACKET, # foo[1]
13
- Lexer::T_FILTER, # foo.[?bar==10]
14
- ])
8
+ Lexer::T_IDENTIFIER, # foo.bar
9
+ Lexer::T_QUOTED_IDENTIFIER, # foo."bar"
10
+ Lexer::T_STAR, # foo.*
11
+ Lexer::T_LBRACE, # foo{a: 0}
12
+ Lexer::T_LBRACKET, # foo[1]
13
+ Lexer::T_FILTER, # foo.[?bar==10]
14
+ ])
15
15
 
16
16
  NUM_COLON_RBRACKET = Set.new([
17
- Lexer::T_NUMBER,
18
- Lexer::T_COLON,
19
- Lexer::T_RBRACKET,
20
- ])
17
+ Lexer::T_NUMBER,
18
+ Lexer::T_COLON,
19
+ Lexer::T_RBRACKET
20
+ ])
21
21
 
22
22
  COLON_RBRACKET = Set.new([
23
- Lexer::T_COLON,
24
- Lexer::T_RBRACKET,
25
- ])
23
+ Lexer::T_COLON,
24
+ Lexer::T_RBRACKET
25
+ ])
26
26
 
27
27
  CURRENT_NODE = Nodes::Current.new
28
28
 
@@ -34,7 +34,7 @@ module JMESPath
34
34
 
35
35
  # @param [String<JMESPath>] expression
36
36
  def parse(expression)
37
- tokens = @lexer.tokenize(expression)
37
+ tokens = @lexer.tokenize(expression)
38
38
  stream = TokenStream.new(expression, tokens)
39
39
  result = expr(stream)
40
40
  if stream.token.type != Lexer::T_EOF
@@ -110,13 +110,11 @@ module JMESPath
110
110
 
111
111
  def nud_lbrace(stream)
112
112
  valid_keys = Set.new([:quoted_identifier, :identifier])
113
- stream.next(match:valid_keys)
113
+ stream.next(match: valid_keys)
114
114
  pairs = []
115
115
  begin
116
116
  pairs << parse_key_value_pair(stream)
117
- if stream.token.type == :comma
118
- stream.next(match:valid_keys)
119
- end
117
+ stream.next(match: valid_keys) if stream.token.type == :comma
120
118
  end while stream.token.type != :rbrace
121
119
  stream.next
122
120
  Nodes::MultiSelectHash.new(pairs)
@@ -167,7 +165,7 @@ module JMESPath
167
165
  end
168
166
 
169
167
  def led_dot(stream, left)
170
- stream.next(match:AFTER_DOT)
168
+ stream.next(match: AFTER_DOT)
171
169
  if stream.token.type == :star
172
170
  parse_wildcard_object(stream, left)
173
171
  else
@@ -217,12 +215,10 @@ module JMESPath
217
215
  stream.next
218
216
  while stream.token.type != :rparen
219
217
  args << expr(stream, 0)
220
- if stream.token.type == :comma
221
- stream.next
222
- end
218
+ stream.next if stream.token.type == :comma
223
219
  end
224
220
  stream.next
225
- Nodes::Function.create(name, args, :disable_visit_errors => @disable_visit_errors)
221
+ Nodes::Function.create(name, args, disable_visit_errors: @disable_visit_errors)
226
222
  end
227
223
 
228
224
  def led_or(stream, left)
@@ -286,7 +282,7 @@ module JMESPath
286
282
 
287
283
  def parse_key_value_pair(stream)
288
284
  key = stream.token.value
289
- stream.next(match:Set.new([:colon]))
285
+ stream.next(match: Set.new([:colon]))
290
286
  stream.next
291
287
  Nodes::MultiSelectHash::KeyValuePair.new(key, expr(stream))
292
288
  end
@@ -311,7 +307,7 @@ module JMESPath
311
307
  if stream.token.binding_power < 10
312
308
  CURRENT_NODE
313
309
  elsif type == :dot
314
- stream.next(match:AFTER_DOT)
310
+ stream.next(match: AFTER_DOT)
315
311
  parse_dot(stream, binding_power)
316
312
  elsif type == :lbracket || type == :filter
317
313
  expr(stream, binding_power)
@@ -321,7 +317,7 @@ module JMESPath
321
317
  end
322
318
 
323
319
  def parse_wildcard_array(stream, left = nil)
324
- stream.next(match:Set.new([:rbracket]))
320
+ stream.next(match: Set.new([:rbracket]))
325
321
  stream.next
326
322
  left ||= CURRENT_NODE
327
323
  right = parse_projection(stream, Token::BINDING_POWER[:star])
@@ -334,6 +330,5 @@ module JMESPath
334
330
  right = parse_projection(stream, Token::BINDING_POWER[:star])
335
331
  Nodes::ObjectProjection.new(left, right)
336
332
  end
337
-
338
333
  end
339
334
  end
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  class Runtime
4
-
5
5
  # @api private
6
6
  DEFAULT_PARSER = CachingParser
7
7
 
@@ -66,6 +66,5 @@ module JMESPath
66
66
  DEFAULT_PARSER.new(options)
67
67
  end
68
68
  end
69
-
70
69
  end
71
70
  end
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  class Token < Struct.new(:type, :value, :position, :binding_power)
4
-
5
5
  NULL_TOKEN = Token.new(:eof, '', nil)
6
6
 
7
7
  BINDING_POWER = {
@@ -28,8 +28,8 @@ module JMESPath
28
28
  Lexer::T_NOT => 45,
29
29
  Lexer::T_LBRACE => 50,
30
30
  Lexer::T_LBRACKET => 55,
31
- Lexer::T_LPAREN => 60,
32
- }
31
+ Lexer::T_LPAREN => 60
32
+ }.freeze
33
33
 
34
34
  # @param [Symbol] type
35
35
  # @param [Mixed] value
@@ -37,6 +37,5 @@ module JMESPath
37
37
  def initialize(type, value, position)
38
38
  super(type, value, position, BINDING_POWER[type])
39
39
  end
40
-
41
40
  end
42
41
  end
@@ -1,7 +1,7 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  class TokenStream
4
-
5
5
  # @param [String<JMESPath>] expression
6
6
  # @param [Array<Token>] tokens
7
7
  def initialize(expression, tokens)
@@ -35,8 +35,8 @@ module JMESPath
35
35
  def inspect
36
36
  str = []
37
37
  @tokens.each do |token|
38
- str << "%3d %-15s %s" %
39
- [token.position, token.type, token.value.inspect]
38
+ str << '%3d %-15s %s' %
39
+ [token.position, token.type, token.value.inspect]
40
40
  end
41
41
  str.join("\n")
42
42
  end
@@ -50,11 +50,10 @@ module JMESPath
50
50
 
51
51
  def validate_match(token, match)
52
52
  if match && !match.include?(token.type)
53
- raise Errors::SyntaxError, "type missmatch"
53
+ raise Errors::SyntaxError, 'type missmatch'
54
54
  else
55
55
  token
56
56
  end
57
57
  end
58
-
59
58
  end
60
59
  end
data/lib/jmespath/util.rb CHANGED
@@ -1,18 +1,34 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
3
  # @api private
3
4
  module Util
4
5
  class << self
5
-
6
6
  # Determines if a value is false as defined by JMESPath:
7
7
  #
8
8
  # https://github.com/jmespath/jmespath.site/blob/master/docs/proposals/improved-filters.rst#and-expressions-1
9
9
  #
10
10
  def falsey?(value)
11
11
  !value ||
12
- (value.respond_to?(:empty?) && value.empty?) ||
13
- (value.respond_to?(:entries) && !value.entries.any?)
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?) ||
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,4 @@
1
+ # frozen_string_literal: true
1
2
  module JMESPath
2
- VERSION = '1.4.0'
3
+ VERSION = File.read(File.expand_path('../../../VERSION', __FILE__)).strip
3
4
  end
data/lib/jmespath.rb CHANGED
@@ -1,9 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  require 'json'
2
3
  require 'stringio'
3
4
  require 'pathname'
4
5
 
5
6
  module JMESPath
6
-
7
7
  require 'jmespath/caching_parser'
8
8
  require 'jmespath/errors'
9
9
  require 'jmespath/lexer'
@@ -16,7 +16,6 @@ module JMESPath
16
16
  require 'jmespath/version'
17
17
 
18
18
  class << self
19
-
20
19
  # @param [String] expression A valid
21
20
  # [JMESPath](https://github.com/boto/jmespath) expression.
22
21
  # @param [Hash] data
@@ -24,18 +23,17 @@ module JMESPath
24
23
  # expression does not resolve inside `data`.
25
24
  def search(expression, data, runtime_options = {})
26
25
  data = case data
27
- when Hash, Struct then data # check for most common case first
28
- when Pathname then load_json(data)
29
- when IO, StringIO then JSON.load(data.read)
30
- else data
26
+ when Hash, Struct then data # check for most common case first
27
+ when Pathname then load_json(data)
28
+ when IO, StringIO then JSON.parse(data.read)
29
+ else data
31
30
  end
32
31
  Runtime.new(runtime_options).search(expression, data)
33
32
  end
34
33
 
35
34
  # @api private
36
35
  def load_json(path)
37
- JSON.load(File.open(path, 'r', encoding: 'UTF-8') { |f| f.read })
36
+ JSON.parse(File.open(path, 'r', encoding: 'UTF-8', &:read))
38
37
  end
39
-
40
38
  end
41
39
  end
metadata CHANGED
@@ -1,22 +1,25 @@
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.6.2
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-11-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Implements JMESPath for Ruby
14
14
  email: trevorrowe@gmail.com
15
- executables: []
15
+ executables:
16
+ - jmespath.rb
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
19
20
  - LICENSE.txt
21
+ - VERSION
22
+ - bin/jmespath.rb
20
23
  - lib/jmespath.rb
21
24
  - lib/jmespath/caching_parser.rb
22
25
  - lib/jmespath/errors.rb
@@ -50,7 +53,7 @@ homepage: http://github.com/trevorrowe/jmespath.rb
50
53
  licenses:
51
54
  - Apache-2.0
52
55
  metadata: {}
53
- post_install_message:
56
+ post_install_message:
54
57
  rdoc_options: []
55
58
  require_paths:
56
59
  - lib
@@ -65,9 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
68
  - !ruby/object:Gem::Version
66
69
  version: '0'
67
70
  requirements: []
68
- rubyforge_project:
69
- rubygems_version: 2.5.1
70
- signing_key:
71
+ rubygems_version: 3.2.22
72
+ signing_key:
71
73
  specification_version: 4
72
74
  summary: JMESPath - Ruby Edition
73
75
  test_files: []