burtpath 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5bfa4c5a420e8defde2a70bdbf2de5a8b4376576
4
+ data.tar.gz: 2ddf695b4cccd57b2e0b93735f89fdeec5878509
5
+ SHA512:
6
+ metadata.gz: b4611855c33f0104c78aa2cebef3a0ef308091f86ba63c04ca66406fa38bbccd513ed9382f551a10c33a192f00d11fb2966e2997b1ba3221ebbbc961924e7996
7
+ data.tar.gz: 4bfd2c5588811ad86348e80ec2fde8ef200eebce105ba58c1b7dcf5566fbedd4dc618e259f84405800ca6b6bcc43d71a893253631273776926dfd3ec4f89567d
data/LICENSE.txt ADDED
@@ -0,0 +1,174 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
data/lib/burtpath.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'jmespath'
2
+
3
+ BurtPath = JMESPath
@@ -0,0 +1,30 @@
1
+ require 'thread'
2
+
3
+ module JMESPath
4
+ class CachingParser
5
+
6
+ def initialize(parser)
7
+ @parser = parser
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
@@ -0,0 +1,17 @@
1
+ module JMESPath
2
+ module Errors
3
+
4
+ class Error < StandardError; end
5
+
6
+ class RuntimeError < Error; end
7
+
8
+ class SyntaxError < Error; end
9
+
10
+ class InvalidTypeError < Error; end
11
+
12
+ class InvalidArityError < Error; end
13
+
14
+ class UnknownFunctionError < Error; end
15
+
16
+ end
17
+ end
@@ -0,0 +1,118 @@
1
+ require 'json'
2
+
3
+ module JMESPath
4
+ # @api private
5
+ class Lexer
6
+
7
+ # @api private
8
+ TOKEN_PATTERNS = {}
9
+
10
+ # @api private
11
+ TOKEN_TYPES = {}
12
+
13
+ {
14
+ '[a-zA-Z_][a-zA-Z_0-9]*' => :identifier,
15
+ '\.' => :dot,
16
+ '\*' => :star,
17
+ '\[\]' => :flatten,
18
+ '-?\d+' => :number,
19
+ '\|\|' => :or,
20
+ '\|' => :pipe,
21
+ '\[\?' => :filter,
22
+ '\[' => :lbracket,
23
+ '\]' => :rbracket,
24
+ '"(?:\\\\\\\\|\\\\"|[^"])*"' => :quoted_identifier,
25
+ '`(?:\\\\\\\\|\\\\`|[^`])*`' => :literal,
26
+ ',' => :comma,
27
+ ':' => :colon,
28
+ '@' => :current,
29
+ '&' => :expref,
30
+ '\(' => :lparen,
31
+ '\)' => :rparen,
32
+ '\{' => :lbrace,
33
+ '\}' => :rbrace,
34
+ '!=' => :comparator,
35
+ '==' => :comparator,
36
+ '<=' => :comparator,
37
+ '>=' => :comparator,
38
+ '<' => :comparator,
39
+ '>' => :comparator,
40
+ '[ \t]' => :skip,
41
+ }.each.with_index do |(pattern, type), n|
42
+ TOKEN_PATTERNS[n] = pattern
43
+ TOKEN_TYPES[n] = type
44
+ end
45
+
46
+ # @api private
47
+ TOKEN_REGEX = /(#{TOKEN_PATTERNS.values.join(')|(')})/
48
+
49
+ # @api private
50
+ JSON_VALUE = /^[\["{]/
51
+
52
+ # @api private
53
+ JSON_NUMBER = /^\-?[0-9]*(\.[0-9]+)?([e|E][+|\-][0-9]+)?$/
54
+
55
+ # @param [String<JMESPath>] expression
56
+ # @return [Array<Hash>]
57
+ def tokenize(expression)
58
+ offset = 0
59
+ tokens = []
60
+ expression.scan(TOKEN_REGEX).each do |match|
61
+ match_index = match.find_index { |token| !token.nil? }
62
+ match_value = match[match_index]
63
+ type = TOKEN_TYPES[match_index]
64
+ token = Token.new(type, match_value, offset)
65
+ if token.type != :skip
66
+ case token.type
67
+ when :number then token_number(token, expression, offset)
68
+ when :literal then token_literal(token, expression, offset)
69
+ when :quoted_identifier
70
+ token_quoted_identifier(token, expression, offset)
71
+ end
72
+ tokens << token
73
+ end
74
+ offset += match_value.size
75
+ end
76
+ tokens << Token.new(:eof, nil, offset)
77
+ unless expression.size == offset
78
+ syntax_error('invalid expression', expression, offset)
79
+ end
80
+ tokens
81
+ end
82
+
83
+ private
84
+
85
+ def token_number(token, expression, offset)
86
+ token[:value] = token[:value].to_i
87
+ end
88
+
89
+ def token_literal(token, expression, offset)
90
+ token[:value] = token[:value][1..-2].lstrip.gsub('\`', '`')
91
+ token[:value] =
92
+ case token[:value]
93
+ when 'true', 'false' then token[:value] == 'true'
94
+ when 'null' then nil
95
+ when '' then syntax_error("empty json literal", expression, offset)
96
+ when JSON_VALUE then decode_json(token[:value], expression, offset)
97
+ when JSON_NUMBER then decode_json(token[:value], expression, offset)
98
+ else decode_json('"' + token[:value] + '"', expression, offset)
99
+ end
100
+ end
101
+
102
+ def token_quoted_identifier(token, expression, offset)
103
+ token[:value] = decode_json(token[:value], expression, offset)
104
+ end
105
+
106
+ def decode_json(json, expression, offset)
107
+ JSON.load(json)
108
+ rescue JSON::ParserError => e
109
+ syntax_error(e.message, expression, offset)
110
+ end
111
+
112
+ def syntax_error(message, expression, offset)
113
+ msg = message + "in #{expression.inspect} at #{offset}"
114
+ raise Errors::SyntaxError.new(msg)
115
+ end
116
+
117
+ end
118
+ end
@@ -0,0 +1,77 @@
1
+ module JMESPath
2
+ # @api private
3
+ module Nodes
4
+ class Comparator < Node
5
+ attr_reader :left, :right
6
+
7
+ def initialize(left, right)
8
+ @left = left
9
+ @right = right
10
+ end
11
+
12
+ def self.create(relation, left, right)
13
+ type = begin
14
+ case relation
15
+ when '==' then EqComparator
16
+ when '!=' then NeqComparator
17
+ when '>' then GtComparator
18
+ when '>=' then GteComparator
19
+ when '<' then LtComparator
20
+ when '<=' then LteComparator
21
+ end
22
+ end
23
+ type.new(left, right)
24
+ end
25
+
26
+ def visit(value)
27
+ check(@left.visit(value), @right.visit(value))
28
+ end
29
+
30
+ def optimize
31
+ self.class.new(@left.optimize, @right.optimize)
32
+ end
33
+
34
+ private
35
+
36
+ def check(left_value, right_value)
37
+ nil
38
+ end
39
+ end
40
+
41
+ class EqComparator < Comparator
42
+ def check(left_value, right_value)
43
+ left_value == right_value
44
+ end
45
+ end
46
+
47
+ class NeqComparator < Comparator
48
+ def check(left_value, right_value)
49
+ left_value != right_value
50
+ end
51
+ end
52
+
53
+ class GtComparator < Comparator
54
+ def check(left_value, right_value)
55
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value > right_value
56
+ end
57
+ end
58
+
59
+ class GteComparator < Comparator
60
+ def check(left_value, right_value)
61
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value >= right_value
62
+ end
63
+ end
64
+
65
+ class LtComparator < Comparator
66
+ def check(left_value, right_value)
67
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value < right_value
68
+ end
69
+ end
70
+
71
+ class LteComparator < Comparator
72
+ def check(left_value, right_value)
73
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value <= right_value
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,136 @@
1
+ module JMESPath
2
+ # @api private
3
+ module Nodes
4
+ class Condition < Node
5
+ def initialize(test, child)
6
+ @test = test
7
+ @child = child
8
+ end
9
+
10
+ def visit(value)
11
+ if @test.visit(value)
12
+ @child.visit(value)
13
+ else
14
+ nil
15
+ end
16
+ end
17
+
18
+ def optimize
19
+ test = @test.optimize
20
+ if (new_type = ComparatorCondition::COMPARATOR_TO_CONDITION[@test.class])
21
+ new_type.new(test.left, test.right, @child).optimize
22
+ else
23
+ self.class.new(test, @child.optimize)
24
+ end
25
+ end
26
+ end
27
+
28
+ class ComparatorCondition < Node
29
+ COMPARATOR_TO_CONDITION = {}
30
+
31
+ def initialize(left, right, child)
32
+ @left = left
33
+ @right = right
34
+ @child = child
35
+ end
36
+
37
+ def visit(value)
38
+ nil
39
+ end
40
+ end
41
+
42
+ class EqCondition < ComparatorCondition
43
+ COMPARATOR_TO_CONDITION[EqComparator] = self
44
+
45
+ def visit(value)
46
+ @left.visit(value) == @right.visit(value) ? @child.visit(value) : nil
47
+ end
48
+
49
+ def optimize
50
+ if @right.is_a?(Literal)
51
+ LiteralRightEqCondition.new(@left, @right, @child)
52
+ else
53
+ self
54
+ end
55
+ end
56
+ end
57
+
58
+ class LiteralRightEqCondition < EqCondition
59
+ def initialize(left, right, child)
60
+ super
61
+ @right = @right.value
62
+ end
63
+
64
+ def visit(value)
65
+ @left.visit(value) == @right ? @child.visit(value) : nil
66
+ end
67
+ end
68
+
69
+ class NeqCondition < ComparatorCondition
70
+ COMPARATOR_TO_CONDITION[NeqComparator] = self
71
+
72
+ def visit(value)
73
+ @left.visit(value) != @right.visit(value) ? @child.visit(value) : nil
74
+ end
75
+
76
+ def optimize
77
+ if @right.is_a?(Literal)
78
+ LiteralRightNeqCondition.new(@left, @right, @child)
79
+ else
80
+ self
81
+ end
82
+ end
83
+ end
84
+
85
+ class LiteralRightNeqCondition < NeqCondition
86
+ def initialize(left, right, child)
87
+ super
88
+ @right = @right.value
89
+ end
90
+
91
+ def visit(value)
92
+ @left.visit(value) != @right ? @child.visit(value) : nil
93
+ end
94
+ end
95
+
96
+ class GtCondition < ComparatorCondition
97
+ COMPARATOR_TO_CONDITION[GtComparator] = self
98
+
99
+ def visit(value)
100
+ left_value = @left.visit(value)
101
+ right_value = @right.visit(value)
102
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value > right_value ? @child.visit(value) : nil
103
+ end
104
+ end
105
+
106
+ class GteCondition < ComparatorCondition
107
+ COMPARATOR_TO_CONDITION[GteComparator] = self
108
+
109
+ def visit(value)
110
+ left_value = @left.visit(value)
111
+ right_value = @right.visit(value)
112
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value >= right_value ? @child.visit(value) : nil
113
+ end
114
+ end
115
+
116
+ class LtCondition < ComparatorCondition
117
+ COMPARATOR_TO_CONDITION[LtComparator] = self
118
+
119
+ def visit(value)
120
+ left_value = @left.visit(value)
121
+ right_value = @right.visit(value)
122
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value < right_value ? @child.visit(value) : nil
123
+ end
124
+ end
125
+
126
+ class LteCondition < ComparatorCondition
127
+ COMPARATOR_TO_CONDITION[LteComparator] = self
128
+
129
+ def visit(value)
130
+ left_value = @left.visit(value)
131
+ right_value = @right.visit(value)
132
+ left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value <= right_value ? @child.visit(value) : nil
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,10 @@
1
+ module JMESPath
2
+ # @api private
3
+ module Nodes
4
+ class Current < Node
5
+ def visit(value)
6
+ value
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module JMESPath
2
+ # @api private
3
+ module Nodes
4
+ class Expression < Node
5
+ attr_reader :expression
6
+
7
+ def initialize(expression)
8
+ @expression = expression
9
+ end
10
+
11
+ def visit(value)
12
+ self
13
+ end
14
+
15
+ def eval(value)
16
+ @expression.visit(value)
17
+ end
18
+
19
+ def optimize
20
+ self.class.new(@expression.optimize)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,63 @@
1
+ module JMESPath
2
+ # @api private
3
+ module Nodes
4
+ class Field < Node
5
+ def initialize(key)
6
+ @key = key
7
+ @keys = [key]
8
+ @key_sym = key.respond_to?(:to_sym) ? key.to_sym : nil
9
+ end
10
+
11
+ def visit(value)
12
+ if value.is_a?(Hash)
13
+ if !(v = value[@key]).nil?
14
+ v
15
+ elsif @key_sym && !(v = value[@key_sym]).nil?
16
+ v
17
+ end
18
+ elsif value.is_a?(Struct) && value.respond_to?(@key)
19
+ value[@key]
20
+ end
21
+ end
22
+
23
+ def chains_with?(other)
24
+ other.is_a?(Field)
25
+ end
26
+
27
+ def chain(other)
28
+ ChainedField.new([*keys, *other.keys])
29
+ end
30
+
31
+ protected
32
+
33
+ attr_reader :keys
34
+ end
35
+
36
+ class ChainedField < Field
37
+ def initialize(keys)
38
+ @keys = keys
39
+ @key_syms = keys.each_with_object({}) do |k, syms|
40
+ if k.respond_to?(:to_sym)
41
+ syms[k] = k.to_sym
42
+ end
43
+ end
44
+ end
45
+
46
+ def visit(value)
47
+ @keys.reduce(value) do |value, key|
48
+ if value.is_a?(Array) && key.is_a?(Integer)
49
+ value[key]
50
+ elsif value.is_a?(Hash)
51
+ if !(v = value[key]).nil?
52
+ v
53
+ elsif (sym = @key_syms[key]) && !(v = value[sym]).nil?
54
+ v
55
+ end
56
+ elsif value.is_a?(Struct) && value.respond_to?(key)
57
+ value[key]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end