Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl object union_n #135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ rand-builtins = ["rng"]
yaml-builtins = ["dep:serde_yaml"]
urlquery-builtins = ["dep:form_urlencoded", "dep:urlencoding"]
time-builtins = ["time", "dep:chrono-tz", "dep:duration-str", "dep:chronoutil"]
object-builtins = []

all-crypto-builtins = [
"crypto-digest-builtins",
Expand All @@ -122,6 +123,7 @@ all-builtins = [
"yaml-builtins",
"urlquery-builtins",
"time-builtins",
"object-builtins",
]

[[test]]
Expand Down
1 change: 1 addition & 0 deletions src/builtins/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod io;
#[cfg(feature = "json-builtins")]
pub mod json;
pub mod net;
#[cfg(feature = "object-builtins")]
pub mod object;
pub mod opa;
#[cfg(feature = "rng")]
Expand Down
34 changes: 31 additions & 3 deletions src/builtins/impls/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,40 @@

//! Builtins to help handling JSON objects

use anyhow::{bail, Result};
use anyhow::Result;
use serde_json::Value;

/// Creates a new object that is the asymmetric union of all objects merged from
/// left to right. For example: `object.union_n([{"a": 1}, {"b": 2}, {"a": 3}])`
/// will result in `{"b": 2, "a": 3}`.
#[tracing::instrument(name = "object.union_n", err)]
pub fn union_n(objects: Vec<serde_json::Value>) -> Result<serde_json::Value> {
bail!("not implemented");
pub fn union_n(objects: Vec<Value>) -> Result<Value> {
let mut result = serde_json::Value::Object(serde_json::Map::default());
for object in objects {
merge_value(&mut result, &object);
}

Ok(result)
}

fn merge_value(a: &mut Value, b: &Value) {
match (a, b) {
(Value::Object(ref mut a), &Value::Object(ref b)) => {
for (k, v) in b {
merge_value(a.entry(k).or_insert(Value::Null), v);
}
}
(Value::Array(ref mut a), &Value::Array(ref b)) => {
*a = vec![];
a.extend(b.clone());
}
(Value::Array(ref mut a), &Value::Object(ref b)) => {
*a = vec![];
a.extend([Value::Object(b.clone())]);
}
(_, Value::Null) => {}
(a, b) => {
*a = b.clone();
}
}
}
3 changes: 3 additions & 0 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ pub fn resolve<C: EvaluationContext>(name: &str) -> Result<Box<dyn Builtin<C>>>
"net.cidr_expand" => Ok(self::impls::net::cidr_expand.wrap()),
"net.cidr_merge" => Ok(self::impls::net::cidr_merge.wrap()),
"net.lookup_ip_addr" => Ok(self::impls::net::lookup_ip_addr.wrap()),

#[cfg(feature = "object-builtins")]
"object.union_n" => Ok(self::impls::object::union_n.wrap()),

"opa.runtime" => Ok(self::impls::opa::runtime.wrap()),

#[cfg(feature = "rng")]
Expand Down
9 changes: 9 additions & 0 deletions tests/infra-fixtures/test-object.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test

object_1 := object.union_n([{"a": 1}, {"b": 2}, {"a": 3}])

object_2 := object.union_n([{"a": 1}, {"b": 2}, {"a": 3, "b": 1}])

object_override_by_string := object.union_n([{"a": 1}, {"b": 2}, {"a": "3"}])

recursive := object.union_n([{"a": {"b": [1], "c": 1}}, {"a": {"b": [1, 2, 3]}}])
1 change: 1 addition & 0 deletions tests/smoke_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ integration_test!(test_rand, "test-rand");
integration_test!(test_yaml, "test-yaml");
integration_test!(test_urlquery, "test-urlquery");
integration_test!(test_time, "test-time");
integration_test!(test_object, "test-object");

/*
#[tokio::test]
Expand Down
21 changes: 21 additions & 0 deletions tests/snapshots/smoke_test__object.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: tests/smoke_test.rs
expression: "test_policy(\"test-object\", None).await.expect(\"error in test suite\")"
---
- result:
object_1:
a: 3
b: 2
object_2:
a: 3
b: 1
object_override_by_string:
a: "3"
b: 2
recursive:
a:
b:
- 1
- 2
- 3
c: 1