Re: [patch net-next v2 3/3] tools: ynl: allow user to pass enum string instead of scalar value

Jakub Kicinski
2024-02-21T18:49:37Z

On Wed, 21 Feb 2024 16:54:15 +0100 Jiri Pirko wrote:
> + def _encode_enum(self, attr_spec, value):
> + try:
> + return int(value)
> + except (ValueError, TypeError) as e:
> + if 'enum' not in attr_spec:
> + raise e
> + enum = self.consts[attr_spec['enum']]
> + if enum.type == 'flags' or attr_spec.get('enum-as-flags', False):
> + scalar = 0
> + if isinstance(value, str):
> + value = [value]
> + for single_value in value:
> + scalar += enum.entries[single_value].user_value(as_flags = True)
> + return scalar
> + else:
> + return enum.entries[value].user_value()

That's not symmetric with _decode_enum(), I said:

vvvvvvvvvvvvvvvvvvvvvv
It'd be cleaner to make it more symmetric with _decode_enum(), and
call it _encode_enum().

How about you go back to your _get_scalar name and only factor out the
parts which encode the enum to be a _encode_enum() ?