Skip to content

Commit

Permalink
feat: enhance schema optional attr check time and put it before the c…
Browse files Browse the repository at this point in the history
…heck expressions (#1341)

Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed May 21, 2024
1 parent aba7e37 commit 7b07bf0
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 120 deletions.
15 changes: 0 additions & 15 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use generational_arena::Index;
use kclvm_ast::ast::{self, CallExpr, ConfigEntry, NodeRef};
use kclvm_ast::walker::TypedResultWalker;
use kclvm_runtime::val_func::invoke_function;
use kclvm_runtime::walker::walk_value_mut;
use kclvm_runtime::{
schema_assert, schema_runtime_type, ConfigEntryOperationKind, DecoratorValue, RuntimeErrorType,
UnionOptions, ValueRef, PKG_PATH_PREFIX,
Expand Down Expand Up @@ -138,16 +137,7 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
let mut value = self.walk_expr(&assign_stmt.value)?;
// Runtime type cast if exists the type annotation.
if let Some(ty) = &assign_stmt.ty {
let is_in_schema = self.is_in_schema() || self.is_in_schema_expr();
value = type_pack_and_check(self, &value, vec![&ty.node.to_string()], false);
// Schema required attribute validating.
if !is_in_schema {
walk_value_mut(&value, &mut |value: &ValueRef| {
if value.is_schema() {
value.schema_check_attr_optional(&mut self.runtime_ctx.borrow_mut(), true);
}
})
}
}
if assign_stmt.targets.len() == 1 {
// Store the single target
Expand Down Expand Up @@ -843,7 +833,6 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
fn walk_schema_expr(&self, schema_expr: &'ctx ast::SchemaExpr) -> Self::Result {
// Check the required attributes only when the values of all attributes
// in the final schema are solved.
let is_in_schema = self.is_in_schema() || self.is_in_schema_expr();
self.push_schema_expr();
let config_value = self.walk_expr(&schema_expr.config)?;
let schema_type = self.walk_identifier_with_ctx(
Expand Down Expand Up @@ -888,7 +877,6 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
&list_value,
&dict_value,
);
{}
self.pop_backtrace();
self.pop_pkgpath();
value
Expand Down Expand Up @@ -916,9 +904,6 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
&UnionOptions::default(),
)
};
if !is_in_schema {
schema.schema_check_attr_optional(&mut self.runtime_ctx.borrow_mut(), true)
}
self.pop_schema_expr();
Ok(schema)
}
Expand Down
3 changes: 2 additions & 1 deletion kclvm/evaluator/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub(crate) fn call_schema_body_from_rule(
pub(crate) fn call_schema_check(
s: &Evaluator,
func: &ValueRef,
schema_value: &ValueRef,
args: &ValueRef,
kwargs: &ValueRef,
ctx: Option<&SchemaEvalContextRef>,
Expand All @@ -125,7 +126,7 @@ pub(crate) fn call_schema_check(
if let Some(ctx) = ctx {
schema.ctx.borrow_mut().set_info_with_schema(&ctx.borrow())
}
(schema.check)(s, &schema.ctx, args, kwargs);
(schema.check)(s, &schema.ctx, schema_value, args, kwargs);
s.pop_backtrace();
s.pop_pkgpath();
}
Expand Down
Loading

0 comments on commit 7b07bf0

Please sign in to comment.