[Pkg-puppet-devel] [facter] 41/180: (FACT-185) Add helper for flattening structured facts

Stig Sandbeck Mathisen ssm at debian.org
Mon Jun 30 15:06:29 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 33b507137558bede13231a3c419021be8a0a7436
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Wed Apr 2 16:38:27 2014 -0700

    (FACT-185) Add helper for flattening structured facts
---
 lib/facter/util/values.rb     | 29 +++++++++++++++++++++++++++++
 spec/unit/util/values_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/lib/facter/util/values.rb b/lib/facter/util/values.rb
index a7048d5..1fbb1b6 100644
--- a/lib/facter/util/values.rb
+++ b/lib/facter/util/values.rb
@@ -1,3 +1,4 @@
+
 module Facter
   module Util
     # A util module for facter containing helper methods
@@ -75,6 +76,34 @@ module Facter
         value = value.downcase if value.is_a?(String)
         value
       end
+
+      # Flatten the given data structure to something that's suitable to return
+      # as flat facts.
+      #
+      # @param path [String] The fact path to be prefixed to the given value.
+      # @param structure [Object] The data structure to flatten. Nested hashes
+      #   will be recursively flattened, everything else will be returned as-is.
+      #
+      # @return [Hash] The given data structure prefixed with the given path
+      def flatten_structure(path, structure)
+        results = {}
+
+        if structure.is_a? Hash
+          structure.each_pair do |name, value|
+            new_path = "#{path}_#{name}".gsub(/\-|\//, '_')
+            results.merge! flatten_structure(new_path, value)
+          end
+        elsif structure.is_a? Array
+          structure.each_with_index do |value, index|
+            new_path = "#{path}_#{index}"
+            results.merge! flatten_structure(new_path, value)
+          end
+        else
+          results[path] = structure
+        end
+
+        results
+      end
     end
   end
 end
diff --git a/spec/unit/util/values_spec.rb b/spec/unit/util/values_spec.rb
index 557eb19..bc347c4 100644
--- a/spec/unit/util/values_spec.rb
+++ b/spec/unit/util/values_spec.rb
@@ -128,4 +128,44 @@ describe Facter::Util::Values do
       end
     end
   end
+
+  describe "flatten_structure" do
+    it "converts a string to a hash containing that string" do
+      input = "foo"
+      output = described_class.flatten_structure("path", input)
+      expect(output).to eq({"path" => "foo"})
+    end
+
+    it "converts an array to a hash with the array elements with indexes" do
+      input = ["foo"]
+      output = described_class.flatten_structure("path", input)
+      expect(output).to eq({"path_0" => "foo"})
+    end
+
+    it "prefixes a non-nested hash with the given path" do
+      input = {"foo" => "bar"}
+      output = described_class.flatten_structure("path", input)
+      expect(output).to eq({"path_foo" => "bar"})
+    end
+
+    it "flattens elements till it reaches the first non-flattenable structure" do
+      input = {
+        "first" => "second",
+        "arr" => ["zero", "one"],
+        "nested_array" => [
+          "hash" => "string",
+        ],
+        "top" => {"middle" => ['bottom']},
+      }
+      output = described_class.flatten_structure("path", input)
+
+      expect(output).to eq({
+        "path_first" => "second",
+        "path_arr_0" => "zero",
+        "path_arr_1" => "one",
+        "path_nested_array_0_hash" => "string",
+        "path_top_middle_0" => "bottom"
+      })
+    end
+  end
 end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list