Constant Fields
constantoutput only
A constant is an output-only field whose value is computed once, at creation, and never accepted
from input or changed by an update (e.g. id).
- Declared with
#[constant(value_or_resolver)]-- a static value, or a|ctx, opts| -> Tresolver (sync or async, with access to context and options like any other handler). - Requires
output(...)on the schema, since it never appears on the input struct. - May have
on_deleteandon_successevent handlers.
Example
id is a static constant. label is computed once via a zero-argument resolver closure:
main.rs
use ivo::ivo_schema;
#[ivo_schema(
input(ItemInput, ivo_attrs(derive(Debug, Clone, PartialEq))),
output(Item, ivo_attrs(derive(Debug, Clone, PartialEq)))
)]
mod item_schema {
struct Fields {
#[constant(1234)]
pub id: i32,
#[constant(|| "generated".to_string())]
pub label: String,
#[lax(String::new())]
pub name: String,
}
}
fn main() {
use item_schema::{ItemModel, PartialItemInput};
let (item, _ctx_options) = ItemModel
.create(PartialItemInput { name: Some("widget".into()) }, ())
.unwrap();
println!("{:?}", item); // Item { id: 1234, label: "generated", name: "widget" }
// the input struct has no `id` or `label`: a constant can't be provided, and an update only
// changes the other fields
let (updated, _ctx_options) = ItemModel
.update(item.clone(), PartialItemInput { name: Some("gadget".into()) }, ())
.unwrap();
println!("{:?}", updated); // PartialItem { id: None, label: None, name: Some("gadget") }
}
The input struct has no id or label field at all: there's nothing to submit for a constant,
and no way to change it after creation.
Try it in the browser
id is a constant (always 1234); username is lax with a default. Edit the input and run it.
Loading playground…