Skip to content

Commit

Permalink
fix optional attr
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Jan 14, 2025
1 parent ba7b6a6 commit b011652
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions kclvm/evaluator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ impl SchemaEvalContext {
}

/// Get all attribute from schema
pub fn get_attrs(s: &Evaluator, ctx: &SchemaEvalContextRef) -> Vec<String> {
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 {
Expand Down
17 changes: 11 additions & 6 deletions kclvm/evaluator/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ pub fn type_pack_and_check(
SchemaEvalContext::has_index_signature(s, &caller.ctx);
if !has_index_signature && no_such_attr {
error_msgs
.push(format!("Schema {} not contains attr {}", tpe, key));
.push(format!("Schema {} does not contain attribute {}", tpe, key));
}
}

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));
.push(format!("Schema {}'s attribute {} is missing", tpe, attr));
}
}
}
Expand All @@ -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
Expand Down

0 comments on commit b011652

Please sign in to comment.