netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Donald Hunter <donald.hunter@gmail.com>
To: Alessandro Marcolini <alessandromarcolini99@gmail.com>
Cc: Jakub Kicinski <kuba@kernel.org>,
	 netdev@vger.kernel.org,  "David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@google.com>,
	 Paolo Abeni <pabeni@redhat.com>,
	 Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org,
	 Jacob Keller <jacob.e.keller@intel.com>,
	Breno Leitao <leitao@debian.org>,  Jiri Pirko <jiri@resnulli.us>,
	donald.hunter@redhat.com
Subject: Re: [PATCH net-next v1 02/12] tools/net/ynl: Support sub-messages in nested attribute spaces
Date: Sun, 28 Jan 2024 19:36:29 +0000	[thread overview]
Message-ID: <m2bk95w8qq.fsf@gmail.com> (raw)
In-Reply-To: <fcf9630e-26fd-4474-a791-68c548a425b6@gmail.com> (Alessandro Marcolini's message of "Sat, 27 Jan 2024 19:52:20 +0100")

Alessandro Marcolini <alessandromarcolini99@gmail.com> writes:

> On 1/27/24 18:18, Donald Hunter wrote:
>> Okay, so I think the behaviour we need is to either search current scope
>> or search the outermost scope. My suggestion would be to replace the
>> ChainMap approach with just choosing between current and outermost
>> scope. The unusual case is needing to search the outermost scope so
>> using a prefix e.g. '/' for that would work.
>>
>> We can have 'selector: kind' continue to refer to current scope and then
>> have 'selector: /kind' refer to the outermost scope.
>>
>> If we run into a case that requires something other than current or
>> outermost then we could add e.g. '../kind' so that the scope to search
>> is always explicitly identified.
>
> Wouldn't add different chars in front of the selctor value be confusing?
>
> IMHO the solution of using a ChainMap with levels could be an easier solution. We could just
> modify the __getitem__() method to output both the value and the level, and the get() method to
> add the chance to specify a level (in our case the level found in the spec) and error out if the
> specified level doesn't match with the found one. Something like this:

If we take the approach of resolving the level from the spec then I
wouldn't use ChainMap. Per the Python docs [1]: "A ChainMap class is
provided for quickly linking a number of mappings so they can be treated
as a single unit."

I think we could instead pass a list of mappings from current to
outermost and then just reference the correct level that was resolved
from the spec.

> from collections import ChainMap
>
> class LevelChainMap(ChainMap):
>     def __getitem__(self, key):
>         for mapping in self.maps:
>             try:
>                 return mapping[key], self.maps[::-1].index(mapping)
>             except KeyError:
>                 pass
>         return self.__missing__(key)
>
>     def get(self, key, default=None, level=None):
>         val, lvl = self[key] if key in self else (default, None)
>         if level:
>             if lvl != level:
>                 raise Exception("Level mismatch")
>         return val, lvl
>
> # example usage
> c = LevelChainMap({'a':1}, {'inner':{'a':1}}, {'outer': {'inner':{'a':1}}})
> print(c.get('a', level=2))
> print(c.get('a', level=1)) #raise err
>
> This will leave the spec as it is and will require small changes.
>
> What do you think?

The more I think about it, the more I agree that using path-like syntax
in the selector is overkill. It makes sense to resolve the selector
level from the spec and then directly access the mappings from the
correct scope level.

[1] https://docs.python.org/3/library/collections.html#collections.ChainMap

  reply	other threads:[~2024-01-29 11:29 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 16:05 [PATCH net-next v1 00/12] tools/net/ynl: Add features for tc family Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 01/12] tools/net/ynl: Add --output-json arg to ynl cli Donald Hunter
2024-01-25 13:50   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 02/12] tools/net/ynl: Support sub-messages in nested attribute spaces Donald Hunter
2024-01-24  0:18   ` Jakub Kicinski
2024-01-24  9:37     ` Donald Hunter
2024-01-24 15:32       ` Jakub Kicinski
2024-01-26 12:44         ` Donald Hunter
2024-01-26 18:50           ` Jakub Kicinski
2024-01-27 17:18             ` Donald Hunter
2024-01-27 18:52               ` Alessandro Marcolini
2024-01-28 19:36                 ` Donald Hunter [this message]
2024-01-29 20:35                   ` Alessandro Marcolini
2024-01-30  1:32                   ` Jakub Kicinski
2024-01-30  1:42               ` Jakub Kicinski
2024-01-30  9:12                 ` Donald Hunter
2024-02-01 20:53                 ` Jacob Keller
2024-02-02  0:04                   ` Jakub Kicinski
2024-02-02 17:12                     ` Jacob Keller
2024-01-23 16:05 ` [PATCH net-next v1 03/12] tools/net/ynl: Refactor fixed header encoding into separate method Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 04/12] tools/net/ynl: Add support for encoding sub-messages Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 05/12] tools/net/ynl: Encode default values for binary blobs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 06/12] tools/net/ynl: Combine struct decoding logic in ynl Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 07/12] tools/net/ynl: Rename _fixed_header_size() to _struct_size() Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 08/12] tools/net/ynl: Move formatted_string method out of NlAttr Donald Hunter
2024-01-25 14:24   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 09/12] tools/net/ynl: Add support for nested structs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 10/12] doc/netlink: Describe nested structs in netlink raw docs Donald Hunter
2024-01-23 16:05 ` [PATCH net-next v1 11/12] tools/net/ynl: Add type info to struct members in generated docs Donald Hunter
2024-01-25 13:59   ` Breno Leitao
2024-01-23 16:05 ` [PATCH net-next v1 12/12] doc/netlink/specs: Update the tc spec Donald Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2bk95w8qq.fsf@gmail.com \
    --to=donald.hunter@gmail.com \
    --cc=alessandromarcolini99@gmail.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@redhat.com \
    --cc=edumazet@google.com \
    --cc=jacob.e.keller@intel.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).