m-lua-scripting: complete Lua Settings API
This patch adds the new WpSettings API in Lua.
This commit is contained in:
@@ -19,6 +19,7 @@ WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
|
||||
|
||||
void wp_lua_scripting_pod_init (lua_State *L);
|
||||
void wp_lua_scripting_json_init (lua_State *L);
|
||||
void push_luajson (lua_State *L, WpSpaJson *json, gint n_recursions);
|
||||
|
||||
/* helpers */
|
||||
|
||||
@@ -588,6 +589,34 @@ push_wpiterator (lua_State *L, WpIterator *it)
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* Settings WpIterator */
|
||||
|
||||
static int
|
||||
settings_iterator_next (lua_State *L)
|
||||
{
|
||||
WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
|
||||
g_auto (GValue) item = G_VALUE_INIT;
|
||||
if (wp_iterator_next (it, &item)) {
|
||||
WpSettingsItem *si = g_value_get_boxed (&item);
|
||||
const gchar *k = wp_settings_item_get_key (si);
|
||||
WpSpaJson *v = wp_settings_item_get_value (si);
|
||||
lua_pushstring (L, k);
|
||||
wplua_pushboxed (L, WP_TYPE_SPA_JSON, v);
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
push_settings_wpiterator (lua_State *L, WpIterator *it)
|
||||
{
|
||||
lua_pushcfunction (L, settings_iterator_next);
|
||||
wplua_pushboxed (L, WP_TYPE_ITERATOR, it);
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* Metadata WpIterator */
|
||||
|
||||
static int
|
||||
@@ -1811,9 +1840,7 @@ static int
|
||||
settings_get (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
|
||||
if (s) {
|
||||
WpSpaJson *j = wp_settings_get (s, setting);
|
||||
if (j)
|
||||
@@ -1825,6 +1852,222 @@ settings_get (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_boolean (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
gboolean val = FALSE;
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j)
|
||||
wp_spa_json_parse_boolean (j, &val);
|
||||
}
|
||||
|
||||
lua_pushboolean (L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_int (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
gint val = 0;
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j)
|
||||
wp_spa_json_parse_int (j, &val);
|
||||
}
|
||||
|
||||
lua_pushinteger (L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_float (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
float val = 0.0;
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j)
|
||||
wp_spa_json_parse_float (j, &val);
|
||||
}
|
||||
|
||||
lua_pushnumber (L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_string (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j) {
|
||||
g_autofree gchar *val = wp_spa_json_parse_string (j);
|
||||
if (val) {
|
||||
lua_pushstring (L, val);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lua_pushstring (L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_array (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
g_autoptr (WpSpaJson) val = NULL;
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j && wp_spa_json_is_array (j)) {
|
||||
push_luajson (L, j, INT_MAX);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
val = wp_spa_json_new_array (NULL, NULL);
|
||||
push_luajson (L, val, INT_MAX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_object (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
g_autoptr (WpSpaJson) val = NULL;
|
||||
|
||||
if (s) {
|
||||
g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
|
||||
if (j && wp_spa_json_is_object (j)) {
|
||||
push_luajson (L, j, INT_MAX);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
val = wp_spa_json_new_object (NULL, NULL, NULL);
|
||||
push_luajson (L, val, INT_MAX);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_get_saved (lua_State *L)
|
||||
{
|
||||
const char *setting = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s) {
|
||||
WpSpaJson *j = wp_settings_get_saved (s, setting);
|
||||
if (j)
|
||||
wplua_pushboxed (L, WP_TYPE_SPA_JSON, j);
|
||||
else
|
||||
lua_pushnil (L);
|
||||
} else
|
||||
lua_pushnil (L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_set (lua_State *L)
|
||||
{
|
||||
const char *key = luaL_checkstring (L, 1);
|
||||
WpSpaJson *val = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s) {
|
||||
lua_pushboolean (L, wp_settings_set (s, key, val));
|
||||
} else {
|
||||
lua_pushboolean (L, FALSE);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_reset (lua_State *L)
|
||||
{
|
||||
const char *key = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s) {
|
||||
lua_pushboolean (L, wp_settings_reset (s, key));
|
||||
} else {
|
||||
lua_pushboolean (L, FALSE);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_save (lua_State *L)
|
||||
{
|
||||
const char *key = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s) {
|
||||
lua_pushboolean (L, wp_settings_save (s, key));
|
||||
} else {
|
||||
lua_pushboolean (L, FALSE);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_delete (lua_State *L)
|
||||
{
|
||||
const char *key = luaL_checkstring (L, 1);
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s) {
|
||||
lua_pushboolean (L, wp_settings_delete (s, key));
|
||||
} else {
|
||||
lua_pushboolean (L, FALSE);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_reset_all (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s)
|
||||
wp_settings_reset_all (s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_save_all (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s)
|
||||
wp_settings_save_all (s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_delete_all (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
if (s)
|
||||
wp_settings_delete_all (s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
settings_iterate (lua_State *L)
|
||||
{
|
||||
g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
|
||||
WpIterator *it = wp_settings_new_iterator (s);
|
||||
return push_settings_wpiterator (L, it);
|
||||
}
|
||||
|
||||
static int
|
||||
settings_subscribe (lua_State *L)
|
||||
{
|
||||
@@ -1858,6 +2101,21 @@ settings_unsubscribe (lua_State *L)
|
||||
|
||||
static const luaL_Reg settings_methods[] = {
|
||||
{ "get", settings_get },
|
||||
{ "get_boolean", settings_get_boolean },
|
||||
{ "get_int", settings_get_int },
|
||||
{ "get_float", settings_get_float },
|
||||
{ "get_string", settings_get_string },
|
||||
{ "get_array", settings_get_array },
|
||||
{ "get_object", settings_get_object },
|
||||
{ "get_saved", settings_get_saved },
|
||||
{ "set", settings_set },
|
||||
{ "reset", settings_reset },
|
||||
{ "save", settings_save },
|
||||
{ "delete", settings_delete },
|
||||
{ "reset_all", settings_reset_all },
|
||||
{ "save_all", settings_save_all },
|
||||
{ "delete_all", settings_delete_all },
|
||||
{ "iterate", settings_iterate },
|
||||
{ "subscribe", settings_subscribe },
|
||||
{ "unsubscribe", settings_unsubscribe },
|
||||
{ NULL, NULL }
|
||||
|
@@ -97,7 +97,7 @@ spa_json_is_object (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
void
|
||||
push_luajson (lua_State *L, WpSpaJson *json, gint n_recursions)
|
||||
{
|
||||
/* Null */
|
||||
|
@@ -9,57 +9,64 @@ value = Settings.get ("test-setting-undefined")
|
||||
assert (value == nil)
|
||||
|
||||
-- Bool
|
||||
value = Settings.get ("test-setting-bool"):parse()
|
||||
value = Settings.get_boolean ("test-setting-bool")
|
||||
assert ("boolean" == type (value))
|
||||
assert (value == true)
|
||||
value = Settings.get_boolean ("test-setting-bool-undefined")
|
||||
assert ("boolean" == type (value))
|
||||
assert (value == false)
|
||||
|
||||
-- Int
|
||||
value = Settings.get ("test-setting-int"):parse()
|
||||
value = Settings.get_int ("test-setting-int")
|
||||
assert ("number" == type (value))
|
||||
assert (value == -20)
|
||||
value = Settings.get_int ("test-setting-int-undefined")
|
||||
assert ("number" == type (value))
|
||||
assert (value == 0)
|
||||
|
||||
-- Float
|
||||
value = Settings.get ("test-setting-float"):parse()
|
||||
value = Settings.get_float ("test-setting-float")
|
||||
assert ("number" == type (value))
|
||||
assert ((value - 3.14) < 0.00001)
|
||||
value = Settings.get_float ("test-setting-float-undefined")
|
||||
assert ("number" == type (value))
|
||||
assert ((value - 0.0) < 0.00001)
|
||||
|
||||
-- String
|
||||
value = Settings.get ("test-setting-string"):parse()
|
||||
value = Settings.get_string ("test-setting-string")
|
||||
assert ("string" == type (value))
|
||||
assert (value == "blahblah")
|
||||
value = Settings.get ("test-setting-string2"):parse()
|
||||
value = Settings.get_string ("test-setting-string2")
|
||||
assert ("string" == type (value))
|
||||
assert (value == "a string with \"quotes\"")
|
||||
value = Settings.get_string ("test-setting-string-undefined")
|
||||
assert ("string" == type (value))
|
||||
assert (value == "")
|
||||
|
||||
-- Array
|
||||
value = Settings.get ("test-setting-array")
|
||||
assert (value ~= nil)
|
||||
assert (value:is_array())
|
||||
assert (value:get_data() == "[1, 2, 3]")
|
||||
value = Settings.get ("test-setting-array2")
|
||||
assert (value ~= nil)
|
||||
assert (value:is_array())
|
||||
assert (value:get_data() ==
|
||||
"[\"test1\", \"test 2\", \"test three\", \"test-four\"]")
|
||||
val = value:parse ()
|
||||
assert (val[1] == "test1")
|
||||
assert (val[2] == "test 2")
|
||||
assert (val[3] == "test three")
|
||||
assert (val[4] == "test-four")
|
||||
assert (val[5] == nil)
|
||||
assert (#val == 4)
|
||||
value = Settings.get_array ("test-setting-array")
|
||||
assert (value[1] == 1)
|
||||
assert (value[2] == 2)
|
||||
assert (value[3] == 3)
|
||||
assert (value[4] == nil)
|
||||
assert (#value == 3)
|
||||
value = Settings.get_array ("test-setting-array2")
|
||||
assert (value[1] == "test1")
|
||||
assert (value[2] == "test 2")
|
||||
assert (value[3] == "test three")
|
||||
assert (value[4] == "test-four")
|
||||
assert (value[5] == nil)
|
||||
assert (#value == 4)
|
||||
value = Settings.get_array ("test-setting-array-undefined")
|
||||
assert (next(value) == nil)
|
||||
|
||||
-- Object
|
||||
value = Settings.get ("test-setting-object")
|
||||
assert (value ~= nil)
|
||||
assert (value:is_object())
|
||||
print (value:get_data())
|
||||
assert (value:get_data() ==
|
||||
"{ key1: \"value\", key2: 2, key3: true }")
|
||||
val = value:parse ()
|
||||
assert (val.key1 == "value")
|
||||
assert (val.key2 == 2)
|
||||
assert (val.key3 == true)
|
||||
value = Settings.get_object ("test-setting-object")
|
||||
assert (value.key1 == "value")
|
||||
assert (value.key2 == 2)
|
||||
assert (value.key3 == true)
|
||||
value = Settings.get_object ("test-setting-object-undefined")
|
||||
assert (next(value) == nil)
|
||||
|
||||
-- Callbacks
|
||||
metadata_om = ObjectManager {
|
||||
@@ -119,7 +126,7 @@ metadata_om:connect("objects-changed", function (om)
|
||||
setting_value = Json.Boolean (true)
|
||||
callback = false
|
||||
|
||||
metadata:set(0, setting, "Spa:String:JSON", setting_value:get_data())
|
||||
assert (Settings.set(setting, setting_value))
|
||||
assert (not callback)
|
||||
|
||||
-- test #3
|
||||
@@ -127,7 +134,7 @@ metadata_om:connect("objects-changed", function (om)
|
||||
setting_value = Json.Int (99)
|
||||
callback = false
|
||||
|
||||
metadata:set(0, setting, "Spa:String:JSON", setting_value:get_data())
|
||||
assert (Settings.set(setting, setting_value))
|
||||
assert (callback)
|
||||
|
||||
-- test #4
|
||||
@@ -136,7 +143,7 @@ metadata_om:connect("objects-changed", function (om)
|
||||
callback = false
|
||||
|
||||
finish_activation = true
|
||||
metadata:set(0, setting, "Spa:String:JSON", setting_value:get_data())
|
||||
assert (Settings.set(setting, setting_value))
|
||||
assert (callback)
|
||||
|
||||
end)
|
||||
|
Reference in New Issue
Block a user