Skip to content

Commit

Permalink
fix: object destruction leftovers check should account for defaults
Browse files Browse the repository at this point in the history
Fixes: #170
  • Loading branch information
CertainLach committed Aug 15, 2024
1 parent e12ba8d commit 7d331b6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions crates/jrsonnet-evaluator/src/evaluate/destructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn destruct(
#[derive(Trace)]
struct DataThunk {
parent: Thunk<Val>,
field_names: Vec<IStr>,
field_names: Vec<(IStr, bool)>,
has_rest: bool,
}
impl ThunkValue for DataThunk {
Expand All @@ -181,14 +181,14 @@ pub fn destruct(
let Val::Obj(obj) = v else {
bail!("expected object");
};
for field in &self.field_names {
if !obj.has_field_ex(field.clone(), true) {
for (field, has_default) in &self.field_names {
if !has_default && !obj.has_field_ex(field.clone(), true) {
bail!("missing field: {field}");
}
}
if !self.has_rest {
let len = obj.len();
if len != self.field_names.len() {
if len > self.field_names.len() {
bail!("too many fields, and rest not found");
}
}
Expand All @@ -197,8 +197,7 @@ pub fn destruct(
}
let field_names: Vec<_> = fields
.iter()
.filter(|f| f.2.is_none())
.map(|f| f.0.clone())
.map(|f| (f.0.clone(), f.2.is_some()))
.collect();
let full = Thunk::new(DataThunk {
parent,
Expand Down

0 comments on commit 7d331b6

Please sign in to comment.