Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.algebraic_alias.transform

Transformation utilities for JSON-like values

See Also:
JSON libraries mir-ion and asdf;
License:
Authors:
Ilia Ki
template transformLeafs(visitors...)
Transforms algebraics leafs recursively in place, ensuring that all leaf types are handled by the visiting functions.
Recursion is done for This[], StringMap!This, This[string], and Annotated!This types.
Examples:
import mir.format: text;
import mir.algebraic_alias.json;
JsonAlgebraic value = ["key" : ["str".JsonAlgebraic, 2.32.JsonAlgebraic, null.JsonAlgebraic].JsonAlgebraic];

// converts all leavs to a text form
value.transformLeafs!text;
assert(value == ["key" : ["str".JsonAlgebraic, "2.32".JsonAlgebraic, "null".JsonAlgebraic].JsonAlgebraic].JsonAlgebraic);

value = ["key" : ["str".JsonAlgebraic, 2.32.JsonAlgebraic, true.JsonAlgebraic].JsonAlgebraic].JsonAlgebraic;

/// converts only bool values
value.transformLeafs!(
    (bool b) => b.text,
    v => v, // other values are copied as is
);

assert(value == ["key" : ["str".JsonAlgebraic, 2.32.JsonAlgebraic, "true".JsonAlgebraic].JsonAlgebraic].JsonAlgebraic);
template tryTransformLeafs(visitors...)
Behaves as transformLeafs but doesn't enforce at compile time that all types can be handled by the visiting functions.
Throws:
Exception if naryFun!visitors can't be called with provided arguments
Examples:
import mir.format: text;
import mir.algebraic_alias.json;
JsonAlgebraic value = ["key" : [true.JsonAlgebraic, 100.JsonAlgebraic, 2.32.JsonAlgebraic].JsonAlgebraic];

// converts long and double numbers to a text form, bool values is converted to `long`
value.tryTransformLeafs!((double v) => v.text, (long v) => v.text);
assert(value == ["key" : ["1".JsonAlgebraic, "100".JsonAlgebraic, "2.32".JsonAlgebraic].JsonAlgebraic].JsonAlgebraic);
template optionalTransformLeafs(visitors...)
Behaves as transformLeafs but doesn't enforce at compile time that all types can be handled by the visiting functions.
The function ignores leafs that can't be handled by the visiting functions.
Examples:
import mir.format: text;
import mir.algebraic_alias.json;
JsonAlgebraic value = ["key" : [null.JsonAlgebraic, true.JsonAlgebraic, 100.JsonAlgebraic, 2.32.JsonAlgebraic].JsonAlgebraic];

// converts long numbers to a text form, ignores other types
value.optionalTransformLeafs!(
    (long v) => v.text,
    (bool b) => b, // needs special overload for bool to get rid of implicit converion to long/double
);
assert(value == ["key" : [null.JsonAlgebraic, true.JsonAlgebraic, "100".JsonAlgebraic, 2.32.JsonAlgebraic].JsonAlgebraic].JsonAlgebraic);
template transformLeafsImpl(alias handler, alias visitor)
ref Algebraic!Types transformLeafsImpl(Types...)(return ref Algebraic!Types value);

Algebraic!Types transformLeafsImpl(Types...)(Algebraic!Types value);