Skip to content

Commit

Permalink
Merge pull request #56 from geophilusd/cleanup
Browse files Browse the repository at this point in the history
Add frozen_string_literal to all files and some static code cleanup
  • Loading branch information
mullermp authored May 24, 2022
2 parents 6e429f6 + fef3ed5 commit b208836
Show file tree
Hide file tree
Showing 45 changed files with 237 additions and 285 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

gem 'rake', require: false
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
$GEM_ROOT = File.dirname(__FILE__)

$: << File.join($GEM_ROOT, 'lib')
$LOAD_PATH << File.join($GEM_ROOT, 'lib')

$VERSION = ENV['VERSION'] || File.read(File.join($GEM_ROOT, 'VERSION'))
$GITHUB_ACCESS_TOKEN = ENV['JMESPATH_GITHUB_ACCESS_TOKEN']
Expand Down
3 changes: 2 additions & 1 deletion bin/jmespath.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'jmespath'
require 'json'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/json-latest.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
eval_gemfile(File.expand_path('../../Gemfile', __FILE__))

gem 'json'
1 change: 1 addition & 0 deletions gemfiles/json-old.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
eval_gemfile(File.expand_path('../../Gemfile', __FILE__))

gem 'json', '< 2.0'
1 change: 1 addition & 0 deletions gemfiles/json-pure.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
eval_gemfile(File.expand_path('../../Gemfile', __FILE__))

gem 'json_pure', require: 'json/pure'
1 change: 1 addition & 0 deletions gemfiles/no-deps.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
eval_gemfile(File.expand_path('../../Gemfile', __FILE__))

# no dependency on `json` gem
3 changes: 2 additions & 1 deletion jmespath.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
Gem::Specification.new do |spec|
spec.name = 'jmespath'
spec.version = File.read(File.expand_path('../VERSION', __FILE__)).strip
Expand All @@ -8,5 +9,5 @@ Gem::Specification.new do |spec|
spec.homepage = 'http://github.com/trevorrowe/jmespath.rb'
spec.license = 'Apache-2.0'
spec.require_paths = ['lib']
spec.files = Dir['lib/**/*.rb'] + %w[LICENSE.txt VERSION]
spec.files = Dir['lib/**/*.rb'] + %w(LICENSE.txt VERSION)
end
14 changes: 6 additions & 8 deletions lib/jmespath.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true
require 'json'
require 'stringio'
require 'pathname'

module JMESPath

require 'jmespath/caching_parser'
require 'jmespath/errors'
require 'jmespath/lexer'
Expand All @@ -16,26 +16,24 @@ module JMESPath
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
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') { |f| f.read })
JSON.parse(File.open(path, 'r', encoding: 'UTF-8', &:read))
end

end
end
3 changes: 1 addition & 2 deletions lib/jmespath/caching_parser.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true
require 'thread'

module JMESPath
class CachingParser

def initialize(options = {})
@parser = options[:parser] || Parser.new(options)
@mutex = Mutex.new
Expand All @@ -25,6 +25,5 @@ def cache_expression(expression)
@cache[expression] = @parser.parse(expression)
end
end

end
end
4 changes: 2 additions & 2 deletions lib/jmespath/errors.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module JMESPath
module Errors

class Error < StandardError; end

class RuntimeError < Error; end
Expand All @@ -14,6 +15,5 @@ class InvalidValueError < Error; end
class InvalidArityError < Error; end

class UnknownFunctionError < Error; end

end
end
41 changes: 19 additions & 22 deletions lib/jmespath/lexer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true
require 'json'
require 'set'

module JMESPath
# @api private
class Lexer

T_DOT = :dot
T_STAR = :star
T_COMMA = :comma
Expand Down Expand Up @@ -134,14 +134,14 @@ class Lexer
'w' => STATE_IDENTIFIER,
'x' => STATE_IDENTIFIER,
'y' => STATE_IDENTIFIER,
'z' => 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
))
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))

Expand All @@ -155,13 +155,12 @@ class Lexer
'(' => T_LPAREN,
')' => T_RPAREN,
'{' => T_LBRACE,
'}' => T_RBRACE,
}
'}' => T_RBRACE
}.freeze

# @param [String<JMESPath>] expression
# @return [Array<Hash>]
def tokenize(expression)

tokens = []
chars = CharacterStream.new(expression.chars.to_a)

Expand Down Expand Up @@ -253,7 +252,7 @@ def tokenize(expression)
tokens << match_or(chars, '&', '&', T_AND, T_EXPREF)
when STATE_NOT
# consume not equals
tokens << match_or(chars, '!', '=', T_COMPARATOR, T_NOT);
tokens << match_or(chars, '!', '=', T_COMPARATOR, T_NOT)
else
# either '<' or '>'
# consume less than and greater than
Expand Down Expand Up @@ -321,11 +320,9 @@ def inside(chars, delim, type)
# if we have to wrap scalar JSON values to parse them or not.
# @api private
def self.requires_wrapping?
begin
JSON.parse('false')
rescue JSON::ParserError
true
end
JSON.parse('false')
rescue JSON::ParserError
true
end

if requires_wrapping?
Expand All @@ -351,7 +348,11 @@ def parse_json(token, quoted = false)
if quoted
token.value = JSON.parse(token.value)
else
token.value = JSON.parse(token.value) rescue JSON.parse(sprintf('"%s"', token.value.lstrip))
token.value = begin
JSON.parse(token.value)
rescue
JSON.parse(sprintf('"%s"', token.value.lstrip))
end
end
rescue JSON::ParserError
token.type = T_UNKNOWN
Expand All @@ -361,7 +362,6 @@ def parse_json(token, quoted = false)
end

class CharacterStream

def initialize(chars)
@chars = chars
@position = 0
Expand All @@ -376,10 +376,7 @@ def next
@chars[@position]
end

def position
@position
end

attr_reader :position
end
end
end
7 changes: 3 additions & 4 deletions lib/jmespath/nodes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
Expand All @@ -9,11 +10,11 @@ def optimize
self
end

def chains_with?(other)
def chains_with?(_other)
false
end
end

require 'jmespath/nodes/subexpression'
require 'jmespath/nodes/and'
require 'jmespath/nodes/comparator'
Expand All @@ -35,7 +36,5 @@ def chains_with?(other)
require 'jmespath/nodes/projection'
require 'jmespath/nodes/projection'
require 'jmespath/nodes/slice'


end
end
3 changes: 1 addition & 2 deletions lib/jmespath/nodes/and.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
module JMESPath
module Nodes
class And < Node

def initialize(left, right)
@left = left
@right = right
Expand All @@ -19,7 +19,6 @@ def visit(value)
def optimize
self.class.new(@left.optimize, @right.optimize)
end

end
end
end
28 changes: 6 additions & 22 deletions lib/jmespath/nodes/comparator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
Expand Down Expand Up @@ -35,7 +36,7 @@ def optimize

private

def check(left_value, right_value)
def check(_left_value, _right_value)
nil
end

Expand All @@ -47,7 +48,6 @@ def comparable?(left_value, right_value)
end

module Comparators

class Eq < Comparator
def check(left_value, right_value)
Util.as_json(left_value) == Util.as_json(right_value)
Expand All @@ -62,41 +62,25 @@ def check(left_value, right_value)

class Gt < Comparator
def check(left_value, right_value)
if comparable?(left_value, right_value)
left_value > right_value
else
nil
end
left_value > right_value if comparable?(left_value, right_value)
end
end

class Gte < Comparator
def check(left_value, right_value)
if comparable?(left_value, right_value)
left_value >= right_value
else
nil
end
left_value >= right_value if comparable?(left_value, right_value)
end
end

class Lt < Comparator
def check(left_value, right_value)
if comparable?(left_value, right_value)
left_value < right_value
else
nil
end
left_value < right_value if comparable?(left_value, right_value)
end
end

class Lte < Comparator
def check(left_value, right_value)
if comparable?(left_value, right_value)
left_value <= right_value
else
nil
end
left_value <= right_value if comparable?(left_value, right_value)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/jmespath/nodes/condition.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
Expand Down Expand Up @@ -35,7 +36,7 @@ def initialize(left, right, child)
@child = child
end

def visit(value)
def visit(_value)
nil
end

Expand Down
1 change: 1 addition & 0 deletions lib/jmespath/nodes/current.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
Expand Down
4 changes: 2 additions & 2 deletions lib/jmespath/nodes/expression.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module JMESPath
# @api private
module Nodes
Expand All @@ -8,7 +9,7 @@ def initialize(expression)
@expression = expression
end

def visit(value)
def visit(_value)
self
end

Expand All @@ -22,4 +23,3 @@ def optimize
end
end
end

Loading

0 comments on commit b208836

Please sign in to comment.