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

Introduce the concept of "producers" #43

Merged
merged 11 commits into from
Jul 16, 2021
Merged

Introduce the concept of "producers" #43

merged 11 commits into from
Jul 16, 2021

Conversation

j4mie
Copy link
Member

@j4mie j4mie commented Jul 15, 2021

This is a big conceptual shift.

Binding the name to the value by having projectors return dictionaries turned out to not be ideal. It was conflating the lower layers (getting values from instances) with the higher layers (presenting the data) because those projector functions had to give a specific name to the value they returned. This necessitated the introducion of "aliasing" throughout the API, whereby the key returned from the projector was replaced with another one. Aliasing was just a hack around the root problem - the names should only be bound in specs.

This PR introduces the concept of "producers", which are functions which just get a single value from an instance. These are then later wrapped to form projectors.

The recommended way to use django-readers is now to write custom producer functions (rather than projector functions), and then mount them explicitly in a spec under a given key:

def produce_upper_name(instance):
    return instance.name.upper()


upper_name = (qs.include_fields("name"), produce_upper_name)


prepare, project = specs.process([
    {"upper_name": upper_name}
])

This makes the internals of the library a little bit more complicated, but the end-user experience is actually less confusing. And the only actual breaking-backwards-compatibility change (I think) is the removal of the alias functions.

@j4mie j4mie changed the title README changes to introduce the concept of "producers" Introduce the concept of "producers" Jul 15, 2021
django_readers/pairs.py Outdated Show resolved Hide resolved
@pmg103
Copy link
Contributor

pmg103 commented Jul 16, 2021

Yeah I think this is an improvement

README.md Show resolved Hide resolved
@@ -142,11 +142,11 @@ Related objects can also be produced using the `producers.relationship` function

```python
project = projectors.combine(
projectors.wrap_producer("name", producers.attr("name")),
projectors.producer_to_projector("name", producers.attr("name")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this extremely verbose but if its internal only I guess its better to be explicitly very explicit and explicit and verbose and extra verbosely explicit

@j4mie j4mie merged commit e539f2c into main Jul 16, 2021
@j4mie j4mie deleted the producer-refactor branch July 16, 2021 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants