# `AshScylla.DataLayer.Udt`
[🔗](https://github.com/ohhi-vn/ash_scylla/blob/main/lib/ash_scylla/data_layer/udt.ex#L15)

User Defined Type (UDT) runtime support for ScyllaDB.

Handles encoding/decoding UDT values for Xandra,
CQL generation for UDT operations, and UDT schema management.

ScyllaDB UDTs are represented as tuples in Xandra. A UDT
`{field1_val, field2_val}` maps to a map `%{field1: val1, field2: val2}`.

## Encoding

UDTs are encoded as tuples ordered by their schema field definition.
The schema is derived from the resource's `@dsl` UDT configuration.

## Decoding

Tuples are decoded back to maps using field names from the UDT schema.

# `udt_field_spec`

```elixir
@type udt_field_spec() :: {atom(), atom()}
```

# `udt_schema`

```elixir
@type udt_schema() :: %{
  name: atom(),
  type_name: String.t(),
  fields: [udt_field_spec()]
}
```

# `alter_type_cql`

```elixir
@spec alter_type_cql(String.t() | atom(), :add | :rename, [udt_field_spec()]) ::
  String.t()
```

Generates CQL for altering a UDT (add/rename fields)

# `create_type_cql`

```elixir
@spec create_type_cql(String.t() | atom(), [udt_field_spec()]) :: String.t()
```

Generates CQL for creating a UDT

# `decode`

```elixir
@spec decode(tuple(), module()) :: map()
```

Decodes a UDT tuple from Xandra into a map

# `drop_type_cql`

```elixir
@spec drop_type_cql(String.t() | atom()) :: String.t()
```

Generates CQL for dropping a UDT

# `encode`

```elixir
@spec encode(map(), module()) :: tuple()
```

Encodes a map value into UDT format for Xandra

# `field_type_to_cql`

```elixir
@spec field_type_to_cql(atom()) :: String.t()
```

Returns the CQL type string for a UDT field type

# `list_types_cql`

```elixir
@spec list_types_cql() :: String.t()
```

Generates CQL to list all UDTs in keyspace

# `resource_udts`

```elixir
@spec resource_udts(module()) :: [udt_schema()]
```

Returns all UDTs defined in a resource's attributes

# `type_exists_cql`

```elixir
@spec type_exists_cql(String.t() | atom()) :: String.t()
```

Generates CQL to check if UDT exists

# `validate_fields`

```elixir
@spec validate_fields([udt_field_spec()]) :: :ok | {:error, String.t()}
```

Validates a UDT field specification

---

*Consult [api-reference.md](api-reference.md) for complete listing*
