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.
This commit is contained in:
Thomas Haller 2019-04-23 16:34:59 +02:00
parent e55a45faa2
commit 020c4c81d8
2 changed files with 26 additions and 45 deletions

View file

@ -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);

View file

@ -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;
}