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 negative list indeces #245

Merged
merged 4 commits into from
Oct 20, 2018

Conversation

dbyron0
Copy link

@dbyron0 dbyron0 commented Oct 18, 2018

so e.g.

{{ (split_me|split('-'))[-2] }}

works instead of

{% set parts = split_me|split('-') %}{{ parts[parts|length - 2] }}"

so e.g.

{{ (split_me|split('-'))[-2] }}

works instead of

{% set parts = split_me.split('-') %}{{ parts[parts|length - 2] }}"
@codecov-io
Copy link

codecov-io commented Oct 18, 2018

Codecov Report

Merging #245 into master will decrease coverage by 0.06%.
The diff coverage is 57.89%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #245      +/-   ##
============================================
- Coverage     71.84%   71.78%   -0.07%     
- Complexity     1396     1400       +4     
============================================
  Files           220      220              
  Lines          4383     4402      +19     
  Branches        693      699       +6     
============================================
+ Hits           3149     3160      +11     
- Misses          996     1002       +6     
- Partials        238      240       +2
Impacted Files Coverage Δ Complexity Δ
.../hubspot/jinjava/el/ext/JinjavaListELResolver.java 47.05% <57.89%> (+13.72%) 6 <5> (+4) ⬆️

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 fb690a4...6cda4ef. Read the comment docs.

@mattcoley
Copy link
Collaborator

It seems easier to first check if the value is resolvable to a list. If so, call toIndex(property). If that value is negative, then property += value.size();

@dbyron0
Copy link
Author

dbyron0 commented Oct 18, 2018

I was trying to tread as lightly as possible -- to leave as much to the superclass as possible. If you're OK with totally taking over, which is what I think you're suggesting, I'll give it a shot.

I could use a hand coming up with test cases that cover the non-string parts of toIndex.

EDIT: Aaah, I think I see what you're saying....and it still leaves the superclass doing much of the work.

} catch (NumberFormatException e) {
throw new IllegalArgumentException("Cannot parse list index: " + property);
}
} else if (property instanceof Character) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a comment here that this seems a little too accommodating and if someone is passing a char here, they probably have a bug somewhere else or they really don't know how to use list indexes :)

But, it looks like this just copied from juel. Crazy.

} catch (IllegalArgumentException e) {
return null;
}
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

nice javadocs!

@dbyron0
Copy link
Author

dbyron0 commented Oct 18, 2018

@mattcoley like this? 7d79492

@@ -30,12 +31,109 @@ public boolean isReadOnly(ELContext context, Object base, Object property) {
@Override
public Object getValue(ELContext context, Object base, Object property) {
try {
return super.getValue(context, base, property);
final Object superValue = super.getValue(context, base, property);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: final within method not needed.

// Leave the range checking to the superclass.
index += list.size();
}
return super.getValue(context, base, index);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can just set property = index + list.size() and just have one super.getValue() outside the if

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, yes. Object can be anything.

60e5d91

@mattcoley
Copy link
Collaborator

Yea that approach is a bit cleaner.

List<?> list = (List<?>) base;
if (index < 0) {
// Leave the range checking to the superclass.
property = index + list.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

list can be inlined.

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

@mattcoley mattcoley left a comment

Choose a reason for hiding this comment

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

This looks pretty good to me, nice work!

@dbyron0
Copy link
Author

dbyron0 commented Oct 20, 2018

Thanks much. Would love to see this merged and a new version released.

@mattcoley mattcoley merged commit 6e85bd8 into HubSpot:master Oct 20, 2018
@dbyron0 dbyron0 deleted the negative_list_indeces branch October 20, 2018 03:59
@dbyron0
Copy link
Author

dbyron0 commented Oct 20, 2018

Thanks for merging!

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