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

feat: add operation for variable in ListVariables #1282

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl KclvmServiceImpl {
Variable {
value: var.value.to_string(),
type_name: var.type_name.to_string(),
op_sym: var.op_sym.to_string(),
},
)
})
Expand Down
47 changes: 38 additions & 9 deletions kclvm/query/src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ impl<'ctx> MutSelfWalker for Selector {

self.select_result.insert(
target.to_string(),
Variable::new(unification_stmt.value.node.name.node.get_name(), kcode),
Variable::new(
unification_stmt.value.node.name.node.get_name(),
ast::ConfigEntryOperation::Union.symbol().to_string(),
kcode,
),
);
} else {
// if length of spec is largr or equal to target
Expand All @@ -165,7 +169,11 @@ impl<'ctx> MutSelfWalker for Selector {
))));
self.select_result.insert(
target.to_string(),
Variable::new(unification_stmt.value.node.name.node.get_name(), kcode),
Variable::new(
unification_stmt.value.node.name.node.get_name(),
ast::ConfigEntryOperation::Union.symbol().to_string(),
kcode,
),
);
} else {
// walk ahead
Expand Down Expand Up @@ -201,8 +209,14 @@ impl<'ctx> MutSelfWalker for Selector {
target.node.clone(),
))));
let key = get_key_path(&target);
self.select_result
.insert(key.to_string(), Variable::new(type_name, kcode));
self.select_result.insert(
key.to_string(),
Variable::new(
type_name,
ast::ConfigEntryOperation::Override.symbol().to_string(),
kcode,
),
);
}
} else {
// Compare the target with the spec
Expand Down Expand Up @@ -230,8 +244,14 @@ impl<'ctx> MutSelfWalker for Selector {
} else {
"".to_string()
};
self.select_result
.insert(target.to_string(), Variable::new(type_name, kcode));
self.select_result.insert(
target.to_string(),
Variable::new(
type_name,
ast::ConfigEntryOperation::Override.symbol().to_string(),
kcode,
),
);
} else {
// walk ahead
self.walk_expr(&assign_stmt.value.node)
Expand Down Expand Up @@ -276,7 +296,11 @@ impl<'ctx> MutSelfWalker for Selector {
};
self.select_result.insert(
self.inner.current_spec.to_string(),
Variable::new(type_name, kcode),
Variable::new(
type_name,
item.node.operation.symbol().to_string(),
kcode,
),
);
} else {
// the spec is still not used up
Expand Down Expand Up @@ -367,12 +391,17 @@ pub struct ListVariablesResult {
#[derive(Debug, PartialEq)]
pub struct Variable {
pub type_name: String,
pub op_sym: String,
pub value: String,
}

impl Variable {
pub fn new(type_name: String, value: String) -> Self {
Self { type_name, value }
pub fn new(type_name: String, op_sym: String, value: String) -> Self {
Self {
type_name,
op_sym,
value,
}
}
}

Expand Down
88 changes: 54 additions & 34 deletions kclvm/query/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ fn test_list_variables() {
.display()
.to_string();
let test_cases = vec![
("a", "1", ""),
("a1", "2", ""),
("a3", "3m", ""),
("b1", "True", ""),
("b2", "False", ""),
("s1", "\"Hello\"", ""),
("array1", "[1, 2, 3]", ""),
("dict1", "{\"a\": 1, \"b\": 2}", ""),
("dict1.a", "1", ""),
("dict1.b", "2", ""),
("a", "1", "", "="),
("a1", "2", "", "="),
("a3", "3m", "", "="),
("b1", "True", "", "="),
("b2", "False", "", "="),
("s1", "\"Hello\"", "", "="),
("array1", "[1, 2, 3]", "", "="),
("dict1", "{\"a\": 1, \"b\": 2}", "", "="),
("dict1.a", "1", "", ":"),
("dict1.b", "2", "", ":"),
(
"dict2",
r#"{
Expand All @@ -245,18 +245,20 @@ fn test_list_variables() {
}
}"#,
"",
"=",
),
("dict2.a", "1", ""),
("dict2.a", "1", "", ":"),
(
"dict2.b",
r#"{
"c": 2
"d": 3
}"#,
"",
":",
),
("dict2.b.c", "2", ""),
("dict2.b.d", "3", ""),
("dict2.b.c", "2", "", ":"),
("dict2.b.d", "3", "", ":"),
(
"sha",
r#"A {
Expand All @@ -269,9 +271,10 @@ fn test_list_variables() {
}
}"#,
"A",
"=",
),
("sha.name", "\"Hello\"", ""),
("sha.ids", "[1, 2, 3]", ""),
("sha.name", "\"Hello\"", "", ":"),
("sha.ids", "[1, 2, 3]", "", ":"),
(
"sha.data",
r#"{
Expand All @@ -280,16 +283,18 @@ fn test_list_variables() {
}
}"#,
"",
":",
),
(
"sha.data.a",
r#"{
"b": {"c": 2}
}"#,
"",
":",
),
("sha.data.a.b", r#"{"c": 2}"#, ""),
("sha.data.a.b.c", "2", ""),
("sha.data.a.b", r#"{"c": 2}"#, "", ":"),
("sha.data.a.b.c", "2", "", ":"),
(
"shb",
r#"B {
Expand All @@ -304,6 +309,7 @@ fn test_list_variables() {
}
}"#,
"B",
"=",
),
(
"shb.a",
Expand All @@ -317,9 +323,10 @@ fn test_list_variables() {
}
}"#,
"",
":",
),
("shb.a.name", "\"HelloB\"", ""),
("shb.a.ids", "[4, 5, 6]", ""),
("shb.a.name", "\"HelloB\"", "", ":"),
("shb.a.ids", "[4, 5, 6]", "", ":"),
(
"shb.a.data",
r#"{
Expand All @@ -328,28 +335,36 @@ fn test_list_variables() {
}
}"#,
"",
":",
),
(
"shb.a.data.d",
r#"{
"e": {"f": 3}
}"#,
"",
":",
),
("shb.a.data.d.e", "{\"f\": 3}", "", ":"),
("uconfa.name", "\"b\"", "", "="),
("c.a", "{ids: [7, 8, 9]}", "", ":"),
(
"job.name",
r#""{}-{}".format("app", "test").lower()"#,
"",
"=",
),
("shb.a.data.d.e", "{\"f\": 3}", ""),
("uconfa.name", "\"b\"", ""),
("c.a", "{ids: [7, 8, 9]}", ""),
("job.name", r#""{}-{}".format("app", "test").lower()"#, ""),
];

for (spec, expected, expected_name) in test_cases {
for (spec, expected, expected_name, op_sym) 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);
assert_eq!(
result.select_result.get(spec).unwrap().type_name,
expected_name
);
assert_eq!(result.select_result.get(spec).unwrap().op_sym, op_sym);
}
}

Expand All @@ -361,14 +376,14 @@ fn test_list_all_variables() {
.display()
.to_string();
let test_cases = vec![
("a", "1", ""),
("a1", "2", ""),
("a3", "3m", ""),
("b1", "True", ""),
("b2", "False", ""),
("s1", "\"Hello\"", ""),
("array1", "[1, 2, 3]", ""),
("dict1", "{\"a\": 1, \"b\": 2}", ""),
("a", "1", "", "="),
("a1", "2", "", "="),
("a3", "3m", "", "="),
("b1", "True", "", "="),
("b2", "False", "", "="),
("s1", "\"Hello\"", "", "="),
("array1", "[1, 2, 3]", "", "="),
("dict1", "{\"a\": 1, \"b\": 2}", "", "="),
(
"dict2",
r#"{
Expand All @@ -379,6 +394,7 @@ fn test_list_all_variables() {
}
}"#,
"",
"=",
),
(
"sha",
Expand All @@ -392,6 +408,7 @@ fn test_list_all_variables() {
}
}"#,
"A",
"=",
),
(
"shb",
Expand All @@ -407,22 +424,25 @@ fn test_list_all_variables() {
}
}"#,
"B",
"=",
),
(
"job",
r#"Job {name = "{}-{}".format("app", "test").lower()}"#,
"Job",
"=",
),
("select", r#"a.b.c {a: 1}"#, "a.b.c"),
("select", r#"a.b.c {a: 1}"#, "a.b.c", "="),
];

for (spec, expected, expected_name) in test_cases {
for (spec, expected, expected_name, op_sym) in test_cases {
let result = list_variables(file.clone(), vec![]).unwrap();
assert_eq!(result.select_result.get(spec).unwrap().value, expected);
assert_eq!(
result.select_result.get(spec).unwrap().type_name,
expected_name
);
assert_eq!(result.select_result.get(spec).unwrap().op_sym, op_sym);
}
}

Expand Down
1 change: 1 addition & 0 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ message ListVariables_Result {
message Variable {
string value = 1;
string type_name = 2;
string op_sym = 3;
}

message GetFullSchemaType_Args {
Expand Down
Loading