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: enhance get schema type API with default value. #658

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions kclvm/api/src/service/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn get_schema_ty_attributes(schema_ty: &SchemaType, line: &mut i32) -> HashMap<S
keywords: d.keywords.clone(),
})
.collect();
ty.default = attr.default.clone().unwrap_or_default();
type_mapping.insert(key.to_string(), ty);
*line += 1
}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/get-schema-type-mapping.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"file": "schema.k",
"code": "@info(name=\"ServerSchema\")\nschema Server:\n \"\"\"Server is the common user interface for long-running\n services adopting the best practice of Kubernetes.\n\n Attributes\n ----------\n workloadType: str, default is \"Deployment\", required\n Use this attribute to specify which kind of long-running service you want.\n Valid values: Deployment, CafeDeployment.\n See also: kusion_models/core/v1/workload_metadata.k.\n name: str, required\n A Server-level attribute.\n The name of the long-running service.\n See also: kusion_models/core/v1/metadata.k.\n labels: {str:str}, optional\n A Server-level attribute.\n The labels of the long-running service.\n See also: kusion_models/core/v1/metadata.k.\n\n Examples\n ----------------------\n myCustomApp = AppConfiguration {\n name = \"componentName\"\n }\n \"\"\"\n workloadType: str = \"Deployment\"\n @info(\"name\", key=\"value\")\n name: str\n labels?: {str:str}\n containers: [Container]\n\nschema Container:\n \"\"\"Container is the common user interface for long-running services.\n\n Attributes\n ----------\n name: str, required\n The name of the long-running container.\n \"\"\"\n name: str\n"
"code": "@info(name=\"ServerSchema\")\nschema Server:\n \"\"\"Server is the common user interface for long-running\n services adopting the best practice of Kubernetes.\n\n Attributes\n ----------\n workloadType: str, default is \"Deployment\", required\n Use this attribute to specify which kind of long-running service you want.\n Valid values: Deployment, CafeDeployment.\n See also: kusion_models/core/v1/workload_metadata.k.\n name: str, required\n A Server-level attribute.\n The name of the long-running service.\n See also: kusion_models/core/v1/metadata.k.\n labels: {str:str}, optional\n A Server-level attribute.\n The labels of the long-running service.\n See also: kusion_models/core/v1/metadata.k.\n\n Examples\n ----------------------\n myCustomApp = AppConfiguration {\n name = \"componentName\"\n }\n \"\"\"\n workloadType: str = \"Deployment\"\n @info(\"name\", key=\"value\")\n name: str\n labels?: {str:str}\n useCustomizeLables: True | False\n containers: [Container]\n\nschema Container:\n \"\"\"Container is the common user interface for long-running services.\n\n Attributes\n ----------\n name: str, required\n The name of the long-running container.\n \"\"\"\n name: str\n"
}
51 changes: 49 additions & 2 deletions kclvm/api/src/testdata/get-schema-type-mapping.response.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"description": "Container is the common user interface for long-running services.",
"examples": {}
},
"line": 4,
"line": 5,
"decorators": [],
"filename": "",
"pkg_path": "",
Expand Down Expand Up @@ -83,7 +83,7 @@
"workloadType": {
"type": "str",
"union_types": [],
"default": "",
"default": "\"Deployment\"",
"schema_name": "",
"schema_doc": "",
"properties": {},
Expand Down Expand Up @@ -139,11 +139,58 @@
"pkg_path": "",
"description": "A Server-level attribute.\nThe labels of the long-running service.\nSee also: kusion_models/core/v1/metadata.k.",
"examples": {}
},
"useCustomizeLables": {
"type": "union",
"union_types": [
{
"type": "bool(True)",
"union_types": [],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"line": 0,
"decorators": [],
"filename": "",
"pkg_path": "",
"description": "",
"examples": {}
},
{
"type": "bool(False)",
"union_types": [],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"line": 0,
"decorators": [],
"filename": "",
"pkg_path": "",
"description": "",
"examples": {}
}
],
"default": "",
"schema_name": "",
"schema_doc": "",
"properties": {},
"required": [],
"line": 4,
"decorators": [],
"filename": "",
"pkg_path": "",
"description": "",
"examples": {}
}
},
"required": [
"workloadType",
"name",
"useCustomizeLables",
"containers"
],
"line": 0,
Expand Down
7 changes: 7 additions & 0 deletions kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,10 @@ pub fn print_ast_node(node: ASTNode) -> String {
printer.write_node(node);
printer.out
}

/// Print schema expression AST node to string.
pub fn print_schema_expr(schema_expr: &ast::SchemaExpr) -> String {
let mut printer = Printer::default();
printer.walk_schema_expr(schema_expr);
printer.out
}
18 changes: 12 additions & 6 deletions kclvm/sema/src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::ty::{
use indexmap::IndexMap;
use kclvm_ast::ast;
use kclvm_ast::walker::MutSelfTypedResultWalker;
use kclvm_ast_pretty::{print_ast_node, print_schema_expr, ASTNode};
use kclvm_error::*;

use super::doc::parse_doc_string;
Expand Down Expand Up @@ -582,6 +583,7 @@ impl<'ctx> Resolver<'ctx> {
SchemaAttr {
is_optional: true,
has_default: false,
default: None,
ty: Type::dict_ref(self.str_ty(), self.any_ty()),
pos: Position {
filename: self.ctx.filename.clone(),
Expand All @@ -594,17 +596,17 @@ impl<'ctx> Resolver<'ctx> {
);
let parsed_doc = parse_doc_string(&schema_stmt.doc);
for stmt in &schema_stmt.body {
let (name, ty, is_optional, has_default, decorators) = match &stmt.node {
let (name, ty, is_optional, default, decorators) = match &stmt.node {
ast::Stmt::Unification(unification_stmt) => {
let name = unification_stmt.value.node.name.node.get_name();
let ty = self.parse_ty_str_with_scope(&name, stmt.get_span_pos());
let is_optional = true;
let has_default = true;
let default = print_schema_expr(&unification_stmt.value.node);
(
unification_stmt.target.node.get_name(),
ty,
is_optional,
has_default,
Some(default),
vec![],
)
}
Expand All @@ -615,14 +617,17 @@ impl<'ctx> Resolver<'ctx> {
schema_attr.ty.get_span_pos(),
);
let is_optional = schema_attr.is_optional;
let has_default = schema_attr.value.is_some();
let default = schema_attr
.value
.as_ref()
.map(|v| print_ast_node(ASTNode::Expr(v)));
// Schema attribute decorators
let decorators = self.resolve_decorators(
&schema_attr.decorators,
DecoratorTarget::Attribute,
&name,
);
(name, ty, is_optional, has_default, decorators)
(name, ty, is_optional, default, decorators)
}
_ => continue,
};
Expand All @@ -643,7 +648,8 @@ impl<'ctx> Resolver<'ctx> {
name.clone(),
SchemaAttr {
is_optional: existed_attr.map_or(is_optional, |attr| attr.is_optional),
has_default,
has_default: default.is_some(),
default,
ty: ty.clone(),
pos: pos.clone(),
doc: doc_str,
Expand Down
19 changes: 18 additions & 1 deletion kclvm/sema/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ impl Type {
TypeKind::None => NONE_TYPE_STR.to_string(),
TypeKind::Any => ANY_TYPE_STR.to_string(),
TypeKind::Bool => BOOL_TYPE_STR.to_string(),
TypeKind::BoolLit(v) => format!("{}({})", BOOL_TYPE_STR, v),
TypeKind::BoolLit(v) => {
format!(
"{}({})",
BOOL_TYPE_STR,
if *v {
NAME_CONSTANT_TRUE
} else {
NAME_CONSTANT_FALSE
}
)
}
TypeKind::Int => INT_TYPE_STR.to_string(),
TypeKind::IntLit(v) => format!("{}({})", INT_TYPE_STR, v),
TypeKind::Float => FLOAT_TYPE_STR.to_string(),
Expand Down Expand Up @@ -206,6 +216,7 @@ impl SchemaType {
let schema_attr = SchemaAttr {
is_optional: true,
has_default: false,
default: None,
ty,
pos: Position::dummy_pos(),
doc: None,
Expand Down Expand Up @@ -251,6 +262,12 @@ impl SchemaType {
pub struct SchemaAttr {
pub is_optional: bool,
pub has_default: bool,
/// `default` denotes the schema attribute optional value string. For example,
/// for the schema attribute definition `name?: str = "Alice"`, the value of
/// `default` is [Some("Alice")].
/// For the schema attribute definition `name?: str`, the value of `default`
/// is [None].
pub default: Option<String>,
pub ty: Rc<Type>,
pub pos: Position,
pub doc: Option<String>,
Expand Down
Loading