difftreelog
feat std.extVar support
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth42 }42 }43}43}4445#[derive(Clone)]46struct ExtStr {47 name: String,48 value: String,49}50impl FromStr for ExtStr {51 type Err = &'static str;52 fn from_str(s: &str) -> Result<Self, Self::Err> {53 let out: Vec<_> = s.split('=').collect();54 match out.len() {55 1 => Ok(ExtStr {56 name: out[0].to_owned(),57 value: std::env::var(out[0]).or(Err("missing env var"))?,58 }),59 2 => Ok(ExtStr {60 name: out[0].to_owned(),61 value: out[1].to_owned(),62 }),63 _ => Err("bad ext-str syntax"),64 }65 }66}446745#[derive(Clap)]68#[derive(Clap)]46#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]69#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]47struct Opts {70struct Opts {48 #[clap(long, about = "Disable global std variable")]71 #[clap(long, about = "Disable global std variable")]49 no_stdlib: bool,72 no_stdlib: bool,50 #[clap(long, about = "Add external string")]73 #[clap(long, about = "Add external string")]51 ext_str: Option<Vec<String>>,74 ext_str: Vec<ExtStr>,52 #[clap(long, about = "Add external string from code")]75 #[clap(long, about = "Add external string from code")]53 ext_code: Option<Vec<String>>,76 ext_code: Vec<ExtStr>,54 #[clap(long, about = "Add TLA")]77 #[clap(long, about = "Add TLA")]55 tla_str: Option<Vec<String>>,78 tla_str: Vec<ExtStr>,56 #[clap(long, about = "Add TLA from code")]79 #[clap(long, about = "Add TLA from code")]57 tla_code: Option<Vec<String>>,80 tla_code: Vec<ExtStr>,58 #[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]81 #[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]59 format: Format,82 format: Format,60 #[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]83 #[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]92 if !opts.no_stdlib {115 if !opts.no_stdlib {93 evaluator.with_stdlib();116 evaluator.with_stdlib();94 }117 }118 for ExtStr { name, value } in opts.ext_str.iter().cloned() {119 evaluator.add_ext_var(name, Val::Str(value));120 }121 for ExtStr { name, value } in opts.ext_code.iter().cloned() {122 evaluator.add_ext_var(name, evaluator.parse_evaluate_raw(&value).unwrap());123 }95 let mut input = current_dir().unwrap();124 let mut input = current_dir().unwrap();96 input.push(opts.input.clone());125 input.push(opts.input.clone());97 let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();126 let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -12,6 +12,8 @@
TooManyArgsFunctionHas(usize),
FunctionParameterNotBoundInCall(String),
+ UndefinedExternalVariable(String),
+
RuntimeError(String),
StackOverflow,
FractionalIndex,
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -533,6 +533,20 @@
panic!("bad pow call");
}
}
+ ("std", "extVar") => {
+ assert_eq!(args.len(), 1);
+ if let Val::Str(a) = evaluate(context, &args[0].1)? {
+ with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(
+ || {
+ create_error::<()>(crate::Error::UndefinedExternalVariable(a))
+ .err()
+ .unwrap()
+ },
+ )?
+ } else {
+ panic!("bad extVar call");
+ }
+ }
(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
},
Val::Func(f) => {
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -72,6 +72,9 @@
files: RefCell<HashMap<PathBuf, FileData>>,
globals: RefCell<HashMap<String, Val>>,
+ /// Values to use with std.extVar
+ ext_vars: RefCell<HashMap<String, Val>>,
+
settings: EvaluationSettings,
}
@@ -177,6 +180,9 @@
pub fn add_global(&self, name: String, value: Val) {
self.0.globals.borrow_mut().insert(name, value);
}
+ pub fn add_ext_var(&self, name: String, value: Val) {
+ self.0.ext_vars.borrow_mut().insert(name, value);
+ }
pub fn with_stdlib(&self) -> &Self {
self.begin_state();