jmespath 1.2.4 → 1.3.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: 2892bed3a7fb6f4285e9120e0f062cde8ef8aab2
4
- data.tar.gz: 1fac588695e21b8f26ec71b286b998c2abe1f3bb
3
+ metadata.gz: 9294e09435c687e359584f73e0ee3426c12996f2
4
+ data.tar.gz: c7baca0f257df2ba6fbf0b4fe08f3bc653836e26
5
5
  SHA512:
6
- metadata.gz: f55a519231ac35c9f680d2b8db88e21fe9f7ff84ee0c189a35600e0751eb82b536193363c1f0fcbba298932b325bdefde1b209f46db9e92aac0c88b84fd2d2a2
7
- data.tar.gz: 9ff7f1d9b5774ee3229009cd0f555ff4fcf0df3bffafedd3300de7388163ba9f95617696c1efc93ca03f81b32bd251917649fc907e354c5c0768fcf696718073
6
+ metadata.gz: d4a4eb1b6d4caea187a719a21ba0a2377ae4ad60e48553d51a41df20be244a3c526418b5bc533158cd86ecafa82bacce2cf69ce1015b3e015b63e5dfa0190b84
7
+ data.tar.gz: aa4c37e16951b1463821cfbda071b5362fb9abd0241a9eacdfa6d9dc511d1e07d687a9ae040f29f752ddff61cd33cf4b4567fe59ef29df89a0f158b82dcc93c6
@@ -1,34 +1,19 @@
1
- if Object.const_defined?(:JSON) && JSON::VERSION < '1.8.1'
2
- warn("WARNING: jmespath gem requires json gem >= 1.8.1; json #{JSON::VERSION} already loaded")
3
- else
4
- begin
5
- # Attempt to load the native version if available, not availble
6
- # by default for Ruby 1.9.3.
7
- gem('json', '>= 1.8.1')
8
- require 'json'
9
- rescue Gem::LoadError
10
- # Fallback on the json_pure gem dependency.
11
- gem('json_pure', '>= 1.8.1')
12
- require 'json'
13
- end
14
- end
15
-
1
+ require 'json'
16
2
  require 'stringio'
17
3
  require 'pathname'
18
4
 
19
5
  module JMESPath
20
6
 
21
- autoload :CachingParser, 'jmespath/caching_parser'
22
- autoload :Errors, 'jmespath/errors'
23
- autoload :ExprNode, 'jmespath/expr_node'
24
- autoload :Lexer, 'jmespath/lexer'
25
- autoload :Nodes, 'jmespath/nodes'
26
- autoload :Parser, 'jmespath/parser'
27
- autoload :Runtime, 'jmespath/runtime'
28
- autoload :Token, 'jmespath/token'
29
- autoload :TokenStream, 'jmespath/token_stream'
30
- autoload :Util, 'jmespath/util'
31
- autoload :VERSION, 'jmespath/version'
7
+ require 'jmespath/caching_parser'
8
+ require 'jmespath/errors'
9
+ require 'jmespath/lexer'
10
+ require 'jmespath/nodes'
11
+ require 'jmespath/parser'
12
+ require 'jmespath/runtime'
13
+ require 'jmespath/token'
14
+ require 'jmespath/token_stream'
15
+ require 'jmespath/util'
16
+ require 'jmespath/version'
32
17
 
33
18
  class << self
34
19
 
@@ -243,7 +243,7 @@ module JMESPath
243
243
  token = inside(chars, '"', T_QUOTED_IDENTIFIER)
244
244
  if token.type == T_QUOTED_IDENTIFIER
245
245
  token.value = "\"#{token.value}\""
246
- token = parse_json(token)
246
+ token = parse_json(token, true)
247
247
  end
248
248
  tokens << token
249
249
  when STATE_EQ
@@ -295,13 +295,36 @@ module JMESPath
295
295
  Token.new(type, buffer.join, position)
296
296
  end
297
297
 
298
- def parse_json(token)
299
- begin
300
- token.value = JSON.load(token.value)
301
- rescue JSON::ParserError
302
- token.type = T_UNKNOWN
298
+ if RUBY_VERSION.match(Regexp.escape('1.9.3'))
299
+ def parse_json(token, quoted = false)
300
+ begin
301
+ if quoted
302
+ token.value = JSON.load("{\"value\":#{token.value}}")['value']
303
+ else
304
+ begin
305
+ token.value = JSON.load("{\"value\":#{token.value}}")['value']
306
+ rescue
307
+ token.value = JSON.load(sprintf('{"value":"%s"}', token.value.lstrip))['value']
308
+ end
309
+ end
310
+ rescue JSON::ParserError
311
+ token.type = T_UNKNOWN
312
+ end
313
+ token
314
+ end
315
+ else
316
+ def parse_json(token, quoted = false)
317
+ begin
318
+ if quoted
319
+ token.value = JSON.load(token.value)
320
+ else
321
+ token.value = JSON.load(token.value) rescue JSON.load(sprintf('"%s"', token.value.lstrip))
322
+ end
323
+ rescue JSON::ParserError
324
+ token.type = T_UNKNOWN
325
+ end
326
+ token
303
327
  end
304
- token
305
328
  end
306
329
 
307
330
  class CharacterStream
@@ -17,28 +17,29 @@ module JMESPath
17
17
  false
18
18
  end
19
19
  end
20
+
21
+ require 'jmespath/nodes/subexpression'
22
+ require 'jmespath/nodes/and'
23
+ require 'jmespath/nodes/comparator'
24
+ require 'jmespath/nodes/comparator'
25
+ require 'jmespath/nodes/condition'
26
+ require 'jmespath/nodes/current'
27
+ require 'jmespath/nodes/expression'
28
+ require 'jmespath/nodes/field'
29
+ require 'jmespath/nodes/flatten'
30
+ require 'jmespath/nodes/function'
31
+ require 'jmespath/nodes/index'
32
+ require 'jmespath/nodes/literal'
33
+ require 'jmespath/nodes/multi_select_hash'
34
+ require 'jmespath/nodes/multi_select_list'
35
+ require 'jmespath/nodes/not'
36
+ require 'jmespath/nodes/or'
37
+ require 'jmespath/nodes/pipe'
38
+ require 'jmespath/nodes/projection'
39
+ require 'jmespath/nodes/projection'
40
+ require 'jmespath/nodes/projection'
41
+ require 'jmespath/nodes/slice'
20
42
 
21
- autoload :And, 'jmespath/nodes/and'
22
- autoload :Comparator, 'jmespath/nodes/comparator'
23
- autoload :Comparators, 'jmespath/nodes/comparator'
24
- autoload :Condition, 'jmespath/nodes/condition'
25
- autoload :Current, 'jmespath/nodes/current'
26
- autoload :Expression, 'jmespath/nodes/expression'
27
- autoload :Field, 'jmespath/nodes/field'
28
- autoload :Flatten, 'jmespath/nodes/flatten'
29
- autoload :Function, 'jmespath/nodes/function'
30
- autoload :Index, 'jmespath/nodes/index'
31
- autoload :Literal, 'jmespath/nodes/literal'
32
- autoload :MultiSelectHash, 'jmespath/nodes/multi_select_hash'
33
- autoload :MultiSelectList, 'jmespath/nodes/multi_select_list'
34
- autoload :Not, 'jmespath/nodes/not'
35
- autoload :Or, 'jmespath/nodes/or'
36
- autoload :Pipe, 'jmespath/nodes/pipe'
37
- autoload :Projection, 'jmespath/nodes/projection'
38
- autoload :ArrayProjection, 'jmespath/nodes/projection'
39
- autoload :ObjectProjection, 'jmespath/nodes/projection'
40
- autoload :Slice, 'jmespath/nodes/slice'
41
- autoload :Subexpression, 'jmespath/nodes/subexpression'
42
43
 
43
44
  end
44
45
  end
@@ -361,7 +361,7 @@ module JMESPath
361
361
  def call(args)
362
362
  if args.count == 1
363
363
  value = args.first
364
- String === value ? value : JSON.dump(value)
364
+ String === value ? value : value.to_json
365
365
  else
366
366
  return maybe_raise Errors::InvalidArityError, "function to_string() expects one argument"
367
367
  end
@@ -424,14 +424,22 @@ module JMESPath
424
424
  if args.count == 1
425
425
  value = args.first
426
426
  if Array === value
427
- value.sort do |a, b|
428
- a_type = get_type(a)
429
- b_type = get_type(b)
430
- if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
431
- a <=> b
432
- else
433
- return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
427
+ # every element in the list must be of the same type
428
+ array_type = get_type(value[0])
429
+ if array_type == STRING_TYPE || array_type == NUMBER_TYPE || value.size == 0
430
+ # stable sort
431
+ n = 0
432
+ value.sort_by do |v|
433
+ value_type = get_type(v)
434
+ if value_type != array_type
435
+ msg = "function sort() expects values to be an array of only numbers, or only integers"
436
+ return maybe_raise Errors::InvalidTypeError, msg
437
+ end
438
+ n += 1
439
+ [v, n]
434
440
  end
441
+ else
442
+ return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
435
443
  end
436
444
  else
437
445
  return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
@@ -452,16 +460,22 @@ module JMESPath
452
460
  if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
453
461
  values = args[0]
454
462
  expression = args[1]
455
- values.sort do |a,b|
456
- a_value = expression.eval(a)
457
- b_value = expression.eval(b)
458
- a_type = get_type(a_value)
459
- b_type = get_type(b_value)
460
- if (a_type == STRING_TYPE || a_type == NUMBER_TYPE) && a_type == b_type
461
- a_value <=> b_value
462
- else
463
- return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
463
+ array_type = get_type(expression.eval(values[0]))
464
+ if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.size == 0
465
+ # stable sort the list
466
+ n = 0
467
+ values.sort_by do |value|
468
+ value = expression.eval(value)
469
+ value_type = get_type(value)
470
+ if value_type != array_type
471
+ msg = "function sort() expects values to be an array of only numbers, or only integers"
472
+ return maybe_raise Errors::InvalidTypeError, msg
473
+ end
474
+ n += 1
475
+ [value, n]
464
476
  end
477
+ else
478
+ return maybe_raise Errors::InvalidTypeError, "function sort() expects values to be an array of numbers or integers"
465
479
  end
466
480
  else
467
481
  return maybe_raise Errors::InvalidTypeError, "function sort_by() expects an array and an expression"
@@ -46,7 +46,7 @@ module JMESPath
46
46
 
47
47
  # @api private
48
48
  def method_missing(method_name, *args)
49
- if matches = method_name.match(/^(nud_|led_)(.*)/)
49
+ if matches = method_name.to_s.match(/^(nud_|led_)(.*)/)
50
50
  raise Errors::SyntaxError, "unexpected token #{matches[2]}"
51
51
  else
52
52
  super
@@ -155,6 +155,10 @@ module JMESPath
155
155
  parse_wildcard_object(stream, CURRENT_NODE)
156
156
  end
157
157
 
158
+ def nud_unknown(stream)
159
+ raise Errors::SyntaxError, "unknown token #{stream.token.value.inspect}"
160
+ end
161
+
158
162
  def led_comparator(stream, left)
159
163
  token = stream.token
160
164
  stream.next
@@ -8,15 +8,11 @@ module JMESPath
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
- value.nil? ||
12
- value === false ||
13
- value == '' ||
14
- value == {} ||
15
- value == [] ||
16
- (value.respond_to?(:entries) && value.entries.compact.empty?)
11
+ !value ||
12
+ (value.respond_to?(:empty?) && value.empty?) ||
13
+ (value.respond_to?(:entries) && !value.entries.any?)
17
14
  # final case necessary to support Enumerable and Struct
18
15
  end
19
-
20
16
  end
21
17
  end
22
18
  end
@@ -1,3 +1,3 @@
1
1
  module JMESPath
2
- VERSION = '1.2.4'
2
+ VERSION = '1.3.0'
3
3
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jmespath
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.3.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: 2016-04-06 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: json_pure
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.8.1
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 1.8.1
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Implements JMESPath for Ruby
28
14
  email: trevorrowe@gmail.com
29
15
  executables: []
@@ -80,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
66
  version: '0'
81
67
  requirements: []
82
68
  rubyforge_project:
83
- rubygems_version: 2.4.5
69
+ rubygems_version: 2.5.1
84
70
  signing_key:
85
71
  specification_version: 4
86
72
  summary: JMESPath - Ruby Edition