Handle null items and values, handle arrays directly under arrays

This commit is contained in:
__JosephAbbey
2025-09-06 16:12:43 +01:00
committed by GitHub
parent d534c60998
commit 6e735ccd30

View File

@@ -386,32 +386,25 @@ class HomeAssistantApp extends Application.AppBase {
var keys = a.keys(); var keys = a.keys();
for (var i = 0; i < keys.size(); i++) { for (var i = 0; i < keys.size(); i++) {
var key = keys[i]; var key = keys[i];
// If the sizes are the same and b contains every item in a,
// then a contains every item in b, i.e. the items are the same.
if (!b.hasKey(key)) { if (!b.hasKey(key)) {
return false; return false;
} }
var valA = a.get(key); var valA = a.get(key);
var valB = b.get(key); var valB = b.get(key);
if (valA instanceof Lang.Dictionary and valB instanceof Lang.Dictionary) { if (valA == null && valB == null) {
// both null, consider true
} else if (valA == null || valB == null) {
return false;
} else if (valA instanceof Lang.Dictionary and valB instanceof Lang.Dictionary) {
if (!structuralEquals(valA, valB)) { if (!structuralEquals(valA, valB)) {
return false; return false;
} }
} else if (valA instanceof Lang.Array and valB instanceof Lang.Array) { } else if (valA instanceof Lang.Array and valB instanceof Lang.Array) {
var arrA = valA as Lang.Array; if (!arrayEquals(valA, valB)) {
var arrB = valB as Lang.Array;
if (arrA.size() != arrB.size()) {
return false; return false;
} }
for (var j = 0; j < arrA.size(); j++) {
var itemA = arrA[j];
var itemB = arrB[j];
if (itemA instanceof Lang.Dictionary and itemB instanceof Lang.Dictionary) {
if (!structuralEquals(itemA, itemB)) {
return false;
}
} else if (!itemA.equals(itemB)) {
return false;
}
}
} else if (!valA.equals(valB)) { } else if (!valA.equals(valB)) {
return false; return false;
} }
@@ -419,6 +412,40 @@ class HomeAssistantApp extends Application.AppBase {
return true; return true;
} }
//! Test if two arrays are structurally equal. Used to see if the JSON menu has been
//! amended but yet to be updated in the application cache.
//!
//! @param a First array in the comparison.
//! @param b Second array in the comparison.
//
function arrayEquals(
a as Lang.Array,
b as Lang.Array
) as Lang.Boolean {
if (a.size() != b.size()) {
return false;
}
for (var j = 0; j < a.size(); j++) {
var itemA = a[j];
var itemB = b[j];
if (itemA == null && itemB == null) {
// both null, consider true
} else if (itemA == null || itemB == null) {
return false;
} else if (itemA instanceof Lang.Dictionary and itemB instanceof Lang.Dictionary) {
if (!structuralEquals(itemA, itemB)) {
return false;
}
} else if (valA instanceof Lang.Array and valB instanceof Lang.Array) {
if (!arrayEquals(valA, valB)) {
return false;
}
} else if (!itemA.equals(itemB)) {
return false;
}
}
}
//! Callback function for the menu check GET request. //! Callback function for the menu check GET request.
//! //!
//! @param responseCode Response code. //! @param responseCode Response code.