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

Fixes #159 : Adds dict support for DefaultFilter #344

Closed
wants to merge 5 commits into from

Conversation

gabru-md
Copy link
Contributor

Fixes #159

Adds a method for processing the dictionary type in DefaultFilter.

Problem : Arguments were being converted to String [] before being processed by the DefaultFilter

Solution : Created a new method for processing the Object [], incase any arg is of type PyWrapper. Implemented the function and created tests for the same.

😄

@gabru-md
Copy link
Contributor Author

Copy link
Contributor

@boulter boulter left a comment

Choose a reason for hiding this comment

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

Thanks @gabru-md!

I have a bunch of style comments and some implementation related comments. Let me know if you have any questions. Thanks again!

@@ -15,16 +15,16 @@
**********************************************************************/
package com.hubspot.jinjava.lib.filter;

import java.math.BigDecimal;
Copy link
Contributor

Choose a reason for hiding this comment

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

could you update your import order settings? java imports first is our preference

import com.hubspot.jinjava.interpret.InvalidReason;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.interpret.InvalidInputException;
Copy link
Contributor

Choose a reason for hiding this comment

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

this was moved out of alphabetical order

* Created by manishdevgan on 25/06/19.
*/
public class DefaultFilterTest {
Jinjava jinjava;
Copy link
Contributor

Choose a reason for hiding this comment

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

our indentation standard is 2 spaces. Could you adjust?


@Test
public void itSetsDefaultStringValues() {
assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>())).isEqualTo("some random value");
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you either leave these as long lines or hard-wrap them on a logical boundary, like , (preferred)?

@@ -55,6 +68,9 @@ default Object filter(Object var, JinjavaInterpreter interpreter, Object[] args,
List<String> stringArgs = new ArrayList<>();

for (Object arg : allArgs) {
if(arg instanceof PyWrapper) {
Copy link
Contributor

Choose a reason for hiding this comment

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

space after if please

@gabru-md
Copy link
Contributor Author

@boulter I've made changes to the files.
please review again 😄

@gabru-md
Copy link
Contributor Author

cc : @boulter

Copy link
Contributor

@boulter boulter left a comment

Choose a reason for hiding this comment

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

Looks like some of the issue have been addressed, but there's still a lot of import ordering changes and diffs that are unnecessary. Could you address those? That should dramatically decrease the number of changes here and it make it easier to review.

@@ -25,6 +25,8 @@
import com.hubspot.jinjava.interpret.InvalidReason;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;


Copy link
Contributor

Choose a reason for hiding this comment

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

can you remove extra space here?

@@ -15,11 +15,12 @@
**********************************************************************/
package com.hubspot.jinjava.lib.filter;

import java.util.HashMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

still some lingering import reordering...


if (args.length < 1) {
throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attribute name to use)");
}

return interpreter.resolveProperty(var, args[0]);
return interpreter.resolveProperty(var, args[0].toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Objects.toString is safer

@@ -27,10 +32,17 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter,
String... args) {

Object... args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you join this with the previous line?


Object... args) {
List<String> stringArgs = new ArrayList<>();
for(Object arg: args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

space after for and before : please

Object... args) {
List<String> stringArgs = new ArrayList<>();
for(Object arg: args) {
stringArgs.add(arg == null ? null : Objects.toString(arg));
Copy link
Contributor

Choose a reason for hiding this comment

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

Objects.toString(arg) will handle nulls for you.

public String getName() {
return "unixtimestamp";
}
@Override
Copy link
Contributor

Choose a reason for hiding this comment

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

please restore original indentation

@@ -26,7 +26,7 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter,
String... args) {
Object... args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can join with previous line

@gabru-md
Copy link
Contributor Author

sure! will do the necessary changes

@gabru-md
Copy link
Contributor Author

gabru-md commented Jul 1, 2019

@boulter I've added changes. please review again 🙏

Copy link
Contributor

@boulter boulter left a comment

Choose a reason for hiding this comment

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

I noticed a bunch of small changes, but there are many more instances that I didn't get to. Can you clean these up first and then I'll take a look when the diffs are smaller? Thanks.

if (arg.length < 1) {
throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (divisor) argument");
}
String toMul = arg[0];
String toMul = (arg[0] == null) ? null : Objects.toString(arg[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Objects.toString will take care of nulls for you.

@@ -26,6 +27,8 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;


Copy link
Contributor

Choose a reason for hiding this comment

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

remove extra newlines please


import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.util.ForLoop;
import com.hubspot.jinjava.util.ObjectIterator;
import org.apache.commons.lang3.math.NumberUtils;
Copy link
Contributor

Choose a reason for hiding this comment

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

fix reordered import

@@ -16,6 +17,7 @@
import com.hubspot.jinjava.lib.fn.Functions;
import com.hubspot.jinjava.objects.date.PyishDate;


Copy link
Contributor

Choose a reason for hiding this comment

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

extra space

@@ -49,5 +49,6 @@ public Object filter(Object var, JinjavaInterpreter interpreter,
return str.equals("1") ? Boolean.TRUE : BooleanUtils.toBoolean(str);
}


Copy link
Contributor

Choose a reason for hiding this comment

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

extra space

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import org.apache.commons.lang3.StringUtils;
Copy link
Contributor

Choose a reason for hiding this comment

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

import order


import com.google.common.collect.Lists;
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import org.apache.commons.lang3.BooleanUtils;
Copy link
Contributor

Choose a reason for hiding this comment

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

import order

@@ -9,6 +9,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;


Copy link
Contributor

Choose a reason for hiding this comment

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

extra space

@@ -21,7 +22,12 @@
})
public class DifferenceFilter extends AbstractSetFilter {

@Override
@Override
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation is off here.

@gabru-md
Copy link
Contributor Author

@boulter I'll add changes to this PR in the weekend.
I'm a bit busy with some stuff. Writing it down so that this PR doesn't get closed for inactivity.
cheers!

@raomuyang
Copy link
Contributor

@boulter I've created a checkstyle configuration to cover some scenarios as much as possible: #353

@gabru-md
Copy link
Contributor Author

opening a different PR for the same.

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.

Bug dict default
3 participants