Performs a comparison of two JSON Values if the expected is a strict subset of the actual # Arguments `expected` - The expected value `actual` - The actual value # Returns An array of top level properties that differ, if any
(expected: &Value, actual: &Value)
| 636 | /// |
| 637 | /// An array of top level properties that differ, if any |
| 638 | pub fn get_diff(expected: &Value, actual: &Value) -> Vec<String> { |
| 639 | let mut diff_properties: Vec<String> = Vec::new(); |
| 640 | if expected.is_null() { |
| 641 | return diff_properties; |
| 642 | } |
| 643 | |
| 644 | let mut expected = expected.clone(); |
| 645 | let mut actual = actual.clone(); |
| 646 | |
| 647 | if let Some(map) = expected.as_object_mut() { |
| 648 | // handle well-known optional properties with default values by adding them |
| 649 | for (key, value) in get_well_known_properties() { |
| 650 | if !map.contains_key(&key) { |
| 651 | map.insert(key.clone(), value.clone()); |
| 652 | } |
| 653 | |
| 654 | if actual.is_object() && actual[&key].is_null() { |
| 655 | actual[key.clone()] = value.clone(); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | for (key, value) in &*map { |
| 660 | if is_secure_value(value) { |
| 661 | // skip secure values as they are not comparable |
| 662 | continue; |
| 663 | } |
| 664 | |
| 665 | if value.is_object() { |
| 666 | let sub_diff = get_diff(value, &actual[key]); |
| 667 | if !sub_diff.is_empty() { |
| 668 | debug!("{}", t!("dscresources.dscresource.subDiff", key = key)); |
| 669 | diff_properties.push(key.to_string()); |
| 670 | } |
| 671 | } |
| 672 | else { |
| 673 | // skip `$schema` key as that is provided as input, but not output typically |
| 674 | if key == "$schema" { |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | if let Some(actual_object) = actual.as_object() { |
| 679 | if actual_object.contains_key(key) { |
| 680 | if let Some(value_array) = value.as_array() { |
| 681 | if let Some(actual_array) = actual[key].as_array() { |
| 682 | if !is_same_array(value_array, actual_array) { |
| 683 | info!("{}", t!("dscresources.dscresource.diffArray", key = key)); |
| 684 | diff_properties.push(key.to_string()); |
| 685 | } |
| 686 | } else { |
| 687 | info!("{}", t!("dscresources.dscresource.diffNotArray", key = actual[key])); |
| 688 | diff_properties.push(key.to_string()); |
| 689 | } |
| 690 | } else if value != &actual[key] { |
| 691 | diff_properties.push(key.to_string()); |
| 692 | } |
| 693 | } else { |
| 694 | info!("{}", t!("dscresources.dscresource.diffKeyMissing", key = key)); |
| 695 | diff_properties.push(key.to_string()); |
no test coverage detected