Skip to content

Commit 8a4136d

Browse files
committed
(FACT-2874) check for fact_type method before usage
When sorting resolutions, not all objects respond to :fact_type(eg Facter::Core::Aggregate does not). This commit adds a defence to use :fact_type only for the object that know to respond to it.
1 parent 8dffc51 commit 8a4136d

File tree

1 file changed

+11
-3
lines changed
  • lib/facter/custom_facts/util

1 file changed

+11
-3
lines changed

lib/facter/custom_facts/util/fact.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,20 @@ def sort_by_weight(resolutions)
177177
# - descending by weight
178178
# - multiple facts have the same weight but different types, the :external fact take precedence
179179
# - multiple facts with the same weight and type, the order is preserved.
180-
# note: sort_by is slower than .sort
180+
# note: sort_by with index is slower than .sort
181181
# we cannot use .sort because it is not stable: https://bugs.ruby-lang.org/issues/1089
182-
183182
# solution from: https://bugs.ruby-lang.org/issues/1089#note-10
183+
184+
# rubocop:disable Style/NestedTernaryOperator
184185
idx = 0
185-
resolutions.sort_by { |x| [-x.weight, (x.fact_type == :external ? 0 : 1), idx += 1] }
186+
resolutions.sort_by do |x|
187+
[
188+
-x.weight,
189+
x.respond_to?(:fact_type) ? x.fact_type == :external ? 0 : 1 : 1,
190+
idx += 1
191+
]
192+
end
193+
# rubocop:enable Style/NestedTernaryOperator
186194
end
187195

188196
def find_first_real_value(resolutions)

0 commit comments

Comments
 (0)