microsoft / regorus

Regorus - A fast, lightweight Rego (OPA policy language) interpreter written in Rust.
MIT License
130 stars 32 forks source link

Function to convert serde_json::Value or serde_yaml::Value into regorus::Value #195

Closed lquerel closed 6 months ago

lquerel commented 6 months ago

This type of conversion will likely be very common. I don't think it's possible, with the current regorus API, to use serde-transcode (or a similar approach) to perform this conversion efficiently. It might be helpful to add a method to perform this type of transformation.

anakrish commented 6 months ago

Would serde_json::from_value and serde_yaml::from_value be sufficient?

use serde_json::json;

fn main() {
    let j:serde_json::Value = json!({
        "fingerprint": "0xF9BA143B95FF6D82",
        "location": "Menlo Park, CA",
        "Hello": [
            1,
            "Foo"
        ]
    });
    println!("{j:#?}");

    let yaml = "
        - 1
        - [0, 0, 0]
        - {x: 1.0, y: 2.0}
    ";
    let k : serde_yaml::Value = serde_yaml::from_str(&yaml).unwrap();
    println!("{k:#?}");

    // Convert serde_json::Value to regorus::Value, consuming it.
    let u: regorus::Value = serde_json::from_value(j).unwrap();
    println!("{u:#?}");

    // Convert serde_yaml::Value to regorus::Value, consuming it.
    let v: regorus::Value = serde_yaml::from_value(k).unwrap();
    println!("{v:#?}");
}
lquerel commented 6 months ago

Sorry, and shame on me, I forgot the basics...

anakrish commented 6 months ago

No worries. I'm thinking of adding a From<serde_json::Value> to regorus::Value since serde_json::from_value may not be that intuitive.

lquerel commented 6 months ago

That will make things straightforward