Skip to content

Commit 871996a

Browse files
committed
Add JMESPath parsing logic to the code generator, preparing for evaluation of JMESPath expressions for waiter acceptors.
The added JmesPathParser.parse method can convert a valid JMESPath expression into an Expression, which is a syntax tree representation of an JMESPath expression. The next step will be to interpret this JMESPath expression to generate an acceptor for waiters. It's likely that this interpreter will only implement a subset of the JMESPath features.
1 parent 5dd15f7 commit 871996a

31 files changed

+3392
-0
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/internal/Jackson.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.databind.SerializationFeature;
2222
import com.fasterxml.jackson.jr.ob.JSON;
2323
import com.fasterxml.jackson.jr.stree.JacksonJrsTreeCodec;
24+
import com.fasterxml.jackson.jr.stree.JrsValue;
2425
import java.io.File;
2526
import java.io.IOException;
2627
import java.io.Writer;
@@ -49,6 +50,10 @@ public final class Jackson {
4950
private Jackson() {
5051
}
5152

53+
public static JrsValue readJrsValue(String input) throws IOException {
54+
return MAPPER.beanFrom(JrsValue.class, input);
55+
}
56+
5257
public static <T> T load(Class<T> clazz, File file) throws IOException {
5358
return MAPPER.beanFrom(clazz, file);
5459
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
/**
19+
* An and expression will evaluate to either the left expression or the right expression. If the expression on the left hand side
20+
* is a truth-like value, then the value on the right hand side is returned. Otherwise the result of the expression on the left
21+
* hand side is returned.
22+
*
23+
* Examples:
24+
* <ul>
25+
* <li>True && False</li>
26+
* <li>Number && EmptyList</li>
27+
* <li>a == `1` && b == `2`</li>
28+
* </ul>
29+
*
30+
* https://jmespath.org/specification.html#and-expressions
31+
*/
32+
public class AndExpression {
33+
private final Expression leftExpression;
34+
private final Expression rightExpression;
35+
36+
public AndExpression(Expression leftExpression, Expression rightExpression) {
37+
this.leftExpression = leftExpression;
38+
this.rightExpression = rightExpression;
39+
}
40+
41+
public Expression leftExpression() {
42+
return leftExpression;
43+
}
44+
45+
public Expression rightExpression() {
46+
return rightExpression;
47+
}
48+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
import software.amazon.awssdk.utils.Validate;
19+
20+
/**
21+
* A bracket specifier within an {@link IndexExpression}. Either:
22+
* <ul>
23+
* <li>With content, as in [1], [*] or [1:2:3]: {@link BracketSpecifierWithContents}</li>
24+
* <li>Without content, as in []: {@link BracketSpecifierWithContents}</li>
25+
* <li>With question-mark content, as in [?foo]: {@link BracketSpecifierWithQuestionMark}</li>
26+
* </ul>
27+
*/
28+
public class BracketSpecifier {
29+
private BracketSpecifierWithContents bracketSpecifierWithContents;
30+
private BracketSpecifierWithoutContents bracketSpecifierWithoutContents;
31+
private BracketSpecifierWithQuestionMark bracketSpecifierWithQuestionMark;
32+
33+
public static BracketSpecifier withContents(BracketSpecifierWithContents bracketSpecifierWithContents) {
34+
Validate.notNull(bracketSpecifierWithContents, "bracketSpecifierWithContents");
35+
BracketSpecifier result = new BracketSpecifier();
36+
result.bracketSpecifierWithContents = bracketSpecifierWithContents;
37+
return result;
38+
}
39+
40+
public static BracketSpecifier withNumberContents(int numberContents) {
41+
return withContents(BracketSpecifierWithContents.number(numberContents));
42+
}
43+
44+
public static BracketSpecifier withSliceExpressionContents(SliceExpression sliceExpression) {
45+
return withContents(BracketSpecifierWithContents.sliceExpression(sliceExpression));
46+
}
47+
48+
public static BracketSpecifier withWildcardExpressionContents(WildcardExpression wildcardExpression) {
49+
return withContents(BracketSpecifierWithContents.wildcardExpression(wildcardExpression));
50+
}
51+
52+
public static BracketSpecifier withoutContents() {
53+
BracketSpecifier result = new BracketSpecifier();
54+
result.bracketSpecifierWithoutContents = new BracketSpecifierWithoutContents();
55+
return result;
56+
}
57+
58+
public static BracketSpecifier withQuestionMark(BracketSpecifierWithQuestionMark bracketSpecifierWithQuestionMark) {
59+
Validate.notNull(bracketSpecifierWithQuestionMark, "bracketSpecifierWithQuestionMark");
60+
BracketSpecifier result = new BracketSpecifier();
61+
result.bracketSpecifierWithQuestionMark = bracketSpecifierWithQuestionMark;
62+
return result;
63+
}
64+
65+
public boolean isBracketSpecifierWithContents() {
66+
return bracketSpecifierWithContents != null;
67+
}
68+
69+
public boolean isBracketSpecifierWithoutContents() {
70+
return bracketSpecifierWithoutContents != null;
71+
}
72+
73+
public boolean isBracketSpecifierWithQuestionMark() {
74+
return bracketSpecifierWithQuestionMark != null;
75+
}
76+
77+
public BracketSpecifierWithContents asBracketSpecifierWithContents() {
78+
Validate.validState(isBracketSpecifierWithContents(), "Not a BracketSpecifierWithContents");
79+
return bracketSpecifierWithContents;
80+
}
81+
82+
public BracketSpecifierWithoutContents asBracketSpecifierWithoutContents() {
83+
Validate.validState(isBracketSpecifierWithoutContents(), "Not a BracketSpecifierWithoutContents");
84+
return bracketSpecifierWithoutContents;
85+
}
86+
87+
public BracketSpecifierWithQuestionMark asBracketSpecifierWithQuestionMark() {
88+
Validate.validState(isBracketSpecifierWithQuestionMark(), "Not a BracketSpecifierWithQuestionMark");
89+
return bracketSpecifierWithQuestionMark;
90+
}
91+
92+
public void visit(Visitor visitor) {
93+
if (isBracketSpecifierWithContents()) {
94+
visitor.visitBracketSpecifierWithContents(asBracketSpecifierWithContents());
95+
} else if (isBracketSpecifierWithoutContents()) {
96+
visitor.visitBracketSpecifierWithoutContents(asBracketSpecifierWithoutContents());
97+
} else if (isBracketSpecifierWithQuestionMark()) {
98+
visitor.visitBracketSpecifierWithQuestionMark(asBracketSpecifierWithQuestionMark());
99+
} else {
100+
throw new IllegalStateException();
101+
}
102+
}
103+
104+
public interface Visitor {
105+
default void visitBracketSpecifierWithContents(BracketSpecifierWithContents bracketSpecifierWithContents) {
106+
}
107+
108+
default void visitBracketSpecifierWithoutContents(BracketSpecifierWithoutContents bracketSpecifierWithContents) {
109+
}
110+
111+
default void visitBracketSpecifierWithQuestionMark(BracketSpecifierWithQuestionMark bracketSpecifierWithContents) {
112+
}
113+
}
114+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
import software.amazon.awssdk.utils.Validate;
19+
20+
/**
21+
* A {@link BracketSpecifier} with some kind of content. Either:
22+
* <ul>
23+
* <li>A number, as in [1]</li>
24+
* <li>A star expression, as in [*]</li>
25+
* <li>A slice expression, as in [1:2:3]</li>
26+
* </ul>
27+
*/
28+
public class BracketSpecifierWithContents {
29+
private Integer number;
30+
private WildcardExpression wildcardExpression;
31+
private SliceExpression sliceExpression;
32+
33+
private BracketSpecifierWithContents() {
34+
}
35+
36+
public static BracketSpecifierWithContents number(Integer number) {
37+
Validate.notNull(number, "number");
38+
BracketSpecifierWithContents result = new BracketSpecifierWithContents();
39+
result.number = number;
40+
return result;
41+
}
42+
43+
public static BracketSpecifierWithContents wildcardExpression(WildcardExpression wildcardExpression) {
44+
Validate.notNull(wildcardExpression, "wildcardExpression");
45+
BracketSpecifierWithContents result = new BracketSpecifierWithContents();
46+
result.wildcardExpression = wildcardExpression;
47+
return result;
48+
}
49+
50+
public static BracketSpecifierWithContents sliceExpression(SliceExpression sliceExpression) {
51+
Validate.notNull(sliceExpression, "sliceExpression");
52+
BracketSpecifierWithContents result = new BracketSpecifierWithContents();
53+
result.sliceExpression = sliceExpression;
54+
return result;
55+
}
56+
57+
public boolean isNumber() {
58+
return number != null;
59+
}
60+
61+
public boolean isWildcardExpression() {
62+
return wildcardExpression != null;
63+
}
64+
65+
public boolean isSliceExpression() {
66+
return sliceExpression != null;
67+
}
68+
69+
public int asNumber() {
70+
Validate.validState(isNumber(), "Not a Number");
71+
return number;
72+
}
73+
74+
public WildcardExpression asWildcardExpression() {
75+
Validate.validState(isWildcardExpression(), "Not a WildcardExpression");
76+
return wildcardExpression;
77+
}
78+
79+
public SliceExpression asSliceExpression() {
80+
Validate.validState(isSliceExpression(), "Not a SliceExpression");
81+
return sliceExpression;
82+
}
83+
84+
public void visit(Visitor visitor) {
85+
if (isNumber()) {
86+
visitor.visitNumber(asNumber());
87+
} else if (isWildcardExpression()) {
88+
visitor.visitWildcardExpression(asWildcardExpression());
89+
} else if (isSliceExpression()) {
90+
visitor.visitSliceExpression(asSliceExpression());
91+
} else {
92+
throw new IllegalStateException();
93+
}
94+
}
95+
96+
public interface Visitor {
97+
default void visitNumber(int asNumber) {
98+
}
99+
100+
default void visitWildcardExpression(WildcardExpression asWildcardExpression) {
101+
}
102+
103+
default void visitSliceExpression(SliceExpression asSliceExpression) {
104+
}
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
/**
19+
* A {@link BracketSpecifier} with a question-mark expression, as in [?Foo].
20+
*/
21+
public class BracketSpecifierWithQuestionMark {
22+
private final Expression expression;
23+
24+
public BracketSpecifierWithQuestionMark(Expression expression) {
25+
this.expression = expression;
26+
}
27+
28+
public Expression expression() {
29+
return expression;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
/**
19+
* A {@link BracketSpecifier} without content, i.e. [].
20+
*/
21+
public class BracketSpecifierWithoutContents {
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.jmespath.component;
17+
18+
/**
19+
* A comparator within a {@link ComparatorExpression}.
20+
*/
21+
public enum Comparator {
22+
LESS_THAN_OR_EQUAL("<="),
23+
EQUAL("=="),
24+
GREATER_THAN_OR_EQUAL(">="),
25+
NOT_EQUAL("!="),
26+
LESS_THAN("<"),
27+
GREATER_THAN(">");
28+
29+
private final String tokenSymbol;
30+
31+
Comparator(String tokenSymbol) {
32+
this.tokenSymbol = tokenSymbol;
33+
}
34+
35+
public String tokenSymbol() {
36+
return tokenSymbol;
37+
}
38+
}

0 commit comments

Comments
 (0)