mirror of
https://github.com/nushell/nushell.git
synced 2025-04-11 06:48:31 +02:00
Fix setting metadata on byte streams (#13416)
# Description Setting metadata on a byte stream converts it to a list stream. This PR makes it so that `metadata set` does not alter its input besides the metadata. ```nushell open --raw test.json | metadata set --content-type application/json | http post https://httpbin.org/post ```
This commit is contained in:
parent
9ab706db62
commit
6fcd09682c
@ -42,32 +42,32 @@ impl Command for MetadataSet {
|
|||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
mut input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let head = call.head;
|
let head = call.head;
|
||||||
let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
|
let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
|
||||||
let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
|
let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
|
||||||
let content_type: Option<String> = call.get_flag(engine_state, stack, "content-type")?;
|
let content_type: Option<String> = call.get_flag(engine_state, stack, "content-type")?;
|
||||||
let signals = engine_state.signals().clone();
|
|
||||||
let metadata = input
|
let mut metadata = match &mut input {
|
||||||
.metadata()
|
PipelineData::Value(_, metadata)
|
||||||
.clone()
|
| PipelineData::ListStream(_, metadata)
|
||||||
.unwrap_or_default()
|
| PipelineData::ByteStream(_, metadata) => metadata.take().unwrap_or_default(),
|
||||||
.with_content_type(content_type);
|
PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span: head }),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(content_type) = content_type {
|
||||||
|
metadata.content_type = Some(content_type);
|
||||||
|
}
|
||||||
|
|
||||||
match (ds_fp, ds_ls) {
|
match (ds_fp, ds_ls) {
|
||||||
(Some(path), false) => Ok(input.into_pipeline_data_with_metadata(
|
(Some(path), false) => metadata.data_source = DataSource::FilePath(path.into()),
|
||||||
head,
|
(None, true) => metadata.data_source = DataSource::Ls,
|
||||||
signals,
|
(Some(_), true) => (), // TODO: error here
|
||||||
metadata.with_data_source(DataSource::FilePath(path.into())),
|
(None, false) => (),
|
||||||
)),
|
|
||||||
(None, true) => Ok(input.into_pipeline_data_with_metadata(
|
|
||||||
head,
|
|
||||||
signals,
|
|
||||||
metadata.with_data_source(DataSource::Ls),
|
|
||||||
)),
|
|
||||||
_ => Ok(input.into_pipeline_data_with_metadata(head, signals, metadata)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(input.set_metadata(Some(metadata)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
@ -85,7 +85,9 @@ impl Command for MetadataSet {
|
|||||||
Example {
|
Example {
|
||||||
description: "Set the metadata of a file path",
|
description: "Set the metadata of a file path",
|
||||||
example: "'crates' | metadata set --content-type text/plain | metadata",
|
example: "'crates' | metadata set --content-type text/plain | metadata",
|
||||||
result: Some(Value::record(record!("content_type" => Value::string("text/plain", Span::test_data())), Span::test_data())),
|
result: Some(Value::test_record(record! {
|
||||||
|
"content_type" => Value::test_string("text/plain"),
|
||||||
|
})),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user