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

refactor: value union with options. #586

Merged
merged 1 commit into from
Jun 25, 2023
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
24 changes: 12 additions & 12 deletions kclvm/runtime/src/value/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,11 @@ pub unsafe extern "C" fn kclvm_value_union(
Value::schema_value(schema) => schema.config.attr_map.clone(),
_ => panic!("invalid object '{}' in attr_map", a.type_str()),
};
let opts = UnionOptions {
list_override: false,
idempotent_check: false,
config_resolve: true,
};
if b.is_config() {
let dict = b.as_dict_ref();
let mut result = schema;
Expand All @@ -1822,21 +1827,15 @@ pub unsafe extern "C" fn kclvm_value_union(
let v = type_pack_and_check(v, vec![attr_map.get(k).unwrap()]);
let mut entry = b.dict_get_entry(k).unwrap().deep_copy();
entry.dict_update_key_value(k, v);
result = a
.union_entry(&entry, true, false, false, true)
.clone()
.into_raw();
result = a.union_entry(&entry, true, &opts).clone().into_raw();
} else {
let entry = b.dict_get_entry(k).unwrap();
result = a
.union_entry(&entry, true, false, false, true)
.clone()
.into_raw();
result = a.union_entry(&entry, true, &opts).clone().into_raw();
}
}
result
} else {
a.union_entry(b, true, false, false, true).into_raw()
a.union_entry(b, true, &opts).into_raw()
}
}

Expand Down Expand Up @@ -2396,9 +2395,10 @@ pub unsafe extern "C" fn kclvm_schema_value_new(
value
} else {
let config = ptr_as_ref(config);
let result = schema_value_or_func
.deep_copy()
.union_entry(config, true, false, true, true);
let result =
schema_value_or_func
.deep_copy()
.union_entry(config, true, &UnionOptions::default());
result.into_raw()
}
}
Expand Down
3 changes: 2 additions & 1 deletion kclvm/runtime/src/value/val_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ impl ValueRef {
if let (Value::int_value(a), Value::int_value(b)) = (&*self.rc.borrow(), &*x.rc.borrow()) {
return Self::int(*a | *b);
};
self.deep_copy().union_entry(x, true, false, true, true)
self.deep_copy()
.union_entry(x, true, &UnionOptions::default())
}

pub fn bin_subscr(&self, x: &Self) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/value/val_bin_aug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl ValueRef {
};
if !valid {
if self.is_list_or_config() || x.is_list_or_config() {
self.union_entry(x, true, false, true, true);
self.union_entry(x, true, &UnionOptions::default());
} else {
panic_unsupported_bin_op!("|", self.type_str(), x.type_str());
}
Expand Down
10 changes: 6 additions & 4 deletions kclvm/runtime/src/value/val_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl ValueRef {
v: &ValueRef,
op: ConfigEntryOperationKind,
insert_index: i32,
should_idempotent_check: bool,
idempotent_check: bool,
) {
let ctx = crate::Context::current_context_mut();

Expand Down Expand Up @@ -314,9 +314,11 @@ impl ValueRef {
self.union_entry(
&ValueRef::from(Value::dict_value(Box::new(dict))),
true,
false,
should_idempotent_check,
false,
&UnionOptions {
config_resolve: false,
idempotent_check,
..Default::default()
},
);
} else {
panic!("invalid dict insert value: {}", self.type_str())
Expand Down
81 changes: 33 additions & 48 deletions kclvm/runtime/src/value/val_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate::unification::value_subsume;
use crate::*;

/// UnionContext records some information during the value merging process,
/// including the merging path and whether there are conflicts.
#[derive(Default, Debug)]
struct UnionContext {
path_backtrace: Vec<String>,
Expand All @@ -11,13 +13,32 @@ struct UnionContext {
delta_json: String,
}

/// UnionOptions denotes the union options between runtime values.
#[derive(Debug, Clone)]
pub struct UnionOptions {
/// Whether to override list values.
pub list_override: bool,
/// Whether to do the idempotent check.
pub idempotent_check: bool,
/// Whether to resolve config including optional attributes, etc.
pub config_resolve: bool,
}

impl Default for UnionOptions {
fn default() -> Self {
Self {
list_override: false,
idempotent_check: true,
config_resolve: true,
}
}
}

impl ValueRef {
fn do_union(
&mut self,
x: &Self,
should_list_override: bool,
should_idempotent_check: bool,
should_config_resolve: bool,
opts: &UnionOptions,
union_context: &mut UnionContext,
) -> Self {
if self.is_same_ref(x) {
Expand Down Expand Up @@ -50,7 +71,7 @@ impl ValueRef {
match operation {
ConfigEntryOperationKind::Union => {
let obj_value = obj.values.get_mut(k).unwrap();
if should_idempotent_check && !value_subsume(v, obj_value, false) {
if opts.idempotent_check && !value_subsume(v, obj_value, false) {
union_context.conflict = true;
union_context.path_backtrace.push(k.clone());
union_context.obj_json = if obj_value.is_config() {
Expand All @@ -70,14 +91,7 @@ impl ValueRef {
};
return;
}
obj_value.union(
v,
false,
should_list_override,
should_idempotent_check,
should_config_resolve,
union_context,
);
obj_value.union(v, false, opts, union_context);
if union_context.conflict {
union_context.path_backtrace.push(k.clone());
return;
Expand Down Expand Up @@ -140,7 +154,7 @@ impl ValueRef {
let mut valid = true;
match (&mut *self.rc.borrow_mut(), &*x.rc.borrow()) {
(Value::list_value(obj), Value::list_value(delta)) => {
if !should_list_override {
if !opts.list_override {
let length = if obj.values.len() > delta.values.len() {
obj.values.len()
} else {
Expand All @@ -152,14 +166,7 @@ impl ValueRef {
if idx >= obj_len {
obj.values.push(delta.values[idx].clone());
} else if idx < delta_len {
obj.values[idx].union(
&delta.values[idx],
false,
should_list_override,
should_idempotent_check,
should_config_resolve,
union_context,
);
obj.values[idx].union(&delta.values[idx], false, opts, union_context);
if union_context.conflict {
union_context.path_backtrace.push(format!("list[{idx}]"));
}
Expand Down Expand Up @@ -221,7 +228,7 @@ impl ValueRef {
&x.schema_config_meta(),
&optional_mapping,
);
if should_config_resolve {
if opts.config_resolve {
*self = resolve_schema(&schema, &common_keys);
} else {
*self = schema;
Expand All @@ -233,9 +240,7 @@ impl ValueRef {
&mut self,
x: &Self,
or_mode: bool,
should_list_override: bool,
should_idempotent_check: bool,
should_config_resolve: bool,
opts: &UnionOptions,
union_context: &mut UnionContext,
) -> Self {
if self.is_none_or_undefined() {
Expand All @@ -246,13 +251,7 @@ impl ValueRef {
return self.clone();
}
if self.is_list_or_config() && x.is_list_or_config() {
self.do_union(
x,
should_list_override,
should_idempotent_check,
should_config_resolve,
union_context,
);
self.do_union(x, opts, union_context);
} else if or_mode {
if let (Value::int_value(a), Value::int_value(b)) =
(&mut *self.rc.borrow_mut(), &*x.rc.borrow())
Expand All @@ -271,23 +270,9 @@ impl ValueRef {
self.clone()
}

pub fn union_entry(
&mut self,
x: &Self,
or_mode: bool,
should_list_override: bool,
should_idempotent_check: bool,
should_config_resolve: bool,
) -> Self {
pub fn union_entry(&mut self, x: &Self, or_mode: bool, opts: &UnionOptions) -> Self {
let mut union_context = UnionContext::default();
let ret = self.union(
x,
or_mode,
should_list_override,
should_idempotent_check,
should_config_resolve,
&mut union_context,
);
let ret = self.union(x, or_mode, opts, &mut union_context);
if union_context.conflict {
union_context.path_backtrace.reverse();
let conflict_key = union_context.path_backtrace.last().unwrap();
Expand Down
Loading