m-lua-scripting: add fallback value for parse_{array|object}_safe() APIs

This commit is contained in:
Julian Bouzas 2022-09-29 11:30:44 -04:00
parent f416482f65
commit f1fbeaa661
11 changed files with 53 additions and 21 deletions

View file

@ -1578,8 +1578,19 @@ settings_parse_array_safe (lua_State *L)
g_autoptr (WpSettings) s = NULL;
g_autoptr (WpSpaJson) json_default = NULL;
if (lua_type (L, 2) == LUA_TSTRING)
m = luaL_checkstring (L, 2);
if (lua_isuserdata (L, 2)) {
WpSpaJson *v = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON);
if (v && wp_spa_json_is_array (v))
json_default = wp_spa_json_ref (v);
}
if (!json_default) {
wp_warning ("Using empty array for setting '%s' as fallback value isn't "
"a JSON array", setting);
json_default = wp_spa_json_new_array (NULL, NULL);
}
if (lua_type (L, 3) == LUA_TSTRING)
m = luaL_checkstring (L, 3);
s = wp_settings_get_instance (get_wp_core (L), m);
if (s) {
@ -1593,7 +1604,7 @@ settings_parse_array_safe (lua_State *L)
}
}
}
json_default = wp_spa_json_new_array (NULL, NULL);
push_luajson (L, json_default);
return 1;
}
@ -1606,8 +1617,19 @@ settings_parse_object_safe (lua_State *L)
g_autoptr (WpSettings) s = NULL;
g_autoptr (WpSpaJson) json_default = NULL;
if (lua_type (L, 2) == LUA_TSTRING)
m = luaL_checkstring (L, 2);
if (lua_isuserdata (L, 2)) {
WpSpaJson *v = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON);
if (v && wp_spa_json_is_object (v))
json_default = wp_spa_json_ref (v);
}
if (!json_default) {
wp_warning ("Using empty object for setting '%s' as fallback value isn't "
"a JSON object", setting);
json_default = wp_spa_json_new_array (NULL, NULL);
}
if (lua_type (L, 3) == LUA_TSTRING)
m = luaL_checkstring (L, 3);
s = wp_settings_get_instance (get_wp_core (L), m);
if (s) {
@ -1621,7 +1643,7 @@ settings_parse_object_safe (lua_State *L)
}
}
}
json_default = wp_spa_json_new_object (NULL, NULL, NULL);
push_luajson (L, json_default);
return 1;
}

View file

@ -6,7 +6,7 @@
-- SPDX-License-Identifier: MIT
local config = {}
config.node_properties = Settings.parse_object_safe ("monitor.alsa.midi.node-properties")
config.node_properties = Settings.parse_object_safe ("monitor.alsa.midi.node-properties", Json.Object {})
SND_PATH = "/dev/snd"
SEQ_NAME = "seq"

View file

@ -11,8 +11,8 @@ local config = {}
config.reserve_priority = Settings.parse_int_safe ("monitor.alsa.reserve.priority", -20)
config.reserve_application_name = Settings.parse_string_safe ("monitor.alsa.reserve.application-name", "WirePlumber")
config.jack_device = Settings.parse_boolean_safe ("monitor.alsa.jack-device", false)
config.properties = Settings.parse_object_safe ("monitor.alsa.properties")
config.vm_node_defaults = Settings.parse_object_safe ("monitor.alsa.vm.node.defaults")
config.properties = Settings.parse_object_safe ("monitor.alsa.properties", Json.Object {})
config.vm_node_defaults = Settings.parse_object_safe ("monitor.alsa.vm.node.defaults", Json.Object {})
-- unique device/node name tables
device_names_table = nil

View file

@ -10,7 +10,7 @@ local COMBINE_OFFSET = 64
local cutils = require ("common-utils")
local config = {}
config.properties = Settings.parse_object_safe ("monitor.bluetooth.properties")
config.properties = Settings.parse_object_safe ("monitor.bluetooth.properties", Json.Object {})
devices_om = ObjectManager {
Interest {

View file

@ -8,7 +8,7 @@
local cutils = require ("common-utils")
local config = {}
config.properties = Settings.parse_object_safe ("monitor.libcamera.properties")
config.properties = Settings.parse_object_safe ("monitor.libcamera.properties", Json.Object {})
function findDuplicate(parent, id, property, value)
for i = 0, id - 1, 1 do

View file

@ -8,7 +8,7 @@
local cutils = require ("common-utils")
local config = {}
config.properties = Settings.parse_object_safe ("monitor.v4l2.properties")
config.properties = Settings.parse_object_safe ("monitor.v4l2.properties", Json.Object {})
function findDuplicate(parent, id, property, value)
for i = 0, id - 1, 1 do

View file

@ -34,7 +34,7 @@ config.use_persistent_storage = Settings.parse_boolean_safe
config.use_headset_profile = Settings.parse_boolean_safe
("policy.bluetooth.media-role.use-headset-profile", true)
config.apps_setting = Settings.parse_array_safe
("policy.bluetooth.media-role.applications")
("policy.bluetooth.media-role.applications", Json.Array {})
state = nil
headset_profiles = nil
@ -60,7 +60,7 @@ local function settingsChangedCallback (_, setting, _)
("policy.bluetooth.media-role.use-headset-profile", config.use_headset_profile)
elseif setting == "policy.bluetooth.media-role.applications" then
local new_apps_setting = Settings.parse_array_safe
("policy.bluetooth.media-role.applications")
("policy.bluetooth.media-role.applications", Json.Array {})
if #new_apps_setting > 0 then
config.apps_setting = new_apps_setting
loadAppNames (config.apps_setting)

View file

@ -7,7 +7,7 @@
local config = {}
config.duck_level = Settings.parse_float_safe ("policy.default.duck-level", 0.3)
config.roles = Settings.parse_object_safe ("endpoints-roles")
config.roles = Settings.parse_object_safe ("endpoints-roles", Json.Object {})
function findRole(role)
if role and not config.roles[role] then

View file

@ -7,7 +7,7 @@
-- Receive script arguments from config.lua
local roles = Settings.parse_object_safe ("endpoints-roles")
local roles = Settings.parse_object_safe ("endpoints-roles", Json.Object {})
local self = {}
self.scanning = false

View file

@ -6,7 +6,7 @@
-- SPDX-License-Identifier: MIT
-- Receive script arguments from config.lua
local endpoints = Settings.parse_object_safe ("endpoints")
local endpoints = Settings.parse_object_safe ("endpoints", Json.Object {})
function createEndpoint (factory_name, properties)
-- create endpoint

View file

@ -75,16 +75,21 @@ assert (value ~= nil)
assert (value:is_array())
assert (value:get_data() == "[1, 2, 3]")
value = Settings.parse_array_safe ("test-setting-json", "test-settings")
value = Settings.parse_array_safe ("test-setting-json", Json.Array {}, "test-settings")
assert (value ~= nil)
assert (value[1] == 1)
assert (value[2] == 2)
assert (value[3] == 3)
value = Settings.parse_array_safe ("test-setting-undefined", "test-settings")
value = Settings.parse_array_safe ("test-setting-undefined", nil, "test-settings")
assert (value ~= nil)
assert (#value == 0)
value = Settings.parse_array_safe ("test-setting-undefined", Json.Array { 1, 2 }, "test-settings")
assert (value ~= nil)
assert (value[1] == 1)
assert (value[2] == 2)
value = Settings.get ("test-setting-json2", "test-settings")
assert (value ~= nil)
assert (value:is_array())
@ -109,16 +114,21 @@ assert (val.key1 == "value")
assert (val.key2 == 2)
assert (val.key3 == true)
value = Settings.parse_object_safe ("test-setting-json3", "test-settings")
value = Settings.parse_object_safe ("test-setting-json3", Json.Object {}, "test-settings")
assert (value ~= nil)
assert (value.key1 == "value")
assert (value.key2 == 2)
assert (value.key3 == true)
value = Settings.parse_object_safe ("test-setting-undefined", "test-settings")
value = Settings.parse_object_safe ("test-setting-undefined", nil, "test-settings")
assert (value ~= nil)
assert (#value == 0)
value = Settings.parse_object_safe ("test-setting-undefined", Json.Object { key1 = "value", key2 = 2}, "test-settings")
assert (value ~= nil)
assert (value.key1 == "value")
assert (value.key2 == 2)
-- test rules
-- test #1
local cp = {