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: support return list and dict structure from api ListVariable #1349

Merged
merged 2 commits into from
May 22, 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
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.

31 changes: 21 additions & 10 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::string::String;
use std::vec;
Peefy marked this conversation as resolved.
Show resolved Hide resolved

use crate::gpyrpc::*;

Expand Down Expand Up @@ -45,6 +46,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 +369,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
Loading