difftreelog
feat(obj) visibility aware field list
in: master
1 file changed
crates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use crate::{evaluate_add_op, Binding, Result, Val};2use jsonnet_parser::Visibility;3use std::{4 cell::RefCell,5 collections::{BTreeMap, BTreeSet, HashMap},6 fmt::Debug,7 rc::Rc,8};910#[derive(Debug)]11pub struct ObjMember {12 pub add: bool,13 pub visibility: Visibility,14 pub invoke: Binding,15}1617#[derive(Debug)]18pub struct ObjValueInternals {19 super_obj: Option<ObjValue>,20 this_entries: Rc<BTreeMap<String, ObjMember>>,21 value_cache: RefCell<HashMap<String, Val>>,22}23pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);24impl Debug for ObjValue {25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {26 if let Some(super_obj) = self.0.super_obj.as_ref() {27 if f.alternate() {28 write!(f, "{:#?}", super_obj)?;29 } else {30 write!(f, "{:?}", super_obj)?;31 }32 write!(f, " + ")?;33 }34 let mut debug = f.debug_struct("ObjValue");35 for (name, member) in self.0.this_entries.iter() {36 debug.field(name, member);37 }38 debug.finish_non_exhaustive()39 }40}4142impl ObjValue {43 pub fn new(44 super_obj: Option<ObjValue>,45 this_entries: Rc<BTreeMap<String, ObjMember>>,46 ) -> ObjValue {47 ObjValue(Rc::new(ObjValueInternals {48 super_obj,49 this_entries,50 value_cache: RefCell::new(HashMap::new()),51 }))52 }53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54 match &self.0.super_obj {55 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 }58 }59 pub fn fields(&self) -> BTreeSet<String> {60 let mut fields = BTreeSet::new();61 self.0.this_entries.keys().for_each(|k| {62 fields.insert(k.clone());63 });64 if self.0.super_obj.is_some() {65 for field in self.0.super_obj.clone().unwrap().fields() {66 fields.insert(field);67 }68 }69 fields70 }71 pub fn get(&self, key: &str) -> Result<Option<Val>> {72 if let Some(v) = self.0.value_cache.borrow().get(key) {73 return Ok(Some(v.clone()));74 }75 if let Some(v) = self.get_raw(key, self)? {76 let v = v.unwrap_if_lazy()?;77 self.078 .value_cache79 .borrow_mut()80 .insert(key.to_owned(), v.clone());81 Ok(Some(v))82 } else {83 Ok(None)84 }85 }86 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {87 match (self.0.this_entries.get(key), &self.0.super_obj) {88 (Some(k), None) => Ok(Some(k.invoke.0(89 Some(real_this.clone()),90 self.0.super_obj.clone(),91 )?)),92 (Some(k), Some(s)) => {93 let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;94 if k.add {95 s.get_raw(key, real_this)?96 .map_or(Ok(Some(our.clone())), |v| {97 Ok(Some(evaluate_add_op(&v, &our)?))98 })99 } else {100 Ok(Some(our))101 }102 }103 (None, Some(s)) => s.get_raw(key, real_this),104 (None, None) => Ok(None),105 }106 }107}108impl Clone for ObjValue {109 fn clone(&self) -> Self {110 ObjValue(self.0.clone())111 }112}113impl PartialEq for ObjValue {114 fn eq(&self, other: &Self) -> bool {115 Rc::ptr_eq(&self.0, &other.0)116 }117}1use crate::{evaluate_add_op, Binding, Result, Val};2use indexmap::IndexMap;3use jsonnet_parser::Visibility;4use std::{5 cell::RefCell,6 collections::{BTreeMap, HashMap},7 fmt::Debug,8 rc::Rc,9};1011#[derive(Debug)]12pub struct ObjMember {13 pub add: bool,14 pub visibility: Visibility,15 pub invoke: Binding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20 super_obj: Option<ObjValue>,21 this_entries: Rc<BTreeMap<String, ObjMember>>,22 value_cache: RefCell<HashMap<String, Val>>,23}24pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);25impl Debug for ObjValue {26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27 if let Some(super_obj) = self.0.super_obj.as_ref() {28 if f.alternate() {29 write!(f, "{:#?}", super_obj)?;30 } else {31 write!(f, "{:?}", super_obj)?;32 }33 write!(f, " + ")?;34 }35 let mut debug = f.debug_struct("ObjValue");36 for (name, member) in self.0.this_entries.iter() {37 debug.field(name, member);38 }39 debug.finish_non_exhaustive()40 }41}4243impl ObjValue {44 pub fn new(45 super_obj: Option<ObjValue>,46 this_entries: Rc<BTreeMap<String, ObjMember>>,47 ) -> ObjValue {48 ObjValue(Rc::new(ObjValueInternals {49 super_obj,50 this_entries,51 value_cache: RefCell::new(HashMap::new()),52 }))53 }54 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {55 match &self.0.super_obj {56 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),57 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),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 }68 pub fn fields_visibility(&self) -> IndexMap<String, bool> {69 let out = Rc::new(RefCell::new(IndexMap::new()));70 self.enum_fields(&|name, visibility| {71 match visibility {72 Visibility::Normal => {}73 Visibility::Hidden => {74 out.borrow_mut().insert(name.to_owned(), false);75 }76 Visibility::Unhide => {77 out.borrow_mut().insert(name.to_owned(), true);78 }79 };80 });81 Rc::try_unwrap(out).unwrap().into_inner()82 }83 pub fn get(&self, key: &str) -> Result<Option<Val>> {84 if let Some(v) = self.0.value_cache.borrow().get(key) {85 return Ok(Some(v.clone()));86 }87 if let Some(v) = self.get_raw(key, self)? {88 let v = v.unwrap_if_lazy()?;89 self.090 .value_cache91 .borrow_mut()92 .insert(key.to_owned(), v.clone());93 Ok(Some(v))94 } else {95 Ok(None)96 }97 }98 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {99 match (self.0.this_entries.get(key), &self.0.super_obj) {100 (Some(k), None) => Ok(Some(k.invoke.0(101 Some(real_this.clone()),102 self.0.super_obj.clone(),103 )?)),104 (Some(k), Some(s)) => {105 let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone())?;106 if k.add {107 s.get_raw(key, real_this)?108 .map_or(Ok(Some(our.clone())), |v| {109 Ok(Some(evaluate_add_op(&v, &our)?))110 })111 } else {112 Ok(Some(our))113 }114 }115 (None, Some(s)) => s.get_raw(key, real_this),116 (None, None) => Ok(None),117 }118 }119}120impl Clone for ObjValue {121 fn clone(&self) -> Self {122 ObjValue(self.0.clone())123 }124}125impl PartialEq for ObjValue {126 fn eq(&self, other: &Self) -> bool {127 Rc::ptr_eq(&self.0, &other.0)128 }129}