Skip to content

Add common set theory filters.#262

Merged
mattcoley merged 3 commits into
masterfrom
set-theory-filters
Dec 19, 2018
Merged

Add common set theory filters.#262
mattcoley merged 3 commits into
masterfrom
set-theory-filters

Conversation

@mattcoley

Copy link
Copy Markdown
Collaborator

Fixes #261
Adds union, intersect, difference, and symmetric_difference similar to Ansible https://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_filters.html#set-theory-filters.

@mattcoley mattcoley requested a review from boulter December 18, 2018 21:03
@codecov-io

codecov-io commented Dec 18, 2018

Copy link
Copy Markdown

Codecov Report

Merging #262 into master will increase coverage by 0.09%.
The diff coverage is 94.73%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #262      +/-   ##
============================================
+ Coverage     71.96%   72.05%   +0.09%     
- Complexity     1437     1453      +16     
============================================
  Files           226      231       +5     
  Lines          4515     4534      +19     
  Branches        717      718       +1     
============================================
+ Hits           3249     3267      +18     
  Misses         1014     1014              
- Partials        252      253       +1
Impacted Files Coverage Δ Complexity Δ
.../com/hubspot/jinjava/lib/filter/FilterLibrary.java 100% <ø> (ø) 4 <0> (ø) ⬇️
...m/hubspot/jinjava/lib/filter/DifferenceFilter.java 100% <100%> (ø) 3 <3> (?)
...va/com/hubspot/jinjava/lib/filter/UnionFilter.java 100% <100%> (ø) 3 <3> (?)
.../jinjava/lib/filter/SymmetricDifferenceFilter.java 100% <100%> (ø) 3 <3> (?)
...om/hubspot/jinjava/lib/filter/IntersectFilter.java 100% <100%> (ø) 3 <3> (?)
.../hubspot/jinjava/lib/filter/AbstractSetFilter.java 85.71% <85.71%> (ø) 4 <4> (?)
... and 1 more

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 fa2cb67...f4292da. Read the comment docs.

import com.hubspot.jinjava.util.ObjectIterator;

@JinjavaDoc(
value = "Returns a list containing elements present in the first list and not the second list",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but not

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map<String, Object> kwargs) {

LinkedHashSet<Object> result = new LinkedHashSet<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about using Sets.union?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The reason I did not use the set function helpers is that they would be slower/require more space. I would need to loop over the two iterables to form the sets, then apply those functions. This wouldn't be noticeable for small sets but might be for large sets. The functions are pretty simple anyways, and I'm not using the set helper functions is worth it but it's up to you.

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map<String, Object> kwargs) {

Set<Object> keysFirst = new HashSet<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sets.symmetricDifference?

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map<String, Object> kwargs) {

Set<Object> keys = new HashSet<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sets.intersection?

Set<Object> keys = new HashSet<>();
Set<Object> result = new LinkedHashSet<>();

Object attr = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sets.difference?

@mattcoley

Copy link
Copy Markdown
Collaborator Author

@boulter Refactored to use the Sets functions to simplify the code.

@mattcoley mattcoley merged commit 43ccadb into master Dec 19, 2018
@mattcoley mattcoley deleted the set-theory-filters branch December 19, 2018 20:34
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.

3 participants