Add functions for each Value case (#9736)

# Description
This PR ensures functions exist to extract and create each and every
`Value` case. It also renames `Value::boolean` to `Value::bool` to match
`Value::test_bool`, `Value::as_bool`, and `Value::Bool`. Similarly,
`Value::as_integer` was renamed to `Value::as_int` to be consistent with
`Value::int`, `Value::test_int`, and `Value::Int`. These two renames can
be undone if necessary.

# User-Facing Changes
No user facing changes, but two public functions were renamed which may
affect downstream dependents.
This commit is contained in:
Ian Manske
2023-07-21 13:20:33 +00:00
committed by GitHub
parent 0b1e368cea
commit 7e1b922ea7
30 changed files with 413 additions and 216 deletions

View File

@ -59,27 +59,27 @@ impl Command for SubCommand {
vals: vec![
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(true, span)],
vals: vec![Value::bool(true, span)],
span,
},
],
@ -89,27 +89,27 @@ impl Command for SubCommand {
Example {
description: "Convert bool to boolean",
example: "true | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert integer to boolean",
example: "1 | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert decimal to boolean",
example: "0.3 | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
Example {
description: "convert decimal string to boolean",
example: "'0.0' | into bool",
result: Some(Value::boolean(false, span)),
result: Some(Value::bool(false, span)),
},
Example {
description: "convert string to boolean",
example: "'true' | into bool",
result: Some(Value::boolean(true, span)),
result: Some(Value::bool(true, span)),
},
]
}

View File

@ -52,7 +52,7 @@ impl Command for SubCommand {
example: "[[value]; [false]] | into record",
result: Some(Value::Record {
cols: vec!["value".to_string()],
vals: vec![Value::boolean(false, span)],
vals: vec![Value::bool(false, span)],
span,
}),
},

View File

@ -34,7 +34,7 @@ impl Command for IsAdmin {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::boolean(is_root(), call.head).into_pipeline_data())
Ok(Value::bool(is_root(), call.head).into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {

View File

@ -118,8 +118,8 @@ impl Command for DropNth {
rows
}
Either::Right(row_range) => {
let from = row_range.from.as_integer()?; // as usize;
let to = row_range.to.as_integer()?; // as usize;
let from = row_range.from.as_int()?; // as usize;
let to = row_range.to.as_int()?; // as usize;
// check for negative range inputs, e.g., (2..-5)
if from.is_negative() || to.is_negative() {
@ -187,7 +187,7 @@ fn extract_int_or_range(
) -> Result<Either<i64, Range>, ShellError> {
let value = call.req::<Value>(engine_state, stack, 0)?;
let int_opt = value.as_integer().map(Either::Left).ok();
let int_opt = value.as_int().map(Either::Left).ok();
let range_opt: Result<nu_protocol::Range, ShellError> = FromValue::from_value(&value);
let range_opt = range_opt.map(Either::Right).ok();

View File

@ -76,13 +76,13 @@ fn empty(
let val = val.clone();
match val.follow_cell_path(&column.members, false) {
Ok(Value::Nothing { .. }) => {}
Ok(_) => return Ok(Value::boolean(false, head).into_pipeline_data()),
Ok(_) => return Ok(Value::bool(false, head).into_pipeline_data()),
Err(err) => return Err(err),
}
}
}
Ok(Value::boolean(true, head).into_pipeline_data())
Ok(Value::bool(true, head).into_pipeline_data())
} else {
match input {
PipelineData::Empty => Ok(PipelineData::Empty),
@ -91,17 +91,17 @@ fn empty(
let bytes = s.into_bytes();
match bytes {
Ok(s) => Ok(Value::boolean(s.item.is_empty(), head).into_pipeline_data()),
Ok(s) => Ok(Value::bool(s.item.is_empty(), head).into_pipeline_data()),
Err(err) => Err(err),
}
}
None => Ok(Value::boolean(true, head).into_pipeline_data()),
None => Ok(Value::bool(true, head).into_pipeline_data()),
},
PipelineData::ListStream(s, ..) => {
Ok(Value::boolean(s.count() == 0, head).into_pipeline_data())
Ok(Value::bool(s.count() == 0, head).into_pipeline_data())
}
PipelineData::Value(value, ..) => {
Ok(Value::boolean(value.is_empty(), head).into_pipeline_data())
Ok(Value::bool(value.is_empty(), head).into_pipeline_data())
}
}
}

View File

@ -145,7 +145,7 @@ fn from_ods(
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};

View File

@ -144,7 +144,7 @@ fn from_xlsx(
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
DataType::Bool(b) => Value::bool(*b, head),
_ => Value::nothing(head),
};

View File

@ -77,9 +77,9 @@ fn integer(
let (min, max) = if let Some(r) = range {
if r.is_end_inclusive() {
(r.from.as_integer()?, r.to.as_integer()?)
} else if r.to.as_integer()? > 0 {
(r.from.as_integer()?, r.to.as_integer()? - 1)
(r.from.as_int()?, r.to.as_int()?)
} else if r.to.as_int()? > 0 {
(r.from.as_int()?, r.to.as_int()? - 1)
} else {
(0, 0)
}

View File

@ -164,7 +164,7 @@ fn action(
head: Span,
) -> Value {
match input {
Value::String { val, .. } => Value::boolean(
Value::String { val, .. } => Value::bool(
match case_insensitive {
true => {
if *not_contain {

View File

@ -94,7 +94,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
} else {
s.ends_with(&args.substring)
};
Value::boolean(ends_with, head)
Value::bool(ends_with, head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {

View File

@ -110,7 +110,7 @@ fn action(
} else {
s.starts_with(substring)
};
Value::boolean(starts_with, head)
Value::bool(starts_with, head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {

View File

@ -246,7 +246,7 @@ fn heuristic_parse(
Vec::new(),
))
} else {
Ok(PipelineData::Value(Value::boolean(false, span), None))
Ok(PipelineData::Value(Value::bool(false, span), None))
}
}
}
@ -291,7 +291,7 @@ fn heuristic_parse_file(
Vec::new(),
))
} else {
Ok(PipelineData::Value(Value::boolean(false, call.head), None))
Ok(PipelineData::Value(Value::bool(false, call.head), None))
}
}
}
@ -337,10 +337,10 @@ fn parse_module(
Vec::new(),
))
} else {
Ok(PipelineData::Value(Value::boolean(false, new_span), None))
Ok(PipelineData::Value(Value::bool(false, new_span), None))
}
} else {
Ok(PipelineData::Value(Value::boolean(true, new_span), None))
Ok(PipelineData::Value(Value::bool(true, new_span), None))
}
}
@ -370,10 +370,10 @@ fn parse_script(
Vec::new(),
))
} else {
Ok(PipelineData::Value(Value::boolean(false, span), None))
Ok(PipelineData::Value(Value::bool(false, span), None))
}
} else {
Ok(PipelineData::Value(Value::boolean(true, span), None))
Ok(PipelineData::Value(Value::bool(true, span), None))
}
}