forked from mdub/config_hound
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate key warnings #4
Draft
tigris
wants to merge
7
commits into
main
Choose a base branch
from
yaml-duplicate-key-warnings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eace4ba
Refactor parsers into their own classes
2aa7f03
Add initial specs for how I want this new feature to work
4671525
Pass the allow_dupes option through to the parser
7a3e9fb
Have the parser bomb out if it knows how to find dupes and is told to
5470560
Use a custom hash class when loading json so we can capture dupes
68c2d39
Hackily capture dupe yaml keys, since we can't pass in a custom hash …
ee373ed
Only use a custom json hash loader when dupe checking
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
module ConfigHound | ||
require 'config_hound/parser/duplicate_key_error' | ||
require 'config_hound/parser/json' | ||
require 'config_hound/parser/toml' | ||
require 'config_hound/parser/yaml' | ||
|
||
module ConfigHound | ||
class Parser | ||
|
||
def self.parse(*args) | ||
new.parse(*args) | ||
end | ||
|
||
def parse(raw, format) | ||
parse_method = "parse_#{format}" | ||
raise "unknown format: #{format}" unless respond_to?(parse_method, true) | ||
send(parse_method, raw) | ||
end | ||
def parse(raw, format, options={}) | ||
begin | ||
parser = eval("#{self.class}::#{format.upcase}") | ||
rescue NameError | ||
raise "unknown format: #{format}" | ||
end | ||
|
||
protected | ||
if !options[:allow_duplicate_keys] && parser.respond_to?(:find_duplicate_keys) | ||
duplicates = parser.find_duplicate_keys(raw) | ||
raise DuplicateKeyError.new(duplicates) if duplicates.any? | ||
end | ||
|
||
def parse_yaml(raw) | ||
require "yaml" | ||
YAML.safe_load(raw, permitted_classes: [], permitted_symbols: [], aliases: true) | ||
end | ||
|
||
alias :parse_yml :parse_yaml | ||
|
||
def parse_json(raw) | ||
require "multi_json" | ||
MultiJson.load(raw) | ||
end | ||
|
||
def parse_toml(raw) | ||
require "toml" | ||
TOML.load(raw) | ||
parser.parse(raw) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module ConfigHound | ||
class Parser | ||
class DuplicateKeyError < ConfigHound::Error | ||
attr_reader :duplicates | ||
|
||
def initialize(duplicates) | ||
@duplicates = duplicates | ||
super(duplicates) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module ConfigHound | ||
class Parser | ||
class JSON | ||
def self.parse(raw) | ||
require "multi_json" | ||
MultiJson.load(raw) | ||
end | ||
|
||
def self.find_duplicate_keys(raw) | ||
require "ext/duplicate_key_checking_hash" | ||
require "multi_json" | ||
parsed = MultiJson.load(raw, object_class: DuplicateKeyCheckingHash) | ||
find_deep_duplicates([], parsed) | ||
end | ||
|
||
def self.find_deep_duplicates(parents, hash) | ||
return [] unless hash.is_a?(Hash) | ||
|
||
duplicates = hash.duplicate_keys.map{|d| [parents, d].join('.') } | ||
|
||
hash.each do |key, value| | ||
next unless value.is_a?(Hash) | ||
duplicates += find_deep_duplicates(parents + [key], value) | ||
end | ||
|
||
duplicates | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module ConfigHound | ||
class Parser | ||
class TOML | ||
def self.parse(raw) | ||
require "toml" | ||
::TOML.load(raw) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module ConfigHound | ||
class Parser | ||
class YAML | ||
def self.parse(raw) | ||
require "yaml" | ||
::YAML.safe_load(raw, permitted_classes: [], permitted_symbols: [], aliases: true) | ||
end | ||
|
||
def self.find_duplicate_keys(raw) | ||
require "psych" | ||
|
||
# Blatantly stolen from https://stackoverflow.com/a/55705853 | ||
duplicate_keys = [] | ||
|
||
validator = ->(node, parent_path) do | ||
if node.is_a?(Psych::Nodes::Mapping) | ||
children = node.children.each_slice(2) | ||
duplicates = children.map { |key_node, _value_node| key_node }.group_by(&:value).select { |_value, nodes| nodes.size > 1 } | ||
|
||
duplicates.each do |key, nodes| | ||
duplicate_keys << (parent_path + [key]).join('.') | ||
end | ||
|
||
children.each { |key_node, value_node| validator.call(value_node, parent_path + [key_node&.value].compact) } | ||
else | ||
node.children.to_a.each { |child| validator.call(child, parent_path) } | ||
end | ||
end | ||
|
||
ast = Psych.parse_stream(raw) | ||
validator.call(ast, []) | ||
|
||
duplicate_keys | ||
end | ||
end | ||
|
||
class YML < YAML ; end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class DuplicateKeyCheckingHash < Hash | ||
attr_reader :duplicate_keys | ||
|
||
def initialize(default=nil) | ||
@duplicate_keys = [] | ||
super(default) | ||
end | ||
|
||
def []=(key, value) | ||
if has_key?(key) | ||
@duplicate_keys << key | ||
end | ||
|
||
super(key, value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "spec_helper" | ||
|
||
require "config_hound" | ||
|
||
describe ConfigHound, "duplicate_keys" do | ||
|
||
let(:config) { ConfigHound.load(resource, options) } | ||
|
||
given_resource "config.yaml", %{ | ||
foo: | ||
bar: baz | ||
bar: qux | ||
} | ||
|
||
given_resource "config.json", %[ | ||
{ "foo": { "bar": "baz", "bar": "qux" } } | ||
] | ||
|
||
%w(yaml json).each do |type| | ||
|
||
context "in #{type}" do | ||
|
||
let(:resource) { "config.#{type}" } | ||
|
||
context "with duplicate keys allowed" do | ||
let(:options) { { allow_duplicate_keys: true } } | ||
|
||
it "takes the later key" do | ||
expect(config).to eq("foo" => { "bar" => "qux" }) | ||
end | ||
end | ||
|
||
context "with duplicate keys disabled" do | ||
let(:options) { { allow_duplicate_keys: false } } | ||
|
||
it "raises a DuplicateKeyError" do | ||
expect { config }.to raise_error(ConfigHound::Parser::DuplicateKeyError) | ||
end | ||
|
||
it "tells you which key is duplicated" do | ||
expect { config }.to raise_error(ConfigHound::Parser::DuplicateKeyError, /foo\.bar/) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣