Skip to content
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

Avoid converting to text representation when parsing JSON/TOML/etc #66

Merged
merged 3 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions lib/mixlib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def from_file(filename)
# 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
Expand All @@ -79,7 +79,7 @@ def from_yaml(filename)
# 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)
Expand All @@ -91,20 +91,8 @@ def from_toml(filename)
#
# === 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
Expand Down Expand Up @@ -567,6 +555,25 @@ def method_missing(method_symbol, *args)
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
Expand Down
9 changes: 8 additions & 1 deletion spec/mixlib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1278,13 +1278,20 @@ class StrictClass3
{
"alpha" => "beta",
"foo" => %w{ bar baz matazz},
"bar" => { "baz" => { "fizz" => "buzz" } },
}
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[:bar][:baz][:fizz]).to eql("buzz")
end
end
end