-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Defines the pattern for taking unique items from collection of JSONArray #957
base: master
Are you sure you want to change the base?
Conversation
512b14a
to
eadcd79
Compare
public class Distinct implements PathFunction { | ||
|
||
@Override | ||
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make sure this function works with arrays of objects as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Added tests on the array of objects and empty array.
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model); | ||
Set<Object> objectSet = new HashSet<>(); | ||
objects.forEach(objectSet::add); | ||
|
||
return new ArrayList<>(objectSet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Java Stream API can be used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
|
||
@Override | ||
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { | ||
if(ctx.configuration().jsonProvider().isArray(model)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(ctx.configuration().jsonProvider().isArray(model)){ | |
if (ctx.configuration().jsonProvider().isArray(model)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { | ||
if(ctx.configuration().jsonProvider().isArray(model)){ | ||
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model); | ||
Set<Object> objectSet = new HashSet<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HashSet may change order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
eadcd79
to
c54fba6
Compare
@@ -37,7 +37,7 @@ public Object invoke(String currentPath, PathRef parent, Object model, Evaluatio | |||
} | |||
} | |||
} | |||
throw new JsonPathException("Aggregation function attempted to calculate value using empty array"); | |||
throw new JsonPathException("Aggregation function attempted to calculate value using non array"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should go as a separate commit or even PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Reverted
|
||
@Test | ||
public void testDistinctOfObjects() throws Exception { | ||
final Object EXPECTED_VALUE = new ObjectMapper().readValue("[{\"a\":\"a-val\"}, {\"b\":\"b-val\"}]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final Object EXPECTED_VALUE = new ObjectMapper().readValue("[{\"a\":\"a-val\"}, {\"b\":\"b-val\"}]", Object.class); | |
final Object expectedArray = new ObjectMapper().readValue("[{\"a\":\"a-val\"}, {\"b\":\"b-val\"}]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
|
||
@Test | ||
public void testDistinctOfEmptyObjects() throws Exception { | ||
final Object EXPECTED_VALUE = new ObjectMapper().readValue("[]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final Object EXPECTED_VALUE = new ObjectMapper().readValue("[]", Object.class); | |
final Object expectedArray = new ObjectMapper().readValue("[]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
c54fba6
to
6f197e0
Compare
README.md
Outdated
| first() | Provides the first item of an array | Depends on the array | | ||
| last() | Provides the last item of an array | Depends on the array | | ||
| index(X) | Provides the item of an array of index: X, if the X is negative, take from backwards | Depends on the array | | ||
| distinct() | Provides the unique items of an array | Depends on the array | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| distinct() | Provides the unique items of an array | Depends on the array | | |
| distinct() | Provides an array containing only unique items from the input array | List<E> | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
protected static final String TEXT_AND_NUMBER_SERIES = "{\"text\" : [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ], \"numbers\" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}"; | ||
protected static final String OBJECT_SERIES = "{\"empty\": [], \"objects\" : [ {\"a\": \"a-val\"}, {\"b\": \"b-val\"}, { \"a\": \"a-val\"} ]}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about array of arrays? does it work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
I added a test for it.
|
||
@Test | ||
public void testDistinctOfObjects() throws Exception { | ||
final Object expectedValue = new ObjectMapper().readValue("[{\"a\":\"a-val\"}, {\"b\":\"b-val\"}]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't conf.mappingProvider()
be used instead of explicit creation of ObjectMapper
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
|
||
@Test | ||
public void testDistinctOfEmptyObjects() throws Exception { | ||
final Object expectedValue = new ObjectMapper().readValue("[]", Object.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't conf.mappingProvider()
be used instead of explicit creation of ObjectMapper
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
6f197e0
to
3017d47
Compare
3017d47
to
e707a46
Compare
No description provided.