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

Support expressions in selectattr/rejectattr #249

Merged
merged 3 commits into from
Nov 7, 2018

Conversation

mattcoley
Copy link
Collaborator

Previous PR allowed nested properties in selectattr and rejectattr #238.

This is great however is still limiting because you cannot execute filters. For example you may have a list of objects you would like to map to a value before executing the filter: |selectattr("optionList|map('id')", "containing", 1) or perhaps you would like to trim the values |selectattr("name|trim|lower", "equalto", "matt").

This resolves the first argument as an expression and then applies the test case. The solution is hacky as it stores a temporary variable to resolve the expression against. I have not thought of a way to get around this.

Motivation: This makes it easier to filter many HubL/HubSpot objects that have properties that are lists of other objects. Previously this required a for loop.

@codecov-io
Copy link

Codecov Report

Merging #249 into master will increase coverage by 0.02%.
The diff coverage is 77.77%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #249      +/-   ##
============================================
+ Coverage     71.84%   71.87%   +0.02%     
- Complexity     1402     1405       +3     
============================================
  Files           220      220              
  Lines          4412     4420       +8     
  Branches        700      701       +1     
============================================
+ Hits           3170     3177       +7     
  Misses         1002     1002              
- Partials        240      241       +1
Impacted Files Coverage Δ Complexity Δ
...m/hubspot/jinjava/lib/filter/SelectAttrFilter.java 71.42% <77.77%> (+1.05%) 10 <2> (+2) ⬆️
...c/main/java/com/hubspot/jinjava/util/ScopeMap.java 79.03% <0%> (+1.61%) 23% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c71bd76...25dd424. Read the comment docs.

@mattcoley mattcoley merged commit fcc1ec3 into master Nov 7, 2018
@mattcoley mattcoley deleted the support-selectattr-expression branch November 7, 2018 15:23
@TheWebTech
Copy link

TheWebTech commented Nov 26, 2018

Understand the performance issues. Perhaps there's a way to allow some of the functionality without allowing everything.
Change:
|selectattr("name|trim|lower", "equalto", "matt")
to
|selectattr("name", "equalto", "matt",["trim","lower"])

The 4th parameter being permitted filters to apply to the value of the attribute. You could then restrict which filters were applied.
While not super sophisticated you could simply have a bunch of if statements to make it work
{%if "trim" in permittedFilters %} {%set attribute = attribute|trim %}

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.

4 participants