# avro.value

> A module of avro's API.

`Value` — a dynamically typed Avro datum.

Like `Schema`, a `Value` is an arena of nodes plus a root index, but the
arena sits behind an `ArcPointer` so navigating (`v.field("x").at(0)`) is a
refcount bump rather than a deep copy. Building a value copies its children
into a fresh arena; reading one never copies.

## Structs

### `ValueNode`

```mojo
struct ValueNode
```

Implements: `Copyable`, `Movable`

Fields:

- `kind: Int`
- `b: Bool`
- `i: Int64`: Int / long payload, and the enum symbol or union branch index.
- `d: Float64`: Float / double payload.
- `data: List[UInt8]`: Bytes / fixed payload.
- `s: String`: String payload, and the enum symbol name.
- `keys: List[String]`: Record field names or map keys, in order.
- `kids: List[Int]`

#### `of`

```mojo
def of(kind: Int) -> Self
```

### `Value`

```mojo
struct Value
```

One Avro datum. Cheap to copy: the node arena is shared.

Implements: `Copyable`, `Equatable`, `Movable`, `Sized`, `Writable`

Fields:

- `arena: ArcPointer[List[ValueNode]]`
- `idx: Int`

#### `__init__`

```mojo
def __init__(out self, var arena: ArcPointer[List[ValueNode]], idx: Int)
```

```mojo
def __init__(out self)
```

#### `__eq__`

```mojo
def __eq__(self, other: Self) -> Bool
```

#### `__ne__`

```mojo
def __ne__(self, other: Self) -> Bool
```

#### `null`

```mojo
def null() -> Self
```

#### `boolean`

```mojo
def boolean(v: Bool) -> Self
```

#### `int`

```mojo
def int(v: Int64) -> Self
```

#### `long`

```mojo
def long(v: Int64) -> Self
```

#### `float`

```mojo
def float(v: Float32) -> Self
```

#### `double`

```mojo
def double(v: Float64) -> Self
```

#### `bytes`

```mojo
def bytes(v: Span[UInt8]) -> Self
```

#### `fixed`

```mojo
def fixed(v: Span[UInt8]) -> Self
```

#### `string`

```mojo
def string(v: StringSpan) -> Self
```

#### `enum`

```mojo
def enum(index: Int, symbol: StringSpan) -> Self
```

#### `array`

```mojo
def array(items: List[Self]) -> Self
```

#### `map`

```mojo
def map(var keys: List[String], values: List[Self]) -> Self
```

#### `record`

```mojo
def record(var names: List[String], values: List[Self]) -> Self
```

#### `union`

```mojo
def union(index: Int, branch: Self) -> Self
```

#### `kind`

```mojo
def kind(self) -> Int
```

#### `type_name`

```mojo
def type_name(self) -> String
```

#### `is_null`

```mojo
def is_null(self) -> Bool
```

#### `as_bool`

```mojo
def as_bool(self) -> Bool
```

#### `as_long`

```mojo
def as_long(self) -> Int64
```

#### `as_int`

```mojo
def as_int(self) -> Int32
```

#### `as_double`

```mojo
def as_double(self) -> Float64
```

#### `as_float`

```mojo
def as_float(self) -> Float32
```

#### `as_string`

```mojo
def as_string(self) -> String
```

#### `as_bytes`

```mojo
def as_bytes(self) -> List[UInt8]
```

#### `enum_index`

```mojo
def enum_index(self) -> Int
```

#### `symbol`

```mojo
def symbol(self) -> String
```

#### `union_index`

```mojo
def union_index(self) -> Int
```

#### `unwrap`

```mojo
def unwrap(self) -> Self
```

The branch value of a union; `self` for anything else.

#### `__len__`

```mojo
def __len__(self) -> Int
```

#### `at`

```mojo
def at(self, k: Int) -> Self
```

Child `k` — an array element, map value or record field value.

#### `key`

```mojo
def key(self, k: Int) -> String
```

Name of child `k` — a record field name or a map key.

#### `index_of`

```mojo
def index_of(self, name: StringSpan) -> Int
```

Position of a record field or map key, or -1 when absent.

#### `has`

```mojo
def has(self, name: StringSpan) -> Bool
```

#### `field`

```mojo
def field(self, name: StringSpan) -> Self
```

A record field (or map entry) by name, unwrapping unions.

#### `field_raw`

```mojo
def field_raw(self, name: StringSpan) -> Self
```

A record field by name, keeping any union wrapper.

#### `to_json`

```mojo
def to_json(self) -> String
```

A JSON rendering used by the tests and the fastavro cross-check.

Unions collapse to their branch value and `bytes`/`fixed` render as
`"0x…"` hex, so the output lines up with a Python dict dumped the
same way.

#### `write_to`

```mojo
def write_to(self, mut writer: T)
```

### `RecordBuilder`

```mojo
struct RecordBuilder
```

Accumulate named fields, then `build()` a record `Value`.

```mojo
var b = RecordBuilder()
b.add("id", Value.long(7))
b.add("name", Value.string("row"))
var v = b^.build()
```

Implements: `Copyable`, `Defaultable`, `Movable`

Fields:

- `names: List[String]`
- `values: List[Value]`

#### `__init__`

```mojo
def __init__(out self)
```

#### `add`

```mojo
def add(mut self, name: StringSpan, value: Value)
```

#### `build`

```mojo
def build(deinit self) -> Value
```

### `ArrayBuilder`

```mojo
struct ArrayBuilder
```

Accumulate elements, then `build()` an array `Value`.

Implements: `Copyable`, `Defaultable`, `Movable`

Fields:

- `values: List[Value]`

#### `__init__`

```mojo
def __init__(out self)
```

#### `add`

```mojo
def add(mut self, value: Value)
```

#### `build`

```mojo
def build(deinit self) -> Value
```

### `MapBuilder`

```mojo
struct MapBuilder
```

Accumulate key/value pairs, then `build()` a map `Value`.

Implements: `Copyable`, `Defaultable`, `Movable`

Fields:

- `keys: List[String]`
- `values: List[Value]`

#### `__init__`

```mojo
def __init__(out self)
```

#### `add`

```mojo
def add(mut self, key: StringSpan, value: Value)
```

#### `build`

```mojo
def build(deinit self) -> Value
```
