Into binary changes (#3758)

* kind of works but not what we really want

* updated `into binary` and `first` to work better together

* attempt to fix wasm build problem

* attempt #2 to fix wasm stuff
This commit is contained in:
Darren Schroeder
2021-07-09 16:43:18 -05:00
committed by GitHub
parent 3262ffc1a6
commit 56c7a99eb4
6 changed files with 125 additions and 136 deletions

View File

@ -12,23 +12,10 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("into binary")
.rest(
SyntaxShape::ColumnPath,
"column paths to convert to binary (for table input)",
)
.named(
"skip",
SyntaxShape::Int,
"skip x number of bytes",
Some('s'),
)
.named(
"bytes",
SyntaxShape::Int,
"show y number of bytes",
Some('b'),
)
Signature::build("into binary").rest(
SyntaxShape::ColumnPath,
"column paths to convert to binary (for table input)",
)
}
fn usage(&self) -> &str {
@ -53,43 +40,21 @@ impl WholeStreamCommand for SubCommand {
)
.into()]),
},
Example {
description: "convert string to a nushell binary primitive",
example:
"echo 'This is a string that is exactly 52 characters long.' | into binary --skip 10",
result: Some(vec![UntaggedValue::binary(
"string that is exactly 52 characters long."
.to_string()
.as_bytes()
.to_vec(),
)
.into()]),
},
Example {
description: "convert string to a nushell binary primitive",
example:
"echo 'This is a string that is exactly 52 characters long.' | into binary --skip 10 --bytes 10",
result: Some(vec![UntaggedValue::binary(
"string tha"
.to_string()
.as_bytes()
.to_vec(),
)
.into()]),
},
Example {
description: "convert a number to a nushell binary primitive",
example: "echo 1 | into binary",
result: Some(vec![
UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
]),
result: Some(vec![UntaggedValue::binary(
i64::from(1).to_le_bytes().to_vec(),
)
.into()]),
},
Example {
description: "convert a boolean to a nushell binary primitive",
example: "echo $true | into binary",
result: Some(vec![
UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
]),
result: Some(vec![UntaggedValue::binary(
i64::from(1).to_le_bytes().to_vec(),
)
.into()]),
},
Example {
description: "convert a filesize to a nushell binary primitive",
@ -113,23 +78,19 @@ impl WholeStreamCommand for SubCommand {
}
fn into_binary(args: CommandArgs) -> Result<OutputStream, ShellError> {
let skip: Option<Value> = args.get_flag("skip")?;
let bytes: Option<Value> = args.get_flag("bytes")?;
let column_paths: Vec<ColumnPath> = args.rest(0)?;
Ok(args
.input
.map(move |v| {
if column_paths.is_empty() {
action(&v, v.tag(), &skip, &bytes)
action(&v, v.tag())
} else {
let mut ret = v;
for path in &column_paths {
let skip_clone = skip.clone();
let bytes_clone = bytes.clone();
ret = ret.swap_data_by_column_path(
path,
Box::new(move |old| action(old, old.tag(), &skip_clone, &bytes_clone)),
Box::new(move |old| action(old, old.tag())),
)?;
}
@ -141,54 +102,26 @@ fn into_binary(args: CommandArgs) -> Result<OutputStream, ShellError> {
fn int_to_endian(n: i64) -> Vec<u8> {
if cfg!(target_endian = "little") {
// eprintln!("Little Endian");
n.to_le_bytes().to_vec()
} else {
// eprintln!("Big Endian");
n.to_be_bytes().to_vec()
}
}
fn bigint_to_endian(n: &BigInt) -> Vec<u8> {
if cfg!(target_endian = "little") {
// eprintln!("Little Endian");
n.to_bytes_le().1
} else {
// eprintln!("Big Endian");
n.to_bytes_be().1
}
}
pub fn action(
input: &Value,
tag: impl Into<Tag>,
skip: &Option<Value>,
bytes: &Option<Value>,
) -> Result<Value, ShellError> {
pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
let tag = tag.into();
let skip_bytes = match skip {
Some(s) => s.as_usize().unwrap_or(0),
None => 0usize,
};
let num_bytes = match bytes {
Some(b) => b.as_usize().unwrap_or(0),
None => 0usize,
};
match &input.value {
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::binary(match prim {
Primitive::Binary(b) => {
if num_bytes == 0usize {
b.to_vec().into_iter().skip(skip_bytes).collect()
} else {
b.to_vec()
.into_iter()
.skip(skip_bytes)
.take(num_bytes)
.collect()
}
}
Primitive::Binary(b) => b.to_vec(),
Primitive::Int(n_ref) => int_to_endian(*n_ref),
Primitive::BigInt(n_ref) => bigint_to_endian(n_ref),
Primitive::Decimal(dec) => match dec.to_bigint() {
@ -207,25 +140,7 @@ pub fn action(
));
}
},
Primitive::String(a_string) => {
// a_string.as_bytes().to_vec()
if num_bytes == 0usize {
a_string
.as_bytes()
.to_vec()
.into_iter()
.skip(skip_bytes)
.collect()
} else {
a_string
.as_bytes()
.to_vec()
.into_iter()
.skip(skip_bytes)
.take(num_bytes)
.collect()
}
}
Primitive::String(a_string) => a_string.as_bytes().to_vec(),
Primitive::Boolean(a_bool) => match a_bool {
false => int_to_endian(0),
true => int_to_endian(1),

View File

@ -23,7 +23,7 @@ impl WholeStreamCommand for First {
"Show only the first number of rows."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
first(args)
}
@ -46,17 +46,94 @@ impl WholeStreamCommand for First {
}
}
fn first(args: CommandArgs) -> Result<ActionStream, ShellError> {
fn first(args: CommandArgs) -> Result<OutputStream, ShellError> {
let rows: Option<Tagged<usize>> = args.opt(0)?;
let input = args.input;
let tag = args.call_info.name_tag;
let rows_desired = if let Some(quantity) = rows {
let mut rows_desired = if let Some(quantity) = rows {
*quantity
} else {
1
};
Ok(input.take(rows_desired).into_action_stream())
let mut input_peek = args.input.peekable();
match &mut input_peek.next_if(|val| val.is_binary()) {
Some(v) => match &v.value {
// We already know it's a binary so we don't have to match
// on the type of primitive
UntaggedValue::Primitive(_) => {
let bytes = match v.as_binary_vec() {
Ok(b) => b,
_ => {
return Err(ShellError::labeled_error(
"error converting data as_binary_vec",
"error conversion",
tag,
))
}
};
// if the current 8192 chunk fits inside our rows_desired
// carve it up and return it
if bytes.len() >= rows_desired {
// We only want to see a certain amount of the binary
// so let's grab those parts
let output_bytes = bytes[0..rows_desired].to_vec();
Ok(OutputStream::one(UntaggedValue::binary(output_bytes)))
} else {
// if we want more rows that the current chunk size (8192)
// we must gradually get bigger chunks while testing
// if it's within the requested rows_desired size
let mut bigger: Vec<u8> = vec![];
bigger.extend(bytes);
while bigger.len() < rows_desired {
match input_peek.next() {
Some(data) => match data.value.into_value(&tag).as_binary_vec() {
Ok(bits) => bigger.extend(bits),
_ => {
return Err(ShellError::labeled_error(
"error converting data as_binary_vec",
"error conversion",
tag,
))
}
},
_ => {
// We're at the end of our data so let's break out of this loop
// and set the rows_desired to the size of our data
rows_desired = bigger.len();
break;
}
}
}
let output_bytes = bigger[0..rows_desired].to_vec();
Ok(OutputStream::one(UntaggedValue::binary(output_bytes)))
}
}
UntaggedValue::Row(_) => Ok(input_peek.take(rows_desired).into_output_stream()),
UntaggedValue::Table(_) => Err(ShellError::labeled_error(
"unsure how to handle UntaggedValue::Table",
"found table",
tag,
)),
UntaggedValue::Error(_) => Err(ShellError::labeled_error(
"unsure how to handle UntaggedValue::Error",
"found error",
tag,
)),
UntaggedValue::Block(_) => Err(ShellError::labeled_error(
"unsure how to handled UntaggedValue::Block",
"found block",
tag,
)),
#[cfg(all(not(target_arch = "wasm32"), feature = "dataframe"))]
UntaggedValue::DataFrame(_) => Err(ShellError::labeled_error(
"unsure how to handled UntaggedValue::DataFrame",
"found dataframe",
tag,
)),
},
None => Ok(input_peek.take(rows_desired).into_output_stream()),
}
}
#[cfg(test)]