Skip to content

Commit

Permalink
refactor(eval): add ObjectData::get_visible_fields_order helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Nov 3, 2024
1 parent 1b6b689 commit 4bc3061
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 77 deletions.
9 changes: 9 additions & 0 deletions rsjsonnet-lang/src/program/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ impl<'p> ObjectData<'p> {
})
}

#[inline]
pub(super) fn get_visible_fields_order(
&self,
) -> impl DoubleEndedIterator<Item = InternedStr<'p>> + '_ {
self.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
}

pub(super) fn field_is_visible(&self, name: InternedStr<'p>) -> bool {
if let Some(field) = self.self_layer.fields.get(&name) {
match field.visibility {
Expand Down
46 changes: 10 additions & 36 deletions rsjsonnet-lang/src/program/eval/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ impl<'p> Evaluator<'_, 'p> {
};
let object = object.view();

let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
for &field_name in visible_fields.iter().rev() {
let visible_fields = object.get_visible_fields_order();
for field_name in visible_fields.rev() {
let field_thunk = self
.program
.find_object_field_thunk(&object, 0, field_name)
Expand Down Expand Up @@ -141,11 +137,7 @@ impl<'p> Evaluator<'_, 'p> {
}
ValueData::Object(object) => {
let object = object.view();
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields: Vec<_> = object.get_visible_fields_order().collect();
if visible_fields.is_empty() {
result.push_str("{}");
} else {
Expand Down Expand Up @@ -253,11 +245,7 @@ impl<'p> Evaluator<'_, 'p> {
}
ValueData::Object(object) => {
let object = object.view();
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields: Vec<_> = object.get_visible_fields_order().collect();
if visible_fields.is_empty() {
if let Some(ref empty_object) = format.empty_object {
result.push_str(empty_object);
Expand Down Expand Up @@ -427,11 +415,7 @@ impl<'p> Evaluator<'_, 'p> {
}
ValueData::Object(object) => {
let object = object.view();
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields: Vec<_> = object.get_visible_fields_order().collect();
if visible_fields.is_empty() {
if parent_is_array || parent_is_object {
result.push(' ');
Expand Down Expand Up @@ -538,12 +522,8 @@ impl<'p> Evaluator<'_, 'p> {
indent,
});

let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
for &field_name in visible_fields.iter().rev() {
let visible_fields = object.get_visible_fields_order();
for field_name in visible_fields.rev() {
let field_thunk = self
.program
.find_object_field_thunk(&object, 0, field_name)
Expand All @@ -563,10 +543,8 @@ impl<'p> Evaluator<'_, 'p> {
indent: Rc<str>,
) {
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter(|&&(_, visible)| visible)
.map(|&(name, _)| {
.get_visible_fields_order()
.map(|name| {
// Check if the field is an object or an array of objects
let (_, field) = object.find_field(0, name).unwrap();
let field_value = field.thunk.get().unwrap().view().get_value().unwrap();
Expand Down Expand Up @@ -785,11 +763,7 @@ impl<'p> Evaluator<'_, 'p> {
}
ValueData::Object(object) => {
let object = object.view();
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields: Vec<_> = object.get_visible_fields_order().collect();
if visible_fields.is_empty() {
result.push_str("{ }");
} else {
Expand Down
16 changes: 2 additions & 14 deletions rsjsonnet-lang/src/program/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,20 +997,8 @@ impl<'p, 'a> Evaluator<'a, 'p> {
(ValueData::Object(lhs), ValueData::Object(rhs)) => {
let lhs = lhs.view();
let rhs = rhs.view();
let lhs_fields: Vec<_> = lhs
.get_fields_order()
.iter()
.rev()
.filter_map(|(name, visible)| visible.then_some(name))
.cloned()
.collect();
let rhs_fields: Vec<_> = rhs
.get_fields_order()
.iter()
.rev()
.filter_map(|(name, visible)| visible.then_some(name))
.cloned()
.collect();
let lhs_fields: Vec<_> = lhs.get_visible_fields_order().rev().collect();
let rhs_fields: Vec<_> = rhs.get_visible_fields_order().rev().collect();

if lhs_fields == rhs_fields {
let mut fields = lhs_fields;
Expand Down
35 changes: 8 additions & 27 deletions rsjsonnet-lang/src/program/eval/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ impl<'p> Evaluator<'_, 'p> {
ValueData::Array(array) => array.view().len(),
ValueData::Object(object) => {
let object = object.view();
object
.get_fields_order()
.iter()
.filter_map(|(name, visible)| visible.then_some(name))
.count()
object.get_visible_fields_order().count()
}
ValueData::Function(func) => func.view().params.order.len(),
_ => {
Expand Down Expand Up @@ -129,10 +125,7 @@ impl<'p> Evaluator<'_, 'p> {
}
ValueData::Object(object) => {
let object = object.view();
let visible_fields = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name));
let visible_fields = object.get_visible_fields_order();

self.object_stack
.push(ObjectData::new_simple(FHashMap::default()));
Expand Down Expand Up @@ -1043,12 +1036,8 @@ impl<'p> Evaluator<'_, 'p> {
};
let object = object.view();

let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
for &field_name in visible_fields.iter().rev() {
let visible_fields = object.get_visible_fields_order();
for field_name in visible_fields.rev() {
let field_thunk = self
.program
.find_object_field_thunk(&object, 0, field_name)
Expand Down Expand Up @@ -1078,13 +1067,9 @@ impl<'p> Evaluator<'_, 'p> {
self.string_stack.push(String::new());
self.state_stack.push(State::StringToValue);

let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields = object.get_visible_fields_order();

for &field_name in visible_fields.iter().rev() {
for field_name in visible_fields.rev() {
self.state_stack.push(State::AppendToString('\n'.into()));

self.push_trace_item(TraceItem::ManifestObjectField { name: field_name });
Expand Down Expand Up @@ -1271,13 +1256,9 @@ impl<'p> Evaluator<'_, 'p> {
self.state_stack.push(State::AppendToString('>'.into()));

let object = object.view();
let visible_fields: Vec<_> = object
.get_fields_order()
.iter()
.filter_map(|&(name, visible)| visible.then_some(name))
.collect();
let visible_fields = object.get_visible_fields_order();

for &field_name in visible_fields.iter().rev() {
for field_name in visible_fields.rev() {
let field_thunk = self
.program
.find_object_field_thunk(&object, 0, field_name)
Expand Down

0 comments on commit 4bc3061

Please sign in to comment.