[PATCH net-next 2/7] tools: ynl: support ignore-index in indexed-array decoding

Asbjørn Sloth Tønnesen posted 7 patches 3 months, 2 weeks ago
[PATCH net-next 2/7] tools: ynl: support ignore-index in indexed-array decoding
Posted by Asbjørn Sloth Tønnesen 3 months, 2 weeks ago
When decoding indexed-arrays with `ignore-index` set, then
elide the index, aka. the nested attribute-type.

Previously the index was always preserved for sub-type nest,
but was elided for all other sub-types.

I have opted to reuse the existing `subattr` variable, as renaming
it will render the diff unreadable at least for this version.

Output if `rates` does NOT have `ignore-index` set:
  $ ./tools/net/ynl/pyynl/cli.py --family nl80211 --dump get-wiphy
  [...]
  'rates': [{0: {'rate': 60}},
            {1: {'rate': 90}},
            {2: {'rate': 120}},
            {3: {'rate': 180}},
            {4: {'rate': 240}},
            [...]

Output if `rates` has `ignore-index` set to true:
  $ ./tools/net/ynl/pyynl/cli.py --family nl80211 --dump get-wiphy
  [...]
  'rates': [{'rate': 60},
            {'rate': 90},
            {'rate': 120},
            {'rate': 180},
            {'rate': 240},
            [...]

If the above example had to be passed back though --json, then it
now aligns with the new output format, and would look like this:
  --json '{"rates":[ {"rate":60}, {"rate":90}, ... ]}'

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
---
 tools/net/ynl/pyynl/lib/ynl.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/net/ynl/pyynl/lib/ynl.py b/tools/net/ynl/pyynl/lib/ynl.py
index 62383c70ebb95..14c7e51db6f5c 100644
--- a/tools/net/ynl/pyynl/lib/ynl.py
+++ b/tools/net/ynl/pyynl/lib/ynl.py
@@ -690,23 +690,26 @@ class YnlFamily(SpecFamily):
             item = NlAttr(attr.raw, offset)
             offset += item.full_len
 
+            subattr = None
             if attr_spec["sub-type"] == 'nest':
-                subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
-                decoded.append({ item.type: subattrs })
+                subattr = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
             elif attr_spec["sub-type"] == 'binary':
                 subattr = item.as_bin()
                 if attr_spec.display_hint:
                     subattr = self._formatted_string(subattr, attr_spec.display_hint)
-                decoded.append(subattr)
             elif attr_spec["sub-type"] in NlAttr.type_formats:
                 subattr = item.as_scalar(attr_spec['sub-type'], attr_spec.byte_order)
                 if 'enum' in attr_spec:
                     subattr = self._decode_enum(subattr, attr_spec)
                 elif attr_spec.display_hint:
                     subattr = self._formatted_string(subattr, attr_spec.display_hint)
-                decoded.append(subattr)
             else:
                 raise Exception(f'Unknown {attr_spec["sub-type"]} with name {attr_spec["name"]}')
+
+            if attr_spec.get('ignore-index', False):
+                decoded.append(subattr)
+            else:
+                decoded.append({ item.type: subattr })
         return decoded
 
     def _decode_nest_type_value(self, attr, attr_spec):
-- 
2.51.0