Skip to content

Commit 7d331b6

Browse files
committed
fix: object destruction leftovers check should account for defaults
Fixes: #170
1 parent e12ba8d commit 7d331b6

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

crates/jrsonnet-evaluator/src/evaluate/destructure.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn destruct(
170170
#[derive(Trace)]
171171
struct DataThunk {
172172
parent: Thunk<Val>,
173-
field_names: Vec<IStr>,
173+
field_names: Vec<(IStr, bool)>,
174174
has_rest: bool,
175175
}
176176
impl ThunkValue for DataThunk {
@@ -181,14 +181,14 @@ pub fn destruct(
181181
let Val::Obj(obj) = v else {
182182
bail!("expected object");
183183
};
184-
for field in &self.field_names {
185-
if !obj.has_field_ex(field.clone(), true) {
184+
for (field, has_default) in &self.field_names {
185+
if !has_default && !obj.has_field_ex(field.clone(), true) {
186186
bail!("missing field: {field}");
187187
}
188188
}
189189
if !self.has_rest {
190190
let len = obj.len();
191-
if len != self.field_names.len() {
191+
if len > self.field_names.len() {
192192
bail!("too many fields, and rest not found");
193193
}
194194
}
@@ -197,8 +197,7 @@ pub fn destruct(
197197
}
198198
let field_names: Vec<_> = fields
199199
.iter()
200-
.filter(|f| f.2.is_none())
201-
.map(|f| f.0.clone())
200+
.map(|f| (f.0.clone(), f.2.is_some()))
202201
.collect();
203202
let full = Thunk::new(DataThunk {
204203
parent,

0 commit comments

Comments
 (0)