Skip to content

Commit

Permalink
feat: support return list and dict structure from api ListVariable (#…
Browse files Browse the repository at this point in the history
…1349)

* feat: support return list and dict structure from api ListVariable

Signed-off-by: zongz <[email protected]>

* fix: rm un-used import

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored May 22, 2024
1 parent 017610b commit f90939f
Show file tree
Hide file tree
Showing 63 changed files with 11,125 additions and 70 deletions.
3 changes: 3 additions & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ pub struct KclvmServiceImpl {
pub plugin_agent: u64,
}

impl From<&kclvm_query::selector::Variable> for Variable {
fn from(var: &kclvm_query::selector::Variable) -> Self {
Variable {
value: var.value.to_string(),
type_name: var.type_name.to_string(),
op_sym: var.op_sym.to_string(),
list_items: var.list_items.iter().map(|item| item.into()).collect(),
dict_entries: var
.dict_entries
.iter()
.map(|entry| MapEntry {
key: entry.key.to_string(),
value: Some((&entry.value).into()),
})
.collect(),
}
}
}

impl KclvmServiceImpl {
/// Ping KclvmService, return the same value as the parameter
///
Expand Down Expand Up @@ -349,16 +368,7 @@ impl KclvmServiceImpl {
let variables: HashMap<String, Variable> = select_res
.variables
.iter()
.map(|(key, var)| {
(
key.clone(),
Variable {
value: var.value.to_string(),
type_name: var.type_name.to_string(),
op_sym: var.op_sym.to_string(),
},
)
})
.map(|(key, var)| (key.clone(), var.into()))
.collect();

let unsupported_codes: Vec<String> = select_res
Expand Down
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/list-variables.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"file": "./src/testdata/variables/main.k",
"specs": ["a"]
"specs": ["a", "b", "c"]
}
61 changes: 60 additions & 1 deletion kclvm/api/src/testdata/list-variables.response.json
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
{"variables":{"a":{"value":"1","type_name":"","op_sym":"="}},"unsupported_codes":[]}
{
"variables": {
"a": {
"value": "1",
"type_name": "",
"op_sym": "=",
"list_items": [],
"dict_entries": []
},
"c": {
"value": "{\"a\": \"b\"}",
"type_name": "",
"op_sym": "=",
"list_items": [],
"dict_entries": [
{
"key": "a",
"value": {
"value": "\"b\"",
"type_name": "",
"op_sym": ":",
"list_items": [],
"dict_entries": []
}
}
]
},
"b": {
"value": "[1, 2, 3]",
"type_name": "",
"op_sym": "=",
"list_items": [
{
"value": "1",
"type_name": "",
"op_sym": "",
"list_items": [],
"dict_entries": []
},
{
"value": "2",
"type_name": "",
"op_sym": "",
"list_items": [],
"dict_entries": []
},
{
"value": "3",
"type_name": "",
"op_sym": "",
"list_items": [],
"dict_entries": []
}
],
"dict_entries": []
}
},
"unsupported_codes": [],
"parse_errors": []
}
4 changes: 3 additions & 1 deletion kclvm/api/src/testdata/variables/main.k
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
a = 1
a = 1
b = [1, 2, 3]
c = {"a": "b"}
1 change: 1 addition & 0 deletions kclvm/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ anyhow = "1.0"
tracing = "0.1"
atty = "0.2"
annotate-snippets = { version = "0.9.2", default-features = false, features = ["color"] }
serde = { version = "1.0", features = ["derive"] }
termize = "0.1.1"
indexmap = "1.0"
serde_json = "1.0"
2 changes: 2 additions & 0 deletions kclvm/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ kclvm-ast-pretty = {path = "../ast_pretty"}
kclvm-parser = {path = "../parser"}
kclvm-sema = {path = "../sema"}
kclvm-error = {path = "../error"}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
maplit = "1.0.2"

[dev-dependencies]
Expand Down
18 changes: 18 additions & 0 deletions kclvm/query/src/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
a = [1, 2, 3, 4]
b = {
"c": "d",
}

_part1 = {
a = "b"
}

_part2 = {
c = "d"
}

_list0 = [1, 2, 3]
_list1 = [4, 5, 6]
union_list = [*_list0, *_list1]

a_dict = {**_part1, **_part2} # {"a: "b", "c": "d"}
Loading

0 comments on commit f90939f

Please sign in to comment.