glite/decode
Decoders that turn a result row into a typed value.
import glite/decode
let user = {
use id <- decode.element(0, decode.int)
use name <- decode.element(1, decode.text)
use email <- decode.element(2, decode.optional(decode.text))
decode.success(User(id:, name:, email:))
}
Types
Decodes one row, a list of column values.
pub opaque type RowDecoder(a)
Decodes one column value. The error is a short description.
pub type ValueDecoder(a) =
fn(value.Value) -> Result(a, String)
Values
pub fn blob(value: value.Value) -> Result(BitArray, String)
Binary data. Text is accepted too, as its UTF-8 bytes, because SQLite doesn’t always say which of the two a value is.
pub fn bool(value: value.Value) -> Result(Bool, String)
A boolean stored as the integer 0 or 1.
pub fn element(
index: Int,
decoder: fn(value.Value) -> Result(a, String),
next: fn(a) -> RowDecoder(b),
) -> RowDecoder(b)
Decode the column at index, counting from 0, then carry on with next.
Made for use.
pub fn optional(
decoder: fn(value.Value) -> Result(a, String),
) -> fn(value.Value) -> Result(option.Option(a), String)
NULL becomes None; anything else goes through decoder.
pub fn run(
decoder: RowDecoder(a),
row: List(value.Value),
at row_index: Int,
) -> Result(a, error.Error)
Run a decoder on one row. row is the row’s position in its result, used
in the error.