Toolbox.Normalizer.LoadTopic.Parser (toolbox v1.1.0)

Generic load topic parser.

Module parses encoded JSON objects with expected attributes.

iex> raw_message = ~s({"load_topic_message": "1","type": "create_asset","timestamp": 12345,"asset_id": "/asset/foo/bar","attributes": {"attr1": "attr1_val"}})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :create_asset, timestamp: 12_345, asset_id: "/asset/foo/bar", attributes: %{"attr1" => "attr1_val"}}}

iex> raw_message = ~s({"load_topic_message": "1","type": "upsert_asset","timestamp": 12345, "asset_id": "/asset/foo/bar", "attributes": {"attr1": "attr1_val"}})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :upsert_asset, timestamp: 12_345, asset_id: "/asset/foo/bar", attributes: %{"attr1" => "attr1_val"}}}

iex> raw_message = ~s({"load_topic_message": "1","type": "update_asset","timestamp": 12345,"asset_id": "/asset/foo/bar","attributes": {"attr1": "attr1_val"}})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :update_asset, timestamp: 12_345, asset_id: "/asset/foo/bar", attributes: %{"attr1" => "attr1_val"}}}

iex> raw_message = ~s({"load_topic_message": "1","type": "delete_asset","timestamp": 12345,"asset_id": "/asset/foo/bar"})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :delete_asset, timestamp: 12_345, asset_id: "/asset/foo/bar"}}

iex> raw_message = ~s({"load_topic_message": "1","type": "create_edge","timestamp": 12345,"from_asset_id": "/asset/foo/1","to_asset_id": "/asset/foo/2","edge_type": "actor"})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :create_edge, timestamp: 12_345, from_asset_id: "/asset/foo/1", to_asset_id: "/asset/foo/2", edge_type: "actor"}}

iex> raw_message = ~s({"load_topic_message": "1","type": "upsert_edge","timestamp": 12345,"from_asset_id": "/asset/foo/1","to_asset_id": "/asset/foo/2","edge_type": "actor"})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :upsert_edge, timestamp: 12_345, from_asset_id: "/asset/foo/1", to_asset_id: "/asset/foo/2", edge_type: "actor"}}

iex> raw_message = ~s({"load_topic_message": "1","type": "delete_edge","timestamp": 12345,"from_asset_id": "/asset/foo/1","to_asset_id": "/asset/foo/2","edge_type": "actor"})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "1", type: :delete_edge, timestamp: 12_345, from_asset_id: "/asset/foo/1", to_asset_id: "/asset/foo/2", edge_type: "actor"}}

iex> raw_message = ~s({"load_topic_message": "2","type": "update_asset","timestamp": 12345,"asset_id": "/asset/foo/bar","update_attributes": {"attr1": "attr1_val"}, "delete_attributes": {"attr2": true}})
...> Toolbox.Normalizer.LoadTopic.Parser.parse(raw_message)
{:ok, %{load_topic_message: "2", type: :update_asset, timestamp: 12_345, asset_id: "/asset/foo/bar", update_attributes: %{"attr1" => "attr1_val"}, delete_attributes: %{"attr2" => true}}}

Link to this section Summary

Functions

Parse raw message into load topic message

Link to this section Types

Link to this type

parsed_load_topic_message()

@type parsed_load_topic_message() ::
  %{
    load_topic_message: String.t(),
    type: :create_asset | :upsert_asset | :update_asset,
    timestamp: integer(),
    asset_id: String.t(),
    attributes: map()
  }
  | %{
      load_topic_message: String.t(),
      type: :delete_asset,
      timestamp: integer(),
      asset_id: String.t()
    }
  | %{
      load_topic_message: String.t(),
      type: :create_edge | :upsert_edge | :delete_edge,
      timestamp: integer(),
      from_asset_id: String.t(),
      to_asset_id: String.t(),
      edge_type: String.t()
    }

Link to this section Functions

@spec parse(String.t()) :: {:ok, parsed_load_topic_message()} | {:error, term()}

Parse raw message into load topic message