2019-05-28 06:00:00 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-05-28 06:00:00 +02:00
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct FromJSON;
|
|
|
|
|
2019-08-27 13:05:51 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct FromJSONArgs {
|
|
|
|
objects: bool,
|
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-08-27 13:05:51 +02:00
|
|
|
impl WholeStreamCommand for FromJSON {
|
2019-08-19 07:16:39 +02:00
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from json"
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from json").switch(
|
2020-02-12 03:24:31 +01:00
|
|
|
"objects",
|
|
|
|
"treat each line as a separate value",
|
|
|
|
Some('o'),
|
|
|
|
)
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .json and create table."
|
2019-08-27 13:05:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
from_json(args).await
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 01:37:16 +01:00
|
|
|
fn convert_json_value_to_nu_value(v: &nu_json::Value, tag: impl Into<Tag>) -> Value {
|
2019-08-05 10:54:29 +02:00
|
|
|
let tag = tag.into();
|
2020-09-21 19:28:31 +02:00
|
|
|
let span = tag.span;
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-05-28 06:00:00 +02:00
|
|
|
match v {
|
2020-11-22 01:37:16 +01:00
|
|
|
nu_json::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag),
|
|
|
|
nu_json::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag),
|
|
|
|
nu_json::Value::F64(n) => UntaggedValue::decimal_from_float(*n, span).into_value(&tag),
|
|
|
|
nu_json::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag),
|
|
|
|
nu_json::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag),
|
|
|
|
nu_json::Value::String(s) => {
|
2019-11-21 15:33:14 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
2020-11-22 01:37:16 +01:00
|
|
|
nu_json::Value::Array(a) => UntaggedValue::Table(
|
2019-06-03 09:41:28 +02:00
|
|
|
a.iter()
|
2019-10-13 06:12:43 +02:00
|
|
|
.map(|x| convert_json_value_to_nu_value(x, &tag))
|
2019-06-03 09:41:28 +02:00
|
|
|
.collect(),
|
2019-07-09 06:31:26 +02:00
|
|
|
)
|
2019-11-21 15:33:14 +01:00
|
|
|
.into_value(tag),
|
2020-11-22 01:37:16 +01:00
|
|
|
nu_json::Value::Object(o) => {
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut collected = TaggedDictBuilder::new(&tag);
|
2019-05-28 06:00:00 +02:00
|
|
|
for (k, v) in o.iter() {
|
2019-11-21 15:33:14 +01:00
|
|
|
collected.insert_value(k.clone(), convert_json_value_to_nu_value(v, &tag));
|
2019-05-28 06:00:00 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
collected.into_value()
|
2019-05-28 06:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 01:37:16 +01:00
|
|
|
pub fn from_json_string_to_value(s: String, tag: impl Into<Tag>) -> nu_json::Result<Value> {
|
|
|
|
let v: nu_json::Value = nu_json::from_str(&s)?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(convert_json_value_to_nu_value(&v, tag))
|
2019-06-01 21:20:48 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn from_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let name_tag = args.call_info.name_tag.clone();
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
let (FromJSONArgs { objects }, input) = args.process().await?;
|
2020-06-13 10:43:21 +02:00
|
|
|
let concat_string = input.collect_string(name_tag.clone()).await?;
|
2019-08-27 13:05:51 +02:00
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
let string_clone: Vec<_> = concat_string.item.lines().map(|x| x.to_string()).collect();
|
|
|
|
|
|
|
|
if objects {
|
|
|
|
Ok(
|
|
|
|
futures::stream::iter(string_clone.into_iter().filter_map(move |json_str| {
|
2019-08-27 13:05:51 +02:00
|
|
|
if json_str.is_empty() {
|
2020-06-13 10:43:21 +02:00
|
|
|
return None;
|
2019-08-27 13:05:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
match from_json_string_to_value(json_str, &name_tag) {
|
|
|
|
Ok(x) => Some(ReturnSuccess::value(x)),
|
2020-01-25 04:13:12 +01:00
|
|
|
Err(e) => {
|
2020-03-06 17:06:39 +01:00
|
|
|
let mut message = "Could not parse as JSON (".to_string();
|
|
|
|
message.push_str(&e.to_string());
|
2020-11-22 01:37:16 +01:00
|
|
|
message.push(')');
|
2020-03-06 17:06:39 +01:00
|
|
|
|
2020-06-13 10:43:21 +02:00
|
|
|
Some(Err(ShellError::labeled_error_with_secondary(
|
2020-03-06 17:06:39 +01:00
|
|
|
message,
|
|
|
|
"input cannot be parsed as JSON",
|
2020-06-13 10:43:21 +02:00
|
|
|
name_tag.clone(),
|
2020-03-06 17:06:39 +01:00
|
|
|
"value originates from here",
|
2020-06-13 10:43:21 +02:00
|
|
|
concat_string.tag.clone(),
|
|
|
|
)))
|
2019-08-24 09:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-13 10:43:21 +02:00
|
|
|
}))
|
|
|
|
.to_output_stream(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
match from_json_string_to_value(concat_string.item, name_tag.clone()) {
|
|
|
|
Ok(x) => match x {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(list),
|
|
|
|
..
|
|
|
|
} => Ok(
|
|
|
|
futures::stream::iter(list.into_iter().map(ReturnSuccess::value))
|
|
|
|
.to_output_stream(),
|
|
|
|
),
|
|
|
|
x => Ok(OutputStream::one(ReturnSuccess::value(x))),
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
let mut message = "Could not parse as JSON (".to_string();
|
|
|
|
message.push_str(&e.to_string());
|
2020-11-22 01:37:16 +01:00
|
|
|
message.push(')');
|
2020-06-13 10:43:21 +02:00
|
|
|
|
|
|
|
Ok(OutputStream::one(Err(
|
|
|
|
ShellError::labeled_error_with_secondary(
|
2020-03-06 17:06:39 +01:00
|
|
|
message,
|
|
|
|
"input cannot be parsed as JSON",
|
|
|
|
name_tag,
|
|
|
|
"value originates from here",
|
2020-06-13 10:43:21 +02:00
|
|
|
concat_string.tag,
|
|
|
|
),
|
|
|
|
)))
|
2019-08-27 13:05:51 +02:00
|
|
|
}
|
2019-08-21 08:39:57 +02:00
|
|
|
}
|
2020-06-13 10:43:21 +02:00
|
|
|
}
|
2019-05-28 06:00:00 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::FromJSON;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(FromJSON {})?)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|