cli: drop GValue transformation of GBytes to string and implement bytes getter via _get_fcn_gobject_impl()
The g_value_register_transform_func() for handling GBytes was not actually used. All properties of type G_TYPE_BYTES have their explit handler how to convert bytes to string. That is good, because the implementation there was very bad (it did not honor pretty/parsable get-type). Also, merge _get_fcn_gobject_bytes() into _get_fcn_gobject_impl(). We already have a generic handler that handles properties solely based on the GObject type: _get_fcn_gobject_impl(). Just let it also handle bytes. It's better to have fewer handlers, if they don't need special context.
This commit is contained in:
@@ -938,34 +938,6 @@ nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
|
|||||||
g_value_take_string (dest_value, g_string_free (string, FALSE));
|
g_value_take_string (dest_value, g_string_free (string, FALSE));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
nmc_convert_bytes_to_string (const GValue *src_value, GValue *dest_value)
|
|
||||||
{
|
|
||||||
GBytes *bytes;
|
|
||||||
const guint8 *array;
|
|
||||||
gsize length;
|
|
||||||
GString *printable;
|
|
||||||
guint i = 0;
|
|
||||||
|
|
||||||
bytes = g_value_get_boxed (src_value);
|
|
||||||
|
|
||||||
printable = g_string_new ("[");
|
|
||||||
|
|
||||||
if (bytes) {
|
|
||||||
array = g_bytes_get_data (bytes, &length);
|
|
||||||
while (i < MIN (length, 35)) {
|
|
||||||
if (i > 0)
|
|
||||||
g_string_append_c (printable, ' ');
|
|
||||||
g_string_append_printf (printable, "0x%02X", array[i++]);
|
|
||||||
}
|
|
||||||
if (i < length)
|
|
||||||
g_string_append (printable, " ... ");
|
|
||||||
}
|
|
||||||
g_string_append_c (printable, ']');
|
|
||||||
|
|
||||||
g_value_take_string (dest_value, g_string_free (printable, FALSE));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nmc_value_transforms_register (void)
|
nmc_value_transforms_register (void)
|
||||||
{
|
{
|
||||||
@@ -975,10 +947,6 @@ nmc_value_transforms_register (void)
|
|||||||
g_value_register_transform_func (G_TYPE_HASH_TABLE,
|
g_value_register_transform_func (G_TYPE_HASH_TABLE,
|
||||||
G_TYPE_STRING,
|
G_TYPE_STRING,
|
||||||
nmc_convert_string_hash_to_string);
|
nmc_convert_string_hash_to_string);
|
||||||
|
|
||||||
g_value_register_transform_func (G_TYPE_BYTES,
|
|
||||||
G_TYPE_STRING,
|
|
||||||
nmc_convert_bytes_to_string);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@@ -77,6 +77,25 @@ _gtype_property_get_gtype (GType gtype, const char *property_name)
|
|||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
static char *
|
||||||
|
bytes_to_string (GBytes *bytes)
|
||||||
|
{
|
||||||
|
const guint8 *data;
|
||||||
|
gsize len;
|
||||||
|
|
||||||
|
if (!bytes)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
data = g_bytes_get_data (bytes, &len);
|
||||||
|
return nm_utils_bin2hexstr_full (data,
|
||||||
|
len,
|
||||||
|
'\0',
|
||||||
|
TRUE,
|
||||||
|
NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_int64_cmp_desc (gconstpointer a,
|
_int64_cmp_desc (gconstpointer a,
|
||||||
gconstpointer b,
|
gconstpointer b,
|
||||||
@@ -836,13 +855,14 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info,
|
|||||||
gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name);
|
gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name);
|
||||||
|
|
||||||
glib_handles_str_transform = !NM_IN_SET (gtype_prop, G_TYPE_BOOLEAN,
|
glib_handles_str_transform = !NM_IN_SET (gtype_prop, G_TYPE_BOOLEAN,
|
||||||
G_TYPE_STRV);
|
G_TYPE_STRV,
|
||||||
|
G_TYPE_BYTES);
|
||||||
|
|
||||||
if (glib_handles_str_transform) {
|
if (glib_handles_str_transform) {
|
||||||
/* We rely on the type convertion of the gobject property to string.
|
/* We rely on the type convertion of the gobject property to string.
|
||||||
*
|
*
|
||||||
* Note that we register some transformations via nmc_value_transforms_register()
|
* Note that we register some transformations via nmc_value_transforms_register()
|
||||||
* to make that working for G_TYPE_HASH_TABLE, and G_TYPE_BYTES.
|
* to make that working for G_TYPE_HASH_TABLE.
|
||||||
*
|
*
|
||||||
* FIXME: that is particularly ugly because it's non-obvious which code relies
|
* FIXME: that is particularly ugly because it's non-obvious which code relies
|
||||||
* on nmc_value_transforms_register(). Also, nmc_value_transforms_register() is
|
* on nmc_value_transforms_register(). Also, nmc_value_transforms_register() is
|
||||||
@@ -894,6 +914,14 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info,
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (gtype_prop == G_TYPE_BYTES) {
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = bytes_to_string (g_value_get_boxed (&val));
|
||||||
|
NM_SET_OUT (out_is_default, !str || !str[0]);
|
||||||
|
RETURN_STR_TO_FREE (str);
|
||||||
|
}
|
||||||
|
|
||||||
nm_assert_not_reached ();
|
nm_assert_not_reached ();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -1652,23 +1680,6 @@ wep_key_type_to_string (NMWepKeyType type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
|
||||||
bytes_to_string (GBytes *bytes)
|
|
||||||
{
|
|
||||||
const guint8 *data;
|
|
||||||
gsize len;
|
|
||||||
|
|
||||||
if (!bytes)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
data = g_bytes_get_data (bytes, &len);
|
|
||||||
return nm_utils_bin2hexstr_full (data,
|
|
||||||
len,
|
|
||||||
'\0',
|
|
||||||
TRUE,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
vlan_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type)
|
vlan_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type)
|
||||||
{
|
{
|
||||||
@@ -2257,21 +2268,6 @@ _set_fcn_cert_8021x (ARGS_SET_FCN)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static gconstpointer
|
|
||||||
_get_fcn_gobject_bytes (ARGS_GET_FCN)
|
|
||||||
{
|
|
||||||
gs_unref_bytes GBytes *bytes = NULL;
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
RETURN_UNSUPPORTED_GET_TYPE ();
|
|
||||||
|
|
||||||
g_object_get (setting, property_info->property_name, &bytes, NULL);
|
|
||||||
|
|
||||||
str = bytes_to_string (bytes);
|
|
||||||
NM_SET_OUT (out_is_default, !str || !str[0]);
|
|
||||||
RETURN_STR_TO_FREE (str);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gconstpointer
|
static gconstpointer
|
||||||
_get_fcn_bond_options (ARGS_GET_FCN)
|
_get_fcn_bond_options (ARGS_GET_FCN)
|
||||||
{
|
{
|
||||||
@@ -4370,7 +4366,7 @@ static const NMMetaPropertyType _pt_gobject_mtu = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const NMMetaPropertyType _pt_gobject_bytes = {
|
static const NMMetaPropertyType _pt_gobject_bytes = {
|
||||||
.get_fcn = _get_fcn_gobject_bytes,
|
.get_fcn = _get_fcn_gobject,
|
||||||
.set_fcn = _set_fcn_gobject_bytes,
|
.set_fcn = _set_fcn_gobject_bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user