A2UI.Protocol.Message (A2UI v0.3.0)

Copy Markdown View Source

Union type and dispatcher for A2UI protocol messages.

Dispatches raw JSON maps to the appropriate message struct based on the message type key.

Summary

Functions

Parses a JSON string into a message struct.

Parses a decoded JSON map into the appropriate message struct.

Converts a message struct to a JSON string.

Converts a message struct to a JSON-compatible map.

Types

Functions

from_json(json)

@spec from_json(String.t()) :: {:ok, t()} | {:error, String.t()}

Parses a JSON string into a message struct.

from_map(map)

@spec from_map(map()) :: {:ok, t()} | {:error, String.t()}

Parses a decoded JSON map into the appropriate message struct.

Dispatches based on the presence of known message type keys. Server→client messages must include "version": "v0.9".

Examples

iex> A2UI.Protocol.Message.from_map(%{"version" => "v0.9", "createSurface" => %{"surfaceId" => "main", "catalogId" => "test"}})
{:ok, %A2UI.Protocol.Messages.CreateSurface{surface_id: "main", catalog_id: "test"}}

to_json(msg)

@spec to_json(t()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}

Converts a message struct to a JSON string.

Returns {:ok, json} or {:error, reason}.

to_map(msg)

@spec to_map(t()) :: map()

Converts a message struct to a JSON-compatible map.

Server→client messages are wrapped with "version": "v0.9". Action (client→server) is returned without a version wrapper.