Skip to content

Commit

Permalink
Merge pull request #118 from OleMchls/feature/guess-event-type
Browse files Browse the repository at this point in the history
Allow callers to omit `event_type` when event data is a struct
  • Loading branch information
slashdotdash committed May 28, 2018
2 parents 2aadc34 + 1f9e386 commit ad6bc17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/event_store/streams/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ defmodule EventStore.Streams.Stream do
end)
end

defp map_to_recorded_event(
%EventData{
data: %{__struct__: event_type},
event_type: nil
} = event,
created_at,
serializer
) do
%{event | event_type: Atom.to_string(event_type)}
|> map_to_recorded_event(created_at, serializer)
end

defp map_to_recorded_event(%EventData{
causation_id: causation_id,
correlation_id: correlation_id,
Expand Down
15 changes: 15 additions & 0 deletions test/streams/single_stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ defmodule EventStore.Streams.SingleStreamTest do

assert {:error, :duplicate_event} = Stream.link_to_stream(conn, target_stream_uuid, 4, source_events)
end

test "should guess the event type when not passed", context do
%{conn: conn, stream_uuid: stream_uuid} = context

{:ok, _subscription} = EventStore.subscribe_to_stream(stream_uuid, @subscription_name, self(), start_from: :current)
wait_for_event_store()

event = %EventStore.EventData{
data: %EventStore.EventFactory.Event{event: "foo"}
}
:ok = Stream.append_to_stream(conn, stream_uuid, :any_version, [event])

assert_receive {:events, [received_event | _]}
assert received_event.event_type == "Elixir.EventStore.EventFactory.Event"
end
end

test "attempt to read an unknown stream forward should error stream not found", %{conn: conn} do
Expand Down

0 comments on commit ad6bc17

Please sign in to comment.