From f42e5e7a70ee2d21634bd94bf28ea413825a3f75 Mon Sep 17 00:00:00 2001 From: Лач Date: Sun, 14 Jun 2020 17:44:07 +0000 Subject: [PATCH] feat(evaluator): add importStr --- --- a/crates/jsonnet-evaluator/src/evaluate.rs +++ b/crates/jsonnet-evaluator/src/evaluate.rs @@ -1,6 +1,7 @@ use crate::{ context_creator, create_error, future_wrapper, lazy_val, push, with_state, Context, ContextCreator, Error, FuncDesc, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val, + ValType, }; use closure::closure; use jsonnet_parser::{ @@ -726,9 +727,15 @@ lib_path.push(path); with_state(|s| s.import_file(&lib_path))? } - _ => panic!( - "evaluation not implemented: {:?}", - LocExpr(expr.clone(), loc.clone()) - ), + ImportStr(path) => { + let mut file_path = loc + .clone() + .expect("imports can't be used without loc_data") + .0 + .clone(); + file_path.pop(); + file_path.push(path); + Val::Str(with_state(|s| s.import_file_str(&file_path))?) + } }) } --- 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>, + str_files: RefCell>, globals: RefCell>, /// 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 { + 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 { let parsed = parse( -- gitstuff