Commit 0c32f4d0 authored by Bob Van Landuyt's avatar Bob Van Landuyt 💬
Browse files

Merge branch 'sh-allow-context-false-values' into 'master'

Allow Labkit::Context to retain `false` values

See merge request !95
parents ad1d7c06 fbf59fd4
Loading
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ module Labkit
      data.merge!(attributes)

      # Remove keys that had their values set to `nil` in the new attributes
      data.keep_if { |_, value| value.present? }
      data.keep_if { |_, value| valid_data?(value) }

      # Assign a correlation if it was missing in the first context or when
      # explicitly removed
@@ -146,12 +146,16 @@ module Labkit
      data.transform_values do |value|
        value = call_or_value(value)

        value.presence
        value if valid_data?(value)
      end.compact
    end

    def new_id
      SecureRandom.hex
    end

    def valid_data?(value)
      value == false || value.present?
    end
  end
end
+9 −0
Original line number Diff line number Diff line
@@ -263,6 +263,15 @@ describe Labkit::Context do
      expect(data_from(new_context).keys).not_to include(described_class.log_key("project"), described_class.log_key("user"))
    end

    it "keeps false values" do
      context = described_class.new(project: "p", root_namespace: "n", flag: false)

      new_context = context.merge(project: "", false: nil)

      expect(data_from(new_context)).to include(log_hash("root_namespace" => "n", "flag" => false))
      expect(context.to_h).to include(log_hash("root_namespace" => "n", "flag" => false))
    end

    it "does not overwrite the correlation id" do
      context = described_class.new(described_class::CORRELATION_ID_KEY => "hello")