janeway-jsonpath 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Janeway
4
- VERSION = '0.5.0'
4
+ # Version for janeway-jsonpath gem
5
+ VERSION = '0.6.0'
5
6
  end
data/lib/janeway.rb CHANGED
@@ -1,59 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'English'
4
+ require_relative 'janeway/enumerator'
5
+ require_relative 'janeway/parser'
4
6
 
5
- # Janeway JSONPath parsing library
7
+ # Janeway JSONPath query library
8
+ #
9
+ # https://github.com/gongfarmer/janeway
6
10
  module Janeway
7
- # Abstract Syntax Tree
8
- module AST
9
- # These are the limits of what javascript's Number type can represent
10
- INTEGER_MIN = -9_007_199_254_740_991
11
- INTEGER_MAX = 9_007_199_254_740_991
12
- end
13
-
14
- # Pair a jsonpath query with data to make an enumerator.
15
- # This can be used to apply the query to the data using Enumerator module
16
- # methods such as #each and #map.
11
+ # Parse a jsonpath string and combine it with data to make an Enumerator.
12
+ #
13
+ # The Enumerator can be used to apply the query to the data using Enumerator
14
+ # module methods such as #each and #map.
15
+ #
16
+ # @example Apply query to data and search to get array of results
17
+ # results = Janeway.parse('$.store.books[? length(@.title) > 20]').search
18
+ #
19
+ # @example Apply query to data and iterate over results
20
+ # enum = Janeway.parse('$.store.books[? length(@.title) > 20]')
21
+ # enum.each do |book|
22
+ # results << book
23
+ # end
24
+ #
25
+ # @see Janeway::Enumerator docs for more ways to use the Enumerator
17
26
  #
18
27
  # @param jsonpath [String] jsonpath query
19
28
  # @param data [Array, Hash] input data
20
29
  # @return [Janeway::Enumerator]
21
30
  def self.enum_for(jsonpath, data)
22
- query = compile(jsonpath)
31
+ query = parse(jsonpath)
23
32
  Janeway::Enumerator.new(query, data)
24
33
  end
25
34
 
26
- # Compile a JSONPath query into an Abstract Syntax Tree.
35
+ # Parse a JSONPath string into a Janeway::Query object.
36
+ #
37
+ # This object can be combined with data to create Enumerators that apply the query to the data.
38
+ #
39
+ # Use this method if you want to parse the query once and re-use it for multiple data sets.
40
+ #
41
+ # Otherwise, use Janeway.enum_for to parse the query and pair it with data in a single step.
27
42
  #
28
- # This can be combined with inputs (using #enum_for) to create Enumerators.
29
- # @example
30
- # query = Janeway.compile('$.store.books[? length(@.title) > 20]')
31
- # long_title_books = query.enum_for(some_data).search
32
- # query.enum_for(other_data).each do |book|
33
- # long_title_books << book
34
- # end
43
+ # @example Use a query to search several JSON files
44
+ # results = []
45
+ # query = Janeway.parse('$.store.books[? length(@.title) > 20]')
46
+ # data_files.each do |path|
47
+ # data = JSON.parse File.read(path)
48
+ # results.concat query.enum_for(data).search
49
+ # end
35
50
  #
36
51
  # @param query [String] jsonpath query
37
52
  # @return [Janeway::AST::Query]
38
- def self.compile(query)
53
+ def self.parse(query)
39
54
  Janeway::Parser.parse(query)
40
55
  end
41
- end
42
-
43
- # Require ruby source files in the given dir. Do not recurse to subdirs.
44
- # @param dir [String] dir path relative to __dir__
45
- # @return [void]
46
- def require_libs(dir)
47
- absolute_path = File.join(__dir__, dir)
48
- raise "No such dir: #{dir.inspect}" unless File.directory?(absolute_path)
49
56
 
50
- Dir.children(absolute_path).sort.each do |filename|
51
- next if File.directory?(File.join(absolute_path, filename))
52
-
53
- rel_path = File.join(dir, filename)
54
- require_relative(rel_path[0..-4]) # omits ".rb" extension
57
+ # Transform a jsonpath singular query into an array of values suitable for
58
+ # providing to Hash#dig or Array#dig.
59
+ #
60
+ # Only singular queries are allowed, meaning queries that contain only name
61
+ # selectors (ie. hash keys) and index selectors (array indexes.)
62
+ # The paths that are yielded to Enumerator#each are all suitable for this.
63
+ #
64
+ # @example convert normalized jsonpath to array of hash keys / array indices
65
+ # Janeway.path_to_diggable('$["a"].b.c[0]') => ["a", "b", "c", 0]
66
+ #
67
+ # @param jsonpath [String] jsonpath query
68
+ # @return [Array<String, Integer>]
69
+ def self.path_to_diggable(jsonpath)
70
+ raise NotImplementedError
55
71
  end
56
72
  end
57
-
58
- require_libs('janeway/ast')
59
- require_libs('janeway')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: janeway-jsonpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fraser Hanson
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-30 00:00:00.000000000 Z
10
+ date: 2025-02-05 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: |+
13
13
  JSONPath is a query language for selecting and extracting values from a JSON text.
@@ -31,6 +31,7 @@ files:
31
31
  - README.md
32
32
  - bin/janeway
33
33
  - lib/janeway.rb
34
+ - lib/janeway/ast.rb
34
35
  - lib/janeway/ast/array_slice_selector.rb
35
36
  - lib/janeway/ast/binary_operator.rb
36
37
  - lib/janeway/ast/boolean.rb
@@ -60,25 +61,33 @@ files:
60
61
  - lib/janeway/functions/search.rb
61
62
  - lib/janeway/functions/value.rb
62
63
  - lib/janeway/interpreter.rb
64
+ - lib/janeway/interpreters/array_slice_selector_delete_if.rb
63
65
  - lib/janeway/interpreters/array_slice_selector_deleter.rb
64
66
  - lib/janeway/interpreters/array_slice_selector_interpreter.rb
65
67
  - lib/janeway/interpreters/base.rb
66
68
  - lib/janeway/interpreters/binary_operator_interpreter.rb
69
+ - lib/janeway/interpreters/child_segment_delete_if.rb
67
70
  - lib/janeway/interpreters/child_segment_deleter.rb
68
71
  - lib/janeway/interpreters/child_segment_interpreter.rb
69
72
  - lib/janeway/interpreters/current_node_interpreter.rb
70
73
  - lib/janeway/interpreters/descendant_segment_interpreter.rb
74
+ - lib/janeway/interpreters/filter_selector_delete_if.rb
71
75
  - lib/janeway/interpreters/filter_selector_deleter.rb
72
76
  - lib/janeway/interpreters/filter_selector_interpreter.rb
73
77
  - lib/janeway/interpreters/function_interpreter.rb
78
+ - lib/janeway/interpreters/index_selector_delete_if.rb
74
79
  - lib/janeway/interpreters/index_selector_deleter.rb
75
80
  - lib/janeway/interpreters/index_selector_interpreter.rb
81
+ - lib/janeway/interpreters/iteration_helper.rb
82
+ - lib/janeway/interpreters/name_selector_delete_if.rb
76
83
  - lib/janeway/interpreters/name_selector_deleter.rb
77
84
  - lib/janeway/interpreters/name_selector_interpreter.rb
85
+ - lib/janeway/interpreters/root_node_delete_if.rb
78
86
  - lib/janeway/interpreters/root_node_deleter.rb
79
87
  - lib/janeway/interpreters/root_node_interpreter.rb
80
88
  - lib/janeway/interpreters/tree_constructor.rb
81
89
  - lib/janeway/interpreters/unary_operator_interpreter.rb
90
+ - lib/janeway/interpreters/wildcard_selector_delete_if.rb
82
91
  - lib/janeway/interpreters/wildcard_selector_deleter.rb
83
92
  - lib/janeway/interpreters/wildcard_selector_interpreter.rb
84
93
  - lib/janeway/interpreters/yielder.rb