Skip to content

Commit

Permalink
refactor: schema type APIs (kcl-lang#1368)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored May 27, 2024
1 parent 0c819ee commit 4c8bbc4
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 573 deletions.
28 changes: 0 additions & 28 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,6 @@ 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_all_full_schema_types() {
test_c_api::<GetFullSchemaTypeArgs, GetSchemaTypeResult, _>(
"KclvmService.GetFullSchemaType",
"get-all-full-schema-types.json",
"get-all-full-schema-types.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
40 changes: 0 additions & 40 deletions kclvm/api/src/service/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ pub(crate) fn kclvm_get_service_fn_ptr_by_name(name: &str) -> u64 {
"KclvmService.BuildProgram" => build_program as *const () as u64,
"KclvmService.ExecArtifact" => exec_artifact 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 @@ -419,44 +417,6 @@ pub(crate) fn override_file(
call!(serv, args, result_len, OverrideFileArgs, override_file)
}

/// Get schema types from a kcl file or code.
///
/// # Parameters
/// file: [&str]. The kcl filename.
///
/// code: [Option<&str>]. The kcl code string
///
/// schema_name: [Option<&str>]. The schema name, when the schema name is empty, all schemas are returned.
pub(crate) fn get_schema_type(
serv: *mut kclvm_service,
args: *const c_char,
result_len: *mut usize,
) -> *const c_char {
call!(serv, args, result_len, 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,
result_len: *mut usize,
) -> *const c_char {
call!(
serv,
args,
result_len,
GetFullSchemaTypeArgs,
get_full_schema_type
)
}

/// Get schema types from a kcl file or code.
///
/// # Parameters
Expand Down
16 changes: 0 additions & 16 deletions kclvm/api/src/service/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,6 @@ fn register_kclvm_service(io: &mut IoHandler) {
};
futures::future::ready(catch!(kclvm_service_impl, args, override_file))
});
io.add_method("KclvmService.GetSchemaType", |params: Params| {
let kclvm_service_impl = KclvmServiceImpl::default();
let args: GetSchemaTypeArgs = match params.parse() {
Ok(val) => val,
Err(err) => return futures::future::ready(Err(err)),
};
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
124 changes: 9 additions & 115 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use kclvm_parser::parse_file;
use kclvm_parser::KCLModuleCache;
use kclvm_parser::LoadProgramOptions;
use kclvm_parser::ParseSessionRef;
use kclvm_query::get_schema_type;
use kclvm_query::override_file;
use kclvm_query::query::get_full_schema_type;
use kclvm_query::query::CompilationOptions;
Expand Down Expand Up @@ -557,58 +556,7 @@ impl KclvmServiceImpl {
})
}

/// Service for getting the schema type list.
///
/// # Examples
///
/// ```
/// use kclvm_api::service::service_impl::KclvmServiceImpl;
/// use kclvm_api::gpyrpc::*;
///
/// let serv = KclvmServiceImpl::default();
/// let file = "schema.k".to_string();
/// let code = r#"
/// schema Person:
/// name: str
/// age: int
///
/// person = Person {
/// name = "Alice"
/// age = 18
/// }
/// "#.to_string();
/// let result = serv.get_schema_type(&GetSchemaTypeArgs {
/// file,
/// code,
/// ..Default::default()
/// }).unwrap();
/// assert_eq!(result.schema_type_list.len(), 2);
/// ```
pub fn get_schema_type(&self, args: &GetSchemaTypeArgs) -> anyhow::Result<GetSchemaTypeResult> {
let mut type_list = Vec::new();
for (_k, schema_ty) in get_schema_type(
&args.file,
if args.code.is_empty() {
None
} else {
Some(&args.code)
},
if args.schema_name.is_empty() {
None
} else {
Some(&args.schema_name)
},
Default::default(),
)? {
type_list.push(kcl_schema_ty_to_pb_ty(&schema_ty));
}

Ok(GetSchemaTypeResult {
schema_type_list: type_list,
})
}

/// Service for getting the full schema type list.
/// Service for getting the schema mapping.
///
/// # Examples
///
Expand All @@ -633,19 +581,19 @@ impl KclvmServiceImpl {
/// ..Default::default()
/// };
///
/// let result = serv.get_full_schema_type(&GetFullSchemaTypeArgs {
/// let result = serv.get_schema_type_mapping(&GetSchemaTypeMappingArgs {
/// exec_args: Some(args),
/// schema_name: "a".to_string()
/// ..Default::default()
/// }).unwrap();
/// assert_eq!(result.schema_type_list.len(), 1);
/// assert_eq!(result.schema_type_mapping.len(), 1);
/// ```
pub fn get_full_schema_type(
pub fn get_schema_type_mapping(
&self,
args: &GetFullSchemaTypeArgs,
) -> anyhow::Result<GetSchemaTypeResult> {
let mut type_list = Vec::new();
args: &GetSchemaTypeMappingArgs,
) -> anyhow::Result<GetSchemaTypeMappingResult> {
let mut type_mapping = HashMap::new();
let exec_args = transform_exec_para(&args.exec_args, self.plugin_agent)?;
for (_k, schema_ty) in get_full_schema_type(
for (k, schema_ty) in get_full_schema_type(
Some(&args.schema_name),
CompilationOptions {
k_files: exec_args.clone().k_filename_list,
Expand All @@ -656,60 +604,6 @@ impl KclvmServiceImpl {
},
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
///
/// ```
/// use kclvm_api::service::service_impl::KclvmServiceImpl;
/// use kclvm_api::gpyrpc::*;
///
/// let serv = KclvmServiceImpl::default();
/// let file = "schema.k".to_string();
/// let code = r#"
/// schema Person:
/// name: str
/// age: int
///
/// person = Person {
/// name = "Alice"
/// age = 18
/// }
/// "#.to_string();
/// let result = serv.get_schema_type_mapping(&GetSchemaTypeMappingArgs {
/// file,
/// code,
/// ..Default::default()
/// }).unwrap();
/// assert_eq!(result.schema_type_mapping.len(), 2);
/// ```
pub fn get_schema_type_mapping(
&self,
args: &GetSchemaTypeMappingArgs,
) -> anyhow::Result<GetSchemaTypeMappingResult> {
let mut type_mapping = HashMap::new();
for (k, schema_ty) in get_schema_type(
&args.file,
if args.code.is_empty() {
None
} else {
Some(&args.code)
},
if args.schema_name.is_empty() {
None
} else {
Some(&args.schema_name)
},
Default::default(),
)? {
type_mapping.insert(k, kcl_schema_ty_to_pb_ty(&schema_ty));
}
Expand Down
19 changes: 0 additions & 19 deletions kclvm/api/src/testdata/get-all-full-schema-types.json

This file was deleted.

78 changes: 0 additions & 78 deletions kclvm/api/src/testdata/get-all-full-schema-types.response.json

This file was deleted.

15 changes: 0 additions & 15 deletions kclvm/api/src/testdata/get-full-schema-type.json

This file was deleted.

Loading

0 comments on commit 4c8bbc4

Please sign in to comment.