From 2bdb11ee20964721b80f94fb422ff3a7798c6a8a Mon Sep 17 00:00:00 2001 From: Лач Date: Wed, 10 Jun 2020 17:36:55 +0000 Subject: [PATCH] feat(obj): visibility aware field list --- --- a/crates/jsonnet-evaluator/src/obj.rs +++ b/crates/jsonnet-evaluator/src/obj.rs @@ -1,8 +1,9 @@ use crate::{evaluate_add_op, Binding, Result, Val}; +use indexmap::IndexMap; use jsonnet_parser::Visibility; use std::{ cell::RefCell, - collections::{BTreeMap, BTreeSet, HashMap}, + collections::{BTreeMap, HashMap}, fmt::Debug, rc::Rc, }; @@ -56,17 +57,28 @@ Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()), } } - pub fn fields(&self) -> BTreeSet { - let mut fields = BTreeSet::new(); - self.0.this_entries.keys().for_each(|k| { - fields.insert(k.clone()); + pub fn enum_fields(&self, handler: &impl Fn(&str, &Visibility)) { + if let Some(s) = &self.0.super_obj { + s.enum_fields(handler); + } + for (name, member) in self.0.this_entries.iter() { + handler(&name, &member.visibility); + } + } + pub fn fields_visibility(&self) -> IndexMap { + let out = Rc::new(RefCell::new(IndexMap::new())); + self.enum_fields(&|name, visibility| { + match visibility { + Visibility::Normal => {} + Visibility::Hidden => { + out.borrow_mut().insert(name.to_owned(), false); + } + Visibility::Unhide => { + out.borrow_mut().insert(name.to_owned(), true); + } + }; }); - if self.0.super_obj.is_some() { - for field in self.0.super_obj.clone().unwrap().fields() { - fields.insert(field); - } - } - fields + Rc::try_unwrap(out).unwrap().into_inner() } pub fn get(&self, key: &str) -> Result> { if let Some(v) = self.0.value_cache.borrow().get(key) { -- gitstuff