Skip to content

Commit eb61d84

Browse files
authored
feat: add service api GetSchemaTypeMappingUnderPath (#1766)
* feat: add service api GetSchemaTypeMappingUnderPath Signed-off-by: he1pa <[email protected]> * fix typo Signed-off-by: he1pa <[email protected]> --------- Signed-off-by: he1pa <[email protected]>
1 parent 18caaca commit eb61d84

File tree

12 files changed

+161
-2
lines changed

12 files changed

+161
-2
lines changed

kclvm/api/src/service/capi.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ pub(crate) fn kclvm_get_service_fn_ptr_by_name(name: &str) -> u64 {
171171
"KclvmService.ExecArtifact" => exec_artifact as *const () as u64,
172172
"KclvmService.OverrideFile" => override_file as *const () as u64,
173173
"KclvmService.GetSchemaTypeMapping" => get_schema_type_mapping as *const () as u64,
174+
"KclvmService.GetSchemaTypeMappingUnderPath" => {
175+
get_schema_type_mapping_under_path as *const () as u64
176+
}
174177
"KclvmService.FormatCode" => format_code as *const () as u64,
175178
"KclvmService.FormatPath" => format_path as *const () as u64,
176179
"KclvmService.LintPath" => lint_path as *const () as u64,
@@ -523,6 +526,30 @@ pub(crate) fn get_schema_type_mapping(
523526
)
524527
}
525528

529+
/// Get schema types under path
530+
///
531+
/// # Parameters
532+
/// file: [&str]. The kcl filename.
533+
///
534+
/// code: [Option<&str>]. The kcl code string
535+
///
536+
/// schema_name: [Option<&str>]. The schema name, when the schema name is empty, all schemas are returned.
537+
pub(crate) fn get_schema_type_mapping_under_path(
538+
serv: *mut kclvm_service,
539+
args: *const c_char,
540+
args_len: usize,
541+
result_len: *mut usize,
542+
) -> *const c_char {
543+
call!(
544+
serv,
545+
args,
546+
args_len,
547+
result_len,
548+
GetSchemaTypeMappingArgs,
549+
get_schema_type_mapping_under_path
550+
)
551+
}
552+
526553
/// Service for formatting a code source and returns the formatted source and
527554
/// whether the source is changed.
528555
pub(crate) fn format_code(

kclvm/api/src/service/jsonrpc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ fn register_kclvm_service(io: &mut IoHandler) {
157157
};
158158
futures::future::ready(catch!(kclvm_service_impl, args, get_schema_type_mapping))
159159
});
160+
io.add_method(
161+
"KclvmService.GetSchemaTypeMappingUnderPath",
162+
|params: Params| {
163+
let kclvm_service_impl = KclvmServiceImpl::default();
164+
let args: GetSchemaTypeMappingArgs = match params.parse() {
165+
Ok(val) => val,
166+
Err(err) => return futures::future::ready(Err(err)),
167+
};
168+
futures::future::ready(catch!(
169+
kclvm_service_impl,
170+
args,
171+
get_schema_type_mapping_under_path
172+
))
173+
},
174+
);
160175
io.add_method("KclvmService.FormatCode", |params: Params| {
161176
let kclvm_service_impl = KclvmServiceImpl::default();
162177
let args: FormatCodeArgs = match params.parse() {

kclvm/api/src/service/service_impl.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::path::PathBuf;
44
use std::string::String;
55

6-
use crate::gpyrpc::*;
6+
use crate::gpyrpc::{self, *};
77

88
use kcl_language_server::rename;
99
use kclvm_ast::ast::SerializeProgram;
@@ -17,8 +17,8 @@ use kclvm_parser::KCLModuleCache;
1717
use kclvm_parser::LoadProgramOptions;
1818
use kclvm_parser::ParseSessionRef;
1919
use kclvm_query::override_file;
20-
use kclvm_query::query::get_full_schema_type;
2120
use kclvm_query::query::CompilationOptions;
21+
use kclvm_query::query::{get_full_schema_type, get_full_schema_type_under_path};
2222
use kclvm_query::selector::{list_variables, ListOptions};
2323
use kclvm_query::GetSchemaOption;
2424
use kclvm_runner::exec_program;
@@ -666,6 +666,74 @@ impl KclvmServiceImpl {
666666
})
667667
}
668668

669+
/// Service for getting the schema mapping under path.
670+
///
671+
/// # Examples
672+
///
673+
/// ```
674+
/// use kclvm_api::service::service_impl::KclvmServiceImpl;
675+
/// use kclvm_api::gpyrpc::*;
676+
/// use std::path::Path;
677+
/// use kclvm_ast::MAIN_PKG;
678+
///
679+
/// let serv = KclvmServiceImpl::default();
680+
/// let work_dir_parent = Path::new(".").join("src").join("testdata").join("get_schema_ty_under_path");
681+
/// let args = ExecProgramArgs {
682+
/// k_filename_list: vec![
683+
/// work_dir_parent.join("aaa").canonicalize().unwrap().display().to_string()
684+
/// ],
685+
/// external_pkgs: vec![
686+
/// ExternalPkg {
687+
/// pkg_name:"bbb".to_string(),
688+
/// pkg_path: work_dir_parent.join("bbb").canonicalize().unwrap().display().to_string()
689+
/// },
690+
/// ExternalPkg {
691+
/// pkg_name:"helloworld".to_string(),
692+
/// pkg_path: work_dir_parent.join("helloworld_0.0.1").canonicalize().unwrap().display().to_string()
693+
/// },
694+
/// ],
695+
/// ..Default::default()
696+
/// };
697+
///
698+
/// let result = serv.get_schema_type_mapping_under_path(&GetSchemaTypeMappingArgs {
699+
/// exec_args: Some(args),
700+
/// ..Default::default()
701+
/// }).unwrap();
702+
/// assert_eq!(result.schema_type_mapping.get(MAIN_PKG).unwrap().schema_type.len(), 1);
703+
/// assert_eq!(result.schema_type_mapping.get("bbb").unwrap().schema_type.len(), 2);
704+
/// assert_eq!(result.schema_type_mapping.get("helloworld").unwrap().schema_type.len(), 1);
705+
/// assert_eq!(result.schema_type_mapping.get("sub").unwrap().schema_type.len(), 1);
706+
/// ```
707+
pub fn get_schema_type_mapping_under_path(
708+
&self,
709+
args: &GetSchemaTypeMappingArgs,
710+
) -> anyhow::Result<GetSchemaTypeMappingUnderPathResult> {
711+
let mut type_mapping = HashMap::new();
712+
let exec_args = transform_exec_para(&args.exec_args, self.plugin_agent)?;
713+
for (k, schema_tys) in get_full_schema_type_under_path(
714+
Some(&args.schema_name),
715+
CompilationOptions {
716+
paths: exec_args.clone().k_filename_list,
717+
loader_opts: Some(exec_args.get_load_program_options()),
718+
resolve_opts: Options {
719+
resolve_val: true,
720+
..Default::default()
721+
},
722+
get_schema_opts: GetSchemaOption::Definitions,
723+
},
724+
)? {
725+
let mut tys = vec![];
726+
for schema_ty in schema_tys {
727+
tys.push(kcl_schema_ty_to_pb_ty(&schema_ty));
728+
}
729+
type_mapping.insert(k, gpyrpc::SchemaTypes { schema_type: tys });
730+
}
731+
732+
Ok(GetSchemaTypeMappingUnderPathResult {
733+
schema_type_mapping: type_mapping,
734+
})
735+
}
736+
669737
/// Service for formatting a code source and returns the formatted source and
670738
/// whether the source is changed.
671739
///
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "aaa"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
bbb = { path = "../bbb" }
8+
helloworld = "0.0.1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import bbb
2+
3+
schema A:
4+
name: str
5+
6+
a = bbb.B {
7+
name: "b instance in a"
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
schema Sub:
2+
name: str
3+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "bbb"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
schema Base:
2+
n: str
3+
schema B(Base):
4+
name: str
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Introduction
2+
This is a kcl package named helloworld.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "helloworld"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+

0 commit comments

Comments
 (0)