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

Shared CQL type mapping and conversion helpers.

Centralizes the canonical type-to-CQL-string mappings used across
`AshScylla.Migration`, `AshScylla.DataLayer.Udt`, and
`AshScylla.DataLayer.Collection`.

## Type Mapping

The canonical mapping resolves Ash DSL types to CQL types:

| Ash Type | CQL Type |
|----------|----------|
| `:string` | TEXT |
| `:integer` | BIGINT |
| `:uuid` | UUID |
| `:boolean` | BOOLEAN |
| `:utc_datetime` | TIMESTAMP |
| `:decimal` | DECIMAL |
| `:float` | DOUBLE |
| `:binary` | BLOB |
| `:date` | DATE |
| `:time` | TIME |

Unknown types fall back to `TEXT`.

# `ash_type_to_cql_type`

```elixir
@spec ash_type_to_cql_type(
  atom() | tuple(),
  keyword()
) :: String.t()
```

Converts an Ash type (atom or tuple) to its CQL type string representation.

Handles special collection and structured types:

- `:map` — rendered as `MAP<key_type, value_type>`
- `:array` — rendered as `LIST<element_type>`
- `:set` — rendered as `SET<element_type>`
- `:udt` — rendered as `frozen<type_name>`
- `{:array, element_type}` — rendered as `LIST<cql_type>`
- `{:map, key_type, value_type}` — rendered as `MAP<k, v>`
- `{:set, element_type}` — rendered as `SET<cql_type>`

All other atoms are resolved via the canonical `cql_type/1` mapping.

The `:frozen` option wraps the result in `frozen<...>`.

## Options

- `:key_type` — for `:map`, the CQL key type (default: `"TEXT"`)
- `:value_type` — for `:map`, the CQL value type (default: `"TEXT"`)
- `:element_type` — for `:array` / `:set`, the CQL element type (default: `"TEXT"`)
- `:type_name` — for `:udt`, the UDT name
- `:frozen` — if `true`, wraps the result in `frozen<...>`

## Examples

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type(:uuid, [])
    "UUID"

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type(:string, [])
    "TEXT"

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type(:map, key_type: "TEXT", value_type: "INT")
    "MAP<TEXT, INT>"

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type(:array, element_type: "UUID")
    "LIST<UUID>"

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type({:array, :string}, [])
    "LIST<TEXT>"

    iex> AshScylla.DataLayer.Types.ash_type_to_cql_type({:map, :string, :integer}, [])
    "MAP<TEXT, BIGINT>"

# `cql_element_type`

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

Returns the CQL type string for a collection element type atom.

Delegates to `cql_type/1`.

## Examples

    iex> AshScylla.DataLayer.Types.cql_element_type(:int)
    "INT"

    iex> AshScylla.DataLayer.Types.cql_element_type(:float)
    "DOUBLE"

# `cql_type`

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

Returns the CQL type string for a given type atom.

Known types are resolved via the canonical mapping.
Unknown types fall back to "TEXT".

## Examples

    iex> AshScylla.DataLayer.Types.cql_type(:text)
    "TEXT"

    iex> AshScylla.DataLayer.Types.cql_type(:bigint)
    "BIGINT"

    iex> AshScylla.DataLayer.Types.cql_type(:custom_type)
    "TEXT"

# `field_type_to_cql`

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

Returns the CQL type string for a UDT field type atom.

Delegates to `cql_type/1`.

## Examples

    iex> AshScylla.DataLayer.Types.field_type_to_cql(:text)
    "TEXT"

    iex> AshScylla.DataLayer.Types.field_type_to_cql(:uuid)
    "UUID"

# `uuid_binary_to_string`

```elixir
@spec uuid_binary_to_string(binary()) :: {:ok, String.t()} | :error
```

Converts a 16-byte UUID binary from Xandra/ScyllaDB back to a
36-character UUID string (e.g. "550e8400-e29b-41d4-a716-446655440000").

Returns `{:ok, String.t()}` on success, `:error` if the binary is not 16 bytes.

# `uuid_string_to_binary`

```elixir
@spec uuid_string_to_binary(String.t()) :: {:ok, binary()} | :error
```

Converts a UUID string (36 chars, e.g. "550e8400-e29b-41d4-a716-446655440000")
to a 16-byte binary for Xandra/ScyllaDB.

Returns `{:ok, binary}` on success, `:error` if the string is not a valid UUID.

# `valid_cql_types`

```elixir
@spec valid_cql_types() :: [atom()]
```

Returns the list of all known valid CQL type atoms.

---

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