ExGram.Cast (ex_gram v0.65.0)

Copy Markdown View Source

Helper module to convert plain values returned from Telegram to ExGram models.

This module handles the conversion of plain maps (JSON responses from the Telegram Bot API) into strongly-typed ExGram model structs. It recursively processes nested structures, arrays, and polymorphic types (subtypes).

See ExGram.Model.Subtype for how polymorphic types are handled.

Summary

Functions

Converts the given plain value to ExGram models.

Converts the given plain value to ExGram models.

Types

type_def()

@type type_def() :: atom() | {:array, type_def()} | [type_def()] | nil

Functions

cast(elem, type)

@spec cast(any(), type_def()) :: {:ok, any()} | {:error, ExGram.Error.t()}

Converts the given plain value to ExGram models.

iex> ExGram.Cast.cast(%{message_id: 3, chat: %{id: 5}}, ExGram.Model.Message) {:ok, %ExGram.Model.Message{message_id: 3, chat: %ExGram.Model.Chat{id: 5}}}

iex> ExGram.Cast.cast(true, ExGram.Model.Message) {:error, %ExGram.Error{message: "Expected a map for type ExGram.Model.Message, got: true"}}

cast!(elem, type)

@spec cast!(any(), type_def()) :: any()

Converts the given plain value to ExGram models.

Raises an error if the conversion fails. See cast/2 for more details.