Macro tremor_value::literal[][src]

macro_rules! literal {
    ($($value:tt)+) => { ... };
}

Convenience to generate tremor-value from json literals

Note that this literal macro does not support binary syntax literals at this time.

Adapted from: https://github.com/serde-rs/json/blob/5b5f95831d9e0d769367b30b76a686339bffd209/src/macros.rs Constructs a tremor_value::Value from a JSON literal.

Create a value literal of the form:

let value: Value = literal!({
    "code": 200,
    "success": true,
    "payload": {
        "features": [
            "serde",
            "json"
        ]
    }
});

Variables or expressions can be interpolated into the literal. Any type interpolated into an array element or object value must implement Serde’s Serialize trait, while any type interpolated into an object key must implement Into<String>. If the Serialize implementation of the interpolated type decides to fail, or if the interpolated type contains a map with non-string keys, the literal! macro will panic.

let code = 200;
let features = vec!["serde", "json"];

let value = literal!({
    "code": code,
    "success": code == 200,
    "payload": {
        features[0]: features[1]
    }
});

Trailing commas are allowed inside both arrays and objects.

let value = literal!([
    "notice",
    "the",
    "trailing",
    "comma -->",
]);