Renaming Fields
Rename a field using schemafy.
If you are aware of the specific field you'd like to rename, "schemafy" offers an interesting and easy way by asking for it in the function as a Box<str> type.
schema: {
#[schema((field: Box<str>) => { field = "desired"; })]
undesired_name: ""
}
target: {
undesired_name: "this is cool"
}
#[main]
fn main() {
self.schema.schemafy(self.target);
pln(self.target);
}
> stof run example.stof
{"desired":"this is cool"}
However, more generally, you can always ask for the target (and schema) objects, where you have the full capabilities of Stof at your disposal. This enables you to create additional fields, drop fields, rename fields, search for fields, etc.
schema: {
#[schema((target: obj, value: unknown): unknown => {
if (value == null) {
let search = target.searchDown("othername");
if (search) return search[0];
}
return value;
})]
desired: ""
}
target: {
subobj: {
another: {
othername: "yo dude"
}
}
}
#[main]
fn main() {
// remove invalid, remove undefined
self.schema.schemafy(self.target, true, true);
pln(self.target);
}
> stof run example.stof
{"desired":"yo dude"}
Last updated
Was this helpful?