Rename Value::CustomValue to Value::Custom (#12309)

# Description
The second `Value` is redundant and will consume five extra bytes on
each transmission of a custom value to/from a plugin.

# User-Facing Changes
This is a breaking change to the plugin protocol.

The [example in the protocol
reference](https://www.nushell.sh/contributor-book/plugin_protocol_reference.html#value)
becomes

```json
{
  "Custom": {
    "val": {
      "type": "PluginCustomValue",
      "name": "database",
      "data": [36, 190, 127, 40, 12, 3, 46, 83],
      "notify_on_drop": true
    },
    "span": {
      "start": 320,
      "end": 340
    }
  }
}
```

instead of 

```json
{
  "CustomValue": {
    ...
  }
}
```


# After Submitting
Update plugin protocol reference
This commit is contained in:
Stefan Holderbach
2024-03-27 22:10:56 +01:00
committed by GitHub
parent dfbbacfdf8
commit b19da158d5
48 changed files with 144 additions and 148 deletions

View File

@ -609,7 +609,7 @@ fn manager_prepare_pipeline_data_embeds_deserialization_errors_in_streams() -> R
let span = Span::new(20, 30);
let data = manager.prepare_pipeline_data(
[Value::custom_value(Box::new(invalid_custom_value), span)].into_pipeline_data(None),
[Value::custom(Box::new(invalid_custom_value), span)].into_pipeline_data(None),
)?;
let value = data
@ -1147,7 +1147,7 @@ enum CantSerialize {
#[typetag::serde]
impl CustomValue for CantSerialize {
fn clone_value(&self, span: Span) -> Value {
Value::custom_value(Box::new(self.clone()), span)
Value::custom(Box::new(self.clone()), span)
}
fn type_name(&self) -> String {
@ -1170,11 +1170,7 @@ fn interface_prepare_pipeline_data_embeds_serialization_errors_in_streams() -> R
let span = Span::new(40, 60);
let data = interface.prepare_pipeline_data(
[Value::custom_value(
Box::new(CantSerialize::BadVariant),
span,
)]
.into_pipeline_data(None),
[Value::custom(Box::new(CantSerialize::BadVariant), span)].into_pipeline_data(None),
)?;
let value = data

View File

@ -53,7 +53,7 @@ fn is_false(b: &bool) -> bool {
#[typetag::serde]
impl CustomValue for PluginCustomValue {
fn clone_value(&self, span: Span) -> Value {
Value::custom_value(Box::new(self.clone()), span)
Value::custom(Box::new(self.clone()), span)
}
fn type_name(&self) -> String {
@ -241,12 +241,12 @@ impl PluginCustomValue {
let span = value.span();
match value {
// Set source on custom value
Value::CustomValue { ref val, .. } => {
Value::Custom { ref val, .. } => {
if let Some(custom_value) = val.as_any().downcast_ref::<PluginCustomValue>() {
// Since there's no `as_mut_any()`, we have to copy the whole thing
let mut custom_value = custom_value.clone();
custom_value.source = Some(source.clone());
*value = Value::custom_value(Box::new(custom_value), span);
*value = Value::custom(Box::new(custom_value), span);
}
Ok(())
}
@ -272,7 +272,7 @@ impl PluginCustomValue {
let span = value.span();
match value {
// Set source on custom value
Value::CustomValue { val, .. } => {
Value::Custom { val, .. } => {
if let Some(custom_value) = val.as_any().downcast_ref::<PluginCustomValue>() {
if custom_value
.source
@ -320,13 +320,13 @@ impl PluginCustomValue {
value.recurse_mut(&mut |value| {
let span = value.span();
match value {
Value::CustomValue { ref val, .. } => {
Value::Custom { ref val, .. } => {
if val.as_any().downcast_ref::<PluginCustomValue>().is_some() {
// Already a PluginCustomValue
Ok(())
} else {
let serialized = Self::serialize_from_custom_value(&**val, span)?;
*value = Value::custom_value(Box::new(serialized), span);
*value = Value::custom(Box::new(serialized), span);
Ok(())
}
}
@ -346,10 +346,10 @@ impl PluginCustomValue {
value.recurse_mut(&mut |value| {
let span = value.span();
match value {
Value::CustomValue { ref val, .. } => {
Value::Custom { ref val, .. } => {
if let Some(val) = val.as_any().downcast_ref::<PluginCustomValue>() {
let deserialized = val.deserialize_to_custom_value(span)?;
*value = Value::custom_value(deserialized, span);
*value = Value::custom(deserialized, span);
Ok(())
} else {
// Already not a PluginCustomValue
@ -371,7 +371,7 @@ impl PluginCustomValue {
value.recurse_mut(&mut |value| {
let span = value.span();
match value {
Value::CustomValue { ref val, .. } => {
Value::Custom { ref val, .. } => {
*value = val.to_base_value(span)?;
Ok(())
}

View File

@ -231,12 +231,12 @@ fn add_source_nested_closure() -> Result<(), ShellError> {
#[test]
fn verify_source_error_message() -> Result<(), ShellError> {
let span = Span::new(5, 7);
let mut ok_val = Value::custom_value(Box::new(test_plugin_custom_value_with_source()), span);
let mut native_val = Value::custom_value(Box::new(TestCustomValue(32)), span);
let mut ok_val = Value::custom(Box::new(test_plugin_custom_value_with_source()), span);
let mut native_val = Value::custom(Box::new(TestCustomValue(32)), span);
let mut foreign_val = {
let mut val = test_plugin_custom_value();
val.source = Some(Arc::new(PluginSource::new_fake("other")));
Value::custom_value(Box::new(val), span)
Value::custom(Box::new(val), span)
};
let source = PluginSource::new_fake("test");
@ -407,7 +407,7 @@ fn verify_source_nested_closure() -> Result<(), ShellError> {
#[test]
fn serialize_in_root() -> Result<(), ShellError> {
let span = Span::new(4, 10);
let mut val = Value::custom_value(Box::new(expected_test_custom_value()), span);
let mut val = Value::custom(Box::new(expected_test_custom_value()), span);
PluginCustomValue::serialize_custom_values_in(&mut val)?;
assert_eq!(span, val.span());
@ -520,7 +520,7 @@ fn serialize_in_closure() -> Result<(), ShellError> {
#[test]
fn deserialize_in_root() -> Result<(), ShellError> {
let span = Span::new(4, 10);
let mut val = Value::custom_value(Box::new(test_plugin_custom_value()), span);
let mut val = Value::custom(Box::new(test_plugin_custom_value()), span);
PluginCustomValue::deserialize_custom_values_in(&mut val)?;
assert_eq!(span, val.span());

View File

@ -9,7 +9,7 @@ pub(crate) struct TestCustomValue(pub i32);
#[typetag::serde]
impl CustomValue for TestCustomValue {
fn clone_value(&self, span: Span) -> Value {
Value::custom_value(Box::new(self.clone()), span)
Value::custom(Box::new(self.clone()), span)
}
fn type_name(&self) -> String {

View File

@ -320,7 +320,7 @@ macro_rules! generate_tests {
let data = vec![1, 2, 3, 4, 5];
let span = Span::new(2, 30);
let value = Value::custom_value(
let value = Value::custom(
Box::new(PluginCustomValue::new(
name.into(),
data.clone(),