Skip to content

Commit

Permalink
get_schema_type function will now return SchemaType instances wit…
Browse files Browse the repository at this point in the history
…h base schema information included (#1272)

* `get_schema_type` and `get_full_schema_type` will now return `SchemaType` instance

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

* added  the recursive function

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

* fixed the types

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

---------

Signed-off-by: utnim2 <mintugogoi567@gmail.com>
  • Loading branch information
Gmin2 committed May 4, 2024
1 parent e023195 commit 5f8ccd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions kclvm/api/src/service/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub(crate) fn kcl_schema_ty_to_pb_ty(schema_ty: &SchemaType) -> KclType {
filename: schema_ty.filename.clone(),
pkg_path: schema_ty.pkgpath.clone(),
description: schema_ty.doc.clone(),
base_schema: schema_ty
.base
.as_ref()
.map(|base| Box::new(kcl_schema_ty_to_pb_ty(&**base))),
..Default::default()
}
}
Expand Down
13 changes: 12 additions & 1 deletion kclvm/query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ pub fn get_full_schema_type(
let scope = resolve_file(&opts)?;
for (name, o) in &scope.borrow().elems {
if o.borrow().ty.is_schema() {
let schema_ty = o.borrow().ty.into_schema_type();
let mut schema_ty = o.borrow().ty.into_schema_type();
if let Some(base) = &schema_ty.base {
schema_ty.base = Some(Box::new(get_full_schema_type_recursive(*base.clone())?));
}
if opts.get_schema_opts == GetSchemaOption::All
|| (opts.get_schema_opts == GetSchemaOption::Definitions && !schema_ty.is_instance)
|| (opts.get_schema_opts == GetSchemaOption::Instances && schema_ty.is_instance)
Expand All @@ -186,6 +189,14 @@ pub fn get_full_schema_type(
Ok(result)
}

fn get_full_schema_type_recursive(schema_ty: SchemaType) -> Result<SchemaType> {
let mut result = schema_ty;
if let Some(base) = result.base {
result.base = Some(Box::new(get_full_schema_type_recursive(*base)?));
}
Ok(result)
}

fn resolve_file(opts: &CompilationOptions) -> Result<Rc<RefCell<Scope>>> {
let sess = Arc::new(ParseSession::default());
let mut program = match load_program(
Expand Down
1 change: 1 addition & 0 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ message KclType {
string pkg_path = 13; // `pkg_path` represents the path name of the package where the attribute is located.
string description = 14; // `description` represents the document of the attribute.
map<string, Example> examples = 15; // A map object to hold examples, the key is the example name.
KclType base_schema = 16;
}

message Decorator {
Expand Down

0 comments on commit 5f8ccd3

Please sign in to comment.