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

fix: schema resolve insert attr op #1402

Merged
merged 1 commit into from
Jun 6, 2024
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
3 changes: 2 additions & 1 deletion kclvm/evaluator/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub fn resolve_schema(s: &Evaluator, schema: &ValueRef, keys: &[String]) -> Valu
let schema_type_name = schema_runtime_type(&schema_value.name, &schema_value.pkgpath);
if let Some(index) = s.schemas.borrow().get(&schema_type_name) {
let keys = keys.iter().map(|v| v.as_str()).collect();
let config_value = schema.dict_get_entries(keys);
let config_value =
schema.dict_get_entries_with_op(keys, &ConfigEntryOperationKind::Override);
let config_meta = {
let ctx = s.runtime_ctx.borrow();
schema_config_meta(
Expand Down
2 changes: 1 addition & 1 deletion kclvm/evaluator/src/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn do_union(
}
};

//union schema vars
// whether to union schema vars and resolve it and do the check of schema
let mut union_schema = false;
let mut pkgpath: String = "".to_string();
let mut name: String = "".to_string();
Expand Down
43 changes: 43 additions & 0 deletions kclvm/runtime/src/value/val_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,49 @@ impl ValueRef {
}
}

/// Dict get entries e.g., {k1: v1, k2, v2}.get_entries([k1, k2]) == {k1: v1, k1: v2}
pub fn dict_get_entries_with_op(
&self,
keys: Vec<&str>,
op: &ConfigEntryOperationKind,
) -> ValueRef {
match &*self.rc.borrow() {
Value::dict_value(ref dict) => {
let mut d = ValueRef::dict(None);
for key in keys {
if dict.values.contains_key(key) {
let value = dict.values.get(key).unwrap();
let index = dict.insert_indexs.get(key).unwrap_or(&-1);
d.dict_update_entry(key, value, op, index);
}
}
d.set_potential_schema_type(&dict.potential_schema.clone().unwrap_or_default());
d
}
Value::schema_value(ref schema) => {
let mut d = ValueRef::dict(None);
for key in keys {
if schema.config.values.contains_key(key) {
let value = schema.config.values.get(key).unwrap();
let index = schema.config.insert_indexs.get(key).unwrap_or(&-1);
d.dict_update_entry(key, value, op, index);
}
}
d.set_potential_schema_type(
&schema
.config
.potential_schema
.as_ref()
.map(|v| v.to_string())
.unwrap_or_default(),
);
d
}
// Panic
_ => panic!("invalid config value in dict_get_entries"),
}
}

/// Update dict value without attribute operator check, only update
pub fn dict_update(&mut self, v: &ValueRef) {
let mut binding = self.rc.borrow_mut();
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/value/val_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn resolve_schema(ctx: &mut Context, schema: &ValueRef, keys: &[String]) ->
let schema_type = schema_type.func.as_function();
let schema_fn_ptr = schema_type.fn_ptr;
let keys = keys.iter().map(|v| v.as_str()).collect();
let config = schema.dict_get_entries(keys);
let config = schema.dict_get_entries_with_op(keys, &ConfigEntryOperationKind::Override);
let config_new = config.clone();
let config_meta = schema_config_meta(
&ctx.panic_info.kcl_file,
Expand Down
9 changes: 9 additions & 0 deletions test/grammar/schema/config_op/insert/insert_5/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema x:
m: [str] = ["hello"]

s = x {
}

t = s | {
m += ["world"]
}
7 changes: 7 additions & 0 deletions test/grammar/schema/config_op/insert/insert_5/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
s:
m:
- hello
t:
m:
- hello
- world
14 changes: 14 additions & 0 deletions test/grammar/schema/config_op/insert/insert_6/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema x:
m: [str] = ["hello"]
nn = m

s = x {
}

t = s | {
m += ["world"]
} | {
m = ["world"]
} | {
m += ["hello"]
}
12 changes: 12 additions & 0 deletions test/grammar/schema/config_op/insert/insert_6/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
s:
m:
- hello
nn:
- hello
t:
m:
- world
- hello
nn:
- world
- hello
Loading