Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model consistency, serialisation and methods #149

Closed
felix-hilden opened this issue Feb 23, 2020 · 4 comments
Closed

Model consistency, serialisation and methods #149

felix-hilden opened this issue Feb 23, 2020 · 4 comments
Assignees
Labels
change Change behaviour of an existing component consideration Future decision to be discussed enhancement New feature or improvement
Milestone

Comments

@felix-hilden
Copy link
Owner

Currently a dataclass behaves like this:

  • asdict: strip dataclass but keep other classes, like Timestamp and enums
  • repr: Dataclass representation (class name, fields and values recursively)
  • str: JSON encode all types

We also have a ModelList to allow string dataclasses. But this is not really comprehensive. It would be nice to include the behavior to all responses, like the tuple of results returned from search. This could be achieved by making all return types share the repr/str structure and using those types as well when parsing inner lists. We could also provide an asdict equivalent for lists like aslist (maybe something else), or call both asbuiltin or something like that.

@felix-hilden felix-hilden added enhancement New feature or improvement change Change behaviour of an existing component labels Feb 23, 2020
@felix-hilden felix-hilden added this to the v2.0.0 milestone Feb 23, 2020
@felix-hilden felix-hilden self-assigned this Feb 23, 2020
@felix-hilden
Copy link
Owner Author

When printing models, str is naturally called. Should it produce the JSON representation? The answer could be no, and we could provide a json method. str(tracks) is far more ambiguous than tracks.json(), and then print(tracks) could produce the readable representation.

@felix-hilden felix-hilden added the consideration Future decision to be discussed label Feb 23, 2020
@felix-hilden
Copy link
Owner Author

felix-hilden commented Feb 24, 2020

We now have a readable repr for models (not lists). I experimented a bit with providing a similar repr for ModelList, but it seems that pretty printing uses the repr of lists, which breaks the whole thing. So the lists would need to be converted back to builtin ones in asdict. As far as I know, there is no way to do that in dataclasses, but attrs might.

I think we should set the thing up like this:

  • Endpoints that would return tuples or lists return a ModelList and inner lists are parsed to ModelLists
  • Both models and lists have their repr and str be a readable summary of the fields much like now
  • Both should define asbuiltin (or similar) that parses dataclasses to dictionaries and ModelLists to builtin lists, enums and timestamps are preserved
  • Both should define json (or similar) to serialise like str does now
  • Both should define pprint to use the builtin representation to view the entire contents of the object

Enums and timestamps could even be converted back to strings. And now that pprint is essentially replaced by the repr it could be discarded.

@felix-hilden felix-hilden changed the title Consistent response models with ModelList Model consistency, serialisation and methods Feb 24, 2020
@felix-hilden
Copy link
Owner Author

Also, serialise should be moved under model. There is no reason to have it as a separate module, because it shouldn't really be imported or used.

@felix-hilden
Copy link
Owner Author

I think we should not preserve timestamps and enums when converting the models. They are technically available outside Tekore, but strings are so much more common and straightforward that we'll use them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change Change behaviour of an existing component consideration Future decision to be discussed enhancement New feature or improvement
Projects
None yet
Development

No branches or pull requests

1 participant