summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2018-07-11 12:23:43 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2018-07-11 12:23:43 -0700
commitc43316fd69daa98e8f1b713622428b0f75da5f1f (patch)
tree988fd2b556a7d215799316a76758c1175369fa27
parent9bf3b5d6a31a7ef7beeab86daaa37022ca9450b7 (diff)
downloadmixlib-config-c43316fd69daa98e8f1b713622428b0f75da5f1f.tar.gz
Avoid converting to text representation when parsing JSON/TOML/etc
Directly apply the config as we descend recursively through the Hash using internal_set/internal_get. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/mixlib/config.rb37
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index 1c8daa4..3ef06e8 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
@@ -590,6 +578,23 @@ module Mixlib
end
end
+ # 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
+
# Internal dispatch setter for config values.
#
# === Parameters