Skip to content

Commit

Permalink
Merge branch 'random-cleanups'
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Aug 23, 2019
2 parents 32fceba + 98b958a commit aa11f33
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface Expression<T> {
/**
* Evaluate this expression against a JSON-like structure and return the result.
*/
public T search(T input);
T search(T input);
}
2 changes: 1 addition & 1 deletion jmespath-core/src/main/java/io/burt/jmespath/JmesPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface JmesPath<T> {
*
* @throws io.burt.jmespath.parser.ParseException when the string is not a valid JMESPath expression
*/
public Expression<T> compile(String expression);
Expression<T> compile(String expression);
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public <T> Iterator<ArgumentError> check(Adapter<T> runtime, Iterator<FunctionAr
if (arguments.hasNext()) {
Iterator<ArgumentError> error = subConstraint.check(runtime, arguments, false);
if (error.hasNext()) {
return error;
return error;
}
} else {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected <T> T callFunction(Adapter<T> runtime, List<FunctionArgument<T>> argum
if (haystackType == JmesPathType.ARRAY) {
return runtime.createBoolean(runtime.toList(haystack).contains(needle));
} else {
return runtime.createBoolean(runtime.toString(haystack).indexOf(runtime.toString(needle)) >= 0);
return runtime.createBoolean(runtime.toString(haystack).contains(runtime.toString(needle)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ private class SortingAggregator<V> extends TransformByFunction.Aggregator<V> {
public SortingAggregator(Adapter<V> runtime, int elementCount, V initialElement, V initialValue) {
super(runtime);
this.pairs = new ArrayList<>(elementCount);
this.pairs.add(new Pair<V>(initialElement, initialValue));
this.pairs.add(new Pair<>(initialElement, initialValue));
}

protected void aggregate(V candidate, V candidateValue) {
pairs.add(new Pair<V>(candidate, candidateValue));
pairs.add(new Pair<>(candidate, candidateValue));
}

protected V result() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.burt.jmespath.function;

import java.util.Collections;
import java.util.List;
import java.util.Arrays;

Expand All @@ -17,7 +18,7 @@ protected <T> T callFunction(Adapter<T> runtime, List<FunctionArgument<T>> argum
if (runtime.typeOf(subject) == JmesPathType.ARRAY) {
return subject;
} else {
return runtime.createArray(Arrays.asList(subject));
return runtime.createArray(Collections.singletonList(subject));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.Collection;
import java.util.Iterator;
import java.util.Collections;

import io.burt.jmespath.BaseRuntime;
Expand Down Expand Up @@ -103,7 +102,7 @@ public boolean isTruthy(Object value) {
case NUMBER:
return true;
case BOOLEAN:
return ((Boolean)value).booleanValue();
return (Boolean) value;
case ARRAY:
return !((Collection<Object>) value).isEmpty();
case OBJECT:
Expand Down Expand Up @@ -156,7 +155,7 @@ public Object createString(String str) {

@Override
public Object createBoolean(boolean b) {
return Boolean.valueOf(b);
return b;
}

@Override
Expand All @@ -178,7 +177,7 @@ public Object createNumber(long n) {
* Helper method to render a value as JSON.
*
* Assumes that <code>null</code>, <code>number</code> and <code>boolean</code>
* render themseves correctly with <code>toString</code>, and that
* render themselves correctly with <code>toString</code>, and that
* <code>string</code> renders itself as an unquoted string.
*/
private String unparse(Object object) {
Expand Down
40 changes: 20 additions & 20 deletions jmespath-core/src/main/java/io/burt/jmespath/node/NodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@
* A node factory is used by the expression compiler to create AST nodes.
*/
public interface NodeFactory<T> {
public Node<T> createCurrent();
Node<T> createCurrent();

public Node<T> createProperty(String name);
Node<T> createProperty(String name);

public Node<T> createIndex(int index);
Node<T> createIndex(int index);

public Node<T> createSlice(Integer start, Integer stop, Integer step);
Node<T> createSlice(Integer start, Integer stop, Integer step);

public Node<T> createProjection(Expression<T> expression);
Node<T> createProjection(Expression<T> expression);

public Node<T> createFlattenArray();
Node<T> createFlattenArray();

public Node<T> createFlattenObject();
Node<T> createFlattenObject();

public Node<T> createSelection(Expression<T> test);
Node<T> createSelection(Expression<T> test);

public Node<T> createComparison(Operator operator, Expression<T> left, Expression<T> right);
Node<T> createComparison(Operator operator, Expression<T> left, Expression<T> right);

public Node<T> createOr(Expression<T> left, Expression<T> right);
Node<T> createOr(Expression<T> left, Expression<T> right);

public Node<T> createAnd(Expression<T> left, Expression<T> right);
Node<T> createAnd(Expression<T> left, Expression<T> right);

public Node<T> createFunctionCall(String functionName, List<? extends Expression<T>> args);
Node<T> createFunctionCall(String functionName, List<? extends Expression<T>> args);

public Node<T> createFunctionCall(Function function, List<? extends Expression<T>> args);
Node<T> createFunctionCall(Function function, List<? extends Expression<T>> args);

public Node<T> createExpressionReference(Expression<T> expression);
Node<T> createExpressionReference(Expression<T> expression);

public Node<T> createString(String str);
Node<T> createString(String str);

public Node<T> createNegate(Expression<T> negated);
Node<T> createNegate(Expression<T> negated);

public Node<T> createCreateObject(List<CreateObjectNode.Entry<T>> entries);
Node<T> createCreateObject(List<CreateObjectNode.Entry<T>> entries);

public Node<T> createCreateArray(List<? extends Expression<T>> entries);
Node<T> createCreateArray(List<? extends Expression<T>> entries);

public Node<T> createJsonLiteral(String json);
Node<T> createJsonLiteral(String json);

public Node<T> createSequence(List<Node<T>> nodes);
Node<T> createSequence(List<Node<T>> nodes);
}
39 changes: 27 additions & 12 deletions jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.burt.jmespath.JmesPathType;
import io.burt.jmespath.RuntimeConfiguration;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -34,24 +35,38 @@ public JsonElement parseString(String str) {
return parser.parse(str);
}

private static class JsonArrayListWrapper extends AbstractList<JsonElement> {
private final JsonArray array;

JsonArrayListWrapper(JsonArray array) {
this.array = array;
}

@Override
public JsonElement get(int index) {
return array.get(index);
}

@Override
public int size() {
return array.size();
}
}

@Override
public List<JsonElement> toList(JsonElement value) {
List<JsonElement> list;
if (value.isJsonObject()) {
list = new ArrayList<>(value.getAsJsonObject().size());
for(Map.Entry<String, JsonElement> entry : value.getAsJsonObject().entrySet()) {
list.add(entry.getValue());
}
return list;
}
if (value.isJsonArray()) {
list = new ArrayList<>(value.getAsJsonArray().size());
for (JsonElement e : value.getAsJsonArray()) {
list.add(e);
return new JsonArrayListWrapper(value.getAsJsonArray());
} else if (value.isJsonObject()) {
JsonObject object = value.getAsJsonObject();
List<JsonElement> list = new ArrayList<>(object.size());
for(Map.Entry<String, JsonElement> entry : object.entrySet()) {
list.add(entry.getValue());
}
return list;
} else {
return Collections.emptyList();
}
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.burt.jmespath.jackson;

import java.util.AbstractList;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -43,9 +44,29 @@ public JsonNode parseString(String string) {
}
}

private static class ArrayNodeListWrapper extends AbstractList<JsonNode> {
private final ArrayNode array;

ArrayNodeListWrapper(ArrayNode array) {
this.array = array;
}

@Override
public JsonNode get(int index) {
return array.get(index);
}

@Override
public int size() {
return array.size();
}
}

@Override
public List<JsonNode> toList(JsonNode value) {
if (value.isArray() || value.isObject()) {
if (value.isArray()) {
return new ArrayNodeListWrapper((ArrayNode) value);
} else if (value.isObject()) {
List<JsonNode> elements = new ArrayList<>(value.size());
for (JsonNode element : value) {
elements.add(element);
Expand Down

0 comments on commit aa11f33

Please sign in to comment.