Loading lib/labkit/context.rb +6 −2 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 spec/labkit/context_spec.rb +9 −0 Original line number Diff line number Diff line Loading @@ -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") Loading Loading
lib/labkit/context.rb +6 −2 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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
spec/labkit/context_spec.rb +9 −0 Original line number Diff line number Diff line Loading @@ -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") Loading