git.delta.rocks / jrsonnet / refs/commits / f42e5e7a70ee

difftreelog

feat(evaluator) add importStr

Лач2020-06-14parent: #d84b1e9.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,2 context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context,
3 ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,3 ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
4 ValType,
4};5};
5use closure::closure;6use closure::closure;
6use jsonnet_parser::{7use jsonnet_parser::{
726 lib_path.push(path);727 lib_path.push(path);
727 with_state(|s| s.import_file(&lib_path))?728 with_state(|s| s.import_file(&lib_path))?
728 }729 }
729 _ => panic!(730 ImportStr(path) => {
730 "evaluation not implemented: {:?}",731 let mut file_path = loc
731 LocExpr(expr.clone(), loc.clone())732 .clone()
732 ),733 .expect("imports can't be used without loc_data")
734 .0
735 .clone();
736 file_path.pop();
737 file_path.push(path);
738 Val::Str(with_state(|s| s.import_file_str(&file_path))?)
739 }
733 })740 })
734}741}
735742
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -68,6 +68,7 @@
 	/// Contains file source codes and evaluated results for imports and pretty
 	/// printing stacktraces
 	files: RefCell<HashMap<PathBuf, FileData>>,
+	str_files: RefCell<HashMap<PathBuf, String>>,
 	globals: RefCell<HashMap<String, Val>>,
 
 	/// Values to use with std.extVar
@@ -177,6 +178,13 @@
 		}
 		self.evaluate_file_in_current_state(path)
 	}
+	pub(crate) fn import_file_str(&self, path: &PathBuf) -> Result<String> {
+		if !self.0.str_files.borrow().contains_key(path) {
+			let file_str = (self.0.settings.import_resolver)(path);
+			self.0.str_files.borrow_mut().insert(path.clone(), file_str);
+		}
+		Ok(self.0.str_files.borrow().get(path).cloned().unwrap())
+	}
 
 	pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {
 		let parsed = parse(