Skip to content
Merged
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: 1 addition & 1 deletion src/builtins/json_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl EditNode {
set.insert(value.render()?);
enforce_limit()?;
}
Value::Set(crate::Rc::new(set))
Value::from_set(set)
}
})
}
Expand Down
26 changes: 11 additions & 15 deletions src/builtins/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::lexer::Span;
use crate::value::Value;
use crate::*;

use alloc::collections::BTreeSet;

use anyhow::{bail, Result};

pub fn register(m: &mut builtins::BuiltinsMap<&'static str, builtins::BuiltinFcn>) {
Expand All @@ -24,19 +22,19 @@ pub fn register(m: &mut builtins::BuiltinsMap<&'static str, builtins::BuiltinFcn
pub fn intersection(expr1: &Expr, expr2: &Expr, v1: Value, v2: Value) -> Result<Value> {
let s1 = ensure_set("intersection", expr1, v1)?;
let s2 = ensure_set("intersection", expr2, v2)?;
Ok(Value::from_set(s1.intersection(&s2).cloned().collect()))
Ok(Value::from(s1.intersection(&s2)))
}

pub fn union(expr1: &Expr, expr2: &Expr, v1: Value, v2: Value) -> Result<Value> {
let s1 = ensure_set("union", expr1, v1)?;
let s2 = ensure_set("union", expr2, v2)?;
Ok(Value::from_set(s1.union(&s2).cloned().collect()))
Ok(Value::from(s1.union(&s2)))
}

pub fn difference(expr1: &Expr, expr2: &Expr, v1: Value, v2: Value) -> Result<Value> {
let s1 = ensure_set("difference", expr1, v1)?;
let s2 = ensure_set("difference", expr2, v2)?;
Ok(Value::from_set(s1.difference(&s2).cloned().collect()))
Ok(Value::from(s1.difference(&s2)))
}

fn binary_set_union(
Expand All @@ -49,7 +47,7 @@ fn binary_set_union(
ensure_args_count(span, name, params, args, 2)?;
let left = ensure_set(name, &params[0], args[0].clone())?;
let right = ensure_set(name, &params[1], args[1].clone())?;
Ok(Value::from_set(left.union(&right).cloned().collect()))
Ok(Value::from(left.union(&right)))
}

fn binary_set_intersection(
Expand All @@ -62,9 +60,7 @@ fn binary_set_intersection(
ensure_args_count(span, name, params, args, 2)?;
let left = ensure_set(name, &params[0], args[0].clone())?;
let right = ensure_set(name, &params[1], args[1].clone())?;
Ok(Value::from_set(
left.intersection(&right).cloned().collect(),
))
Ok(Value::from(left.intersection(&right)))
}

fn intersection_of_set_of_sets(
Expand All @@ -77,7 +73,7 @@ fn intersection_of_set_of_sets(
ensure_args_count(span, name, params, args, 1)?;
let set = ensure_set(name, &params[0], args[0].clone())?;

let mut res = BTreeSet::new();
let mut res = crate::value::Set::new();
let mut first = true;

for s in set.iter() {
Expand All @@ -92,11 +88,11 @@ fn intersection_of_set_of_sets(
res.clone_from(s);
first = false;
} else {
res = res.intersection(s).cloned().collect();
res = res.intersection(s);
}
}

Ok(Value::from_set(res))
Ok(Value::from(res))
}

fn union_of_set_of_sets(
Expand All @@ -109,7 +105,7 @@ fn union_of_set_of_sets(
ensure_args_count(span, name, params, args, 1)?;
let set = ensure_set(name, &params[0], args[0].clone())?;

let mut res = BTreeSet::new();
let mut res = crate::value::Set::new();

for s in set.iter() {
let s = match s {
Expand All @@ -119,8 +115,8 @@ fn union_of_set_of_sets(
),
};

res = res.union(s).cloned().collect();
res = res.union(s);
}

Ok(Value::from_set(res))
Ok(Value::from(res))
}
6 changes: 2 additions & 4 deletions src/builtins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
use crate::ast::{Expr, Ref};
use crate::lexer::Span;
use crate::number::Number;
use crate::value::Object;
use crate::value::{Object, Set};
use crate::Rc;
use crate::Value;
use crate::*;

use alloc::collections::BTreeSet;

use anyhow::{bail, Result};

#[inline]
Expand Down Expand Up @@ -159,7 +157,7 @@ pub fn ensure_array(fcn: &str, arg: &Expr, v: Value) -> Result<Rc<Vec<Value>>> {
})
}

pub fn ensure_set(fcn: &str, arg: &Expr, v: Value) -> Result<Rc<BTreeSet<Value>>> {
pub fn ensure_set(fcn: &str, arg: &Expr, v: Value) -> Result<Rc<Set>> {
Ok(match v {
Value::Set(s) => s,
_ => {
Expand Down
6 changes: 3 additions & 3 deletions src/languages/azure_policy/compiler/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::languages::azure_policy::ast::{
Condition, EffectKind, FieldKind, JsonValue, Lhs, OperatorKind, PolicyDefinition, PolicyRule,
ValueOrExpr,
};
use crate::{Rc, Value};
use crate::Value;

use super::core::Compiler;

Expand Down Expand Up @@ -237,7 +237,7 @@ impl Compiler {
.iter()
.map(|p| Value::String(p.name.as_str().into()))
.collect();
annot.insert("parameter_names".to_string(), Value::Set(Rc::new(set)));
annot.insert("parameter_names".to_string(), Value::from_set(set));
}

// Extra fields: policyType → policy_type, id → policy_id, name → policy_name.
Expand Down Expand Up @@ -279,6 +279,6 @@ fn insert_string_set_annotation(
.iter()
.map(|s| Value::String(s.as_str().into()))
.collect();
annot.insert(key.to_string(), Value::Set(Rc::new(set)));
annot.insert(key.to_string(), Value::from_set(set));
}
}
6 changes: 2 additions & 4 deletions src/languages/azure_rbac/builtins/lists.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use alloc::collections::BTreeSet;

use crate::value::Value;
use crate::value::{Set, Value};

use super::evaluator::RbacBuiltinError;

Expand All @@ -28,7 +26,7 @@ fn list_contains_values(list: &[Value], needle: &Value) -> bool {
}

// For sets, treat a list/set needle as "all elements are contained".
fn set_contains_values(set: &BTreeSet<Value>, needle: &Value) -> bool {
fn set_contains_values(set: &Set, needle: &Value) -> bool {
match *needle {
// For collection needles, require all elements to be present.
Value::Array(ref right_list) => right_list.iter().all(|item| set.contains(item)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(in crate::languages::rego::compiler) fn try_eval_const(expr: &Expr) -> Optio
.iter()
.map(|i| try_eval_const(i.as_ref()))
.collect::<Option<BTreeSet<_>>>()
.map(|s| Value::Set(Rc::new(s))),
.map(Value::from_set),
Expr::Object { fields, .. } => fields
.iter()
.map(|(_, k, v)| Some((try_eval_const(k.as_ref())?, try_eval_const(v.as_ref())?)))
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'a> Compiler<'a> {
items.iter().map(|i| try_eval_const(i.as_ref())).collect();
if let Some(values) = all_const {
let dest = self.alloc_register();
let literal_idx = self.add_literal(Value::Set(Rc::new(values)));
let literal_idx = self.add_literal(Value::from_set(values));
self.emit_instruction(Instruction::Load { dest, literal_idx }, span);
return Ok(dest);
}
Expand Down
17 changes: 10 additions & 7 deletions src/rvm/program/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl MetadataValue {
.collect(),
)
} else {
MetadataValue::List(set.iter().map(MetadataValue::from_value).collect())
MetadataValue::List(set.iter_sorted().map(MetadataValue::from_value).collect())
}
}
Value::Object(ref obj) => {
Expand Down Expand Up @@ -202,7 +202,7 @@ impl MetadataValue {
for s in set {
bset.insert(Value::String(s.as_str().into()));
}
Value::Set(Rc::new(bset))
Value::from_set(bset)
}
MetadataValue::Bool(b) => Value::Bool(b),
MetadataValue::Integer(n) => Value::from(n),
Expand Down Expand Up @@ -301,7 +301,7 @@ mod tests {
let mut set = BTreeSet::new();
set.insert(Value::String("a".into()));
set.insert(Value::String("b".into()));
let v = Value::Set(Rc::new(set));
let v = Value::from_set(set);
assert_round_trip(&v, &v);
}

Expand All @@ -328,11 +328,14 @@ mod tests {
let mut set = BTreeSet::new();
set.insert(Value::String("a".into()));
set.insert(Value::from(1_i64));
let v = Value::Set(Rc::new(set));
let v = Value::from_set(set);
let mv = MetadataValue::from_value(&v);
assert!(
matches!(mv, MetadataValue::List(_)),
"mixed-type set should produce List, got {mv:?}"
assert_eq!(
mv,
MetadataValue::List(alloc::vec![
MetadataValue::Integer(1),
MetadataValue::String("a".into()),
])
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/rvm/program/serialization/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use serde::ser::{SerializeSeq as _, SerializeTuple as _};
use serde::{Deserialize, Serialize};

use crate::number::Number;
use crate::value::Object;
use crate::value::Value;
use crate::value::{Object, Set};

const VARIANT_NULL: u32 = 0;
const VARIANT_BOOL: u32 = 1;
Expand Down Expand Up @@ -118,15 +118,15 @@ impl<'a> Serialize for BinaryValueSlice<'a> {
}
}

struct BinarySetRef<'a>(&'a BTreeSet<Value>);
struct BinarySetRef<'a>(&'a Set);

impl<'a> Serialize for BinarySetRef<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
for value in self.0.iter() {
for value in self.0.iter_sorted() {
seq.serialize_element(&BinaryValueRef(value))?;
}
seq.end()
Expand Down
4 changes: 1 addition & 3 deletions src/rvm/vm/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ impl RegoVM {
match (a, b) {
(&Value::Number(ref x), &Value::Number(ref y)) => Ok(Value::from(x.sub(y)?)),
(&Value::Set(ref left), &Value::Set(ref right)) => {
let diff: alloc::collections::BTreeSet<Value> =
left.difference(right).cloned().collect();
Ok(Value::from(diff))
Ok(Value::from(left.difference(right)))
}
_ => Err(VmError::InvalidSubtraction {
left: a.clone(),
Expand Down
Loading
Loading