Skip to content

Commit

Permalink
fix: fix missing variables in api ListVariable() when list from KCL w…
Browse files Browse the repository at this point in the history
…ith unsupported expr (#1315)

Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed May 15, 2024
1 parent b377d05 commit a933717
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
5 changes: 2 additions & 3 deletions kclvm/query/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ impl<'ctx> MutSelfWalker for Selector {
if key.is_empty() {
// chack the value of the config entry is supported
if self.check_node_supported(&item.node.value.node) {
self.inner.restore();
return;
continue;
}
}
// match the key with the selector
Expand All @@ -286,7 +285,7 @@ impl<'ctx> MutSelfWalker for Selector {
// If all the spec items are matched
// check and return
if self.check_node_supported(&item.node.value.node) {
return;
continue;
}
let kcode = print_ast_node(ASTNode::Expr(&item.node.value));
let type_name = if let ast::Expr::Schema(schema) = &item.node.value.node {
Expand Down
34 changes: 34 additions & 0 deletions kclvm/query/src/test_data/test_list_variables/unsupported.k
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,47 @@ dict = {str(i): 2 * i for i in range(3)}

func = lambda x: int, y: int -> int { x + y }

schema IfSchemaInner:
innerValue: int

schema IfSchema:
trueValue?: int
falseValue?: int
name: str
age: int
inner: IfSchemaInner
inner2: IfSchemaInner

if_schema = IfSchema {
if True :
trueValue: 1
else :
falseValue: 2

name: "name"
age: 1
inner: IfSchemaInner {
innerValue: 1
}

inner2: {
innerValue: 2
}
}

if_schema1 = {
if True :
trueValue: 1
else :
falseValue: 2

name: "name"
age: 1
inner: IfSchemaInner {
innerValue: 1
}

inner2: {
innerValue: 2
}
}
24 changes: 24 additions & 0 deletions kclvm/query/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ fn test_list_unsupported_variables() {
.unwrap()
.display()
.to_string();

// test unsupport code
let test_cases = vec![
("list", "[_x for _x in range(20) if _x % 2 == 0]"),
("list1", "[i if i > 2 else i + 1 for i in [1, 2, 3]]"),
Expand All @@ -482,6 +484,28 @@ fn test_list_unsupported_variables() {
assert_eq!(result.select_result.get(spec), None);
assert_eq!(result.unsupported[0].code, expected_code);
}

// test list variables from unsupported code
let test_cases = vec![
("if_schema.name", r#""name""#),
("if_schema.age", "1"),
("if_schema.inner", r#"IfSchemaInner {innerValue: 1}"#),
("if_schema.inner.innerValue", "1"),
("if_schema.inner2", r#"{innerValue: 2}"#),
("if_schema.inner2.innerValue", "2"),
("if_schema1.name", r#""name""#),
("if_schema1.age", "1"),
("if_schema1.inner", r#"IfSchemaInner {innerValue: 1}"#),
("if_schema1.inner.innerValue", "1"),
("if_schema1.inner2", r#"{innerValue: 2}"#),
("if_schema1.inner2.innerValue", "2"),
];

for (spec, expected_code) in test_cases {
let specs = vec![spec.to_string()];
let result = list_variables(file.clone(), specs).unwrap();
assert_eq!(result.select_result.get(spec).unwrap().value, expected_code);
}
}

#[test]
Expand Down

0 comments on commit a933717

Please sign in to comment.