Skip to content

Commit

Permalink
feat: add api 'get_full_schema_type' to return the schema ty with thi…
Browse files Browse the repository at this point in the history
…rd party (#906)

* feat: add api 'get_full_schema_type' to return the schema ty with third party

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

* fix: fix windows test case

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

* feat: fix CR comments

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

* fix: add missing api in service

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

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Nov 23, 2023
1 parent 2a2832c commit 6096c41
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 15 deletions.
7 changes: 7 additions & 0 deletions kclvm/Cargo.lock

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

14 changes: 14 additions & 0 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ fn test_c_api_call_override_file() {
);
}

#[test]
fn test_c_api_get_full_schema_type() {
test_c_api::<GetFullSchemaTypeArgs, GetSchemaTypeResult, _>(
"KclvmService.GetFullSchemaType",
"get-full-schema-type.json",
"get-full-schema-type.response.json",
|r| {
for s_ty in &mut r.schema_type_list {
s_ty.filename = s_ty.filename.replace("/", "").replace("\\", "")
}
},
);
}

#[test]
fn test_c_api_get_schema_type_mapping() {
test_c_api_without_wrapper::<GetSchemaTypeMappingArgs, GetSchemaTypeMappingResult>(
Expand Down
13 changes: 13 additions & 0 deletions kclvm/api/src/service/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub(crate) fn kclvm_get_service_fn_ptr_by_name(name: &str) -> u64 {
"KclvmService.ExecProgram" => exec_program as *const () as u64,
"KclvmService.OverrideFile" => override_file as *const () as u64,
"KclvmService.GetSchemaType" => get_schema_type as *const () as u64,
"KclvmService.GetFullSchemaType" => get_full_schema_type as *const () as u64,
"KclvmService.GetSchemaTypeMapping" => get_schema_type_mapping as *const () as u64,
"KclvmService.FormatCode" => format_code as *const () as u64,
"KclvmService.FormatPath" => format_path as *const () as u64,
Expand Down Expand Up @@ -180,6 +181,18 @@ pub(crate) fn get_schema_type(serv: *mut kclvm_service, args: *const c_char) ->
call!(serv, args, GetSchemaTypeArgs, get_schema_type)
}

/// Get full schema types from a kcl file or code.
///
/// # Parameters
/// `exec_args`: [Option<ExecProgramArgs>]
/// the items and compile parameters selected by the user in the KCL CLI
/// serialized as protobuf byte sequence
///
/// `schema_name`: [Option<&str>]. The schema name, when the schema name is empty, all schemas are returned.
pub(crate) fn get_full_schema_type(serv: *mut kclvm_service, args: *const c_char) -> *const c_char {
call!(serv, args, GetFullSchemaTypeArgs, get_full_schema_type)
}

/// Get schema types from a kcl file or code.
///
/// # Parameters
Expand Down
37 changes: 37 additions & 0 deletions kclvm/api/src/service/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ fn register_kclvm_service(io: &mut IoHandler) {
};
futures::future::ready(catch!(kclvm_service_impl, args, get_schema_type))
});
io.add_method("KclvmService.GetFullSchemaType", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: GetFullSchemaTypeArgs = match params.parse() {
Ok(val) => val,
Err(err) => return futures::future::ready(Err(err)),
};
futures::future::ready(catch!(kclvm_service_impl, args, get_full_schema_type))
});
io.add_method("KclvmService.GetSchemaTypeMapping", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: GetSchemaTypeMappingArgs = match params.parse() {
Expand Down Expand Up @@ -139,6 +147,30 @@ fn register_kclvm_service(io: &mut IoHandler) {
};
futures::future::ready(catch!(kclvm_service_impl, args, load_settings_files))
});
io.add_method("KclvmService.Rename", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: RenameArgs = match params.parse() {
Ok(val) => val,
Err(err) => return futures::future::ready(Err(err)),
};
futures::future::ready(catch!(kclvm_service_impl, args, rename))
});
io.add_method("KclvmService.RenameCode", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: RenameCodeArgs = match params.parse() {
Ok(val) => val,
Err(err) => return futures::future::ready(Err(err)),
};
futures::future::ready(catch!(kclvm_service_impl, args, rename_code))
});
io.add_method("KclvmService.Test", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: TestArgs = match params.parse() {
Ok(val) => val,
Err(err) => return futures::future::ready(Err(err)),
};
futures::future::ready(catch!(kclvm_service_impl, args, test))
});
}

fn register_builtin_service(io: &mut IoHandler) {
Expand All @@ -153,12 +185,17 @@ fn register_builtin_service(io: &mut IoHandler) {
"KclvmService.Ping".to_owned(),
"KclvmService.ExecProgram".to_owned(),
"KclvmService.OverrideFile".to_owned(),
"KclvmService.GetSchemaType".to_owned(),
"KclvmService.GetFullSchemaType".to_owned(),
"KclvmService.GetSchemaTypeMapping".to_owned(),
"KclvmService.FormatCode".to_owned(),
"KclvmService.FormatPath".to_owned(),
"KclvmService.LintPath".to_owned(),
"KclvmService.ValidateCode".to_owned(),
"KclvmService.LoadSettingsFiles".to_owned(),
"KclvmService.Rename".to_owned(),
"KclvmService.RenameCode".to_owned(),
"KclvmService.Test".to_owned(),
"BuiltinService.Ping".to_owned(),
"BuiltinService.PingListMethod".to_owned(),
],
Expand Down
61 changes: 61 additions & 0 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use kclvm_driver::canonicalize_input_files;
use kclvm_parser::ParseSession;
use kclvm_query::get_schema_type;
use kclvm_query::override_file;
use kclvm_query::query::get_full_schema_type;
use kclvm_query::query::CompilationOptions;
use kclvm_query::GetSchemaOption;
use kclvm_runner::exec_program;
use kclvm_sema::resolver::Options;
use kclvm_tools::format::{format, format_source, FormatOptions};
use kclvm_tools::lint::lint_files;
use kclvm_tools::testing;
Expand Down Expand Up @@ -201,6 +205,63 @@ impl KclvmServiceImpl {
})
}

/// Service for getting the full schema type list.
///
/// # Examples
///
/// ```
/// use kclvm_api::service::service_impl::KclvmServiceImpl;
/// use kclvm_api::gpyrpc::*;
/// use std::path::Path;
///
/// let serv = KclvmServiceImpl::default();
/// let work_dir_parent = Path::new(".").join("src").join("testdata").join("get_schema_ty");
/// let args = ExecProgramArgs {
/// work_dir: work_dir_parent.join("aaa").canonicalize().unwrap().display().to_string(),
/// k_filename_list: vec![
/// work_dir_parent.join("aaa").join("main.k").canonicalize().unwrap().display().to_string()
/// ],
/// external_pkgs: vec![
/// CmdExternalPkgSpec{
/// pkg_name:"bbb".to_string(),
/// pkg_path: work_dir_parent.join("bbb").canonicalize().unwrap().display().to_string()
/// }
/// ],
/// ..Default::default()
/// };
///
/// let result = serv.get_full_schema_type(&GetFullSchemaTypeArgs {
/// exec_args: Some(args),
/// schema_name: "a".to_string()
/// }).unwrap();
/// assert_eq!(result.schema_type_list.len(), 1);
/// ```
pub fn get_full_schema_type(
&self,
args: &GetFullSchemaTypeArgs,
) -> anyhow::Result<GetSchemaTypeResult> {
let args_json = serde_json::to_string(&args.exec_args.clone().unwrap()).unwrap();

let mut type_list = Vec::new();

let exec_args = kclvm_runner::ExecProgramArgs::from_str(args_json.as_str());
for (_k, schema_ty) in get_full_schema_type(
Some(&args.schema_name),
CompilationOptions {
k_files: exec_args.clone().k_filename_list,
loader_opts: Some(exec_args.get_load_program_options()),
resolve_opts: Options::default(),
get_schema_opts: GetSchemaOption::default(),
},
)? {
type_list.push(kcl_schema_ty_to_pb_ty(&schema_ty));
}

Ok(GetSchemaTypeResult {
schema_type_list: type_list,
})
}

/// Service for getting the schema mapping.
///
/// # Examples
Expand Down
15 changes: 15 additions & 0 deletions kclvm/api/src/testdata/get-full-schema-type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"exec_args": {
"work_dir" : "./src/testdata/get_schema_ty/aaa",
"k_filename_list":[
"./src/testdata/get_schema_ty/aaa/main.k"
],
"external_pkgs": [
{
"pkg_name": "bbb",
"pkg_path": "./src/testdata/get_schema_ty/bbb"
}
]
},
"schema_name": "a"
}
41 changes: 41 additions & 0 deletions kclvm/api/src/testdata/get-full-schema-type.response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"schema_type_list": [
{
"type": "schema",
"union_types": [],
"default": "",
"schema_name": "B",
"schema_doc": "",
"properties": {
"name": {
"type": "str",
"union_types": [],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"key": null,
"item": null,
"line": 1,
"decorators": [],
"filename": "",
"pkg_path": "",
"description": "",
"examples": {}
}
},
"required": [
"name"
],
"key": null,
"item": null,
"line": 0,
"decorators": [],
"filename": "./src/testdata/get_schema_ty/bbb/main.k",
"pkg_path": "bbb",
"description": "",
"examples": {}
}
]
}
5 changes: 5 additions & 0 deletions kclvm/api/src/testdata/get_schema_ty/aaa/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"

5 changes: 5 additions & 0 deletions kclvm/api/src/testdata/get_schema_ty/aaa/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import bbb as b

a = b.B {
name: "b instance in a"
}
5 changes: 5 additions & 0 deletions kclvm/api/src/testdata/get_schema_ty/bbb/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "bbb"
edition = "0.0.1"
version = "0.0.1"

2 changes: 2 additions & 0 deletions kclvm/api/src/testdata/get_schema_ty/bbb/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema B:
name: str
1 change: 1 addition & 0 deletions kclvm/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kclvm-ast-pretty = {path = "../ast_pretty"}
kclvm-parser = {path = "../parser"}
kclvm-sema = {path = "../sema"}
kclvm-error = {path = "../error"}
maplit = "1.0.2"

[dev-dependencies]
pretty_assertions = "1.2.1"
Loading

0 comments on commit 6096c41

Please sign in to comment.