summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2018-07-11 14:13:35 -0700
committerGitHub <noreply@github.com>2018-07-11 14:13:35 -0700
commit08dcd6502ee9fe77c44ad215f510192605a6fbc4 (patch)
treefc47d6a57e5f2d5e7052c56a67b42e12d18e8e50
parent9bf3b5d6a31a7ef7beeab86daaa37022ca9450b7 (diff)
parent1e76fdf03d4ba680cef4b9e35449a2b6e5e55c7b (diff)
downloadmixlib-config-08dcd6502ee9fe77c44ad215f510192605a6fbc4.tar.gz
Merge pull request #66 from chef/lcg/fix-from-stuff
Avoid converting to text representation when parsing JSON/TOML/etc
-rw-r--r--lib/mixlib/config.rb39
-rw-r--r--spec/mixlib/config_spec.rb13
2 files changed, 35 insertions, 17 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index 1c8daa4..aa56cea 100644
--- a/lib/mixlib/config.rb
+++ b/lib/mixlib/config.rb
@@ -70,7 +70,7 @@ module Mixlib
# filename<String>:: A filename to read from
def from_yaml(filename)
require "yaml"
- from_hash(YAML.load(IO.read(filename)), filename)
+ from_hash(YAML.load(IO.read(filename)))
end
# Parses valid JSON structure into Ruby
@@ -79,7 +79,7 @@ module Mixlib
# filename<String>:: A filename to read from
def from_json(filename)
require "json"
- from_hash(JSON.parse(IO.read(filename)), filename)
+ from_hash(JSON.parse(IO.read(filename)))
end
def from_toml(filename)
@@ -91,20 +91,8 @@ module Mixlib
#
# === Parameters
# hash<Hash>:: A Hash containing configuration
- def from_hash(hash, filename = "in_memory")
- ruby_translation = []
-
- to_dotted_hash(hash).each do |k, v|
- if v.is_a? Array
- ruby_translation << "#{k} #{v}"
- elsif v.is_a? String
- ruby_translation << "#{k} \"#{v}\""
- else
- ruby_translation << "#{k} #{v}"
- end
- end
-
- instance_eval(ruby_translation.join("\n"), filename, 1)
+ def from_hash(hash)
+ apply_nested_hash(hash)
end
# Pass Mixlib::Config.configure() a block, and it will yield itself
@@ -567,6 +555,25 @@ module Mixlib
internal_get_or_set(method_symbol, *args)
end
+ protected
+
+ # Given a (nested) Hash, apply it to the config object and any contexts.
+ #
+ # This is preferable to converting it to the string representation with
+ # the #to_dotted_hash method above.
+ #
+ # === Parameters
+ # hash<Hash>:: The hash to apply to the config oject
+ def apply_nested_hash(hash)
+ hash.each do |k, v|
+ if v.is_a? Hash
+ internal_get(k.to_sym).apply_nested_hash(v)
+ else
+ internal_set(k.to_sym, v)
+ end
+ end
+ end
+
private
# Given a (nested) Hash, turn it into a single top-level hash using dots as
diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb
index 1786789..a08e3cc 100644
--- a/spec/mixlib/config_spec.rb
+++ b/spec/mixlib/config_spec.rb
@@ -1277,14 +1277,25 @@ alpha = "beta"
let(:hash) do
{
"alpha" => "beta",
+ gamma: "delta",
"foo" => %w{ bar baz matazz},
+ "bar" => { "baz" => { "fizz" => "buzz" } },
+ "windows_path" => 'C:\Windows Has Awful\Paths',
}
end
- it "translates the Hash into method-style" do
+ it "configures the config object from a hash" do
+ ConfigIt.config_context :bar do
+ config_context :baz do
+ default :fizz, "quux"
+ end
+ end
ConfigIt.from_hash(hash)
expect(ConfigIt.foo).to eql(%w{ bar baz matazz })
expect(ConfigIt.alpha).to eql("beta")
+ expect(ConfigIt.gamma).to eql("delta")
+ expect(ConfigIt[:bar][:baz][:fizz]).to eql("buzz")
+ expect(ConfigIt.windows_path).to eql('C:\Windows Has Awful\Paths')
end
end
end