jmespath-1.6.2/0000755000004100000410000000000014350576160013372 5ustar www-datawww-datajmespath-1.6.2/bin/0000755000004100000410000000000014350576160014142 5ustar www-datawww-datajmespath-1.6.2/bin/jmespath.rb0000755000004100000410000000041114350576160016301 0ustar www-datawww-data#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'jmespath' require 'json' expression = ARGV[0] json = JSON.parse(STDIN.read) $stdout.puts(JSON.dump(JMESPath.search(expression, json))) jmespath-1.6.2/lib/0000755000004100000410000000000014350576160014140 5ustar www-datawww-datajmespath-1.6.2/lib/jmespath/0000755000004100000410000000000014350576160015753 5ustar www-datawww-datajmespath-1.6.2/lib/jmespath/version.rb0000644000004100000410000000017614350576160017771 0ustar www-datawww-data# frozen_string_literal: true module JMESPath VERSION = File.read(File.expand_path('../../../VERSION', __FILE__)).strip end jmespath-1.6.2/lib/jmespath/lexer.rb0000644000004100000410000002540414350576160017424 0ustar www-datawww-data# frozen_string_literal: true require 'json' require 'set' module JMESPath # @api private class Lexer T_DOT = :dot T_STAR = :star T_COMMA = :comma T_COLON = :colon T_CURRENT = :current T_EXPREF = :expref T_LPAREN = :lparen T_RPAREN = :rparen T_LBRACE = :lbrace T_RBRACE = :rbrace T_LBRACKET = :lbracket T_RBRACKET = :rbracket T_FLATTEN = :flatten T_IDENTIFIER = :identifier T_NUMBER = :number T_QUOTED_IDENTIFIER = :quoted_identifier T_UNKNOWN = :unknown T_PIPE = :pipe T_OR = :or T_AND = :and T_NOT = :not T_FILTER = :filter T_LITERAL = :literal T_EOF = :eof T_COMPARATOR = :comparator STATE_IDENTIFIER = 0 STATE_NUMBER = 1 STATE_SINGLE_CHAR = 2 STATE_WHITESPACE = 3 STATE_STRING_LITERAL = 4 STATE_QUOTED_STRING = 5 STATE_JSON_LITERAL = 6 STATE_LBRACKET = 7 STATE_PIPE = 8 STATE_LT = 9 STATE_GT = 10 STATE_EQ = 11 STATE_NOT = 12 STATE_AND = 13 TRANSLATION_TABLE = { '<' => STATE_LT, '>' => STATE_GT, '=' => STATE_EQ, '!' => STATE_NOT, '[' => STATE_LBRACKET, '|' => STATE_PIPE, '&' => STATE_AND, '`' => STATE_JSON_LITERAL, '"' => STATE_QUOTED_STRING, "'" => STATE_STRING_LITERAL, '-' => STATE_NUMBER, '0' => STATE_NUMBER, '1' => STATE_NUMBER, '2' => STATE_NUMBER, '3' => STATE_NUMBER, '4' => STATE_NUMBER, '5' => STATE_NUMBER, '6' => STATE_NUMBER, '7' => STATE_NUMBER, '8' => STATE_NUMBER, '9' => STATE_NUMBER, ' ' => STATE_WHITESPACE, "\t" => STATE_WHITESPACE, "\n" => STATE_WHITESPACE, "\r" => STATE_WHITESPACE, '.' => STATE_SINGLE_CHAR, '*' => STATE_SINGLE_CHAR, ']' => STATE_SINGLE_CHAR, ',' => STATE_SINGLE_CHAR, ':' => STATE_SINGLE_CHAR, '@' => STATE_SINGLE_CHAR, '(' => STATE_SINGLE_CHAR, ')' => STATE_SINGLE_CHAR, '{' => STATE_SINGLE_CHAR, '}' => STATE_SINGLE_CHAR, '_' => STATE_IDENTIFIER, 'A' => STATE_IDENTIFIER, 'B' => STATE_IDENTIFIER, 'C' => STATE_IDENTIFIER, 'D' => STATE_IDENTIFIER, 'E' => STATE_IDENTIFIER, 'F' => STATE_IDENTIFIER, 'G' => STATE_IDENTIFIER, 'H' => STATE_IDENTIFIER, 'I' => STATE_IDENTIFIER, 'J' => STATE_IDENTIFIER, 'K' => STATE_IDENTIFIER, 'L' => STATE_IDENTIFIER, 'M' => STATE_IDENTIFIER, 'N' => STATE_IDENTIFIER, 'O' => STATE_IDENTIFIER, 'P' => STATE_IDENTIFIER, 'Q' => STATE_IDENTIFIER, 'R' => STATE_IDENTIFIER, 'S' => STATE_IDENTIFIER, 'T' => STATE_IDENTIFIER, 'U' => STATE_IDENTIFIER, 'V' => STATE_IDENTIFIER, 'W' => STATE_IDENTIFIER, 'X' => STATE_IDENTIFIER, 'Y' => STATE_IDENTIFIER, 'Z' => STATE_IDENTIFIER, 'a' => STATE_IDENTIFIER, 'b' => STATE_IDENTIFIER, 'c' => STATE_IDENTIFIER, 'd' => STATE_IDENTIFIER, 'e' => STATE_IDENTIFIER, 'f' => STATE_IDENTIFIER, 'g' => STATE_IDENTIFIER, 'h' => STATE_IDENTIFIER, 'i' => STATE_IDENTIFIER, 'j' => STATE_IDENTIFIER, 'k' => STATE_IDENTIFIER, 'l' => STATE_IDENTIFIER, 'm' => STATE_IDENTIFIER, 'n' => STATE_IDENTIFIER, 'o' => STATE_IDENTIFIER, 'p' => STATE_IDENTIFIER, 'q' => STATE_IDENTIFIER, 'r' => STATE_IDENTIFIER, 's' => STATE_IDENTIFIER, 't' => STATE_IDENTIFIER, 'u' => STATE_IDENTIFIER, 'v' => STATE_IDENTIFIER, 'w' => STATE_IDENTIFIER, 'x' => STATE_IDENTIFIER, 'y' => STATE_IDENTIFIER, 'z' => STATE_IDENTIFIER }.freeze VALID_IDENTIFIERS = Set.new(%w( A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z _ 0 1 2 3 4 5 6 7 8 9 )) NUMBERS = Set.new(%w(0 1 2 3 4 5 6 7 8 9)) SIMPLE_TOKENS = { '.' => T_DOT, '*' => T_STAR, ']' => T_RBRACKET, ',' => T_COMMA, ':' => T_COLON, '@' => T_CURRENT, '(' => T_LPAREN, ')' => T_RPAREN, '{' => T_LBRACE, '}' => T_RBRACE }.freeze # @param [String] expression # @return [Array] def tokenize(expression) tokens = [] chars = CharacterStream.new(expression.chars.to_a) while chars.current case TRANSLATION_TABLE[chars.current] when nil tokens << Token.new( T_UNKNOWN, chars.current, chars.position ) chars.next when STATE_SINGLE_CHAR # consume simple tokens like ".", ",", "@", etc. tokens << Token.new( SIMPLE_TOKENS[chars.current], chars.current, chars.position ) chars.next when STATE_IDENTIFIER start = chars.position buffer = [] begin buffer << chars.current chars.next end while VALID_IDENTIFIERS.include?(chars.current) tokens << Token.new( T_IDENTIFIER, buffer.join, start ) when STATE_WHITESPACE # skip whitespace chars.next when STATE_LBRACKET # consume "[", "[?" and "[]" position = chars.position actual = chars.next if actual == ']' chars.next tokens << Token.new(T_FLATTEN, '[]', position) elsif actual == '?' chars.next tokens << Token.new(T_FILTER, '[?', position) else tokens << Token.new(T_LBRACKET, '[', position) end when STATE_STRING_LITERAL # consume raw string literals t = inside(chars, "'", T_LITERAL) t.value = t.value.gsub("\\'", "'") tokens << t when STATE_PIPE # consume pipe and OR tokens << match_or(chars, '|', '|', T_OR, T_PIPE) when STATE_JSON_LITERAL # consume JSON literals token = inside(chars, '`', T_LITERAL) if token.type == T_LITERAL token.value = token.value.gsub('\\`', '`') token = parse_json(token) end tokens << token when STATE_NUMBER start = chars.position buffer = [] begin buffer << chars.current chars.next end while NUMBERS.include?(chars.current) tokens << Token.new( T_NUMBER, buffer.join.to_i, start ) when STATE_QUOTED_STRING # consume quoted identifiers token = inside(chars, '"', T_QUOTED_IDENTIFIER) if token.type == T_QUOTED_IDENTIFIER token.value = "\"#{token.value}\"" token = parse_json(token, true) end tokens << token when STATE_EQ # consume equals tokens << match_or(chars, '=', '=', T_COMPARATOR, T_UNKNOWN) when STATE_AND tokens << match_or(chars, '&', '&', T_AND, T_EXPREF) when STATE_NOT # consume not equals tokens << match_or(chars, '!', '=', T_COMPARATOR, T_NOT) else # either '<' or '>' # consume less than and greater than tokens << match_or(chars, chars.current, '=', T_COMPARATOR, T_COMPARATOR) end end tokens << Token.new(T_EOF, nil, chars.position) tokens end private def match_or(chars, current, expected, type, or_type) if chars.next == expected chars.next Token.new(type, current + expected, chars.position - 1) else Token.new(or_type, current, chars.position - 1) end end def inside(chars, delim, type) position = chars.position current = chars.next buffer = [] while current != delim if current == '\\' buffer << current current = chars.next end if current.nil? # unclosed delimiter return Token.new(T_UNKNOWN, buffer.join, position) end buffer << current current = chars.next end chars.next Token.new(type, buffer.join, position) end # Certain versions of Ruby and of the pure_json gem not support loading # scalar JSON values, such a numbers, booleans, strings, etc. These # simple values must be first wrapped inside a JSON object before calling # `JSON.parse`. # # # works in most JSON versions, raises in some versions # JSON.parse("true") # JSON.parse("123") # JSON.parse("\"abc\"") # # This is an known issue for: # # * Ruby 1.9.3 bundled v1.5.5 of json; Ruby 1.9.3 defaults to bundled # version despite newer versions being available. # # * json_pure v2.0.0+ # # It is not possible to change the version of JSON loaded in the # user's application. Adding an explicit dependency on json gem # causes issues in environments that cannot compile the gem. We previously # had a direct dependency on `json_pure`, but this broke with the v2 update. # # This method allows us to detect how the `JSON.parse` behaves so we know # if we have to wrap scalar JSON values to parse them or not. # @api private def self.requires_wrapping? JSON.parse('false') rescue JSON::ParserError true end if requires_wrapping? def parse_json(token, quoted = false) begin if quoted token.value = JSON.parse("{\"value\":#{token.value}}")['value'] else begin token.value = JSON.parse("{\"value\":#{token.value}}")['value'] rescue token.value = JSON.parse(sprintf('{"value":"%s"}', token.value.lstrip))['value'] end end rescue JSON::ParserError token.type = T_UNKNOWN end token end else def parse_json(token, quoted = false) begin if quoted token.value = JSON.parse(token.value) else token.value = begin JSON.parse(token.value) rescue JSON.parse(sprintf('"%s"', token.value.lstrip)) end end rescue JSON::ParserError token.type = T_UNKNOWN end token end end class CharacterStream def initialize(chars) @chars = chars @position = 0 end def current @chars[@position] end def next @position += 1 @chars[@position] end attr_reader :position end end end jmespath-1.6.2/lib/jmespath/caching_parser.rb0000644000004100000410000000110014350576160021240 0ustar www-datawww-data# frozen_string_literal: true require 'thread' module JMESPath class CachingParser def initialize(options = {}) @parser = options[:parser] || Parser.new(options) @mutex = Mutex.new @cache = {} end def parse(expression) if cached = @cache[expression] cached else cache_expression(expression) end end private def cache_expression(expression) @mutex.synchronize do @cache.clear if @cache.size > 1000 @cache[expression] = @parser.parse(expression) end end end end jmespath-1.6.2/lib/jmespath/runtime.rb0000644000004100000410000000413014350576160017761 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private class Runtime # @api private DEFAULT_PARSER = CachingParser # Constructs a new runtime object for evaluating JMESPath expressions. # # runtime = JMESPath::Runtime.new # runtime.search(expression, data) # #=> ... # # ## Caching # # When constructing a {Runtime}, the default parser caches expressions. # This significantly speeds up calls to {#search} multiple times # with the same expression but different data. To disable caching, pass # `:cache_expressions => false` to the constructor or pass a custom # `:parser`. # # @example Re-use a Runtime, caching enabled by default # # runtime = JMESPath::Runtime.new # runtime.parser # #=> # # # @example Disable caching # # runtime = JMESPath::Runtime.new(cache_expressions: false) # runtime.parser # #=> # # # @option options [Boolean] :cache_expressions (true) When `false`, a non # caching parser will be used. When `true`, a shared instance of # {CachingParser} is used. Defaults to `true`. # # @option options [Boolean] :disable_visit_errors (false) When `true`, # no errors will be raised during runtime processing. Parse errors # will still be raised, but unexpected data sent to visit will # result in nil being returned. # # @option options [Parser,CachingParser] :parser # def initialize(options = {}) @parser = options[:parser] || default_parser(options) end # @return [Parser, CachingParser] attr_reader :parser # @param [String] expression # @param [Hash] data # @return [Mixed,nil] def search(expression, data) optimized_expression = @parser.parse(expression).optimize optimized_expression.visit(data) end private def default_parser(options) if options[:cache_expressions] == false Parser.new(options) else DEFAULT_PARSER.new(options) end end end end jmespath-1.6.2/lib/jmespath/nodes/0000755000004100000410000000000014350576160017063 5ustar www-datawww-datajmespath-1.6.2/lib/jmespath/nodes/multi_select_hash.rb0000644000004100000410000000134714350576160023111 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class MultiSelectHash < Node def initialize(kv_pairs) @kv_pairs = kv_pairs end def visit(value) if value.nil? nil else @kv_pairs.each_with_object({}) do |pair, hash| hash[pair.key] = pair.value.visit(value) end end end def optimize self.class.new(@kv_pairs.map(&:optimize)) end class KeyValuePair attr_reader :key, :value def initialize(key, value) @key = key @value = value end def optimize self.class.new(@key, @value.optimize) end end end end end jmespath-1.6.2/lib/jmespath/nodes/current.rb0000644000004100000410000000025014350576160021067 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Current < Node def visit(value) value end end end end jmespath-1.6.2/lib/jmespath/nodes/projection.rb0000644000004100000410000000323014350576160021562 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Projection < Node def initialize(target, projection) @target = target @projection = projection end def visit(value) if (targets = extract_targets(@target.visit(value))) list = [] targets.each do |v| vv = @projection.visit(v) list << vv unless vv.nil? end list end end def optimize if @projection.is_a?(Current) fast_instance else self.class.new(@target.optimize, @projection.optimize) end end private def extract_targets(_left_value) nil end end module FastProjector def visit(value) if (targets = extract_targets(@target.visit(value))) targets.compact end end end class ArrayProjection < Projection def extract_targets(target) target.to_ary if target.respond_to?(:to_ary) end def fast_instance FastArrayProjection.new(@target.optimize, @projection.optimize) end end class FastArrayProjection < ArrayProjection include FastProjector end class ObjectProjection < Projection def extract_targets(target) if target.respond_to?(:to_hash) target.to_hash.values elsif target.is_a?(Struct) target.values end end def fast_instance FastObjectProjection.new(@target.optimize, @projection.optimize) end end class FastObjectProjection < ObjectProjection include FastProjector end end end jmespath-1.6.2/lib/jmespath/nodes/condition.rb0000644000004100000410000000731314350576160021402 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Condition < Node def initialize(test, child) @test = test @child = child end def visit(value) if JMESPath::Util.falsey?(@test.visit(value)) nil else @child.visit(value) end end def optimize test = @test.optimize if (new_type = ComparatorCondition::COMPARATOR_TO_CONDITION[@test.class]) new_type.new(test.left, test.right, @child).optimize else self.class.new(test, @child.optimize) end end end class ComparatorCondition < Node COMPARATOR_TO_CONDITION = {} COMPARABLE_TYPES = [Numeric, String].freeze def initialize(left, right, child) @left = left @right = right @child = child end def visit(_value) nil end private def comparable?(left_value, right_value) COMPARABLE_TYPES.any? do |type| left_value.is_a?(type) && right_value.is_a?(type) end end end class EqCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Eq] = self def visit(value) Util.as_json(@left.visit(value)) == Util.as_json(@right.visit(value)) ? @child.visit(value) : nil end def optimize if @right.is_a?(Literal) LiteralRightEqCondition.new(@left, @right, @child) else self end end end class LiteralRightEqCondition < EqCondition def initialize(left, right, child) super @right = @right.value end def visit(value) Util.as_json(@left.visit(value)) == @right ? @child.visit(value) : nil end end class NeqCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Neq] = self def visit(value) Util.as_json(@left.visit(value)) != Util.as_json(@right.visit(value)) ? @child.visit(value) : nil end def optimize if @right.is_a?(Literal) LiteralRightNeqCondition.new(@left, @right, @child) else self end end end class LiteralRightNeqCondition < NeqCondition def initialize(left, right, child) super @right = @right.value end def visit(value) Util.as_json(@left.visit(value)) != @right ? @child.visit(value) : nil end end class GtCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Gt] = self def visit(value) left_value = @left.visit(value) right_value = @right.visit(value) comparable?(left_value, right_value) && left_value > right_value ? @child.visit(value) : nil end end class GteCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Gte] = self def visit(value) left_value = @left.visit(value) right_value = @right.visit(value) comparable?(left_value, right_value) && left_value >= right_value ? @child.visit(value) : nil end end class LtCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Lt] = self def visit(value) left_value = @left.visit(value) right_value = @right.visit(value) comparable?(left_value, right_value) && left_value < right_value ? @child.visit(value) : nil end end class LteCondition < ComparatorCondition COMPARATOR_TO_CONDITION[Comparators::Lte] = self def visit(value) left_value = @left.visit(value) right_value = @right.visit(value) comparable?(left_value, right_value) && left_value <= right_value ? @child.visit(value) : nil end end end end jmespath-1.6.2/lib/jmespath/nodes/multi_select_list.rb0000644000004100000410000000064714350576160023143 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class MultiSelectList < Node def initialize(children) @children = children end def visit(value) if value.nil? value else @children.map { |n| n.visit(value) } end end def optimize self.class.new(@children.map(&:optimize)) end end end end jmespath-1.6.2/lib/jmespath/nodes/or.rb0000644000004100000410000000073214350576160020032 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Or < Node def initialize(left, right) @left = left @right = right end def visit(value) result = @left.visit(value) if JMESPath::Util.falsey?(result) @right.visit(value) else result end end def optimize self.class.new(@left.optimize, @right.optimize) end end end end jmespath-1.6.2/lib/jmespath/nodes/and.rb0000644000004100000410000000071214350576160020152 0ustar www-datawww-data# frozen_string_literal: true module JMESPath module Nodes class And < Node def initialize(left, right) @left = left @right = right end def visit(value) result = @left.visit(value) if JMESPath::Util.falsey?(result) result else @right.visit(value) end end def optimize self.class.new(@left.optimize, @right.optimize) end end end end jmespath-1.6.2/lib/jmespath/nodes/index.rb0000644000004100000410000000015214350576160020515 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes Index = Field end end jmespath-1.6.2/lib/jmespath/nodes/subexpression.rb0000644000004100000410000000254114350576160022323 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Subexpression < Node def initialize(left, right) @left = left @right = right end def visit(value) @right.visit(@left.visit(value)) end def optimize Chain.new(flatten).optimize end protected attr_reader :left, :right def flatten nodes = [@left, @right] until nodes.none? { |node| node.is_a?(Subexpression) } nodes = nodes.flat_map do |node| if node.is_a?(Subexpression) [node.left, node.right] else [node] end end end nodes.map(&:optimize) end end class Chain def initialize(children) @children = children end def visit(value) @children.reduce(value) do |v, child| child.visit(v) end end def optimize children = @children.map(&:optimize) index = 0 while index < children.size - 1 if children[index].chains_with?(children[index + 1]) children[index] = children[index].chain(children[index + 1]) children.delete_at(index + 1) else index += 1 end end Chain.new(children) end end end end jmespath-1.6.2/lib/jmespath/nodes/literal.rb0000644000004100000410000000040214350576160021040 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Literal < Node attr_reader :value def initialize(value) @value = value end def visit(_value) @value end end end end jmespath-1.6.2/lib/jmespath/nodes/slice.rb0000644000004100000410000000424314350576160020512 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Slice < Node def initialize(start, stop, step) @start = start @stop = stop @step = step raise Errors::InvalidValueError, 'slice step cannot be 0' if @step == 0 end def visit(value) if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil) start, stop, step = adjust_slice(value.size, @start, @stop, @step) result = [] if step > 0 i = start while i < stop result << value[i] i += step end else i = start while i > stop result << value[i] i += step end end value.respond_to?(:to_str) ? result.join : result end end def optimize if (@step.nil? || @step == 1) && @start && @stop && @start > 0 && @stop > @start SimpleSlice.new(@start, @stop) else self end end private def adjust_slice(length, start, stop, step) step = 1 if step.nil? start = if start.nil? step < 0 ? length - 1 : 0 else adjust_endpoint(length, start, step) end stop = if stop.nil? step < 0 ? -1 : length else adjust_endpoint(length, stop, step) end [start, stop, step] end def adjust_endpoint(length, endpoint, step) if endpoint < 0 endpoint += length endpoint = step < 0 ? -1 : 0 if endpoint < 0 endpoint elsif endpoint >= length step < 0 ? length - 1 : length else endpoint end end end class SimpleSlice < Slice def initialize(start, stop) super(start, stop, 1) end def visit(value) if (value = value.respond_to?(:to_str) ? value.to_str : value.respond_to?(:to_ary) ? value.to_ary : nil) value[@start, @stop - @start] end end end end end jmespath-1.6.2/lib/jmespath/nodes/comparator.rb0000644000004100000410000000415214350576160021561 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Comparator < Node COMPARABLE_TYPES = [Numeric, String].freeze attr_reader :left, :right def initialize(left, right) @left = left @right = right end def self.create(relation, left, right) type = begin case relation when '==' then Comparators::Eq when '!=' then Comparators::Neq when '>' then Comparators::Gt when '>=' then Comparators::Gte when '<' then Comparators::Lt when '<=' then Comparators::Lte end end type.new(left, right) end def visit(value) check(@left.visit(value), @right.visit(value)) end def optimize self.class.new(@left.optimize, @right.optimize) end private def check(_left_value, _right_value) nil end def comparable?(left_value, right_value) COMPARABLE_TYPES.any? do |type| left_value.is_a?(type) && right_value.is_a?(type) end end end module Comparators class Eq < Comparator def check(left_value, right_value) Util.as_json(left_value) == Util.as_json(right_value) end end class Neq < Comparator def check(left_value, right_value) Util.as_json(left_value) != Util.as_json(right_value) end end class Gt < Comparator def check(left_value, right_value) left_value > right_value if comparable?(left_value, right_value) end end class Gte < Comparator def check(left_value, right_value) left_value >= right_value if comparable?(left_value, right_value) end end class Lt < Comparator def check(left_value, right_value) left_value < right_value if comparable?(left_value, right_value) end end class Lte < Comparator def check(left_value, right_value) left_value <= right_value if comparable?(left_value, right_value) end end end end end jmespath-1.6.2/lib/jmespath/nodes/pipe.rb0000644000004100000410000000016114350576160020343 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes Pipe = Subexpression end end jmespath-1.6.2/lib/jmespath/nodes/function.rb0000644000004100000410000004645514350576160021253 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Function < Node FUNCTIONS = {} def initialize(children, options = {}) @children = children @options = options @disable_visit_errors = @options[:disable_visit_errors] end def self.create(name, children, options = {}) if (type = FUNCTIONS[name]) type.new(children, options) else raise Errors::UnknownFunctionError, "unknown function #{name}()" end end def visit(value) call(@children.map { |child| child.visit(value) }) end def optimize self.class.new(@children.map(&:optimize), @options) end class FunctionName attr_reader :name def initialize(name) @name = name end end private def maybe_raise(error_type, message) raise error_type, message unless @disable_visit_errors end def call(_args) nil end end module TypeChecker def get_type(value) if value.respond_to?(:to_str) STRING_TYPE elsif value == true || value == false BOOLEAN_TYPE elsif value.nil? NULL_TYPE elsif value.is_a?(Numeric) NUMBER_TYPE elsif value.respond_to?(:to_hash) || value.is_a?(Struct) OBJECT_TYPE elsif value.respond_to?(:to_ary) ARRAY_TYPE elsif value.is_a?(Expression) EXPRESSION_TYPE end end ARRAY_TYPE = 0 BOOLEAN_TYPE = 1 EXPRESSION_TYPE = 2 NULL_TYPE = 3 NUMBER_TYPE = 4 OBJECT_TYPE = 5 STRING_TYPE = 6 TYPE_NAMES = { ARRAY_TYPE => 'array', BOOLEAN_TYPE => 'boolean', EXPRESSION_TYPE => 'expression', NULL_TYPE => 'null', NUMBER_TYPE => 'number', OBJECT_TYPE => 'object', STRING_TYPE => 'string' }.freeze end class AbsFunction < Function FUNCTIONS['abs'] = self def call(args) if args.count == 1 value = args.first else return maybe_raise Errors::InvalidArityError, 'function abs() expects one argument' end if Numeric === value value.abs else return maybe_raise Errors::InvalidTypeError, 'function abs() expects a number' end end end class AvgFunction < Function FUNCTIONS['avg'] = self def call(args) if args.count == 1 values = args.first else return maybe_raise Errors::InvalidArityError, 'function avg() expects one argument' end if values.respond_to?(:to_ary) values = values.to_ary return nil if values.empty? values.inject(0) do |total, n| if Numeric === n total + n else return maybe_raise Errors::InvalidTypeError, 'function avg() expects numeric values' end end / values.size.to_f else return maybe_raise Errors::InvalidTypeError, 'function avg() expects a number' end end end class CeilFunction < Function FUNCTIONS['ceil'] = self def call(args) if args.count == 1 value = args.first else return maybe_raise Errors::InvalidArityError, 'function ceil() expects one argument' end if Numeric === value value.ceil else return maybe_raise Errors::InvalidTypeError, 'function ceil() expects a numeric value' end end end class ContainsFunction < Function FUNCTIONS['contains'] = self def call(args) if args.count == 2 haystack = args[0] needle = Util.as_json(args[1]) if haystack.respond_to?(:to_str) haystack.to_str.include?(needle) elsif haystack.respond_to?(:to_ary) haystack.to_ary.any? { |e| Util.as_json(e) == needle } else return maybe_raise Errors::InvalidTypeError, 'contains expects 2nd arg to be a list' end else return maybe_raise Errors::InvalidArityError, 'function contains() expects 2 arguments' end end end class FloorFunction < Function FUNCTIONS['floor'] = self def call(args) if args.count == 1 value = args.first else return maybe_raise Errors::InvalidArityError, 'function floor() expects one argument' end if Numeric === value value.floor else return maybe_raise Errors::InvalidTypeError, 'function floor() expects a numeric value' end end end class LengthFunction < Function FUNCTIONS['length'] = self def call(args) if args.count == 1 value = args.first else return maybe_raise Errors::InvalidArityError, 'function length() expects one argument' end if value.respond_to?(:to_hash) value.to_hash.size elsif value.respond_to?(:to_ary) value.to_ary.size elsif value.respond_to?(:to_str) value.to_str.size else return maybe_raise Errors::InvalidTypeError, 'function length() expects string, array or object' end end end class Map < Function FUNCTIONS['map'] = self def call(args) if args.count != 2 return maybe_raise Errors::InvalidArityError, 'function map() expects two arguments' end if Nodes::Expression === args[0] expr = args[0] else return maybe_raise Errors::InvalidTypeError, 'function map() expects the first argument to be an expression' end if args[1].respond_to?(:to_ary) list = args[1].to_ary else return maybe_raise Errors::InvalidTypeError, 'function map() expects the second argument to be an list' end list.map { |value| expr.eval(value) } end end class MaxFunction < Function include TypeChecker FUNCTIONS['max'] = self def call(args) if args.count == 1 values = args.first else return maybe_raise Errors::InvalidArityError, 'function max() expects one argument' end if values.respond_to?(:to_ary) values = values.to_ary return nil if values.empty? first = values.first first_type = get_type(first) unless first_type == NUMBER_TYPE || first_type == STRING_TYPE msg = String.new('function max() expects numeric or string values') return maybe_raise Errors::InvalidTypeError, msg end values.inject([first, first_type]) do |(max, max_type), v| v_type = get_type(v) if max_type == v_type v > max ? [v, v_type] : [max, max_type] else msg = String.new('function max() encountered a type mismatch in sequence: ') msg << "#{max_type}, #{v_type}" return maybe_raise Errors::InvalidTypeError, msg end end.first else return maybe_raise Errors::InvalidTypeError, 'function max() expects an array' end end end class MinFunction < Function include TypeChecker FUNCTIONS['min'] = self def call(args) if args.count == 1 values = args.first else return maybe_raise Errors::InvalidArityError, 'function min() expects one argument' end if values.respond_to?(:to_ary) values = values.to_ary return nil if values.empty? first = values.first first_type = get_type(first) unless first_type == NUMBER_TYPE || first_type == STRING_TYPE msg = String.new('function min() expects numeric or string values') return maybe_raise Errors::InvalidTypeError, msg end values.inject([first, first_type]) do |(min, min_type), v| v_type = get_type(v) if min_type == v_type v < min ? [v, v_type] : [min, min_type] else msg = String.new('function min() encountered a type mismatch in sequence: ') msg << "#{min_type}, #{v_type}" return maybe_raise Errors::InvalidTypeError, msg end end.first else return maybe_raise Errors::InvalidTypeError, 'function min() expects an array' end end end class TypeFunction < Function include TypeChecker FUNCTIONS['type'] = self def call(args) if args.count == 1 TYPE_NAMES[get_type(args.first)] else return maybe_raise Errors::InvalidArityError, 'function type() expects one argument' end end end class KeysFunction < Function FUNCTIONS['keys'] = self def call(args) if args.count == 1 value = args.first if value.respond_to?(:to_hash) value.to_hash.keys.map(&:to_s) elsif value.is_a?(Struct) value.members.map(&:to_s) else return maybe_raise Errors::InvalidTypeError, 'function keys() expects a hash' end else return maybe_raise Errors::InvalidArityError, 'function keys() expects one argument' end end end class ValuesFunction < Function FUNCTIONS['values'] = self def call(args) if args.count == 1 value = args.first if value.respond_to?(:to_hash) value.to_hash.values elsif value.is_a?(Struct) value.values elsif value.respond_to?(:to_ary) value.to_ary else return maybe_raise Errors::InvalidTypeError, 'function values() expects an array or a hash' end else return maybe_raise Errors::InvalidArityError, 'function values() expects one argument' end end end class JoinFunction < Function FUNCTIONS['join'] = self def call(args) if args.count == 2 glue = args[0] values = args[1] if !glue.respond_to?(:to_str) return maybe_raise Errors::InvalidTypeError, 'function join() expects the first argument to be a string' elsif values.respond_to?(:to_ary) && values.to_ary.all? { |v| v.respond_to?(:to_str) } values.to_ary.join(glue) else return maybe_raise Errors::InvalidTypeError, 'function join() expects values to be an array of strings' end else return maybe_raise Errors::InvalidArityError, 'function join() expects an array of strings' end end end class ToStringFunction < Function FUNCTIONS['to_string'] = self def call(args) if args.count == 1 value = args.first value.respond_to?(:to_str) ? value.to_str : value.to_json else return maybe_raise Errors::InvalidArityError, 'function to_string() expects one argument' end end end class ToNumberFunction < Function FUNCTIONS['to_number'] = self def call(args) if args.count == 1 begin value = Float(args.first) Integer(value) === value ? value.to_i : value rescue nil end else return maybe_raise Errors::InvalidArityError, 'function to_number() expects one argument' end end end class SumFunction < Function FUNCTIONS['sum'] = self def call(args) if args.count == 1 && args.first.respond_to?(:to_ary) args.first.to_ary.inject(0) do |sum, n| if Numeric === n sum + n else return maybe_raise Errors::InvalidTypeError, 'function sum() expects values to be numeric' end end else return maybe_raise Errors::InvalidArityError, 'function sum() expects one argument' end end end class NotNullFunction < Function FUNCTIONS['not_null'] = self def call(args) if args.count > 0 args.find { |value| !value.nil? } else return maybe_raise Errors::InvalidArityError, 'function not_null() expects one or more arguments' end end end class SortFunction < Function include TypeChecker FUNCTIONS['sort'] = self def call(args) if args.count == 1 value = args.first if value.respond_to?(:to_ary) value = value.to_ary # every element in the list must be of the same type array_type = get_type(value[0]) if array_type == STRING_TYPE || array_type == NUMBER_TYPE || value.empty? # stable sort n = 0 value.sort_by do |v| value_type = get_type(v) if value_type != array_type msg = 'function sort() expects values to be an array of only numbers, or only integers' return maybe_raise Errors::InvalidTypeError, msg end n += 1 [v, n] end else return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers' end else return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers' end else return maybe_raise Errors::InvalidArityError, 'function sort() expects one argument' end end end class SortByFunction < Function include TypeChecker FUNCTIONS['sort_by'] = self def call(args) if args.count == 2 if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE values = args[0].to_ary expression = args[1] array_type = get_type(expression.eval(values[0])) if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.empty? # stable sort the list n = 0 values.sort_by do |value| value = expression.eval(value) value_type = get_type(value) if value_type != array_type msg = 'function sort() expects values to be an array of only numbers, or only integers' return maybe_raise Errors::InvalidTypeError, msg end n += 1 [value, n] end else return maybe_raise Errors::InvalidTypeError, 'function sort() expects values to be an array of numbers or integers' end else return maybe_raise Errors::InvalidTypeError, 'function sort_by() expects an array and an expression' end else return maybe_raise Errors::InvalidArityError, 'function sort_by() expects two arguments' end end end module CompareBy include TypeChecker def compare_by(mode, *args) if args.count == 2 values = args[0] expression = args[1] if get_type(values) == ARRAY_TYPE && get_type(expression) == EXPRESSION_TYPE values = values.to_ary type = get_type(expression.eval(values.first)) if type != NUMBER_TYPE && type != STRING_TYPE msg = "function #{mode}() expects values to be strings or numbers" return maybe_raise Errors::InvalidTypeError, msg end values.send(mode) do |entry| value = expression.eval(entry) value_type = get_type(value) if value_type != type msg = String.new("function #{mode}() encountered a type mismatch in ") msg << "sequence: #{type}, #{value_type}" return maybe_raise Errors::InvalidTypeError, msg end value end else msg = "function #{mode}() expects an array and an expression" return maybe_raise Errors::InvalidTypeError, msg end else msg = "function #{mode}() expects two arguments" return maybe_raise Errors::InvalidArityError, msg end end end class MaxByFunction < Function include CompareBy FUNCTIONS['max_by'] = self def call(args) compare_by(:max_by, *args) end end class MinByFunction < Function include CompareBy FUNCTIONS['min_by'] = self def call(args) compare_by(:min_by, *args) end end class EndsWithFunction < Function include TypeChecker FUNCTIONS['ends_with'] = self def call(args) if args.count == 2 search, suffix = args search_type = get_type(search) suffix_type = get_type(suffix) if search_type != STRING_TYPE msg = 'function ends_with() expects first argument to be a string' return maybe_raise Errors::InvalidTypeError, msg end if suffix_type != STRING_TYPE msg = 'function ends_with() expects second argument to be a string' return maybe_raise Errors::InvalidTypeError, msg end search.end_with?(suffix) else msg = 'function ends_with() expects two arguments' return maybe_raise Errors::InvalidArityError, msg end end end class StartsWithFunction < Function include TypeChecker FUNCTIONS['starts_with'] = self def call(args) if args.count == 2 search, prefix = args search_type = get_type(search) prefix_type = get_type(prefix) if search_type != STRING_TYPE msg = 'function starts_with() expects first argument to be a string' return maybe_raise Errors::InvalidTypeError, msg end if prefix_type != STRING_TYPE msg = 'function starts_with() expects second argument to be a string' return maybe_raise Errors::InvalidTypeError, msg end search.start_with?(prefix) else msg = 'function starts_with() expects two arguments' return maybe_raise Errors::InvalidArityError, msg end end end class MergeFunction < Function FUNCTIONS['merge'] = self def call(args) if args.count == 0 msg = 'function merge() expects 1 or more arguments' return maybe_raise Errors::InvalidArityError, msg end args.inject({}) do |h, v| h.merge(v) end end end class ReverseFunction < Function FUNCTIONS['reverse'] = self def call(args) if args.count == 0 msg = 'function reverse() expects 1 or more arguments' return maybe_raise Errors::InvalidArityError, msg end value = args.first if value.respond_to?(:to_ary) value.to_ary.reverse elsif value.respond_to?(:to_str) value.to_str.reverse else msg = 'function reverse() expects an array or string' return maybe_raise Errors::InvalidTypeError, msg end end end class ToArrayFunction < Function FUNCTIONS['to_array'] = self def call(args) value = args.first value.respond_to?(:to_ary) ? value.to_ary : [value] end end end end jmespath-1.6.2/lib/jmespath/nodes/flatten.rb0000644000004100000410000000110714350576160021044 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Flatten < Node def initialize(child) @child = child end def visit(value) value = @child.visit(value) if value.respond_to?(:to_ary) value.to_ary.each_with_object([]) do |v, values| if v.respond_to?(:to_ary) values.concat(v.to_ary) else values.push(v) end end end end def optimize self.class.new(@child.optimize) end end end end jmespath-1.6.2/lib/jmespath/nodes/expression.rb0000644000004100000410000000064414350576160021613 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Expression < Node attr_reader :expression def initialize(expression) @expression = expression end def visit(_value) self end def eval(value) @expression.visit(value) end def optimize self.class.new(@expression.optimize) end end end end jmespath-1.6.2/lib/jmespath/nodes/not.rb0000644000004100000410000000052614350576160020213 0ustar www-datawww-data# frozen_string_literal: true module JMESPath module Nodes class Not < Node def initialize(expression) @expression = expression end def visit(value) JMESPath::Util.falsey?(@expression.visit(value)) end def optimize self.class.new(@expression.optimize) end end end end jmespath-1.6.2/lib/jmespath/nodes/field.rb0000644000004100000410000000327514350576160020502 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Field < Node def initialize(key) @key = key @key_sym = key.respond_to?(:to_sym) ? key.to_sym : nil end def visit(value) if value.respond_to?(:to_ary) && @key.is_a?(Integer) value.to_ary[@key] elsif value.respond_to?(:to_hash) value = value.to_hash if !(v = value[@key]).nil? v elsif @key_sym && !(v = value[@key_sym]).nil? v end elsif value.is_a?(Struct) && value.respond_to?(@key) value[@key] end end def chains_with?(other) other.is_a?(Field) end def chain(other) ChainedField.new([@key, *other.keys]) end protected def keys [@key] end end class ChainedField < Field def initialize(keys) @keys = keys @key_syms = keys.each_with_object({}) do |k, syms| syms[k] = k.to_sym if k.respond_to?(:to_sym) end end def visit(obj) @keys.reduce(obj) do |value, key| if value.respond_to?(:to_ary) && key.is_a?(Integer) value.to_ary[key] elsif value.respond_to?(:to_hash) value = value.to_hash if !(v = value[key]).nil? v elsif (sym = @key_syms[key]) && !(v = value[sym]).nil? v end elsif value.is_a?(Struct) && value.respond_to?(key) value[key] end end end def chain(other) ChainedField.new([*@keys, *other.keys]) end private attr_reader :keys end end end jmespath-1.6.2/lib/jmespath/errors.rb0000644000004100000410000000054114350576160017614 0ustar www-datawww-data# frozen_string_literal: true module JMESPath module Errors class Error < StandardError; end class RuntimeError < Error; end class SyntaxError < Error; end class InvalidTypeError < Error; end class InvalidValueError < Error; end class InvalidArityError < Error; end class UnknownFunctionError < Error; end end end jmespath-1.6.2/lib/jmespath/token_stream.rb0000644000004100000410000000242014350576160020771 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private class TokenStream # @param [String] expression # @param [Array] tokens def initialize(expression, tokens) @expression = expression @tokens = tokens @token = nil @position = -1 self.next end # @return [String] attr_reader :expression # @return [Token] attr_reader :token # @return [Integer] attr_reader :position # @option options [Array] :match Requires the next token to be # one of the given symbols or an error is raised. def next(options = {}) validate_match(_next, options[:match]) end def lookahead(count) @tokens[@position + count] || Token::NULL_TOKEN end # @api private def inspect str = [] @tokens.each do |token| str << '%3d %-15s %s' % [token.position, token.type, token.value.inspect] end str.join("\n") end private def _next @position += 1 @token = @tokens[@position] || Token::NULL_TOKEN end def validate_match(token, match) if match && !match.include?(token.type) raise Errors::SyntaxError, 'type missmatch' else token end end end end jmespath-1.6.2/lib/jmespath/token.rb0000644000004100000410000000251314350576160017421 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private class Token < Struct.new(:type, :value, :position, :binding_power) NULL_TOKEN = Token.new(:eof, '', nil) BINDING_POWER = { Lexer::T_UNKNOWN => 0, Lexer::T_EOF => 0, Lexer::T_QUOTED_IDENTIFIER => 0, Lexer::T_IDENTIFIER => 0, Lexer::T_RBRACKET => 0, Lexer::T_RPAREN => 0, Lexer::T_COMMA => 0, Lexer::T_RBRACE => 0, Lexer::T_NUMBER => 0, Lexer::T_CURRENT => 0, Lexer::T_EXPREF => 0, Lexer::T_COLON => 0, Lexer::T_PIPE => 1, Lexer::T_OR => 2, Lexer::T_AND => 3, Lexer::T_COMPARATOR => 5, Lexer::T_FLATTEN => 9, Lexer::T_STAR => 20, Lexer::T_FILTER => 21, Lexer::T_DOT => 40, Lexer::T_NOT => 45, Lexer::T_LBRACE => 50, Lexer::T_LBRACKET => 55, Lexer::T_LPAREN => 60 }.freeze # @param [Symbol] type # @param [Mixed] value # @param [Integer] position def initialize(type, value, position) super(type, value, position, BINDING_POWER[type]) end end end jmespath-1.6.2/lib/jmespath/parser.rb0000644000004100000410000002242014350576160017574 0ustar www-datawww-data# frozen_string_literal: true require 'set' module JMESPath # @api private class Parser AFTER_DOT = Set.new([ Lexer::T_IDENTIFIER, # foo.bar Lexer::T_QUOTED_IDENTIFIER, # foo."bar" Lexer::T_STAR, # foo.* Lexer::T_LBRACE, # foo{a: 0} Lexer::T_LBRACKET, # foo[1] Lexer::T_FILTER, # foo.[?bar==10] ]) NUM_COLON_RBRACKET = Set.new([ Lexer::T_NUMBER, Lexer::T_COLON, Lexer::T_RBRACKET ]) COLON_RBRACKET = Set.new([ Lexer::T_COLON, Lexer::T_RBRACKET ]) CURRENT_NODE = Nodes::Current.new # @option options [Lexer] :lexer def initialize(options = {}) @lexer = options[:lexer] || Lexer.new @disable_visit_errors = options[:disable_visit_errors] end # @param [String] expression def parse(expression) tokens = @lexer.tokenize(expression) stream = TokenStream.new(expression, tokens) result = expr(stream) if stream.token.type != Lexer::T_EOF raise Errors::SyntaxError, "expected :eof got #{stream.token.type}" else result end end # @api private def method_missing(method_name, *args) if matches = method_name.to_s.match(/^(nud_|led_)(.*)/) raise Errors::SyntaxError, "unexpected token #{matches[2]}" else super end end private # @param [TokenStream] stream # @param [Integer] rbp Right binding power def expr(stream, rbp = 0) left = send("nud_#{stream.token.type}", stream) while rbp < (stream.token.binding_power || 0) left = send("led_#{stream.token.type}", stream, left) end left end def nud_current(stream) stream.next CURRENT_NODE end def nud_expref(stream) stream.next Nodes::Expression.new(expr(stream, Token::BINDING_POWER[:expref])) end def nud_not(stream) stream.next Nodes::Not.new(expr(stream, Token::BINDING_POWER[:not])) end def nud_lparen(stream) stream.next result = expr(stream, 0) if stream.token.type != Lexer::T_RPAREN raise Errors::SyntaxError, 'Unclosed `(`' end stream.next result end def nud_filter(stream) led_filter(stream, CURRENT_NODE) end def nud_flatten(stream) led_flatten(stream, CURRENT_NODE) end def nud_identifier(stream) token = stream.token n = stream.next if n.type == :lparen Nodes::Function::FunctionName.new(token.value) else Nodes::Field.new(token.value) end end def nud_lbrace(stream) valid_keys = Set.new([:quoted_identifier, :identifier]) stream.next(match: valid_keys) pairs = [] begin pairs << parse_key_value_pair(stream) stream.next(match: valid_keys) if stream.token.type == :comma end while stream.token.type != :rbrace stream.next Nodes::MultiSelectHash.new(pairs) end def nud_lbracket(stream) stream.next type = stream.token.type if type == :number || type == :colon parse_array_index_expression(stream) elsif type == :star && stream.lookahead(1).type == :rbracket parse_wildcard_array(stream) else parse_multi_select_list(stream) end end def nud_literal(stream) value = stream.token.value stream.next Nodes::Literal.new(value) end def nud_quoted_identifier(stream) token = stream.token next_token = stream.next if next_token.type == :lparen msg = 'quoted identifiers are not allowed for function names' raise Errors::SyntaxError, msg else Nodes::Field.new(token[:value]) end end def nud_star(stream) parse_wildcard_object(stream, CURRENT_NODE) end def nud_unknown(stream) raise Errors::SyntaxError, "unknown token #{stream.token.value.inspect}" end def led_comparator(stream, left) token = stream.token stream.next right = expr(stream, Token::BINDING_POWER[:comparator]) Nodes::Comparator.create(token.value, left, right) end def led_dot(stream, left) stream.next(match: AFTER_DOT) if stream.token.type == :star parse_wildcard_object(stream, left) else right = parse_dot(stream, Token::BINDING_POWER[:dot]) Nodes::Subexpression.new(left, right) end end def led_filter(stream, left) stream.next expression = expr(stream) if stream.token.type != Lexer::T_RBRACKET raise Errors::SyntaxError, 'expected a closing rbracket for the filter' end stream.next rhs = parse_projection(stream, Token::BINDING_POWER[Lexer::T_FILTER]) left ||= CURRENT_NODE right = Nodes::Condition.new(expression, rhs) Nodes::ArrayProjection.new(left, right) end def led_flatten(stream, left) stream.next left = Nodes::Flatten.new(left) right = parse_projection(stream, Token::BINDING_POWER[:flatten]) Nodes::ArrayProjection.new(left, right) end def led_lbracket(stream, left) stream.next(match: Set.new([:number, :colon, :star])) type = stream.token.type if type == :number || type == :colon right = parse_array_index_expression(stream) Nodes::Subexpression.new(left, right) else parse_wildcard_array(stream, left) end end def led_lparen(stream, left) args = [] if Nodes::Function::FunctionName === left name = left.name else raise Errors::SyntaxError, 'invalid function invocation' end stream.next while stream.token.type != :rparen args << expr(stream, 0) stream.next if stream.token.type == :comma end stream.next Nodes::Function.create(name, args, disable_visit_errors: @disable_visit_errors) end def led_or(stream, left) stream.next right = expr(stream, Token::BINDING_POWER[:or]) Nodes::Or.new(left, right) end def led_and(stream, left) stream.next right = expr(stream, Token::BINDING_POWER[:or]) Nodes::And.new(left, right) end def led_pipe(stream, left) stream.next right = expr(stream, Token::BINDING_POWER[:pipe]) Nodes::Pipe.new(left, right) end # parse array index expressions, for example [0], [1:2:3], etc. def parse_array_index_expression(stream) pos = 0 parts = [nil, nil, nil] expected = NUM_COLON_RBRACKET begin if stream.token.type == Lexer::T_COLON pos += 1 expected = NUM_COLON_RBRACKET elsif stream.token.type == Lexer::T_NUMBER parts[pos] = stream.token.value expected = COLON_RBRACKET end stream.next(match: expected) end while stream.token.type != Lexer::T_RBRACKET stream.next # consume the closing bracket if pos == 0 # no colons found, this is a single index extraction Nodes::Index.new(parts[0]) elsif pos > 2 raise Errors::SyntaxError, 'invalid array slice syntax: too many colons' else Nodes::ArrayProjection.new( Nodes::Slice.new(*parts), parse_projection(stream, Token::BINDING_POWER[Lexer::T_STAR]) ) end end def parse_dot(stream, binding_power) if stream.token.type == :lbracket stream.next parse_multi_select_list(stream) else expr(stream, binding_power) end end def parse_key_value_pair(stream) key = stream.token.value stream.next(match: Set.new([:colon])) stream.next Nodes::MultiSelectHash::KeyValuePair.new(key, expr(stream)) end def parse_multi_select_list(stream) nodes = [] begin nodes << expr(stream) if stream.token.type == :comma stream.next if stream.token.type == :rbracket raise Errors::SyntaxError, 'expression epxected, found rbracket' end end end while stream.token.type != :rbracket stream.next Nodes::MultiSelectList.new(nodes) end def parse_projection(stream, binding_power) type = stream.token.type if stream.token.binding_power < 10 CURRENT_NODE elsif type == :dot stream.next(match: AFTER_DOT) parse_dot(stream, binding_power) elsif type == :lbracket || type == :filter expr(stream, binding_power) else raise Errors::SyntaxError, 'syntax error after projection' end end def parse_wildcard_array(stream, left = nil) stream.next(match: Set.new([:rbracket])) stream.next left ||= CURRENT_NODE right = parse_projection(stream, Token::BINDING_POWER[:star]) Nodes::ArrayProjection.new(left, right) end def parse_wildcard_object(stream, left = nil) stream.next left ||= CURRENT_NODE right = parse_projection(stream, Token::BINDING_POWER[:star]) Nodes::ObjectProjection.new(left, right) end end end jmespath-1.6.2/lib/jmespath/util.rb0000644000004100000410000000203714350576160017257 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Util class << self # Determines if a value is false as defined by JMESPath: # # https://github.com/jmespath/jmespath.site/blob/master/docs/proposals/improved-filters.rst#and-expressions-1 # def falsey?(value) !value || (value.respond_to?(:to_ary) && value.to_ary.empty?) || (value.respond_to?(:to_hash) && value.to_hash.empty?) || (value.respond_to?(:to_str) && value.to_str.empty?) || (value.respond_to?(:entries) && !value.entries.any?) # final case necessary to support Enumerable and Struct end def as_json(value) if value.respond_to?(:to_ary) value.to_ary.map { |e| as_json(e) } elsif value.respond_to?(:to_hash) hash = {} value.to_hash.each_pair { |k, v| hash[k] = as_json(v) } hash elsif value.respond_to?(:to_str) value.to_str else value end end end end end jmespath-1.6.2/lib/jmespath/nodes.rb0000644000004100000410000000205314350576160017410 0ustar www-datawww-data# frozen_string_literal: true module JMESPath # @api private module Nodes class Node def visit(value) end def optimize self end def chains_with?(_other) false end end require 'jmespath/nodes/subexpression' require 'jmespath/nodes/and' require 'jmespath/nodes/comparator' require 'jmespath/nodes/comparator' require 'jmespath/nodes/condition' require 'jmespath/nodes/current' require 'jmespath/nodes/expression' require 'jmespath/nodes/field' require 'jmespath/nodes/flatten' require 'jmespath/nodes/function' require 'jmespath/nodes/index' require 'jmespath/nodes/literal' require 'jmespath/nodes/multi_select_hash' require 'jmespath/nodes/multi_select_list' require 'jmespath/nodes/not' require 'jmespath/nodes/or' require 'jmespath/nodes/pipe' require 'jmespath/nodes/projection' require 'jmespath/nodes/projection' require 'jmespath/nodes/projection' require 'jmespath/nodes/slice' end end jmespath-1.6.2/lib/jmespath.rb0000644000004100000410000000220414350576160016276 0ustar www-datawww-data# frozen_string_literal: true require 'json' require 'stringio' require 'pathname' module JMESPath require 'jmespath/caching_parser' require 'jmespath/errors' require 'jmespath/lexer' require 'jmespath/nodes' require 'jmespath/parser' require 'jmespath/runtime' require 'jmespath/token' require 'jmespath/token_stream' require 'jmespath/util' require 'jmespath/version' class << self # @param [String] expression A valid # [JMESPath](https://github.com/boto/jmespath) expression. # @param [Hash] data # @return [Mixed,nil] Returns the matched values. Returns `nil` if the # expression does not resolve inside `data`. def search(expression, data, runtime_options = {}) data = case data when Hash, Struct then data # check for most common case first when Pathname then load_json(data) when IO, StringIO then JSON.parse(data.read) else data end Runtime.new(runtime_options).search(expression, data) end # @api private def load_json(path) JSON.parse(File.open(path, 'r', encoding: 'UTF-8', &:read)) end end end jmespath-1.6.2/jmespath.gemspec0000644000004100000410000000376414350576160016564 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: jmespath 1.6.2 ruby lib Gem::Specification.new do |s| s.name = "jmespath".freeze s.version = "1.6.2" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Trevor Rowe".freeze] s.date = "2022-11-25" s.description = "Implements JMESPath for Ruby".freeze s.email = "trevorrowe@gmail.com".freeze s.executables = ["jmespath.rb".freeze] s.files = ["LICENSE.txt".freeze, "VERSION".freeze, "bin/jmespath.rb".freeze, "lib/jmespath.rb".freeze, "lib/jmespath/caching_parser.rb".freeze, "lib/jmespath/errors.rb".freeze, "lib/jmespath/lexer.rb".freeze, "lib/jmespath/nodes.rb".freeze, "lib/jmespath/nodes/and.rb".freeze, "lib/jmespath/nodes/comparator.rb".freeze, "lib/jmespath/nodes/condition.rb".freeze, "lib/jmespath/nodes/current.rb".freeze, "lib/jmespath/nodes/expression.rb".freeze, "lib/jmespath/nodes/field.rb".freeze, "lib/jmespath/nodes/flatten.rb".freeze, "lib/jmespath/nodes/function.rb".freeze, "lib/jmespath/nodes/index.rb".freeze, "lib/jmespath/nodes/literal.rb".freeze, "lib/jmespath/nodes/multi_select_hash.rb".freeze, "lib/jmespath/nodes/multi_select_list.rb".freeze, "lib/jmespath/nodes/not.rb".freeze, "lib/jmespath/nodes/or.rb".freeze, "lib/jmespath/nodes/pipe.rb".freeze, "lib/jmespath/nodes/projection.rb".freeze, "lib/jmespath/nodes/slice.rb".freeze, "lib/jmespath/nodes/subexpression.rb".freeze, "lib/jmespath/parser.rb".freeze, "lib/jmespath/runtime.rb".freeze, "lib/jmespath/token.rb".freeze, "lib/jmespath/token_stream.rb".freeze, "lib/jmespath/util.rb".freeze, "lib/jmespath/version.rb".freeze] s.homepage = "http://github.com/trevorrowe/jmespath.rb".freeze s.licenses = ["Apache-2.0".freeze] s.rubygems_version = "3.2.5".freeze s.summary = "JMESPath - Ruby Edition".freeze end jmespath-1.6.2/LICENSE.txt0000644000004100000410000002273614350576160015227 0ustar www-datawww-data Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. jmespath-1.6.2/VERSION0000644000004100000410000000000614350576160014436 0ustar www-datawww-data1.6.2