Add `command_prelude` module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
```
This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```
This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 22:17:30 +01:00
|
|
|
use crate::Query;
|
2022-02-01 19:45:48 +01:00
|
|
|
use gjson::Value as gjValue;
|
2024-03-21 12:27:21 +01:00
|
|
|
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
|
2024-03-27 11:59:57 +01:00
|
|
|
use nu_protocol::{Category, LabeledError, Record, Signature, Span, Spanned, SyntaxShape, Value};
|
Reorganize plugin API around commands (#12170)
[Context on
Discord](https://discord.com/channels/601130461678272522/855947301380947968/1216517833312309419)
# Description
This is a significant breaking change to the plugin API, but one I think
is worthwhile. @ayax79 mentioned on Discord that while trying to start
on a dataframes plugin, he was a little disappointed that more wasn't
provided in terms of code organization for commands, particularly since
there are *a lot* of `dfr` commands.
This change treats plugins more like miniatures of the engine, with
dispatch of the command name being handled inherently, each command
being its own type, and each having their own signature within the trait
impl for the command type rather than having to find a way to centralize
it all into one `Vec`.
For the example plugins that have multiple commands, I definitely like
how this looks a lot better. This encourages doing code organization the
right way and feels very good.
For the plugins that have only one command, it's just a little bit more
boilerplate - but still worth it, in my opinion.
The `Box<dyn PluginCommand<Plugin = Self>>` type in `commands()` is a
little bit hairy, particularly for Rust beginners, but ultimately not so
bad, and it gives the desired flexibility for shared state for a whole
plugin + the individual commands.
# User-Facing Changes
Pretty big breaking change to plugin API, but probably one that's worth
making.
```rust
use nu_plugin::*;
use nu_protocol::{PluginSignature, PipelineData, Type, Value};
struct LowercasePlugin;
struct Lowercase;
// Plugins can now have multiple commands
impl PluginCommand for Lowercase {
type Plugin = LowercasePlugin;
// The signature lives with the command
fn signature(&self) -> PluginSignature {
PluginSignature::build("lowercase")
.usage("Convert each string in a stream to lowercase")
.input_output_type(Type::List(Type::String.into()), Type::List(Type::String.into()))
}
// We also provide SimplePluginCommand which operates on Value like before
fn run(
&self,
plugin: &LowercasePlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
let span = call.head;
Ok(input.map(move |value| {
value.as_str()
.map(|string| Value::string(string.to_lowercase(), span))
// Errors in a stream should be returned as values.
.unwrap_or_else(|err| Value::error(err, span))
}, None)?)
}
}
// Plugin now just has a list of commands, and the custom value op stuff still goes here
impl Plugin for LowercasePlugin {
fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin=Self>>> {
vec![Box::new(Lowercase)]
}
}
fn main() {
serve_plugin(&LowercasePlugin{}, MsgPackSerializer)
}
```
Time this however you like - we're already breaking stuff for 0.92, so
it might be good to do it now, but if it feels like a lot all at once,
it could wait.
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# After Submitting
- [ ] Update examples in the book
- [x] Fix #12088 to match - this change would actually simplify it a
lot, because the methods are currently just duplicated between `Plugin`
and `StreamingPlugin`, but they only need to be on `Plugin` with this
change
2024-03-14 22:40:02 +01:00
|
|
|
|
|
|
|
pub struct QueryJson;
|
|
|
|
|
|
|
|
impl SimplePluginCommand for QueryJson {
|
|
|
|
type Plugin = Query;
|
|
|
|
|
2024-03-27 11:59:57 +01:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"query json"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"execute json query on json file (open --raw <file> | query json 'query string')"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
Reorganize plugin API around commands (#12170)
[Context on
Discord](https://discord.com/channels/601130461678272522/855947301380947968/1216517833312309419)
# Description
This is a significant breaking change to the plugin API, but one I think
is worthwhile. @ayax79 mentioned on Discord that while trying to start
on a dataframes plugin, he was a little disappointed that more wasn't
provided in terms of code organization for commands, particularly since
there are *a lot* of `dfr` commands.
This change treats plugins more like miniatures of the engine, with
dispatch of the command name being handled inherently, each command
being its own type, and each having their own signature within the trait
impl for the command type rather than having to find a way to centralize
it all into one `Vec`.
For the example plugins that have multiple commands, I definitely like
how this looks a lot better. This encourages doing code organization the
right way and feels very good.
For the plugins that have only one command, it's just a little bit more
boilerplate - but still worth it, in my opinion.
The `Box<dyn PluginCommand<Plugin = Self>>` type in `commands()` is a
little bit hairy, particularly for Rust beginners, but ultimately not so
bad, and it gives the desired flexibility for shared state for a whole
plugin + the individual commands.
# User-Facing Changes
Pretty big breaking change to plugin API, but probably one that's worth
making.
```rust
use nu_plugin::*;
use nu_protocol::{PluginSignature, PipelineData, Type, Value};
struct LowercasePlugin;
struct Lowercase;
// Plugins can now have multiple commands
impl PluginCommand for Lowercase {
type Plugin = LowercasePlugin;
// The signature lives with the command
fn signature(&self) -> PluginSignature {
PluginSignature::build("lowercase")
.usage("Convert each string in a stream to lowercase")
.input_output_type(Type::List(Type::String.into()), Type::List(Type::String.into()))
}
// We also provide SimplePluginCommand which operates on Value like before
fn run(
&self,
plugin: &LowercasePlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
let span = call.head;
Ok(input.map(move |value| {
value.as_str()
.map(|string| Value::string(string.to_lowercase(), span))
// Errors in a stream should be returned as values.
.unwrap_or_else(|err| Value::error(err, span))
}, None)?)
}
}
// Plugin now just has a list of commands, and the custom value op stuff still goes here
impl Plugin for LowercasePlugin {
fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin=Self>>> {
vec![Box::new(Lowercase)]
}
}
fn main() {
serve_plugin(&LowercasePlugin{}, MsgPackSerializer)
}
```
Time this however you like - we're already breaking stuff for 0.92, so
it might be good to do it now, but if it feels like a lot all at once,
it could wait.
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# After Submitting
- [ ] Update examples in the book
- [x] Fix #12088 to match - this change would actually simplify it a
lot, because the methods are currently just duplicated between `Plugin`
and `StreamingPlugin`, but they only need to be on `Plugin` with this
change
2024-03-14 22:40:02 +01:00
|
|
|
.required("query", SyntaxShape::String, "json query")
|
|
|
|
.category(Category::Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_plugin: &Query,
|
|
|
|
_engine: &EngineInterface,
|
|
|
|
call: &EvaluatedCall,
|
|
|
|
input: &Value,
|
|
|
|
) -> Result<Value, LabeledError> {
|
|
|
|
let query: Option<Spanned<String>> = call.opt(0)?;
|
|
|
|
|
|
|
|
execute_json_query(call, input, query)
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 19:45:48 +01:00
|
|
|
|
|
|
|
pub fn execute_json_query(
|
|
|
|
call: &EvaluatedCall,
|
|
|
|
input: &Value,
|
|
|
|
query: Option<Spanned<String>>,
|
|
|
|
) -> Result<Value, LabeledError> {
|
2024-02-18 17:47:10 +01:00
|
|
|
let input_string = match input.coerce_str() {
|
|
|
|
Ok(s) => s,
|
2022-02-01 19:45:48 +01:00
|
|
|
Err(e) => {
|
2024-03-21 12:27:21 +01:00
|
|
|
return Err(LabeledError::new("Problem with input data").with_inner(e));
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let query_string = match &query {
|
|
|
|
Some(v) => &v.item,
|
|
|
|
None => {
|
2024-03-21 12:27:21 +01:00
|
|
|
return Err(LabeledError::new("Problem with input data")
|
|
|
|
.with_label("query string missing", call.head));
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Validate the json before trying to query it
|
|
|
|
let is_valid_json = gjson::valid(&input_string);
|
|
|
|
|
|
|
|
if !is_valid_json {
|
2024-03-21 12:27:21 +01:00
|
|
|
return Err(
|
|
|
|
LabeledError::new("Invalid JSON").with_label("this is not valid JSON", call.head)
|
|
|
|
);
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let val: gjValue = gjson::get(&input_string, query_string);
|
|
|
|
|
|
|
|
if query_contains_modifiers(query_string) {
|
|
|
|
let json_str = val.json();
|
2022-12-24 14:41:57 +01:00
|
|
|
Ok(Value::string(json_str, call.head))
|
2022-02-01 19:45:48 +01:00
|
|
|
} else {
|
2023-07-31 21:47:46 +02:00
|
|
|
Ok(convert_gjson_value_to_nu_value(&val, call.head))
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn query_contains_modifiers(query: &str) -> bool {
|
|
|
|
// https://github.com/tidwall/gjson.rs documents 7 modifiers as of 4/19/21
|
|
|
|
// Some of these modifiers mean we really need to output the data as a string
|
|
|
|
// instead of tabular data. Others don't matter.
|
|
|
|
|
|
|
|
// Output as String
|
|
|
|
// @ugly: Remove all whitespace from a json document.
|
|
|
|
// @pretty: Make the json document more human readable.
|
|
|
|
query.contains("@ugly") || query.contains("@pretty")
|
|
|
|
|
2023-01-16 12:43:46 +01:00
|
|
|
// Output as Tabular
|
2022-02-01 19:45:48 +01:00
|
|
|
// Since it's output as tabular, which is our default, we can just ignore these
|
|
|
|
// @reverse: Reverse an array or the members of an object.
|
|
|
|
// @this: Returns the current element. It can be used to retrieve the root element.
|
|
|
|
// @valid: Ensure the json document is valid.
|
|
|
|
// @flatten: Flattens an array.
|
|
|
|
// @join: Joins multiple objects into a single object.
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:47:46 +02:00
|
|
|
fn convert_gjson_value_to_nu_value(v: &gjValue, span: Span) -> Value {
|
2022-02-01 19:45:48 +01:00
|
|
|
match v.kind() {
|
|
|
|
gjson::Kind::Array => {
|
|
|
|
let mut vals = vec![];
|
|
|
|
v.each(|_k, v| {
|
|
|
|
vals.push(convert_gjson_value_to_nu_value(&v, span));
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::list(vals, span)
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
2023-07-31 21:47:46 +02:00
|
|
|
gjson::Kind::Null => Value::nothing(span),
|
|
|
|
gjson::Kind::False => Value::bool(false, span),
|
2022-02-01 19:45:48 +01:00
|
|
|
gjson::Kind::Number => {
|
|
|
|
let str_value = v.str();
|
|
|
|
if str_value.contains('.') {
|
2023-07-31 21:47:46 +02:00
|
|
|
Value::float(v.f64(), span)
|
2022-02-01 19:45:48 +01:00
|
|
|
} else {
|
2023-07-31 21:47:46 +02:00
|
|
|
Value::int(v.i64(), span)
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-31 21:47:46 +02:00
|
|
|
gjson::Kind::String => Value::string(v.str(), span),
|
|
|
|
gjson::Kind::True => Value::bool(true, span),
|
2022-02-01 19:45:48 +01:00
|
|
|
gjson::Kind::Object => {
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 21:50:29 +02:00
|
|
|
let mut record = Record::new();
|
2022-02-01 19:45:48 +01:00
|
|
|
v.each(|k, v| {
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 21:50:29 +02:00
|
|
|
record.push(k.to_string(), convert_gjson_value_to_nu_value(&v, span));
|
2022-02-01 19:45:48 +01:00
|
|
|
true
|
|
|
|
});
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 21:50:29 +02:00
|
|
|
Value::record(record, span)
|
2022-02-01 19:45:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use gjson::{valid, Value as gjValue};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validate_string() {
|
|
|
|
let json = r#"{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "friends": [ { "first": "James", "last": "Murphy" }, { "first": "Roger", "last": "Craig" } ] }"#;
|
|
|
|
let val = valid(json);
|
|
|
|
assert!(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn answer_from_get_age() {
|
|
|
|
let json = r#"{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "friends": [ { "first": "James", "last": "Murphy" }, { "first": "Roger", "last": "Craig" } ] }"#;
|
|
|
|
let val: gjValue = gjson::get(json, "age");
|
|
|
|
assert_eq!(val.str(), "37");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn answer_from_get_children() {
|
|
|
|
let json = r#"{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "friends": [ { "first": "James", "last": "Murphy" }, { "first": "Roger", "last": "Craig" } ] }"#;
|
|
|
|
let val: gjValue = gjson::get(json, "children");
|
|
|
|
assert_eq!(val.str(), r#"["Sara", "Alex", "Jack"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn answer_from_get_children_count() {
|
|
|
|
let json = r#"{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "friends": [ { "first": "James", "last": "Murphy" }, { "first": "Roger", "last": "Craig" } ] }"#;
|
|
|
|
let val: gjValue = gjson::get(json, "children.#");
|
|
|
|
assert_eq!(val.str(), "3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn answer_from_get_friends_first_name() {
|
|
|
|
let json = r#"{ "name": { "first": "Tom", "last": "Anderson" }, "age": 37, "children": ["Sara", "Alex", "Jack"], "friends": [ { "first": "James", "last": "Murphy" }, { "first": "Roger", "last": "Craig" } ] }"#;
|
|
|
|
let val: gjValue = gjson::get(json, "friends.#.first");
|
|
|
|
assert_eq!(val.str(), r#"["James","Roger"]"#);
|
|
|
|
}
|
|
|
|
}
|