From d7ea17f9f8a0d118409179524ffdce66db972564 Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Tue, 14 Jan 2025 11:45:08 +0800 Subject: [PATCH] fix optional attr Signed-off-by: he1pa <18012015693@163.com> --- kclvm/evaluator/src/schema.rs | 4 ++-- kclvm/evaluator/src/ty.rs | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/kclvm/evaluator/src/schema.rs b/kclvm/evaluator/src/schema.rs index 444b4d0ba..a67d4f368 100644 --- a/kclvm/evaluator/src/schema.rs +++ b/kclvm/evaluator/src/schema.rs @@ -206,11 +206,11 @@ impl SchemaEvalContext { } /// Get all attribute from schema - pub fn get_attrs(s: &Evaluator, ctx: &SchemaEvalContextRef) -> Vec { + pub fn get_attrs(s: &Evaluator, ctx: &SchemaEvalContextRef) -> Vec<(String, bool)> { let mut attrs = vec![]; for stmt in &ctx.borrow().node.body { if let ast::Stmt::SchemaAttr(attr) = &stmt.node { - attrs.push(attr.name.node.clone()); + attrs.push((attr.name.node.clone(), attr.is_optional)); } } if let Some(index) = ctx.borrow().parent { diff --git a/kclvm/evaluator/src/ty.rs b/kclvm/evaluator/src/ty.rs index f9fc8cb32..d6f1bc646 100644 --- a/kclvm/evaluator/src/ty.rs +++ b/kclvm/evaluator/src/ty.rs @@ -124,8 +124,9 @@ pub fn type_pack_and_check( } } - for attr in SchemaEvalContext::get_attrs(s, &caller.ctx) { - if !config.values.contains_key(&attr) { + for (attr, is_optional) in SchemaEvalContext::get_attrs(s, &caller.ctx) + { + if !config.values.contains_key(&attr) && !is_optional { error_msgs .push(format!("Schema {}'s attr {} is missing", tpe, attr)); } @@ -136,9 +137,13 @@ pub fn type_pack_and_check( } } panic!( - "expect {expected_type}, got {}. For details:\n{}", + "expect {expected_type}, got {}.{}", val_plan::type_of(value, true), - error_msgs.join("\n") + if error_msgs.is_empty() { + "".to_string() + } else { + format!("For details:\n{}", error_msgs.join("\n")) + } ); } converted_value