MCPcopy Create free account
hub / github.com/PowerShell/DSC / get_diff

Function get_diff

lib/dsc-lib/src/dscresources/dscresource.rs:638–706  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

636///
637/// An array of top level properties that differ, if any
638pub 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());

Callers 9

testMethod · 0.85
array_containsFunction · 0.85
invoke_setFunction · 0.85
invoke_testFunction · 0.85
invoke_synthetic_testFunction · 0.85
invoke_setMethod · 0.85
setFunction · 0.85

Calls 5

is_same_arrayFunction · 0.85
newFunction · 0.50
is_secure_valueFunction · 0.50
is_emptyMethod · 0.45

Tested by

no test coverage detected