From b4eb32f1db8c7166718ff9210ab619f3d6508b4f Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 19 Jun 2019 17:51:24 +1200 Subject: [PATCH] Touchups to external values --- src/commands/classified.rs | 14 +++++--------- src/object/base.rs | 7 ++++++- tests/external_num.out | 1 + tests/external_num.txt | 3 +++ tests/test.json | 22 +++++++++++++--------- tests/tests.rs | 6 ++++++ 6 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 tests/external_num.out create mode 100644 tests/external_num.txt diff --git a/src/commands/classified.rs b/src/commands/classified.rs index 6d22962cc1..bb476187ee 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -240,15 +240,11 @@ impl ExternalCommand { span = Some(arg.span); } } - if let Some(span) = span { - return Err(ShellError::labeled_error( - "External $it needs string data", - "given object instead of string data", - span, - )); - } else { - return Err(ShellError::string("Error: $it needs string data")); - } + return Err(ShellError::maybe_labeled_error( + "External $it needs string data", + "given object instead of string data", + span, + )); } if !first { new_arg_string.push_str("&&"); diff --git a/src/object/base.rs b/src/object/base.rs index 506de9e8d4..d9a531387c 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -291,7 +291,12 @@ impl Value { crate fn as_string(&self) -> Result { match self { - Value::Primitive(Primitive::String(s)) => Ok(s.clone()), + Value::Primitive(Primitive::String(x)) => Ok(format!("{}", x)), + Value::Primitive(Primitive::Boolean(x)) => Ok(format!("{}", x)), + Value::Primitive(Primitive::Float(x)) => Ok(format!("{}", x.into_inner())), + Value::Primitive(Primitive::Int(x)) => Ok(format!("{}", x)), + Value::Primitive(Primitive::Bytes(x)) => Ok(format!("{}", x)), + //Value::Primitive(Primitive::String(s)) => Ok(s.clone()), // TODO: this should definitely be more general with better errors other => Err(ShellError::string(format!( "Expected string, got {:?}", diff --git a/tests/external_num.out b/tests/external_num.out new file mode 100644 index 0000000000..f599e28b8a --- /dev/null +++ b/tests/external_num.out @@ -0,0 +1 @@ +10 diff --git a/tests/external_num.txt b/tests/external_num.txt new file mode 100644 index 0000000000..b7a3d149dd --- /dev/null +++ b/tests/external_num.txt @@ -0,0 +1,3 @@ +cd tests +open test.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | echo $it +exit diff --git a/tests/test.json b/tests/test.json index d5ca56d195..8feebaa0ab 100644 --- a/tests/test.json +++ b/tests/test.json @@ -1,20 +1,24 @@ { "glossary": { "title": "example glossary", - "GlossDiv": { + "GlossDiv": { "title": "S", - "GlossList": { + "GlossList": { "GlossEntry": { "ID": "SGML", - "SortAs": "SGML", - "GlossTerm": "Standard Generalized Markup Language", - "Acronym": "SGML", - "Abbrev": "ISO 8879:1986", - "GlossDef": { + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "Height": 10, + "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", - "GlossSeeAlso": ["GML", "XML"] + "GlossSeeAlso": [ + "GML", + "XML" + ] }, - "GlossSee": "markup" + "GlossSee": "markup" } } } diff --git a/tests/tests.rs b/tests/tests.rs index ad84d695f1..802eea0a5a 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -101,4 +101,10 @@ mod tests { fn lines() { test_helper("lines"); } + + + #[test] + fn external_num() { + test_helper("external_num"); + } }