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

Make symbolize keys optional #364

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 8 additions & 31 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-12-12 12:48:14 UTC using RuboCop version 1.57.2.
# on 2024-06-08 21:54:16 UTC using RuboCop version 1.63.5.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -20,11 +20,12 @@ Lint/SuppressedException:

# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AutoCorrect.
Lint/UselessAssignment:
Exclude:
- 'spec/facts_spec.rb'

# Offense count: 2
# Offense count: 3
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Expand All @@ -38,7 +39,7 @@ Performance/MapCompact:
Exclude:
- 'lib/facterdb.rb'

# Offense count: 7
# Offense count: 8
# This cop supports unsafe autocorrection (--autocorrect-all).
Performance/StringInclude:
Exclude:
Expand All @@ -64,7 +65,7 @@ RSpec/DescribeClass:

# Offense count: 12
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: SkipBlocks, EnforcedStyle.
# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
# SupportedStyles: described_class, explicit
RSpec/DescribedClass:
Exclude:
Expand All @@ -79,7 +80,7 @@ RSpec/ExampleLength:
RSpec/MultipleExpectations:
Max: 7

# Offense count: 11
# Offense count: 19
# Configuration parameters: AllowSubject.
RSpec/MultipleMemoizedHelpers:
Max: 6
Expand Down Expand Up @@ -114,7 +115,7 @@ Style/Documentation:
- 'lib/facterdb.rb'
- 'lib/facterdb/bin.rb'

# Offense count: 13
# Offense count: 12
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand All @@ -141,24 +142,6 @@ Style/HashEachMethods:
Exclude:
- 'spec/facts_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/HashTransformKeys:
Exclude:
- 'lib/facterdb.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
Style/IfUnlessModifier:
Exclude:
- 'lib/facterdb.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/MapToHash:
Exclude:
- 'lib/facterdb.rb'

# Offense count: 1
Style/MixinUsage:
Exclude:
Expand Down Expand Up @@ -189,12 +172,6 @@ Style/OptionalBooleanParameter:
Exclude:
- 'lib/facterdb.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/SlicingWithRange:
Exclude:
- 'Rakefile'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Mode.
Expand All @@ -215,4 +192,4 @@ Style/SymbolProc:
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 179
Max: 154
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
23 changes: 22 additions & 1 deletion spec/facter_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,35 @@
end

describe '.get_facts' do
subject(:result) { FacterDB.get_facts(filter) }
subject(:result) { FacterDB.get_facts(filter, symbolize_keys: symbolize_keys) }

let(:filter) { nil }
let(:symbolize_keys) { nil }

context 'without parameters' do
include_examples 'returns a result'
end

context 'with stringified output' do
let(:symbolize_keys) { false }

it 'returns strings as keys in factsets' do
result.each do |factset|
expect(factset.keys).to all(be_an_instance_of(String))
end
end
end

context 'with symbolized output' do
let(:symbolize_keys) { true }

it 'returns strings as keys in factsets' do
result.each do |factset|
expect(factset.keys).to all(be_an_instance_of(Symbol))
end
end
end

context 'with an Array filter' do
let(:filter) { [osfamily: 'Debian'] }

Expand Down