feat: add previous responses as a default#58
Conversation
| @@ -0,0 +1,19 @@ | |||
| name = "Super basic" | |||
There was a problem hiding this comment.
default_from_variable would be a better name for the file
src/definition.rs
Outdated
| } | ||
|
|
||
| fn has_template_variables<'a>(s: &'a String, _already_vars: &HashMap<String, Value>) -> Option<HashSet<&'a str>> { | ||
| let re = Regex::new(r"\{\{(?:[a-zA-Z][0-9a-zA-Z_]*)\}\}").unwrap(); |
There was a problem hiding this comment.
that's going to compile that regex every time this function is called, you can lift it in some lazy static or once_cell, whatever this crate is using (or add one of these libs to it)
| Some(variables) => replace_with_previous_responses( | ||
| variables, &vals, &s), | ||
| None => s.clone(), | ||
| }; |
There was a problem hiding this comment.
I think doing it that way is more complicated than it actually requires.
A default value will always be a valid Tera template as it's just a string so you can just check whether {{ and }} are in the string and handle both cases:
- no: just use that string a default, no need to do anything
- yes: render that string as a one off template with the variables so far added to the Tera context.
|
got it! never used Tera before, I will address your comments asap! |
src/definition.rs
Outdated
| Some(variables) => replace_with_previous_responses( | ||
| variables, &vals, &s), | ||
| None => s.clone(), | ||
| let contains_template = has_template_variables(&s); |
There was a problem hiding this comment.
you can just check if s.contains("{{") && s.contains("}}") rather than using regex
src/utils.rs
Outdated
| context: &Context, | ||
| path: Option<PathBuf>, | ||
| ) -> Result<String> { | ||
| let mut tera = Tera::default(); |
There was a problem hiding this comment.
there's actually a better way to do that now: https://docs.rs/tera/latest/tera/struct.Tera.html#method.one_off
There was a problem hiding this comment.
question: should we set the autoscape to true or transform it into a function parameter?
|
Thanks, I'll merge it and tweak a bit later this week |
* feat: use previous responses as default values * chore: split in different functions * chore: update examples folder * chore: include unit test * chore: add lazy_static crate to avoid regex eval evey time * chore: change to `default-fro-variable` * chore: use tera and render template to replace values * chore: remove regex and use `s.contains` instead * chore: use `tera::one_off` function * chore: set `autoscape` to false
Description
Use a previous response/default value as value to another variable into toml file
Example
The following toml file describes the feature included in this PR, basically will be possible to use a variable template to get already assigned values from other variables. eg will be assigned to the
manifestvariable the valuemy_project-other_project-manifest.mdif the user leave the all the inputs empty, however if the user populate the variableproject_onewithtreeand variableproject_twowithleafand leavemanifestvariable empty then the value assigned to manifest will betree-leaf-manifest.md.The prompt default value is updated too!

Issue
#22