Spear.Event.from_read_response
from_read_response
, go back to Spear.Event module for more information.
Specs
Converts a read-response message to a Spear.Event
This function is applied by Stream.map/2
onto streams returned by
reading operations such as Spear.stream!/3
, Spear.read_stream/3
, etc.
by default. This can be turned off by passing the raw?: true
opt to
a reading function.
This function follows links. For example, if an read event belongs to a
projected stream such as an event type stream, this function will give the
event body of the source event, not the link. Forcing the return of the
link body can be accomplished with the :link?
option set to true
(it is
false
by default).
Options
:link?
- (default:false
) forces returning the body of the link event for events read from projected streams. Has no effect on events from non- projected streams.:json_decoder
- (default:Jason.decode!/2
) a 2-arity function to use for events with a"content-type"
of"application/json"
.
All remaining options passed as opts
other than :link?
and
:json_decoder
are passed to the second argument of the :json_decoder
2-arity function.
JSON decoding
Event bodies are commonly written to the EventStoreDB in JSON format as the
format is a human-readable and supported in nearly any language. Events
carry a small piece of metadata in the ReadResp.ReadEvent.RecordedEvent
's
:metadata
map field which declares the content-type of the event body:
"content-type"
. This function will automatically attempt to decode any
events which declare an "application/json"
content-type as JSON using
the :json_decoder
2-arity function option. Other content-types will not
trigger any automatic behavior.
Spear
takes an optional dependency on the Jason
library as it is
currently the most popular JSON (en/de)coding library. If you add this
project to the deps/0
in a mix.exs
file and wish to take advantage of
the automatic JSON decoding functionality, you may also need to include
:jason
. As an optional dependency, :jason
is not included in your
dependencies just by dependending on :spear
.
# mix.exs
def deps do
[
{:spear, ">= 0.0.0"},
{:jason, ">= 0.0.0"},
..
]
end
Other JSON (en/de)coding libraries may be swapped in, such as with Poison
iex> Spear.stream!(conn, "es_supported_clients", raw?: true)
...> |> Stream.map(&Spear.Event.from_read_response(&1, json_decoder: &Poison.decode!/2, keys: :atoms))
Examples
Spear.stream!(conn, "es_supported_clients", raw?: true)
|> Stream.map(&Spear.Event.from_read_response/1)
|> Enum.to_list()
# => [%Spear.Event{}, %Spear.Event{}, ..]