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

Add addon filter objects #2136

Merged
merged 4 commits into from
Mar 30, 2020
Merged

Add addon filter objects #2136

merged 4 commits into from
Mar 30, 2020

Conversation

uhlissuh
Copy link
Contributor

@uhlissuh uhlissuh commented Mar 10, 2020

fixes #2104

So, splitting these into two filter objects came from Rehan, which I thought was a good idea. He mentioned seeing about having them share some code, because there is duplication here, and I kinda looked at that for a while and arrived at wondering if we will really gain much here trying to have these two share code? There's only two that are shaped like this right now. There isn't a clear thing I could pull out, maybe someone sees something. Perhaps that's an argument for making them one filter?

@uhlissuh
Copy link
Contributor Author

uhlissuh commented Mar 10, 2020

I added the @jaredlockhart review and then removed it because he was doing a trial of his slackbot and wanted to see if it was working :)

Comment on lines 255 to 266
if any_or_all == "all":
return "&&".join(
(f'normandy.addons["{addon}"].isActive' for addon in self.initial_data["addons"])
)
elif any_or_all == "any":
return "||".join(
(f'normandy.addons["{addon}"].isActive' for addon in self.initial_data["addons"])
)
else:
raise serializers.ValidationError(
f"Unrecognized string for any_or_all: {any_or_all!r}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

probably a cleaner way to do this is:

if any_or_all == "all":
    glue = "&&"
elif any_or_all == "any":
    glue = "||"
else:
    raise # Exception here

return glue.join(f'...')

Copy link
Contributor Author

@uhlissuh uhlissuh Mar 10, 2020

Choose a reason for hiding this comment

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

yes you're right.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not to butt in but generally for this kind of mapping pattern you can use a dictionary

Copy link
Contributor

@rehandalal rehandalal left a comment

Choose a reason for hiding this comment

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

wondering if we will really gain much here trying to have these two share code

I think in general anytime you are doing the same thing (with the same underlying motivation) this is code that can be factored out to keep things DRYer. The benefit is maintenance is easier.

this is far from complete code, but you could have something in this shape:

class BaseAddonFilter(BaseFilter):
    # all the usual stuff goes here

    def get_formatted_string(self, addon):
        raise NotImpementedError("Error message.")

    def to_jexl(self):
          # all the stuff to determine glue
          return glue.join(self.get_formatted_string(addon) for addon in self.initial_data["addons"])


class AddonInstalledFilter(BaseAddonFilter):
    def get_formatted_string(self, addon):
        return f'normandy.addons["{addon}"]'


class AddonActiveFilter(BaseAddonFilter):
    def get_formatted_string(self, addon):
        return f'normandy.addons["{addon}"].isActive'

def create_basic_filter(self, addons=["@abcdef", "ghijk@lmnop"], any_or_all="any"):
return AddonInstalledFilter.create(addons=addons, any_or_all=any_or_all)

def test_generates_jexl_installed_not_active(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I the the names of these tests are out of date? It looks like they may have been written before the filter was split into two.

Copy link
Contributor

@rehandalal rehandalal left a comment

Choose a reason for hiding this comment

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

bors r+

bors bot added a commit that referenced this pull request Mar 30, 2020
2136: Add addon filter objects r=rehandalal a=uhlissuh

fixes #2104 

So, splitting these into two filter objects came from Rehan, which I thought was a good idea. He mentioned seeing about having them share some code, because there is duplication here, and I kinda looked at that for a while and arrived at wondering if we will really gain much here trying to have these two share code? There's only two that are shaped like this right now. There isn't a clear thing I could pull out, maybe someone sees something. Perhaps that's an argument for making them one filter? 

Co-authored-by: Alissa Sobo <[email protected]>
@bors
Copy link
Contributor

bors bot commented Mar 30, 2020

Canceled

@uhlissuh
Copy link
Contributor Author

bors merge

@bors
Copy link
Contributor

bors bot commented Mar 30, 2020

@bors bors bot merged commit 7b912b6 into master Mar 30, 2020
@bors bors bot deleted the addon-fo branch March 30, 2020 19:27
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.

Filter Object: Addon
4 participants