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: enhance config override at compile time. #221

Merged
merged 1 commit into from
Sep 27, 2022
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
9 changes: 8 additions & 1 deletion kclvm/sema/src/pre_process/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,14 @@ fn unify_config_entries(
};
let entry = entry.clone();
match bucket.get_mut(&name) {
Some(values) => values.push(entry),
Some(values) => {
// If the attribute operation is override, clear all previous entries and override
// with current entry.
if let ast::ConfigEntryOperation::Override = entry.node.operation {
values.clear();
}
values.push(entry);
}
None => {
let values = vec![entry];
bucket.insert(name, values);
Expand Down
17 changes: 17 additions & 0 deletions kclvm/sema/src/pre_process/test_data/config_override.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
schema ConfigMapping:
[str]: Config

schema Config:
[str]: any

configMapping: ConfigMapping {
key = Config {
data.key: "value"
}
}

configMapping: ConfigMapping {
key = Config {
data.key: "value1"
}
}
43 changes: 41 additions & 2 deletions kclvm/sema/src/pre_process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ fn test_config_merge() {
if let ast::Stmt::Unification(unification) = &module.body[0].node {
let schema = &unification.value.node;
if let ast::Expr::Config(config) = &schema.config.node {
// 2 contains `name` in `config1.k`, `age` in `config2.k` and `age` in `config2.k`
// 2 contains `name` in `config1.k`, `age` in `config2.k`.
// person: Person {
// name = "Alice"
// age = 18
// }
assert_eq!(config.items.len(), 2);
assert_eq!(
get_attr_paths_from_config_expr(&config),
vec!["name".to_string(), "age".to_string(), "age".to_string(),]
vec!["name".to_string(), "age".to_string()]
);
} else {
panic!(
Expand All @@ -96,3 +101,37 @@ fn test_config_merge() {
)
}
}

#[test]
fn test_config_override() {
let mut program =
load_program(&["./src/pre_process/test_data/config_override.k"], None).unwrap();
merge_program(&mut program);
let modules = program.pkgs.get_mut(kclvm_ast::MAIN_PKG).unwrap();
assert_eq!(modules.len(), 1);
// Test the module merge result
let module = modules.first().unwrap();
if let ast::Stmt::Unification(unification) = &module.body[2].node {
let schema = &unification.value.node;
if let ast::Expr::Config(config) = &schema.config.node {
// key = Config {
// data.key: "value1"
// }
assert_eq!(config.items.len(), 1);
assert_eq!(
get_attr_paths_from_config_expr(&config),
vec!["key".to_string(), "key.data.key".to_string()]
);
} else {
panic!(
"test failed, expect config expression, got {:?}",
schema.config
)
}
} else {
panic!(
"test failed, expect unification statement, got {:?}",
module.body[2]
)
}
}