Module
pooka/dynamic/decode
src/pooka/dynamic/decode.pookaRead the label, check the contents, and keep the address of any mistake. A Decoder is a typed recipe over Dynamic. Errors carry paths from outermost field to innermost index. Primitives never coerce strings to numbers, integer values to floats, or arbitrary bytes to UTF-8. List decoding collects all bad elements; field composition and then stop at the first failed step.
let recipe = decode.field("age", decode.int())
assert recipe.run(dynamic.from_pairs([#("age", 7)])) == Ok(7)
API reference
class DecodeError(path: List(String), expected: String, found: String)
Where a value disagreed with the recipe, and the two runtime shapes involved.
path: List(String)
List(String)expected: String
Stringfound: String
String
fn(List(String), String, String) -> pooka/dynamic/decode.DecodeErrorclass Decoder(a)
A reusable recipe. The function remains private; run checks one input.
new(read: fn(Dynamic) -> Result(a, List(DecodeError)))
fn(fn(pooka/dynamic.Dynamic) -> Result(a, List(pooka/dynamic/decode.DecodeError))) -> pooka/dynamic/decode.Decoder(a)run(value: Dynamic) -> Result(a, List(DecodeError))
fn(pooka/dynamic.Dynamic) -> Result(a, List(pooka/dynamic/decode.DecodeError))Decode one input, retaining the structured failures.
map(change: fn(a) -> b) -> Decoder(b)
fn(fn(a) -> a) -> pooka/dynamic/decode.Decoder(a)Transform a decoded value without rereading the foreign term.
assert decode.int().map(fn(n) { n + 1 }).run(dynamic.from(1)) == Ok(2)
then(next: fn(a) -> Decoder(b)) -> Decoder(b)
fn(fn(a) -> pooka/dynamic/decode.Decoder(a)) -> pooka/dynamic/decode.Decoder(a)Select the next recipe from a decoded value, then read the same input.
let recipe = decode.field("kind", decode.string()).then(fn(kind) {
match kind { "age" -> decode.field("value", decode.int()) _ -> decode.failure("age kind") }
})
int() -> Decoder(Int)
fn() -> pooka/dynamic/decode.Decoder(Int)Read a BEAM integer, including values beyond machine-word precision.
float() -> Decoder(Float)
fn() -> pooka/dynamic/decode.Decoder(Float)Read an actual BEAM float. Integers require an explicit conversion.
string() -> Decoder(String)
fn() -> pooka/dynamic/decode.Decoder(String)Read only valid UTF-8 binaries; malformed text remains an error.
bool() -> Decoder(Bool)
fn() -> pooka/dynamic/decode.Decoder(Bool)Read the two boolean atoms, without treating other atoms as booleans.
bit_array() -> Decoder(BitArray)
fn() -> pooka/dynamic/decode.Decoder(BitArray)Read any bitstring, including a non-byte-aligned suffix.
succeed(value: a) -> Decoder(a)
fn(a) -> pooka/dynamic/decode.Decoder(a)Return a known value without inspecting the input.
failure(expected: String) -> Decoder(a)
fn(String) -> pooka/dynamic/decode.Decoder(a)Reject the input with an application-defined expectation.
field(name: String, decoder: Decoder(a)) -> Decoder(a)
fn(String, pooka/dynamic/decode.Decoder(a)) -> pooka/dynamic/decode.Decoder(a)Read a required binary-key map field and prefix any failure with its name.
assert decode.field("age", decode.int()).run(dynamic.from_pairs([#("age", 7)])) == Ok(7)
optional_field(name: String, decoder: Decoder(a)) -> Decoder(Option(a))
fn(String, pooka/dynamic/decode.Decoder(a)) -> pooka/dynamic/decode.Decoder(Option(a))Missing fields become None; present invalid values still report an error.
element(index: Int, decoder: Decoder(a)) -> Decoder(a)
fn(Int, pooka/dynamic/decode.Decoder(a)) -> pooka/dynamic/decode.Decoder(a)Read a zero-based tuple element. Lists use the list decoder instead.
list(decoder: Decoder(a)) -> Decoder(List(a))
fn(pooka/dynamic/decode.Decoder(a)) -> pooka/dynamic/decode.Decoder(List(a))Decode every proper-list element, collecting errors in input order.
one_of(decoders: List(Decoder(a))) -> Decoder(a)
fn(List(pooka/dynamic/decode.Decoder(a))) -> pooka/dynamic/decode.Decoder(a)Try alternatives in order. Keep all explanations when none succeeds.
map2(first: Decoder(a), second: Decoder(b), combine: fn(a, b) -> c) -> Decoder(
c
)
fn(pooka/dynamic/decode.Decoder(a), pooka/dynamic/decode.Decoder(b), fn(a, b) -> c) -> pooka/dynamic/decode.Decoder(c)Read two recipes from one input, then combine their successful values.
let pair = decode.map2(decode.field("name", decode.string()), decode.field("age", decode.int()), fn(name, age) { #(name, age) })
nil() -> Decoder(Nil)
fn() -> pooka/dynamic/decode.Decoder(Nil)Read the Nil atom. Empty lists and absent fields remain different values.
assert decode.nil().run(dynamic.from(Nil)) == Ok(Nil)