From 6503541bd5a03f93ebba024216ff9203c2696b2a Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 29 May 2024 12:19:16 +0200 Subject: [PATCH] Make symbolize keys optional rspec-puppet-facts has a setting to stringify keys again. Converting to symbols and then converting back to strings is inefficient. This parameter allows skipping the step. --- lib/facterdb.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/facterdb.rb b/lib/facterdb.rb index 4b623c38..661e794d 100644 --- a/lib/facterdb.rb +++ b/lib/facterdb.rb @@ -137,13 +137,17 @@ def self.valid_filters?(filters) # @return [Array[Hash[Symbol, Any]]] array of hashes of facts # @param filter [Object] The filter to convert to jgrep string - def self.get_facts(filter = nil, cache = true) + # @param symbolize_keys [Boolean] + # Whether to symbolize the keys. Note this is only on the top level and not + # on nested values. + def self.get_facts(filter = nil, cache = true, symbolize_keys: true) if cache && filter && filter == Thread.current[:facterdb_last_filter_seen] return Thread.current[:facterdb_last_facts_seen] end filter_str = generate_filter_str(filter) - result = JGrep.jgrep(database, filter_str).map { |hash| hash.map { |k, v| [k.to_sym, v] }.to_h } + result = JGrep.jgrep(database, filter_str) + result = result.map { |hash| hash.transform_keys(&:to_sym) } if symbolize_keys if cache Thread.current[:facterdb_last_filter_seen] = filter Thread.current[:facterdb_last_facts_seen] = result