2022-11-04 00:49:07 +08:00
|
|
|
defmodule LivebookProto do
|
|
|
|
@moduledoc false
|
|
|
|
|
2023-02-28 00:45:14 +08:00
|
|
|
alias LivebookProto.{
|
2023-06-01 23:01:43 +08:00
|
|
|
Event,
|
|
|
|
SecretCreated,
|
|
|
|
SecretUpdated,
|
|
|
|
SecretDeleted,
|
|
|
|
UserSynchronized
|
2023-02-28 00:45:14 +08:00
|
|
|
}
|
2022-12-21 22:28:27 +08:00
|
|
|
|
2023-06-01 23:01:43 +08:00
|
|
|
@event_mapping (for {_id, field_prop} <- Event.__message_props__().field_props,
|
|
|
|
into: %{} do
|
|
|
|
{field_prop.type, field_prop.name_atom}
|
|
|
|
end)
|
2023-01-07 03:14:44 +08:00
|
|
|
|
2023-06-01 23:01:43 +08:00
|
|
|
@type event_proto ::
|
|
|
|
SecretCreated.t()
|
|
|
|
| SecretUpdated.t()
|
|
|
|
| SecretDeleted.t()
|
|
|
|
| UserSynchronized.t()
|
2023-02-28 00:45:14 +08:00
|
|
|
|
|
|
|
@doc """
|
2023-06-01 23:01:43 +08:00
|
|
|
Builds an event with given data.
|
2023-02-28 00:45:14 +08:00
|
|
|
"""
|
2023-06-01 23:01:43 +08:00
|
|
|
@spec build_event(event_proto()) :: Event.t()
|
|
|
|
def build_event(%struct{} = data) do
|
|
|
|
Event.new!(type: {event_type(struct), data})
|
2023-01-07 03:14:44 +08:00
|
|
|
end
|
|
|
|
|
2023-06-01 23:01:43 +08:00
|
|
|
defp event_type(module), do: Map.fetch!(@event_mapping, module)
|
2022-11-04 00:49:07 +08:00
|
|
|
end
|