allow int and float as strings for arguments (#4615)

* allow int and float as strings for arguments

* consume iterator
This commit is contained in:
Fernando Herrera 2022-02-24 05:09:02 +00:00 committed by GitHub
parent 4b18fdcc6e
commit 21c0f7d738
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 32 deletions

View file

@ -58,43 +58,35 @@ impl Command for External {
let config = stack.get_config().unwrap_or_default();
let env_vars_str = env_to_strings(engine_state, stack, &config)?;
let mut args_strs = vec![];
fn value_as_spanned(value: Value) -> Result<Spanned<String>, ShellError> {
let span = value.span()?;
for arg in args {
let span = if let Ok(span) = arg.span() {
span
} else {
Span { start: 0, end: 0 }
};
if let Ok(s) = arg.as_string() {
args_strs.push(Spanned { item: s, span });
} else if let Value::List { vals, span } = arg {
// Interpret a list as a series of arguments
for val in vals {
if let Ok(s) = val.as_string() {
args_strs.push(Spanned { item: s, span });
} else {
return Err(ShellError::ExternalCommand(
"Cannot convert argument to a string".into(),
"All arguments to an external command need to be string-compatible"
.into(),
val.span()?,
));
}
}
} else {
return Err(ShellError::ExternalCommand(
"Cannot convert argument to a string".into(),
"All arguments to an external command need to be string-compatible".into(),
arg.span()?,
));
}
value
.as_string()
.map(|item| Spanned { item, span })
.map_err(|_| {
ShellError::ExternalCommand(
"Cannot convert argument to a string".into(),
"All arguments to an external command need to be string-compatible".into(),
span,
)
})
}
let args = args
.into_iter()
.flat_map(|arg| match arg {
Value::List { vals, .. } => vals
.into_iter()
.map(value_as_spanned)
.collect::<Vec<Result<Spanned<String>, ShellError>>>(),
val => vec![value_as_spanned(val)],
})
.collect::<Result<Vec<Spanned<String>>, ShellError>>()?;
let command = ExternalCommand {
name,
args: args_strs,
args,
redirect_stdout,
redirect_stderr,
env_vars: env_vars_str,

View file

@ -173,6 +173,8 @@ impl Clone for Value {
impl Value {
pub fn as_string(&self) -> Result<String, ShellError> {
match self {
Value::Int { val, .. } => Ok(val.to_string()),
Value::Float { val, .. } => Ok(val.to_string()),
Value::String { val, .. } => Ok(val.to_string()),
Value::Binary { val, .. } => Ok(match std::str::from_utf8(val) {
Ok(s) => s.to_string(),