summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-04-23 16:34:59 +0200
committerThomas Haller <thaller@redhat.com>2019-04-25 08:20:03 +0200
commit020c4c81d82ad314f1998b271a4fdfbf96e1b7c3 (patch)
treee318f653736e07582a4bd9cda4e0067ac4e10f61
parente55a45faa25eaa282d8dc96e9d1ffa5d049977d3 (diff)
downloadNetworkManager-020c4c81d82ad314f1998b271a4fdfbf96e1b7c3.tar.gz
cli: drop GValue transform functions for strdict and implement it in _get_fcn_gobject_impl()
The only remaining GValue transform function was from GHashTable (of (str,str) type) to string. Drop that too, and implement the conversion in _get_fcn_gobject_impl(). Note that there are few GObject properties of type GHashTable and most of them implement their own logic. This only applies to "802-3-ethernet.s390-options". Also, always sort the keys. Otherwise, the output is not stable.
-rw-r--r--clients/cli/nmcli.c36
-rw-r--r--clients/common/nm-meta-setting-desc.c35
2 files changed, 26 insertions, 45 deletions
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c
index 9076e13d7c..ad45f179a5 100644
--- a/clients/cli/nmcli.c
+++ b/clients/cli/nmcli.c
@@ -915,40 +915,6 @@ signal_handler (gpointer user_data)
return G_SOURCE_CONTINUE;
}
-static void
-nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
-{
- GHashTable *hash;
- GHashTableIter iter;
- const char *key, *value;
- GString *string;
-
- hash = (GHashTable *) g_value_get_boxed (src_value);
-
- string = g_string_new (NULL);
- if (hash) {
- g_hash_table_iter_init (&iter, hash);
- while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) {
- if (string->len)
- g_string_append_c (string, ',');
- g_string_append_printf (string, "%s=%s", key, value);
- }
- }
-
- g_value_take_string (dest_value, g_string_free (string, FALSE));
-}
-
-static void
-nmc_value_transforms_register (void)
-{
- /* This depends on the fact that all of the hash-table-valued properties
- * in libnm-core are string->string.
- */
- g_value_register_transform_func (G_TYPE_HASH_TABLE,
- G_TYPE_STRING,
- nmc_convert_string_hash_to_string);
-}
-
void
nm_cli_spawn_pager (NmCli *nmc)
{
@@ -1006,8 +972,6 @@ main (int argc, char *argv[])
/* Save terminal settings */
tcgetattr (STDIN_FILENO, &termios_orig);
- nmc_value_transforms_register ();
-
nm_cli.return_text = g_string_new (_("Success"));
loop = g_main_loop_new (NULL, FALSE);
diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c
index 5136d98a2b..98cb50405c 100644
--- a/clients/common/nm-meta-setting-desc.c
+++ b/clients/common/nm-meta-setting-desc.c
@@ -856,17 +856,11 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info,
glib_handles_str_transform = !NM_IN_SET (gtype_prop, G_TYPE_BOOLEAN,
G_TYPE_STRV,
- G_TYPE_BYTES);
+ G_TYPE_BYTES,
+ G_TYPE_HASH_TABLE);
if (glib_handles_str_transform) {
- /* We rely on the type convertion of the gobject property to string.
- *
- * Note that we register some transformations via nmc_value_transforms_register()
- * to make that working for G_TYPE_HASH_TABLE.
- *
- * FIXME: that is particularly ugly because it's non-obvious which code relies
- * on nmc_value_transforms_register(). Also, nmc_value_transforms_register() is
- * in clients/cli, while we are here in clients/common. */
+ /* We rely on the type convertion of the gobject property to string. */
g_value_init (&val, G_TYPE_STRING);
} else
g_value_init (&val, gtype_prop);
@@ -922,6 +916,29 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info,
RETURN_STR_TO_FREE (str);
}
+ if (gtype_prop == G_TYPE_HASH_TABLE) {
+ GHashTable *strdict;
+ gs_free const char **keys = NULL;
+ GString *str;
+ gsize i;
+
+ strdict = g_value_get_boxed (&val);
+ keys = nm_utils_strdict_get_keys (strdict, TRUE, NULL);
+ if (!keys)
+ return NULL;
+
+ str = g_string_new (NULL);
+ for (i = 0; keys[i]; i++) {
+ if (str->len > 0)
+ g_string_append_c (str, ',');
+ g_string_append_printf (str,
+ "%s=%s",
+ keys[i],
+ (const char *) g_hash_table_lookup (strdict, keys[i]));
+ }
+ RETURN_STR_TO_FREE (g_string_free (str, FALSE));
+ }
+
nm_assert_not_reached ();
return NULL;
}