Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Give possibility to override all metadata fields (#129)
Browse files Browse the repository at this point in the history
Including `app_id`, `content_type` and `timestamp`. At the moment these values can not be modified by the user.

**Implementation details:**
Just reverse metadata merging, so custom values can override default ones.
  • Loading branch information
mkorszun authored Nov 13, 2019
1 parent 065fd81 commit 6ff6c8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/publisher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ defmodule GenRMQ.Publisher do
# Put standard metadata fields to top level, everything else into headers
defp merge_metadata(base, custom) do
{metadata, headers} = custom |> Keyword.split(@metadata_fields)
# take "standard" fields and put them into metadata top-level
metadata
# put default values, override custom values on conflict
|> Keyword.merge(base)
# take "base" fields and put them into metadata top-level
base
# put custom values, override base values on conflict
|> Keyword.merge(metadata)
# put all custom fields in the headers
|> Keyword.merge(headers: headers)
end
Expand Down
13 changes: 13 additions & 0 deletions test/gen_rmq_publisher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ defmodule GenRMQ.PublisherTest do
assert [{"header1", :longstr, "value"}] == meta[:headers]
end

test "should override default metadata fields", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
sent_metadata = [app_id: "custom_id", content_type: "text/plain", timestamp: 1]
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key", sent_metadata)

Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, _received_message, received_metadata} = get_message_from_queue(context)

assert received_metadata[:app_id] == sent_metadata[:app_id]
assert received_metadata[:content_type] == sent_metadata[:content_type]
assert received_metadata[:timestamp] == sent_metadata[:timestamp]
end

test "should publish a message with priority", %{publisher: publisher_pid} = context do
message = %{"msg" => "with prio"}

Expand Down

0 comments on commit 6ff6c8e

Please sign in to comment.