conf: remove wp_conf_apply_rules()

This commit is contained in:
George Kiagiadakis
2023-11-07 16:18:08 +02:00
parent 299e671ffa
commit a6dd60ff83
5 changed files with 0 additions and 485 deletions

View File

@@ -581,161 +581,3 @@ wp_conf_get_value_string (WpConf *self, const gchar *section,
return_fallback:
return fallback ? g_strdup (fallback) : NULL;
}
static gboolean
matches_properties (WpSpaJson *match, WpProperties *props)
{
g_autoptr (WpIterator) it = NULL;
g_auto (GValue) item = G_VALUE_INIT;
g_return_val_if_fail (match, FALSE);
if (!wp_spa_json_is_array (match))
return FALSE;
it = wp_spa_json_new_iterator (match);
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
WpSpaJson *match = g_value_get_boxed (&item);
if (match && wp_spa_json_is_object (match)) {
g_autoptr (WpObjectInterest) interest =
wp_object_interest_new_type (WP_TYPE_PROPERTIES);
g_autoptr (WpIterator) it_match = wp_spa_json_new_iterator (match);
g_auto (GValue) item_match = G_VALUE_INIT;
for (; wp_iterator_next (it_match, &item_match);
g_value_unset (&item_match)) {
WpSpaJson *p;
g_autofree gchar *key = NULL;
g_autofree gchar *val = NULL;
const gchar *value = NULL;
WpConstraintVerb verb = WP_CONSTRAINT_VERB_EQUALS;
p = g_value_get_boxed (&item_match);
key = wp_spa_json_parse_string (p);
g_value_unset (&item_match);
g_return_val_if_fail (wp_iterator_next (it_match, &item_match), FALSE);
p = g_value_get_boxed (&item_match);
value = val = wp_spa_json_parse_string (p);
if (value[0] == '~') {
verb = WP_CONSTRAINT_VERB_MATCHES;
value++;
}
wp_object_interest_add_constraint (interest,
WP_CONSTRAINT_TYPE_PW_PROPERTY, key, verb,
g_variant_new_string (value));
}
if (wp_object_interest_matches (interest, props))
return TRUE;
}
}
return FALSE;
}
static gboolean
apply_properties (WpSpaJson *update_props, WpProperties *applied_props)
{
g_autoptr (WpIterator) it = NULL;
g_auto (GValue) item = G_VALUE_INIT;
g_return_val_if_fail (update_props, FALSE);
g_return_val_if_fail (applied_props, FALSE);
if (!wp_spa_json_is_object (update_props))
return FALSE;
it = wp_spa_json_new_iterator (update_props);
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
g_autoptr (WpSpaJson) key = NULL;
g_autoptr (WpSpaJson) val = NULL;
g_autofree gchar *key_str = NULL;
g_autofree gchar *val_str = NULL;
key = g_value_dup_boxed (&item);
key_str = wp_spa_json_parse_string (key);
g_return_val_if_fail (key_str, FALSE);
g_value_unset (&item);
g_return_val_if_fail (wp_iterator_next (it, &item), FALSE);
val = g_value_dup_boxed (&item);
val_str = wp_spa_json_parse_string (val);
g_return_val_if_fail (val_str, FALSE);
wp_properties_set (applied_props, key_str, val_str);
}
return TRUE;
}
static gboolean
apply_rules_json (WpSpaJson *rules, WpProperties *match_props,
WpProperties *applied_props)
{
g_autoptr (WpIterator) it = NULL;
g_auto (GValue) item = G_VALUE_INIT;
g_return_val_if_fail (rules, FALSE);
g_return_val_if_fail (match_props, FALSE);
it = wp_spa_json_new_iterator (rules);
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
g_autoptr (WpSpaJson) match = NULL;
g_autoptr (WpSpaJson) update_props = NULL;
WpSpaJson *rule = g_value_get_boxed (&item);
if (rule &&
wp_spa_json_is_object (rule) &&
wp_spa_json_object_get (rule,
"matches", "J", &match,
"update-props", "J", &update_props,
NULL) &&
matches_properties (match, match_props)) {
if (!applied_props)
return apply_properties (update_props, match_props);
wp_properties_update (applied_props, match_props);
return apply_properties (update_props, applied_props);
}
}
return FALSE;
}
/*!
* This function applies the rules on a given matched properties. If the
* applied_props param is not NULL, The applied rules are copied into
* applied_props and matched_props is not altered. The fallback rules are only
* used if the rules section cannot be found in the configuration, or the
* section is not an JSON array.
*
* \ingroup wpconf
* \param self the configuration
* \param section name of the configuration section that has the rules
* \param match_props (transfer none)(inout): the properties to match the rules
* \param applied_props (transfer none)(nullable)(out): the properties with
* the rules applied
* \param fallback (transfer full)(nullable): The fallback rules to apply if the
* rules section could not be found in the configuration, or the section is not
* a JSON array.
* \returns TRUE if rules were applied, FALSE otherwise
*/
gboolean
wp_conf_apply_rules (WpConf *self, const gchar *section,
WpProperties *match_props, WpProperties *applied_props, WpSpaJson *fallback)
{
g_autoptr (WpSpaJson) s = NULL;
g_autoptr (WpSpaJson) fb = fallback;
g_return_val_if_fail (WP_IS_CONF (self), FALSE);
g_return_val_if_fail (section, FALSE);
g_return_val_if_fail (match_props, FALSE);
s = wp_conf_get_section (self, section, NULL);
if (!s || !wp_spa_json_is_array (s))
return fb ? apply_rules_json (fb, match_props, applied_props) : FALSE;
return apply_rules_json (s, match_props, applied_props);
}

View File

@@ -50,11 +50,6 @@ WP_API
gchar *wp_conf_get_value_string (WpConf *self,
const gchar *section, const gchar *key, const gchar *fallback);
WP_API
gboolean wp_conf_apply_rules (WpConf *self, const gchar *section,
WpProperties *match_props, WpProperties *applied_props,
WpSpaJson *fallback);
G_END_DECLS
#endif

View File

@@ -1675,33 +1675,6 @@ conf_get_value_string (lua_State *L)
return 1;
}
static int
conf_apply_rules (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
g_autoptr (WpProperties) ap = NULL;
const char *section;
g_autoptr (WpProperties) mp = NULL;
g_autoptr (WpSpaJson) fb = NULL;
g_return_val_if_fail (conf, 0);
ap = wp_properties_new_empty ();
section = luaL_checkstring (L, 1);
luaL_checktype (L, 2, LUA_TTABLE);
mp = wplua_table_to_properties (L, 2);
if (lua_isuserdata (L, 3)) {
WpSpaJson *v = wplua_checkboxed (L, 3, WP_TYPE_SPA_JSON);
if (v)
fb = wp_spa_json_ref (v);
}
lua_pushboolean (L, wp_conf_apply_rules (conf, section, mp, ap,
g_steal_pointer (&fb)));
wplua_properties_to_table (L, ap);
return 2;
}
static const luaL_Reg conf_methods[] = {
{ "get_section", conf_get_section },
{ "get_value", conf_get_value },
@@ -1709,7 +1682,6 @@ static const luaL_Reg conf_methods[] = {
{ "get_value_int", conf_get_value_int },
{ "get_value_float", conf_get_value_float },
{ "get_value_string", conf_get_value_string },
{ "apply_rules", conf_apply_rules },
{ NULL, NULL }
};

View File

@@ -521,273 +521,6 @@ test_conf_get_value (TestConfFixture *f, gconstpointer data)
}
}
static void
test_conf_apply_rules (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
/* Unmatched */
{
g_autoptr (WpProperties) match_props = NULL;
match_props = wp_properties_new (
"device.name", "unmatched-device-name",
NULL);
g_assert_false (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, NULL, NULL));
g_assert_false (wp_conf_apply_rules (f->conf, "invalid-section",
match_props, NULL, NULL));
}
/* Match equal */
{
g_autoptr (WpProperties) match_props = NULL;
match_props = wp_properties_new (
"node.name", "alsa_output.0.my-alsa-device",
NULL);
g_assert_true (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, NULL, NULL));
}
/* Without applied_props */
{
g_autoptr (WpProperties) match_props = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
g_assert_true (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, NULL, NULL));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_cmpstr (str, ==, "true");
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_cmpstr (str, ==, "false");
}
/* With applied_props */
{
g_autoptr (WpProperties) match_props = NULL;
g_autoptr (WpProperties) applied_props = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
applied_props = wp_properties_new_empty ();
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
g_assert_true (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, applied_props, NULL));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (applied_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (applied_props, "api.alsa.use-acp");
g_assert_cmpstr (str, ==, "true");
str = wp_properties_get (applied_props, "api.acp.auto-port");
g_assert_cmpstr (str, ==, "false");
}
/* Fallback without applied_props and invalid section */
{
g_autoptr (WpProperties) match_props = NULL;
g_autoptr (WpSpaJson) fallback = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
fallback = wp_spa_json_new_from_string (
"[{matches = [{device.name = ~alsa_card.*}], update-props = {fallback.key = true}}]");
g_assert_true (wp_conf_apply_rules (f->conf, "invalid-section",
match_props, NULL, wp_spa_json_ref (fallback)));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_cmpstr (str, ==, "true");
}
/* Fallback without applied_props and valid section */
{
g_autoptr (WpProperties) match_props = NULL;
g_autoptr (WpSpaJson) fallback = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
fallback = wp_spa_json_new_from_string (
"[{matches = [{device.name = ~alsa_card.*}], update-props = {fallback.key = true}}]");
g_assert_true (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, NULL, wp_spa_json_ref (fallback)));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_cmpstr (str, ==, "true");
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_cmpstr (str, ==, "false");
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
}
/* Fallback with applied_props and invalid section */
{
g_autoptr (WpProperties) match_props = NULL;
g_autoptr (WpProperties) applied_props = NULL;
g_autoptr (WpSpaJson) fallback = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
applied_props = wp_properties_new_empty ();
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
fallback = wp_spa_json_new_from_string (
"[{matches = [{device.name = ~alsa_card.*}], update-props = {fallback.key = true}}]");
g_assert_true (wp_conf_apply_rules (f->conf, "invalid-section",
match_props, applied_props, wp_spa_json_ref (fallback)));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
str = wp_properties_get (applied_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (applied_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (applied_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (applied_props, "fallback.key");
g_assert_cmpstr (str, ==, "true");
}
/* Fallback with applied_props and valid section */
{
g_autoptr (WpProperties) match_props = NULL;
g_autoptr (WpProperties) applied_props = NULL;
g_autoptr (WpSpaJson) fallback = NULL;
const gchar *str = NULL;
match_props = wp_properties_new (
"device.name", "alsa_card.0.my-alsa-device",
NULL);
g_assert_nonnull (match_props);
applied_props = wp_properties_new_empty ();
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
fallback = wp_spa_json_new_from_string (
"[{matches = [{device.name = ~alsa_card.*}], update-props = {fallback.key = true}}]");
g_assert_true (wp_conf_apply_rules (f->conf, "wireplumber.section.rules",
match_props, applied_props, wp_spa_json_ref (fallback)));
str = wp_properties_get (match_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (match_props, "api.alsa.use-acp");
g_assert_null (str);
str = wp_properties_get (match_props, "api.acp.auto-port");
g_assert_null (str);
str = wp_properties_get (match_props, "fallback.key");
g_assert_null (str);
str = wp_properties_get (applied_props, "device.name");
g_assert_cmpstr (str, ==, "alsa_card.0.my-alsa-device");
str = wp_properties_get (applied_props, "api.alsa.use-acp");
g_assert_cmpstr (str, ==, "true");
str = wp_properties_get (applied_props, "api.acp.auto-port");
g_assert_cmpstr (str, ==, "false");
str = wp_properties_get (applied_props, "fallback.key");
g_assert_null (str);
}
}
gint
main (gint argc, gchar *argv[])
{
@@ -806,8 +539,6 @@ main (gint argc, gchar *argv[])
test_conf_setup, test_conf_override_nested, test_conf_teardown);
g_test_add ("/wp/conf/get_value", TestConfFixture, NULL,
test_conf_setup, test_conf_get_value, test_conf_teardown);
g_test_add ("/wp/conf/apply_rules", TestConfFixture, NULL,
test_conf_setup, test_conf_apply_rules, test_conf_teardown);
return g_test_run ();
}

View File

@@ -58,28 +58,3 @@ wireplumber.section-nested-override = {
key1 = true
}
}
wireplumber.section.rules = [
{
matches = [
{
device.name = "~alsa_card.*"
}
]
update-props = {
api.alsa.use-acp = true
api.acp.auto-port = false
}
}
{
matches = [
{
node.name = "alsa_output.0.my-alsa-device"
}
]
update-props = {
api.alsa.use-acp = true
api.acp.auto-port = false
}
}
]