git.delta.rocks / jrsonnet / refs/commits / 74729ec02fba

difftreelog

refactor move field sorting to visible_fields

Лач2020-07-02parent: #cee13e8.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj.rs
1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::{ExprLocation, Visibility};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56#[derive(Debug)]7pub struct ObjMember {8	pub add: bool,9	pub visibility: Visibility,10	pub invoke: LazyBinding,11	pub location: Option<ExprLocation>,12}1314#[derive(Debug)]15pub struct ObjValueInternals {16	super_obj: Option<ObjValue>,17	this_entries: Rc<HashMap<Rc<str>, ObjMember>>,18	value_cache: RefCell<HashMap<Rc<str>, Val>>,19}20#[derive(Clone)]21pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);22impl Debug for ObjValue {23	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {24		if let Some(super_obj) = self.0.super_obj.as_ref() {25			if f.alternate() {26				write!(f, "{:#?}", super_obj)?;27			} else {28				write!(f, "{:?}", super_obj)?;29			}30			write!(f, " + ")?;31		}32		let mut debug = f.debug_struct("ObjValue");33		for (name, member) in self.0.this_entries.iter() {34			debug.field(name, member);35		}36		debug.finish_non_exhaustive()37	}38}3940impl ObjValue {41	pub fn new(42		super_obj: Option<ObjValue>,43		this_entries: Rc<HashMap<Rc<str>, ObjMember>>,44	) -> ObjValue {45		ObjValue(Rc::new(ObjValueInternals {46			super_obj,47			this_entries,48			value_cache: RefCell::new(HashMap::new()),49		}))50	}51	pub fn new_empty() -> ObjValue {52		Self::new(None, Rc::new(HashMap::new()))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(&Rc<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<Rc<str>, bool> {69		let out = Rc::new(RefCell::new(IndexMap::new()));70		self.enum_fields(&|name, visibility| {71			let mut out = out.borrow_mut();72			match visibility {73				Visibility::Normal => {74					if !out.contains_key(name) {75						out.insert(name.to_owned(), true);76					}77				}78				Visibility::Hidden => {79					out.insert(name.to_owned(), false);80				}81				Visibility::Unhide => {82					out.insert(name.to_owned(), true);83				}84			};85		});86		Rc::try_unwrap(out).unwrap().into_inner()87	}88	pub fn visible_fields(&self) -> Vec<Rc<str>> {89		self.fields_visibility()90			.into_iter()91			.filter(|(_k, v)| *v)92			.map(|(k, _)| k)93			.collect()94	}95	pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {96		if let Some(v) = self.0.value_cache.borrow().get(&key) {97			return Ok(Some(v.clone()));98		}99		if let Some(v) = self.get_raw(&key, self)? {100			let v = v.unwrap_if_lazy()?;101			self.0.value_cache.borrow_mut().insert(key, v.clone());102			Ok(Some(v))103		} else {104			Ok(None)105		}106	}107	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {108		match (self.0.this_entries.get(key), &self.0.super_obj) {109			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),110			(Some(k), Some(s)) => {111				let our = self.evaluate_this(k, real_this)?;112				if k.add {113					s.get_raw(key, real_this)?114						.map_or(Ok(Some(our.clone())), |v| {115							Ok(Some(evaluate_add_op(&v, &our)?))116						})117				} else {118					Ok(Some(our))119				}120			}121			(None, Some(s)) => s.get_raw(key, real_this),122			(None, None) => Ok(None),123		}124	}125	fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {126		Ok(v.invoke127			.evaluate(Some(real_this.clone()), self.0.super_obj.clone())?128			.evaluate()?)129	}130}131impl PartialEq for ObjValue {132	fn eq(&self, other: &Self) -> bool {133		Rc::ptr_eq(&self.0, &other.0)134	}135}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -255,8 +255,7 @@
 		}
 		Val::Obj(obj) => {
 			buf.push_str("{\n");
-			let mut fields = obj.visible_fields();
-			fields.sort();
+			let fields = obj.visible_fields();
 			if !fields.is_empty() {
 				let old_len = cur_padding.len();
 				cur_padding.push_str(padding);