1use crate::{evaluate_add_op, LazyBinding, 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: LazyBinding,16}1718#[derive(Debug)]19pub struct ObjValueInternals {20 super_obj: Option<ObjValue>,21 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,22 value_cache: RefCell<HashMap<Rc<str>, Val>>,23}24#[derive(Clone)]25pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);26impl Debug for ObjValue {27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {28 if let Some(super_obj) = self.0.super_obj.as_ref() {29 if f.alternate() {30 write!(f, "{:#?}", super_obj)?;31 } else {32 write!(f, "{:?}", super_obj)?;33 }34 write!(f, " + ")?;35 }36 let mut debug = f.debug_struct("ObjValue");37 for (name, member) in self.0.this_entries.iter() {38 debug.field(name, member);39 }40 debug.finish_non_exhaustive()41 }42}4344impl ObjValue {45 pub fn new(46 super_obj: Option<ObjValue>,47 this_entries: Rc<BTreeMap<Rc<str>, ObjMember>>,48 ) -> ObjValue {49 ObjValue(Rc::new(ObjValueInternals {50 super_obj,51 this_entries,52 value_cache: RefCell::new(HashMap::new()),53 }))54 }55 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {56 match &self.0.super_obj {57 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),58 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),59 }60 }61 pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {62 if let Some(s) = &self.0.super_obj {63 s.enum_fields(handler);64 }65 for (name, member) in self.0.this_entries.iter() {66 handler(&name, &member.visibility);67 }68 }69 pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {70 let out = Rc::new(RefCell::new(IndexMap::new()));71 self.enum_fields(&|name, visibility| {72 let mut out = out.borrow_mut();73 match visibility {74 Visibility::Normal => {75 if !out.contains_key(name) {76 out.insert(name.to_owned(), true);77 }78 }79 Visibility::Hidden => {80 out.insert(name.to_owned(), false);81 }82 Visibility::Unhide => {83 out.insert(name.to_owned(), true);84 }85 };86 });87 Rc::try_unwrap(out).unwrap().into_inner()88 }89 pub fn visible_fields(&self) -> Vec<Rc<str>> {90 self.fields_visibility()91 .into_iter()92 .filter(|(_k, v)| *v)93 .map(|(k, _)| k)94 .collect()95 }96 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {97 if let Some(v) = self.0.value_cache.borrow().get(&key) {98 return Ok(Some(v.clone()));99 }100 if let Some(v) = self.get_raw(&key, self)? {101 let v = v.unwrap_if_lazy()?;102 self.0.value_cache.borrow_mut().insert(key, v.clone());103 Ok(Some(v))104 } else {105 Ok(None)106 }107 }108 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {109 match (self.0.this_entries.get(key), &self.0.super_obj) {110 (Some(k), None) => Ok(Some(111 k.invoke112 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?113 .evaluate()?,114 )),115 (Some(k), Some(s)) => {116 let lazy = k117 .invoke118 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?;119 let our = lazy.evaluate()?;120 if k.add {121 s.get_raw(key, real_this)?122 .map_or(Ok(Some(our.clone())), |v| {123 Ok(Some(evaluate_add_op(&v, &our)?))124 })125 } else {126 Ok(Some(our))127 }128 }129 (None, Some(s)) => s.get_raw(key, real_this),130 (None, None) => Ok(None),131 }132 }133}134impl PartialEq for ObjValue {135 fn eq(&self, other: &Self) -> bool {136 Rc::ptr_eq(&self.0, &other.0)137 }138}