Skip to content

Commit

Permalink
Make symbolize keys optional
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ekohl committed May 29, 2024
1 parent 9c33730 commit 6503541
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/facterdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6503541

Please sign in to comment.