check_please 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +6 -0
- data/bin/check_please +70 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/check_please.gemspec +32 -0
- data/lib/check_please.rb +38 -0
- data/lib/check_please/comparison.rb +85 -0
- data/lib/check_please/diff.rb +38 -0
- data/lib/check_please/diffs.rb +52 -0
- data/lib/check_please/path.rb +23 -0
- data/lib/check_please/printers.rb +22 -0
- data/lib/check_please/printers/base.rb +25 -0
- data/lib/check_please/printers/json.rb +27 -0
- data/lib/check_please/printers/table_print.rb +37 -0
- data/lib/check_please/version.rb +3 -0
- data/usage_examples.rb +14 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9e2ff8cbe19131bcf4995eb057ef566bc8f4f102e138d7db5dc908befa1fbcf
|
4
|
+
data.tar.gz: 616388b3ee563b7f384b7fc19b051117c0f83bd00e47092f7f6f7b9ec712c81b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f4b7bcef1fda2618631f59fe8ad17a2630ba33271a2c6e295b96f204076c29ba283b3bb19cfba1c8550cc03ecd709fd1aa9ecbf22db4846d3d1f7b302b7c4ee5
|
7
|
+
data.tar.gz: 18be8f0813b99bb66930ceb5dc2b658b377522c84d77ed3361daaecfc1aaff08f439582bb0e63056e8f8617239379c25ed44b8a517d7d8eaf92834b616d77267
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at geeksam@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
check_please (0.1.0)
|
5
|
+
table_print
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
amazing_print (1.2.2)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
rake (12.3.3)
|
13
|
+
rspec (3.10.0)
|
14
|
+
rspec-core (~> 3.10.0)
|
15
|
+
rspec-expectations (~> 3.10.0)
|
16
|
+
rspec-mocks (~> 3.10.0)
|
17
|
+
rspec-core (3.10.0)
|
18
|
+
rspec-support (~> 3.10.0)
|
19
|
+
rspec-expectations (3.10.0)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.10.0)
|
22
|
+
rspec-mocks (3.10.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.10.0)
|
25
|
+
rspec-support (3.10.0)
|
26
|
+
table_print (1.5.7)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
amazing_print
|
33
|
+
check_please!
|
34
|
+
rake (~> 12.0)
|
35
|
+
rspec (~> 3.0)
|
36
|
+
|
37
|
+
BUNDLED WITH
|
38
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Sam Livingston-Gray
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# CheckPlease
|
2
|
+
|
3
|
+
Check for differences between two JSON strings (or Ruby data structures parsed from them).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'check_please'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install check_please
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### CLI
|
24
|
+
|
25
|
+
Use the `bin/check_please` executable. (To get started, run it with the '-h' flag.)
|
26
|
+
|
27
|
+
### From Within Ruby
|
28
|
+
|
29
|
+
Create two JSON strings and pass them to `CheckPlease.render_diff`. You'll get
|
30
|
+
back a third string containing a nicely formatted report of all the differences
|
31
|
+
CheckPlease found in the two JSON strings. (See also: ./usage_examples.rb.)
|
32
|
+
|
33
|
+
(You can also parse the JSON strings yourself and pass the resulting data
|
34
|
+
structures in, if you're into that. I mean, I wrote this to help compare JSON
|
35
|
+
data that's too big and complicated to scan through visually, but you do you!
|
36
|
+
|
37
|
+
## TODO
|
38
|
+
|
39
|
+
* rspec custom matcher (separate gem?)
|
40
|
+
* command line flags for :allthethings:!
|
41
|
+
* limit to first N
|
42
|
+
* sort by path?
|
43
|
+
* max depth (for iterative refinement?)
|
44
|
+
* detect timestamps and compare after parsing?
|
45
|
+
* ignore sub-second precision (option / CLI flag)?
|
46
|
+
* possibly support plugins for other folks to add custom coercions?
|
47
|
+
* support expressions of specific paths to ignore
|
48
|
+
* wildcards? `#` for indexes, `**` to match one or more path segments?
|
49
|
+
(This could get ugly fast.)
|
50
|
+
* display filters? (e.g., { a: 1, b: 2 } ==> "Hash#3")
|
51
|
+
* shorter descriptions of values with different classes
|
52
|
+
(but maybe just the existing :type_mismatch diffs?)
|
53
|
+
* another "possibly support plugins" expansion point here
|
54
|
+
* more output formats, maybe?
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
59
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
60
|
+
prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
63
|
+
release a new version, update the version number in `version.rb`, and then run
|
64
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
65
|
+
git commits and tags, and push the `.gem` file to
|
66
|
+
[rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at
|
71
|
+
https://github.com/[USERNAME]/check_please. This project is intended to be a
|
72
|
+
safe, welcoming space for collaboration, and contributors are expected to
|
73
|
+
adhere to the [code of
|
74
|
+
conduct](https://github.com/[USERNAME]/check_please/blob/master/CODE_OF_CONDUCT.md).
|
75
|
+
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT
|
80
|
+
License](https://opensource.org/licenses/MIT).
|
81
|
+
|
82
|
+
## Code of Conduct
|
83
|
+
|
84
|
+
Everyone interacting in the CheckPlease project's codebases, issue trackers,
|
85
|
+
chat rooms and mailing lists is expected to follow the [code of
|
86
|
+
conduct](https://github.com/[USERNAME]/check_please/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/check_please
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
require_relative '../lib/check_please'
|
6
|
+
|
7
|
+
argv = ARGV.dup
|
8
|
+
|
9
|
+
ref_file = argv.shift
|
10
|
+
can_file = argv.shift
|
11
|
+
diff_opts = {}
|
12
|
+
|
13
|
+
@parser = OptionParser.new do |opts|
|
14
|
+
opts.banner = <<~EOF
|
15
|
+
Usage: #{__FILE__} <reference> <candidate> <options>
|
16
|
+
|
17
|
+
Tool for parsing and diffing two JSON files.
|
18
|
+
|
19
|
+
Arguments:
|
20
|
+
<reference> is the name of a file to use as the reference.
|
21
|
+
<candidate> is the name of a file to compare against the reference.
|
22
|
+
|
23
|
+
NOTE: If the <candidate> arg is omitted, stdin will be used instead.
|
24
|
+
This allows you to copy candidate JSON to the clipboard and (on a Mac) do:
|
25
|
+
|
26
|
+
$ pbpaste | #{__FILE__} <reference>
|
27
|
+
|
28
|
+
<options>:
|
29
|
+
EOF
|
30
|
+
|
31
|
+
formats = CheckPlease::Printers::FORMATS.join(", ")
|
32
|
+
|
33
|
+
opts.on("-f FORMAT", "--format FORMAT", "specify the format (available options: [#{formats}]") do |val|
|
34
|
+
diff_opts[:format] = val
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
def print_help_and_exit
|
41
|
+
@parser.parse(%w[--help])
|
42
|
+
exit # technically redundant but helps me feel better
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_file(filename)
|
46
|
+
return nil if filename.to_s =~ /^\s*$/
|
47
|
+
File.read(filename)
|
48
|
+
rescue Errno::ENOENT
|
49
|
+
# no such file, buddy
|
50
|
+
return nil
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
# First off, try to read in the files the user told us about...
|
56
|
+
reference = read_file(ref_file)
|
57
|
+
candidate = read_file(can_file) || $stdin.read
|
58
|
+
|
59
|
+
print_help_and_exit if reference.to_s =~ /^\s*$/
|
60
|
+
print_help_and_exit if candidate.to_s =~ /^\s*$/
|
61
|
+
|
62
|
+
begin
|
63
|
+
@parser.parse(argv)
|
64
|
+
rescue OptionParser::InvalidOption, OptionParser::AmbiguousOption => e
|
65
|
+
puts "\n>>> #{e.message}\n\n"
|
66
|
+
print_help_and_exit
|
67
|
+
end
|
68
|
+
|
69
|
+
report = CheckPlease.render_diff(reference, candidate, **diff_opts)
|
70
|
+
puts report
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "check_please"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'lib/check_please/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "check_please"
|
5
|
+
spec.version = CheckPlease::VERSION
|
6
|
+
spec.authors = ["Sam Livingston-Gray"]
|
7
|
+
spec.email = ["geeksam@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Check for differences between two JSON strings (or Ruby data structures parsed from them)}
|
10
|
+
spec.description = %q{Check for differences between two JSON strings (or Ruby data structures parsed from them)}
|
11
|
+
spec.homepage = "https://github.com/RealGeeks/check_please"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
17
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "table_print"
|
29
|
+
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
31
|
+
spec.add_development_dependency "amazing_print"
|
32
|
+
end
|
data/lib/check_please.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative "check_please/version"
|
2
|
+
require_relative "check_please/path"
|
3
|
+
require_relative "check_please/comparison"
|
4
|
+
require_relative "check_please/diff"
|
5
|
+
require_relative "check_please/diffs"
|
6
|
+
require_relative "check_please/printers"
|
7
|
+
|
8
|
+
module CheckPlease
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
def self.diff(reference, candidate)
|
12
|
+
reference = maybe_parse(reference)
|
13
|
+
candidate = maybe_parse(candidate)
|
14
|
+
Comparison.perform(reference, candidate)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.render_diff(reference, candidate, format: nil)
|
18
|
+
diffs = diff(reference, candidate)
|
19
|
+
Printers.render(diffs, format)
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
private
|
24
|
+
|
25
|
+
# Maybe you gave us JSON strings, maybe you gave us Ruby objects.
|
26
|
+
# We just don't know! That's what makes it so exciting!
|
27
|
+
def maybe_parse(maybe_json)
|
28
|
+
|
29
|
+
case maybe_json
|
30
|
+
when String ; JSON.parse(maybe_json) # don't worry, if this raises we'll assume you've already parsed it
|
31
|
+
else ; maybe_json
|
32
|
+
end
|
33
|
+
|
34
|
+
rescue JSON::ParserError
|
35
|
+
return maybe_json
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module CheckPlease
|
2
|
+
|
3
|
+
module Comparison
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def perform(reference, candidate)
|
7
|
+
root = CheckPlease::Path.new
|
8
|
+
diffs = Diffs.new
|
9
|
+
compare reference, candidate, root, diffs
|
10
|
+
diffs
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def compare(ref, can, path, diffs)
|
16
|
+
case types(ref, can)
|
17
|
+
when [ :array, :array ] ; compare_arrays ref, can, path, diffs
|
18
|
+
when [ :hash, :hash ] ; compare_hashes ref, can, path, diffs
|
19
|
+
when [ :other, :other ] ; compare_others ref, can, path, diffs
|
20
|
+
else
|
21
|
+
diffs.record ref, can, path, :type_mismatch
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def types(*list)
|
26
|
+
list.map { |e|
|
27
|
+
case e
|
28
|
+
when Array ; :array
|
29
|
+
when Hash ; :hash
|
30
|
+
else ; :other
|
31
|
+
end
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def compare_arrays(ref_array, can_array, path, diffs)
|
36
|
+
max_len = [ ref_array, can_array ].map(&:length).max
|
37
|
+
(0...max_len).each do |i|
|
38
|
+
n = i + 1 # count in human pls
|
39
|
+
new_path = path + n
|
40
|
+
|
41
|
+
ref = ref_array[i]
|
42
|
+
can = can_array[i]
|
43
|
+
|
44
|
+
case
|
45
|
+
when ref_array.length < n ; diffs.record ref, can, new_path, :extra
|
46
|
+
when can_array.length < n ; diffs.record ref, can, new_path, :missing
|
47
|
+
else ; compare ref, can, new_path, diffs
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def compare_hashes(ref_hash, can_hash, path, diffs)
|
53
|
+
record_missing_keys ref_hash, can_hash, path, diffs
|
54
|
+
compare_common_keys ref_hash, can_hash, path, diffs
|
55
|
+
record_extra_keys ref_hash, can_hash, path, diffs
|
56
|
+
end
|
57
|
+
|
58
|
+
def record_missing_keys(ref_hash, can_hash, path, diffs)
|
59
|
+
keys = ref_hash.keys - can_hash.keys
|
60
|
+
keys.each do |k|
|
61
|
+
diffs.record ref_hash[k], nil, path + k, :missing
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def compare_common_keys(ref_hash, can_hash, path, diffs)
|
66
|
+
keys = ref_hash.keys & can_hash.keys
|
67
|
+
keys.each do |k|
|
68
|
+
compare ref_hash[k], can_hash[k], path + k, diffs
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def record_extra_keys(ref_hash, can_hash, path, diffs)
|
73
|
+
keys = can_hash.keys - ref_hash.keys
|
74
|
+
keys.each do |k|
|
75
|
+
diffs.record nil, can_hash[k], path + k, :extra
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def compare_others(ref, can, path, diffs)
|
80
|
+
return if ref == can
|
81
|
+
diffs.record ref, can, path, :mismatch
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module CheckPlease
|
2
|
+
|
3
|
+
class Diff
|
4
|
+
COLUMNS = %i[ type path reference candidate ]
|
5
|
+
|
6
|
+
attr_reader :type, :reference, :candidate, :path
|
7
|
+
def initialize(type, reference, candidate, path)
|
8
|
+
@type = type
|
9
|
+
@reference = reference
|
10
|
+
@candidate = candidate
|
11
|
+
@path = path.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def ref_display
|
15
|
+
reference.inspect
|
16
|
+
end
|
17
|
+
|
18
|
+
def can_display
|
19
|
+
candidate.inspect
|
20
|
+
end
|
21
|
+
|
22
|
+
def attributes
|
23
|
+
Hash[ COLUMNS.map { |name| [ name, send(name) ] } ]
|
24
|
+
end
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
s = "<"
|
28
|
+
s << self.class.name
|
29
|
+
s << " type=#{type}"
|
30
|
+
s << " path=#{path}"
|
31
|
+
s << " ref=#{ref_display}"
|
32
|
+
s << " can=#{can_display}"
|
33
|
+
s << ">"
|
34
|
+
s
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module CheckPlease
|
4
|
+
|
5
|
+
# Custom collection class for Diff instances.
|
6
|
+
# Can retrieve members using indexes or paths.
|
7
|
+
class Diffs
|
8
|
+
def initialize(diff_list = nil)
|
9
|
+
@list = []
|
10
|
+
@hash = {}
|
11
|
+
Array(diff_list).each do |diff|
|
12
|
+
self << diff
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# this is probably a terrible idea, but this method:
|
17
|
+
# - treats integer keys as array-style positional indexes
|
18
|
+
# - treats string keys as path strings and does a hash-like lookup (raising if the path is not found)
|
19
|
+
#
|
20
|
+
# (In my defense, I only did it to make the tests easier to write.)
|
21
|
+
def [](key)
|
22
|
+
if key.is_a?(Integer)
|
23
|
+
@list[key]
|
24
|
+
else
|
25
|
+
@hash.fetch(key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def <<(diff)
|
30
|
+
@list << diff
|
31
|
+
@hash[diff.path] = diff
|
32
|
+
end
|
33
|
+
|
34
|
+
def record(ref, can, path, type)
|
35
|
+
self << Diff.new(type, ref, can, path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def data
|
39
|
+
@list.map(&:attributes)
|
40
|
+
end
|
41
|
+
|
42
|
+
extend Forwardable
|
43
|
+
def_delegators :@list, *%i[
|
44
|
+
each
|
45
|
+
empty?
|
46
|
+
length
|
47
|
+
map
|
48
|
+
to_a
|
49
|
+
]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CheckPlease
|
2
|
+
|
3
|
+
class Path
|
4
|
+
SEPARATOR = "/"
|
5
|
+
|
6
|
+
def initialize(segments = [])
|
7
|
+
@segments = Array(segments)
|
8
|
+
end
|
9
|
+
|
10
|
+
def +(new_basename)
|
11
|
+
self.class.new( Array(@segments) + Array(new_basename.to_s) )
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
SEPARATOR + @segments.join(SEPARATOR)
|
16
|
+
end
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
to_s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'printers/base'
|
2
|
+
require_relative 'printers/json'
|
3
|
+
require_relative 'printers/table_print'
|
4
|
+
|
5
|
+
module CheckPlease
|
6
|
+
|
7
|
+
module Printers
|
8
|
+
PRINTERS_BY_FORMAT = {
|
9
|
+
table: Printers::TablePrint,
|
10
|
+
json: Printers::JSON,
|
11
|
+
}
|
12
|
+
FORMATS = PRINTERS_BY_FORMAT.keys.sort
|
13
|
+
DEFAULT_FORMAT = :table
|
14
|
+
|
15
|
+
def self.render(diffs, format)
|
16
|
+
format ||= DEFAULT_FORMAT
|
17
|
+
printer = PRINTERS_BY_FORMAT[format.to_sym]
|
18
|
+
printer.render(diffs)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CheckPlease
|
2
|
+
module Printers
|
3
|
+
|
4
|
+
class Base
|
5
|
+
def self.render(diffs)
|
6
|
+
new(diffs).to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(diffs)
|
10
|
+
@diffs = diffs
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :diffs
|
16
|
+
|
17
|
+
def build_string
|
18
|
+
io = StringIO.new
|
19
|
+
yield io
|
20
|
+
io.string.strip
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CheckPlease
|
4
|
+
module Printers
|
5
|
+
|
6
|
+
class JSON < Base
|
7
|
+
def to_s
|
8
|
+
return "[]" if diffs.empty?
|
9
|
+
|
10
|
+
build_string do |io|
|
11
|
+
io.puts "["
|
12
|
+
io.puts diffs.map { |diff| diff_json(diff) }.join(",\n")
|
13
|
+
io.puts "]"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def diff_json(diff, prefix = " ")
|
20
|
+
h = diff.attributes
|
21
|
+
json = ::JSON.pretty_generate(h)
|
22
|
+
prefix.to_s + json.gsub(/\n\s*/, " ")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'table_print'
|
2
|
+
|
3
|
+
module CheckPlease
|
4
|
+
module Printers
|
5
|
+
|
6
|
+
class TablePrint < Base
|
7
|
+
TP_OPTS = [
|
8
|
+
:type,
|
9
|
+
{ :path => { width: 250 } }, # if you hit this limit, you have other problems
|
10
|
+
:reference,
|
11
|
+
:candidate,
|
12
|
+
]
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
return "" if diffs.empty?
|
16
|
+
|
17
|
+
build_string do |io|
|
18
|
+
switch_tableprint_io(io) do
|
19
|
+
tp diffs.data, *TP_OPTS
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def switch_tableprint_io(new_io)
|
27
|
+
config = ::TablePrint::Config
|
28
|
+
@old_io = config.io
|
29
|
+
config.io = new_io
|
30
|
+
yield
|
31
|
+
ensure
|
32
|
+
config.io = @old_io
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/usage_examples.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'check_please'
|
2
|
+
|
3
|
+
reference = { foo: "wibble" }
|
4
|
+
candidate = { bar: "wibble" }
|
5
|
+
|
6
|
+
puts CheckPlease.render_diff(reference, candidate)
|
7
|
+
|
8
|
+
# this should print the following to stdout:
|
9
|
+
_ = <<EOF
|
10
|
+
TYPE | PATH | REFERENCE | CANDIDATE
|
11
|
+
--------|------|-----------|----------
|
12
|
+
missing | /foo | wibble |
|
13
|
+
extra | /bar | | wibble
|
14
|
+
EOF
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: check_please
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Livingston-Gray
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: table_print
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: amazing_print
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Check for differences between two JSON strings (or Ruby data structures
|
56
|
+
parsed from them)
|
57
|
+
email:
|
58
|
+
- geeksam@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/check_please
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- check_please.gemspec
|
76
|
+
- lib/check_please.rb
|
77
|
+
- lib/check_please/comparison.rb
|
78
|
+
- lib/check_please/diff.rb
|
79
|
+
- lib/check_please/diffs.rb
|
80
|
+
- lib/check_please/path.rb
|
81
|
+
- lib/check_please/printers.rb
|
82
|
+
- lib/check_please/printers/base.rb
|
83
|
+
- lib/check_please/printers/json.rb
|
84
|
+
- lib/check_please/printers/table_print.rb
|
85
|
+
- lib/check_please/version.rb
|
86
|
+
- usage_examples.rb
|
87
|
+
homepage: https://github.com/RealGeeks/check_please
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
homepage_uri: https://github.com/RealGeeks/check_please
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.3.0
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.0.3
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Check for differences between two JSON strings (or Ruby data structures parsed
|
111
|
+
from them)
|
112
|
+
test_files: []
|