diff --git a/crates/nu-command/src/formats/from/yaml.rs b/crates/nu-command/src/formats/from/yaml.rs index 9400546049..ede932809c 100644 --- a/crates/nu-command/src/formats/from/yaml.rs +++ b/crates/nu-command/src/formats/from/yaml.rs @@ -174,6 +174,30 @@ fn convert_yaml_value_to_nu_value( Value::from(collected) } + serde_yaml::Value::Tagged(t) => { + let tag = &t.tag; + let value = match &t.value { + serde_yaml::Value::String(s) => { + let val = format!("{} {}", tag, s).trim().to_string(); + Value::String { val, span } + } + serde_yaml::Value::Number(n) => { + let val = format!("{} {}", tag, n).trim().to_string(); + Value::String { val, span } + } + serde_yaml::Value::Bool(b) => { + let val = format!("{} {}", tag, b).trim().to_string(); + Value::String { val, span } + } + serde_yaml::Value::Null => { + let val = format!("{}", tag).trim().to_string(); + Value::String { val, span } + } + v => convert_yaml_value_to_nu_value(v, span, val_span)?, + }; + + value + } serde_yaml::Value::Null => Value::nothing(span), x => unimplemented!("Unsupported YAML case: {:?}", x), }) @@ -314,4 +338,63 @@ mod test { test_examples(FromYaml {}) } + + #[test] + fn test_convert_yaml_value_to_nu_value_for_tagged_values() { + struct TestCase { + input: &'static str, + expected: Result, + } + + let test_cases: Vec = vec![ + TestCase { + input: "Key: !Value ${TEST}-Test-role", + expected: Ok(Value::Record { + cols: vec!["Key".to_string()], + vals: vec![Value::test_string("!Value ${TEST}-Test-role")], + span: Span::test_data(), + }), + }, + TestCase { + input: "Key: !Value test-${TEST}", + expected: Ok(Value::Record { + cols: vec!["Key".to_string()], + vals: vec![Value::test_string("!Value test-${TEST}")], + span: Span::test_data(), + }), + }, + TestCase { + input: "Key: !Value", + expected: Ok(Value::Record { + cols: vec!["Key".to_string()], + vals: vec![Value::test_string("!Value")], + span: Span::test_data(), + }), + }, + TestCase { + input: "Key: !True", + expected: Ok(Value::Record { + cols: vec!["Key".to_string()], + vals: vec![Value::test_string("!True")], + span: Span::test_data(), + }), + }, + TestCase { + input: "Key: !123", + expected: Ok(Value::Record { + cols: vec!["Key".to_string()], + vals: vec![Value::test_string("!123")], + span: Span::test_data(), + }), + }, + ]; + + for test_case in test_cases { + let doc = serde_yaml::Deserializer::from_str(test_case.input); + let v: serde_yaml::Value = serde_yaml::Value::deserialize(doc.last().unwrap()).unwrap(); + let result = convert_yaml_value_to_nu_value(&v, Span::test_data(), Span::test_data()); + assert!(result.is_ok()); + assert!(result.ok().unwrap() == test_case.expected.ok().unwrap()); + } + } }