Uninett / zino

Zino 2.0 - Network state monitor for research networks
Apache License 2.0
3 stars 5 forks source link

`Event.model_dump_simple_attrs` should replace `_` with `-` when dumping field names #281

Closed lunkwill42 closed 2 months ago

lunkwill42 commented 3 months ago

Background

The legacy Zino protocol features some event attributes that contain dashes (-) in their names. This is not currently supported by the Zino 2 codebase, as the attributes need to be valid Python names. Generally, the - is represented as _ in the Python attribute names, so the model_dump_simple_attrs method could easily be adapted to just replacing _ with - on output to improve compatibility with the legacy protocol.

Additionally, some field names also has weird casing in the legacy protocol, which are also not PEP-8 compliant. These could potentially be solved by adding alias names in the Pydantic models, and having model_dump_simple_attrs uses the aliases if defined.

References

True, model_dump_simple_attrs exists specifically for the legacy protocol - so it should indeed replace _ with - in the output (are there any attrs in the protocol that use an underscore?)

However, that still isn't enough if the casing is also important for the protocol clients, which makes me think a Field alias might be the way to go. model_dump_simple_attrs could look up field aliases and use them if they exist, in addition to replacing underscores.

_Originally posted by @lunkwill42 in https://github.com/Uninett/zino/pull/265#discussion_r1658447483_