Skip to content

Commit

Permalink
fix: fixed OverrideFile insert unification position error (#1702)
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Oct 21, 2024
1 parent c6ecc87 commit f298eeb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
103 changes: 53 additions & 50 deletions kclvm/query/src/override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,52 +408,66 @@ impl<'ctx> MutSelfMutWalker<'ctx> for OverrideTransformer {
}
}
} else if let ast::Stmt::Unification(unification_stmt) = &mut stmt.node {
let target = match unification_stmt.target.node.names.get(0) {
Some(name) => name,
None => bug!(
"Invalid AST unification target names {:?}",
unification_stmt.target.node.names
),
};
if target.node == self.target_id {
let item = unification_stmt.value.clone();
let mut value = self.clone_override_value();
// Use position information that needs to override the expression.
value.set_pos(item.pos());
let schema_expr = &mut unification_stmt.value.node;
match &self.operation {
ast::ConfigEntryOperation::Union => {
if let ast::Expr::Config(merged_config_expr) = &value.node {
if let ast::Expr::Config(config_expr) =
&mut schema_expr.config.node
{
self.has_override = merge_config_expr(
config_expr,
merged_config_expr,
&self.action,
);
}
} else if let ast::Expr::Schema(merged_schema_expr) =
&value.node
{
if schema_expr.name.node.get_name()
== merged_schema_expr.name.node.get_name()
{
if let (
ast::Expr::Config(merged_config_expr),
ast::Expr::Config(config_expr),
) = (
&merged_schema_expr.config.node,
&mut schema_expr.config.node,
) {
if self.field_paths.len() == 0 {
let target = match unification_stmt.target.node.names.get(0) {
Some(name) => name,
None => bug!(
"Invalid AST unification target names {:?}",
unification_stmt.target.node.names
),
};
if target.node == self.target_id {
let item = unification_stmt.value.clone();
let mut value = self.clone_override_value();
// Use position information that needs to override the expression.
value.set_pos(item.pos());
let schema_expr = &mut unification_stmt.value.node;
match &self.operation {
ast::ConfigEntryOperation::Union => {
if let ast::Expr::Config(merged_config_expr) = &value.node {
if let ast::Expr::Config(config_expr) =
&mut schema_expr.config.node
{
self.has_override = merge_config_expr(
config_expr,
merged_config_expr,
&self.action,
);
}
} else if let ast::Expr::Schema(merged_schema_expr) =
&value.node
{
if schema_expr.name.node.get_name()
== merged_schema_expr.name.node.get_name()
{
if let (
ast::Expr::Config(merged_config_expr),
ast::Expr::Config(config_expr),
) = (
&merged_schema_expr.config.node,
&mut schema_expr.config.node,
) {
self.has_override = merge_config_expr(
config_expr,
merged_config_expr,
&self.action,
);
}
}
} else {
// Unification is only support to override the schema expression.
if let ast::Expr::Schema(schema_expr) = value.node {
if self.field_paths.len() == 0 {
unification_stmt.value = Box::new(
ast::Node::dummy_node(schema_expr),
);
self.has_override = true;
}
}
}
} else {
}
ast::ConfigEntryOperation::Insert
| ast::ConfigEntryOperation::Override => {
// Unification is only support to override the schema expression.
if let ast::Expr::Schema(schema_expr) = value.node {
if self.field_paths.len() == 0 {
Expand All @@ -464,17 +478,6 @@ impl<'ctx> MutSelfMutWalker<'ctx> for OverrideTransformer {
}
}
}
ast::ConfigEntryOperation::Insert
| ast::ConfigEntryOperation::Override => {
// Unification is only support to override the schema expression.
if let ast::Expr::Schema(schema_expr) = value.node {
if self.field_paths.len() == 0 {
unification_stmt.value =
Box::new(ast::Node::dummy_node(schema_expr));
self.has_override = true;
}
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions kclvm/query/src/test_data/expect.k
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ uni_config = {

config_unification: Config {
"image": "image/image:v4"
env: {
"aaa": "aaa"
bbb: "bbb"
}
}
2 changes: 2 additions & 0 deletions kclvm/query/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn test_override_file_simple() {
"insert_config.key=1".to_string(),
"uni_config.labels.key1=1".to_string(),
"config_unification=Config {\"image\": \"image/image:v4\"}".to_string(),
"config_unification:Config {\"env\": {\"aaa\": \"aaa\"}}".to_string(),
"config_unification.env: {\"bbb\": \"bbb\"}}".to_string(),
"config_unification_delete-".to_string()
];

Expand Down

0 comments on commit f298eeb

Please sign in to comment.