ACPex.Schema.Types.ContentBlock (ACPex v0.1.1)

Copy Markdown View Source

Content block union type.

A ContentBlock can be one of several variants, discriminated by the type field:

  • "text" - Plain text content (ACPex.Schema.Types.ContentBlock.Text)
  • "image" - Image data (ACPex.Schema.Types.ContentBlock.Image)
  • "audio" - Audio data (ACPex.Schema.Types.ContentBlock.Audio)
  • "resource_link" - External resource reference (ACPex.Schema.Types.ContentBlock.ResourceLink)
  • "resource" - Embedded resource contents (ACPex.Schema.Types.ContentBlock.Resource)

Usage

You can use any of the variant structs directly:

# Text content
%ACPex.Schema.Types.ContentBlock.Text{
  text: "Hello, world!"
}

# Image content
%ACPex.Schema.Types.ContentBlock.Image{
  data: "base64-encoded-data...",
  mime_type: "image/png"
}

Or use maps (which will be validated at runtime):

%{
  "type" => "text",
  "text" => "Hello, world!"
}

Decoding from JSON

Use the decode/1 function to decode JSON into the appropriate variant struct:

json = ~s({"type":"text","text":"Hello"})
{:ok, %ACPex.Schema.Types.ContentBlock.Text{}} = ContentBlock.decode(json)

Encoding to JSON

All variant structs implement Jason.Encoder:

text = %ACPex.Schema.Types.ContentBlock.Text{text: "Hello"}
Jason.encode!(text)
# => {"type":"text","text":"Hello"}

Summary

Functions

Decodes a JSON string or map into the appropriate ContentBlock variant struct.

Same as decode/1 but raises on error.

Types

Functions

decode(json)

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

Decodes a JSON string or map into the appropriate ContentBlock variant struct.

The type field is used as a discriminator to determine which variant to decode to.

Examples

iex> ContentBlock.decode(~s({"type":"text","text":"Hello"}))
{:ok, %ContentBlock.Text{type: "text", text: "Hello"}}

iex> ContentBlock.decode(%{"type" => "image", "data" => "...", "mimeType" => "image/png"})
{:ok, %ContentBlock.Image{type: "image", data: "...", mime_type: "image/png"}}

iex> ContentBlock.decode(%{"type" => "unknown"})
{:error, "Unknown content block type: unknown"}

decode!(json)

@spec decode!(String.t() | map()) :: variant()

Same as decode/1 but raises on error.