libnmc-setting: add new flag for property descriptors

Add a new flag "print_hex_negative_as_base10" in the property
descriptor _NMMetaPropertyTypData.

Normally, when a property has "base = 16", it is printed as unsigned
even if the gtype is signed.

For some properties, we want to print the hexadecimal representation
for positive values, and the base10 representation with minus sign for
negative values. A typical use case is to encode the default value as
"-1" and use positive values as a hexadecimal number.
This commit is contained in:
Beniamino Galvani
2025-03-12 15:17:06 +01:00
parent dbc4ff0a1d
commit 037b14965e
2 changed files with 30 additions and 5 deletions

View File

@@ -1005,8 +1005,16 @@ _get_fcn_gobject_int(ARGS_GET_FCN)
case 16:
if (is_uint64)
return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", v.u64);
else
return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", (guint64) v.i64);
else {
if (property_info->property_typ_data
&& property_info->property_typ_data->subtype.gobject_int
.print_hex_negative_as_base10
&& v.i64 < 0) {
return_str = g_strdup_printf("%" G_GINT64_FORMAT, v.i64);
} else {
return_str = g_strdup_printf("0x%" G_GINT64_MODIFIER "x", (guint64) v.i64);
}
}
break;
default:
return_str = NULL;
@@ -1422,6 +1430,11 @@ _set_fcn_gobject_int(ARGS_SET_FCN)
nm_meta_property_int_get_range(property_info, &min, &max);
/* See the comment on "print_hex_negative_as_base10" */
nm_assert(!property_info->property_typ_data
|| !property_info->property_typ_data->subtype.gobject_int.print_hex_negative_as_base10
|| (!is_uint64 && min.i64 > -10));
if (is_uint64)
v.u64 = _nm_utils_ascii_str_to_uint64(value, base, min.u64, max.u64, 0);
else

View File

@@ -293,9 +293,21 @@ struct _NMMetaPropertyTypData {
int value);
} gobject_enum;
struct {
NMMetaSignUnsignInt64 min;
NMMetaSignUnsignInt64 max;
guint base;
NMMetaSignUnsignInt64 min;
NMMetaSignUnsignInt64 max;
guint base;
/* Normally, when a property has "base = 16", it is printed
* as unsigned even if the gtype is signed. For some properties,
* we want to print the hexadecimal representation for positive
* values, and the base10 representation with minus sign for negative
* values. A typical use case is to encode the default value as
* "-1" and use positive values as a hexadecimal number. To avoid
* ambiguity when setting the value via nmcli, the property minimum
* allowed value should not be <= -10.
*/
bool print_hex_negative_as_base10;
const NMMetaUtilsIntValueInfo *value_infos;
} gobject_int;
struct {