Skip to content

Commit 106ca65

Browse files
Added fix for bug #8278 to read tag values from YAML files (#8354)
# Description This PR adds a fix for reading tag values from YAML file. A tag in YAML file is denoted by using the exclamation point ("!") symbol. For example - Key: !Value Additional passing test has also been added supporting the bug fix - - `test_convert_yaml_value_to_nu_value_for_tagged_values` The fix passes all the below required tests suites locally - >To check standard code formatting. - `cargo fmt --all -- --check` (`cargo fmt --all` applies these changes) >To check that you're using the standard code style. - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` >To check that all tests pass - `cargo test --workspace` --------- Co-authored-by: Nitin Londhe <nitin.londhe@genmills.com>
1 parent 4c97b3d commit 106ca65

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

  • crates/nu-command/src/formats/from

crates/nu-command/src/formats/from/yaml.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,30 @@ fn convert_yaml_value_to_nu_value(
174174

175175
Value::from(collected)
176176
}
177+
serde_yaml::Value::Tagged(t) => {
178+
let tag = &t.tag;
179+
let value = match &t.value {
180+
serde_yaml::Value::String(s) => {
181+
let val = format!("{} {}", tag, s).trim().to_string();
182+
Value::String { val, span }
183+
}
184+
serde_yaml::Value::Number(n) => {
185+
let val = format!("{} {}", tag, n).trim().to_string();
186+
Value::String { val, span }
187+
}
188+
serde_yaml::Value::Bool(b) => {
189+
let val = format!("{} {}", tag, b).trim().to_string();
190+
Value::String { val, span }
191+
}
192+
serde_yaml::Value::Null => {
193+
let val = format!("{}", tag).trim().to_string();
194+
Value::String { val, span }
195+
}
196+
v => convert_yaml_value_to_nu_value(v, span, val_span)?,
197+
};
198+
199+
value
200+
}
177201
serde_yaml::Value::Null => Value::nothing(span),
178202
x => unimplemented!("Unsupported YAML case: {:?}", x),
179203
})
@@ -314,4 +338,63 @@ mod test {
314338

315339
test_examples(FromYaml {})
316340
}
341+
342+
#[test]
343+
fn test_convert_yaml_value_to_nu_value_for_tagged_values() {
344+
struct TestCase {
345+
input: &'static str,
346+
expected: Result<Value, ShellError>,
347+
}
348+
349+
let test_cases: Vec<TestCase> = vec![
350+
TestCase {
351+
input: "Key: !Value ${TEST}-Test-role",
352+
expected: Ok(Value::Record {
353+
cols: vec!["Key".to_string()],
354+
vals: vec![Value::test_string("!Value ${TEST}-Test-role")],
355+
span: Span::test_data(),
356+
}),
357+
},
358+
TestCase {
359+
input: "Key: !Value test-${TEST}",
360+
expected: Ok(Value::Record {
361+
cols: vec!["Key".to_string()],
362+
vals: vec![Value::test_string("!Value test-${TEST}")],
363+
span: Span::test_data(),
364+
}),
365+
},
366+
TestCase {
367+
input: "Key: !Value",
368+
expected: Ok(Value::Record {
369+
cols: vec!["Key".to_string()],
370+
vals: vec![Value::test_string("!Value")],
371+
span: Span::test_data(),
372+
}),
373+
},
374+
TestCase {
375+
input: "Key: !True",
376+
expected: Ok(Value::Record {
377+
cols: vec!["Key".to_string()],
378+
vals: vec![Value::test_string("!True")],
379+
span: Span::test_data(),
380+
}),
381+
},
382+
TestCase {
383+
input: "Key: !123",
384+
expected: Ok(Value::Record {
385+
cols: vec!["Key".to_string()],
386+
vals: vec![Value::test_string("!123")],
387+
span: Span::test_data(),
388+
}),
389+
},
390+
];
391+
392+
for test_case in test_cases {
393+
let doc = serde_yaml::Deserializer::from_str(test_case.input);
394+
let v: serde_yaml::Value = serde_yaml::Value::deserialize(doc.last().unwrap()).unwrap();
395+
let result = convert_yaml_value_to_nu_value(&v, Span::test_data(), Span::test_data());
396+
assert!(result.is_ok());
397+
assert!(result.ok().unwrap() == test_case.expected.ok().unwrap());
398+
}
399+
}
317400
}

0 commit comments

Comments
 (0)