extensionUtils: Include arrays and objects in serialization

We currently only handle simple types when (de)serializing,
which means we miss keys like "shell-version" and "session-modes".

While there is no immediate use for those, handing arrays and
objects will allow to support the newly added "donate" metadata
in the Extensions app in the future.

https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/6575

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3379>
This commit is contained in:
Florian Müllner 2024-06-20 20:46:18 +02:00 committed by Marge Bot
parent ad0e2c940b
commit 0188c453b9

View File

@ -36,6 +36,7 @@ const SERIALIZED_PROPERTIES = [
'hasPrefs', 'hasPrefs',
'hasUpdate', 'hasUpdate',
'canChange', 'canChange',
'sessionModes',
]; ];
/** /**
@ -52,9 +53,7 @@ export function serializeExtension(extension) {
obj[prop] = extension[prop]; obj[prop] = extension[prop];
}); });
let res = {}; function packValue(val) {
for (let key in obj) {
let val = obj[key];
let type; let type;
switch (typeof val) { switch (typeof val) {
case 'string': case 'string':
@ -66,13 +65,28 @@ export function serializeExtension(extension) {
case 'boolean': case 'boolean':
type = 'b'; type = 'b';
break; break;
case 'object':
if (Array.isArray(val)) {
type = 'av';
val = val.map(v => packValue(v));
} else {
type = 'a{sv}';
let res = {};
for (let key in val) {
let packed = packValue(val[key]);
if (packed)
res[key] = packed;
}
val = res;
}
break;
default: default:
continue; return null;
} }
res[key] = GLib.Variant.new(type, val); return GLib.Variant.new(type, val);
} }
return res; return packValue(obj).deepUnpack();
} }
/** /**
@ -84,7 +98,7 @@ export function serializeExtension(extension) {
export function deserializeExtension(variant) { export function deserializeExtension(variant) {
let res = {metadata: {}}; let res = {metadata: {}};
for (let prop in variant) { for (let prop in variant) {
let val = variant[prop].unpack(); let val = variant[prop].recursiveUnpack();
if (SERIALIZED_PROPERTIES.includes(prop)) if (SERIALIZED_PROPERTIES.includes(prop))
res[prop] = val; res[prop] = val;
else else