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

Add ability to serialize the JMESPath AST #1059

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,383 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.jmespath;

import java.util.Map;
import java.util.Set;
import software.amazon.smithy.jmespath.ast.AndExpression;
import software.amazon.smithy.jmespath.ast.BinaryExpression;
import software.amazon.smithy.jmespath.ast.ComparatorExpression;
import software.amazon.smithy.jmespath.ast.CurrentExpression;
import software.amazon.smithy.jmespath.ast.ExpressionTypeExpression;
import software.amazon.smithy.jmespath.ast.FieldExpression;
import software.amazon.smithy.jmespath.ast.FilterProjectionExpression;
import software.amazon.smithy.jmespath.ast.FlattenExpression;
import software.amazon.smithy.jmespath.ast.FunctionExpression;
import software.amazon.smithy.jmespath.ast.IndexExpression;
import software.amazon.smithy.jmespath.ast.LiteralExpression;
import software.amazon.smithy.jmespath.ast.MultiSelectHashExpression;
import software.amazon.smithy.jmespath.ast.MultiSelectListExpression;
import software.amazon.smithy.jmespath.ast.NotExpression;
import software.amazon.smithy.jmespath.ast.ObjectProjectionExpression;
import software.amazon.smithy.jmespath.ast.OrExpression;
import software.amazon.smithy.jmespath.ast.ProjectionExpression;
import software.amazon.smithy.jmespath.ast.SliceExpression;
import software.amazon.smithy.jmespath.ast.Subexpression;

/**
* Serializes the JMESPath expression AST back to a JMESPath expression.
*/
public final class ExpressionSerializer {

/**
* Serialize the given JMESPath expression to a string.
*
* @param expression JMESPath expression to serialize.
* @return Returns the serialized expression.
*/
public String serialize(JmespathExpression expression) {
StringBuilder builder = new StringBuilder();
Visitor visitor = new Visitor(builder);
expression.accept(visitor);
return builder.toString();
}

private static final class Visitor implements ExpressionVisitor<Void> {
private final StringBuilder builder;

Visitor(StringBuilder builder) {
this.builder = builder;
}

@Override
public Void visitComparator(ComparatorExpression expression) {
// e.g.: foo > bar
expression.getLeft().accept(this);
builder.append(' ');
builder.append(expression.getComparator());
builder.append(' ');
expression.getRight().accept(this);
return null;
}

@Override
public Void visitCurrentNode(CurrentExpression expression) {
builder.append('@');
return null;
}

@Override
public Void visitExpressionType(ExpressionTypeExpression expression) {
// e.g.: &(foo.bar)
builder.append("&(");
expression.getExpression().accept(this);
builder.append(')');
return null;
}

@Override
public Void visitFlatten(FlattenExpression expression) {
// e.g.: foo[]
expression.getExpression().accept(this);
builder.append("[]");
return null;
}

@Override
public Void visitFunction(FunctionExpression expression) {
// e.g.: some_function(@, foo)
builder.append(expression.getName());
builder.append('(');
for (int i = 0; i < expression.getArguments().size(); i++) {
expression.getArguments().get(i).accept(this);
if (i < expression.getArguments().size() - 1) {
builder.append(", ");
}
}
builder.append(')');
return null;
}

@Override
public Void visitField(FieldExpression expression) {
// Always quote fields: "foo"
builder.append('"');
builder.append(sanitizeString(expression.getName(), false));
builder.append('"');
return null;
}

private String sanitizeString(String str, boolean escapeBackticks) {
String result = str.replace("\\", "\\\\").replace("\"", "\\\"");
if (escapeBackticks) {
result = result.replace("`", "\\`");
}
return result;
}

@Override
public Void visitIndex(IndexExpression expression) {
// e.g.: [1]
builder.append('[').append(expression.getIndex()).append(']');
return null;
}

@Override
public Void visitLiteral(LiteralExpression expression) {
// e.g.: `[true]`
visitLiteral(expression, false);
return null;
}

private void visitLiteral(LiteralExpression expression, boolean nestedInsideLiteral) {
if (!nestedInsideLiteral) {
builder.append('`');
}

switch (expression.getType()) {
case NUMBER:
builder.append(expression.expectNumberValue());
break;
case NULL:
case ANY: // treat "any" like null
builder.append("null");
break;
case ARRAY:
builder.append('[');
int ai = 0;
for (Object value : expression.expectArrayValue()) {
LiteralExpression exp = LiteralExpression.from(value);
visitLiteral(exp, true);
if (ai++ < expression.expectArrayValue().size() - 1) {
builder.append(", ");
}
}
builder.append(']');
break;
case OBJECT:
builder.append('{');
int oi = 0;
Set<Map.Entry<String, Object>> objectEntries = expression.expectObjectValue().entrySet();
for (Map.Entry<String, Object> objectEntry : objectEntries) {
builder.append('"')
.append(sanitizeString(objectEntry.getKey(), true))
.append("\": ");
LiteralExpression exp = LiteralExpression.from(objectEntry.getValue());
visitLiteral(exp, true);
if (oi++ < objectEntries.size() - 1) {
builder.append(", ");
}
}
builder.append('}');
break;
case STRING:
builder.append('"')
.append(sanitizeString(expression.expectStringValue(), true))
.append('"');
break;
case BOOLEAN:
builder.append(expression.expectBooleanValue());
break;
case EXPRESSION:
// fall-through
default:
throw new JmespathException("Unable to serialize literal runtime value: " + expression);
}

if (!nestedInsideLiteral) {
builder.append('`');
}
}

@Override
public Void visitMultiSelectList(MultiSelectListExpression expression) {
// e.g.: [foo, bar[].baz]
builder.append('[');
for (int i = 0; i < expression.getExpressions().size(); i++) {
expression.getExpressions().get(i).accept(this);
if (i < expression.getExpressions().size() - 1) {
builder.append(", ");
}
}
builder.append(']');
return null;
}

@Override
public Void visitMultiSelectHash(MultiSelectHashExpression expression) {
// e.g.: {foo: `true`, bar: bar}
builder.append('{');
int i = 0;
for (Map.Entry<String, JmespathExpression> entry : expression.getExpressions().entrySet()) {
builder.append('"').append(sanitizeString(entry.getKey(), false)).append("\": ");
entry.getValue().accept(this);
if (i < expression.getExpressions().entrySet().size() - 1) {
builder.append(", ");
}
i++;
}
builder.append('}');
return null;
}

@Override
public Void visitAnd(AndExpression expression) {
// e.g.: (a && b)
builder.append('(');
expression.getLeft().accept(this);
builder.append(" && ");
expression.getRight().accept(this);
builder.append(')');
return null;
}

@Override
public Void visitOr(OrExpression expression) {
// e.g.: (a || b)
builder.append('(');
expression.getLeft().accept(this);
builder.append(" || ");
expression.getRight().accept(this);
builder.append(')');
return null;
}

@Override
public Void visitNot(NotExpression expression) {
// e.g.: !(foo.bar)
builder.append("!(");
expression.getExpression().accept(this);
builder.append(')');
return null;
}

@Override
public Void visitProjection(ProjectionExpression expression) {
if (!(expression.getLeft() instanceof CurrentExpression)) {
expression.getLeft().accept(this);
}

// Without this check, [::1].a would be reserialized into [::-1][*]."a",
// which is equivalent, but convoluted.
if (!(expression.getLeft() instanceof SliceExpression)) {
// Flatten expressions, when parsed, inherently create a project. Unroll
mtdowling marked this conversation as resolved.
Show resolved Hide resolved
// that here.
if (!(expression.getLeft() instanceof FlattenExpression)) {
builder.append("[*]");
}
}

// Avoid unnecessary addition of extracting the current node.
// We add the current node in projections if there's no RHS.
if (!(expression.getRight() instanceof CurrentExpression)) {
// Add a "." if the right hand side needs it.
if (rhsNeedsDot(expression.getRight())) {
builder.append('.');
}
expression.getRight().accept(this);
}

return null;
}

@Override
public Void visitFilterProjection(FilterProjectionExpression expression) {
// e.g.: foo[?bar == `10`].baz
// Don't emit a pointless the current node select.
if (!(expression.getLeft() instanceof CurrentExpression)) {
expression.getLeft().accept(this);
}

builder.append("[?");
expression.getComparison().accept(this);
builder.append(']');

// Avoid unnecessary addition of extracting the current node.
if (!(expression.getRight() instanceof CurrentExpression)) {
// Add a "." if the right hand side needs it.
if (rhsNeedsDot(expression.getRight())) {
builder.append('.');
}
expression.getRight().accept(this);
}

return null;
}

@Override
public Void visitObjectProjection(ObjectProjectionExpression expression) {
// Avoid the unnecessary "@.*" by just emitting "*".
if (!(expression.getLeft() instanceof CurrentExpression)) {
expression.getLeft().accept(this);
builder.append(".*");
} else {
builder.append('*');
}

// Avoid unnecessary addition of extracting the current node.
// We add the current node in projections if there's no RHS.
if (!(expression.getRight() instanceof CurrentExpression)) {
// Add a "." if the right hand side needs it.
if (rhsNeedsDot(expression.getRight())) {
builder.append('.');
}
expression.getRight().accept(this);
}

return null;
}

@Override
public Void visitSlice(SliceExpression expression) {
// e.g.: [0::1], [0:1:2], etc
builder.append('[');
expression.getStart().ifPresent(builder::append);
builder.append(':');
expression.getStop().ifPresent(builder::append);
builder.append(':');
builder.append(expression.getStep());
builder.append(']');
return null;
}

@Override
public Void visitSubexpression(Subexpression expression) {
// e.g.: "foo"."bar", "foo" | "bar"
expression.getLeft().accept(this);

if (expression.isPipe()) {
// pipe has a different precedence than dot, so this is important.
builder.append(" | ");
} else if (rhsNeedsDot(expression.getRight())) {
builder.append('.');
}

expression.getRight().accept(this);
return null;
}

// These expression need to be preceded by a "." in a binary expression.
private boolean rhsNeedsDot(JmespathExpression expression) {
return expression instanceof FieldExpression
|| expression instanceof MultiSelectHashExpression
|| expression instanceof MultiSelectListExpression
|| expression instanceof ObjectProjectionExpression
|| expression instanceof FunctionExpression
|| (expression instanceof BinaryExpression
&& rhsNeedsDot(((BinaryExpression) expression).getLeft()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private JmespathExpression led(JmespathExpression left) {
case AND: // Example: a && b
return new AndExpression(left, expression(token.type.lbp), token.line, token.column);
case PIPE: // Example: a | b
return new Subexpression(left, expression(token.type.lbp), token.line, token.column);
return new Subexpression(left, expression(token.type.lbp), token.line, token.column, true);
case FILTER: // Example: a[?foo == bar]
return parseFilter(left);
case LBRACKET:
Expand Down
Loading