git.delta.rocks / jrsonnet / refs/commits / 2bdb11ee2096

difftreelog

feat(obj) visibility aware field list

Лач2020-06-10parent: #a1dd402.patch.diff
in: master

1 file changed

modifiedcrates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth
1use crate::{evaluate_add_op, Binding, Result, Val};1use crate::{evaluate_add_op, Binding, Result, Val};
2use indexmap::IndexMap;
2use jsonnet_parser::Visibility;3use jsonnet_parser::Visibility;
3use std::{4use std::{
4 cell::RefCell,5 cell::RefCell,
5 collections::{BTreeMap, BTreeSet, HashMap},6 collections::{BTreeMap, HashMap},
6 fmt::Debug,7 fmt::Debug,
7 rc::Rc,8 rc::Rc,
8};9};
56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),
57 }58 }
58 }59 }
60 pub fn enum_fields(&self, handler: &impl Fn(&str, &Visibility)) {
61 if let Some(s) = &self.0.super_obj {
62 s.enum_fields(handler);
63 }
64 for (name, member) in self.0.this_entries.iter() {
65 handler(&name, &member.visibility);
66 }
67 }
59 pub fn fields(&self) -> BTreeSet<String> {68 pub fn fields_visibility(&self) -> IndexMap<String, bool> {
60 let mut fields = BTreeSet::new();69 let out = Rc::new(RefCell::new(IndexMap::new()));
61 self.0.this_entries.keys().for_each(|k| {70 self.enum_fields(&|name, visibility| {
71 match visibility {
72 Visibility::Normal => {}
73 Visibility::Hidden => {
62 fields.insert(k.clone());74 out.borrow_mut().insert(name.to_owned(), false);
75 }
76 Visibility::Unhide => {
77 out.borrow_mut().insert(name.to_owned(), true);
78 }
79 };
63 });80 });
64 if self.0.super_obj.is_some() {
65 for field in self.0.super_obj.clone().unwrap().fields() {81 Rc::try_unwrap(out).unwrap().into_inner()
66 fields.insert(field);
67 }
68 }
69 fields
70 }82 }
71 pub fn get(&self, key: &str) -> Result<Option<Val>> {83 pub fn get(&self, key: &str) -> Result<Option<Val>> {
72 if let Some(v) = self.0.value_cache.borrow().get(key) {84 if let Some(v) = self.0.value_cache.borrow().get(key) {