diff options
author | Thomas Haller <thaller@redhat.com> | 2018-06-14 15:02:46 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-03-16 13:46:33 +0100 |
commit | 10eaa7ad474475ebd3b5b2662b86f9cb11b2c288 (patch) | |
tree | 78ab4e2640a95d41dba19f60ef1415fa7806e7c8 | |
parent | 780926765ec291e72a0eabdd4c0d07633aee9497 (diff) | |
download | NetworkManager-10eaa7ad474475ebd3b5b2662b86f9cb11b2c288.tar.gz |
cli: extend processing of fields selection for common/all types
It's complicated. The function that parses the fields list and selects
the relevant bits out of a list of NMMetaAbstractInfo entries,
gets extended to natively understand the "all" and "common" fields.
Up to now, "all" and "common" was handled by preprocessing the fields
string and replace "all" and "common" with appropriate list of fields
(in case of "all", %NULL was used, which mean to select all fields).
However, that does not work with nested fields, and if we want to nest
fields deeper than one level. If you have a "common" selector, we must
select common fields recursively. Same with "all". Hence, these special
fields must be handled by the parsing code itself.
Also, previously it was not possible to select -f "SOME_GROUP.COMMON" or
-f "SOME_GROUP.ALL". This is still not supported, however it becomes
feasable.
-rw-r--r-- | clients/cli/utils.c | 14 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-access.c | 41 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-access.h | 10 |
3 files changed, 47 insertions, 18 deletions
diff --git a/clients/cli/utils.c b/clients/cli/utils.c index fe873f1d59..5b3f54c2f7 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -685,15 +685,23 @@ _output_selection_append (GArray *cols, guint i; const NMMetaAbstractInfo *const*nested; NMMetaSelectionResultList *selection; + gboolean is_leaf; col_idx = cols->len; + is_leaf = TRUE; + if ( selection_item->selection_type == NM_META_SELECTION_TYPE_COMMON + && !nm_meta_abstract_info_included_in_common (selection_item->info, NULL, NULL)) { + /* this item is not actually relevant. It's here for the nested entries. Skip it. */ + is_leaf = FALSE; + } + { PrintDataCol col = { .selection_item = selection_item, ._parent_idx = parent_idx, .self_idx = col_idx, - .is_leaf = TRUE, + .is_leaf = is_leaf, }; g_array_append_val (cols, col); @@ -731,11 +739,11 @@ _output_selection_append (GArray *cols, nm_assert (selection->num == 1); } else { nested = nm_meta_abstract_info_get_nested (selection_item->info, - FALSE, + selection_item->selection_type == NM_META_SELECTION_TYPE_ALL, NULL, &nested_to_free); if (nested) { - selection = nm_meta_selection_create_all (nested); + selection = nm_meta_selection_create_all (nested, selection_item->selection_type); nm_assert (selection && selection->num > 0); } else selection = NULL; diff --git a/clients/common/nm-meta-setting-access.c b/clients/common/nm-meta-setting-access.c index 1fdb561aa1..7205291122 100644 --- a/clients/common/nm-meta-setting-access.c +++ b/clients/common/nm-meta-setting-access.c @@ -573,9 +573,10 @@ typedef struct { } OutputSelectionItem; static NMMetaSelectionResultList * -_output_selection_pack (const NMMetaAbstractInfo *const* fields_array, +_output_selection_pack (const NMMetaAbstractInfo *const*fields_array, GArray *array, - GString *str) + GString *str, + NMMetaSelectionType selection_type) { NMMetaSelectionResultList *result; guint i; @@ -599,10 +600,18 @@ _output_selection_pack (const NMMetaAbstractInfo *const* fields_array, p->info = fields_array[a->idx]; p->idx = a->idx; - if (a->self_offset_plus_1 > 0) + p->selection_type = selection_type; + if (NM_IN_SET (selection_type, NM_META_SELECTION_TYPE_COMMON, + NM_META_SELECTION_TYPE_ALL)) { + nm_assert (a->self_offset_plus_1 == 0); + nm_assert (a->sub_offset_plus_1 == 0); + } else { + nm_assert (selection_type == NM_META_SELECTION_TYPE_SUB_FIELD); + nm_assert (a->self_offset_plus_1 > 0); p->self_selection = &pdata[a->self_offset_plus_1 - 1]; - if (a->sub_offset_plus_1 > 0) - p->sub_selection = &pdata[a->sub_offset_plus_1 - 1]; + if (a->sub_offset_plus_1 > 0) + p->sub_selection = &pdata[a->sub_offset_plus_1 - 1]; + } } } @@ -610,7 +619,7 @@ _output_selection_pack (const NMMetaAbstractInfo *const* fields_array, } static gboolean -_output_selection_select_one (const NMMetaAbstractInfo *const* fields_array, +_output_selection_select_one (const NMMetaAbstractInfo *const*fields_array, const char *fields_prefix, const char *fields_str, gboolean validate_nested, @@ -728,11 +737,15 @@ not_found: } NMMetaSelectionResultList * -nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array) +nm_meta_selection_create_all (const NMMetaAbstractInfo *const*fields_array, + NMMetaSelectionType selection_type) { gs_unref_array GArray *array = NULL; guint i; + nm_assert (NM_IN_SET (selection_type, NM_META_SELECTION_TYPE_COMMON, + NM_META_SELECTION_TYPE_ALL)); + if (fields_array) { array = g_array_new (FALSE, FALSE, sizeof (OutputSelectionItem)); for (i = 0; fields_array[i]; i++) { @@ -744,11 +757,11 @@ nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array) } } - return _output_selection_pack (fields_array, array, NULL); + return _output_selection_pack (fields_array, array, NULL, selection_type); } NMMetaSelectionResultList * -nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_array, +nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const*fields_array, const char *fields_prefix, const char *fields_str, /* one field selector (contains no commas) and is already stripped of spaces. */ gboolean validate_nested, @@ -768,12 +781,12 @@ nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_arra &str, error)) return NULL; - return _output_selection_pack (fields_array, array, str); + return _output_selection_pack (fields_array, array, str, NM_META_SELECTION_TYPE_SUB_FIELD); } NMMetaSelectionResultList * -nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_array, +nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const*fields_array, const char *fields_prefix, const char *fields_str, /* a comma separated list of selectors */ gboolean validate_nested, @@ -807,13 +820,13 @@ nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_arr if ( fields_len == 0 || ( fields_len == 1 && !g_ascii_strcasecmp (fields_words[0], "all"))) - return nm_meta_selection_create_all (fields_array); + return nm_meta_selection_create_all (fields_array, NM_META_SELECTION_TYPE_ALL); else if ( fields_len == 1 && !g_ascii_strcasecmp (fields_words[0], "common")) { gs_free gpointer f = NULL; fields_array = nm_meta_abstract_infos_select_included_in_common (fields_array, -1, NULL, &f); - return nm_meta_selection_create_all (fields_array); + return nm_meta_selection_create_all (fields_array, NM_META_SELECTION_TYPE_COMMON); } if (fields_prefix) { @@ -835,5 +848,5 @@ nm_meta_selection_create_parse_list (const NMMetaAbstractInfo *const* fields_arr return NULL; } - return _output_selection_pack (fields_array, array, str); + return _output_selection_pack (fields_array, array, str, NM_META_SELECTION_TYPE_SUB_FIELD); } diff --git a/clients/common/nm-meta-setting-access.h b/clients/common/nm-meta-setting-access.h index 776998e257..a1d69d77ac 100644 --- a/clients/common/nm-meta-setting-access.h +++ b/clients/common/nm-meta-setting-access.h @@ -72,10 +72,17 @@ char *nm_meta_abstract_infos_get_names_str (const NMMetaAbstractInfo *const*fiel /*****************************************************************************/ +typedef enum { + NM_META_SELECTION_TYPE_SUB_FIELD, + NM_META_SELECTION_TYPE_ALL, + NM_META_SELECTION_TYPE_COMMON, +} NMMetaSelectionType; + typedef struct { const NMMetaAbstractInfo *info; const char *self_selection; const char *sub_selection; + NMMetaSelectionType selection_type; guint idx; } NMMetaSelectionItem; @@ -84,7 +91,8 @@ typedef struct { const NMMetaSelectionItem items[]; } NMMetaSelectionResultList; -NMMetaSelectionResultList *nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array); +NMMetaSelectionResultList *nm_meta_selection_create_all (const NMMetaAbstractInfo *const* fields_array, + NMMetaSelectionType selection_type); NMMetaSelectionResultList *nm_meta_selection_create_parse_one (const NMMetaAbstractInfo *const* fields_array, const char *fields_prefix, const char *fields_str, |