Skip to content

Commit

Permalink
changes to support more jmespath expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbera87 committed Oct 30, 2024
1 parent 69c07e5 commit a940f72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public Pair<String, Shape> visitProjection(ProjectionExpression expression) {
elem.left,
right.right);
}
).orElseThrow(() -> new SmithyBuildException("Unsupported JMESPath expression"));
).orElseThrow(() ->
new SmithyBuildException("Unsupported JMESPath expression")
);
}

@Override
Expand Down Expand Up @@ -101,9 +103,10 @@ public Pair<String, Shape> visitField(FieldExpression expression) {
String varName = expression.getName() + "Elem";
// if a new scope started, declare variable accessed
if (context.isStartOfNewScope() ||
context.getVarName().isEmpty()) {
context.getVarName().isEmpty() ) {
context.addVariableInScope(varName);
}

// chain accessors
context.getCppCode()
.append(MessageFormat.format(".Get{0}()", CppViewHelper.convertToUpperCamel(expression.getName())));
Expand All @@ -112,6 +115,10 @@ public Pair<String, Shape> visitField(FieldExpression expression) {
// if leaf element, push to result
if (member.getShape().isString()) {
context.addInScopeVariableToResult(Optional.empty());

//this is where we can refer to the original scope variable again
context.cleanupVariablesCurrentScope();

}
}
return Pair.of(
Expand Down Expand Up @@ -146,7 +153,11 @@ public Pair<String, Shape> visitCurrentNode(CurrentExpression expression) {

@Override
public Pair<String, Shape> visitFlatten(FlattenExpression expression) {
throw new SmithyBuildException("Unsupported JMESPath expression");
if (expression.getExpression() instanceof ProjectionExpression) {
return visitProjection((ProjectionExpression) expression.getExpression());
}
// If it's not a ProjectionExpression, proceed with handling FlattenExpression
return expression.accept(this);
}

@Override
Expand All @@ -161,9 +172,13 @@ public Pair<String, Shape> visitLiteral(LiteralExpression expression) {

@Override
public Pair<String, Shape> visitMultiSelectList(MultiSelectListExpression expression) {
throw new SmithyBuildException("Unsupported JMESPath expression");
expression.getExpressions().forEach(expr -> {
expr.accept(this);
});
return Pair.of(
"",
this.input);
}

@Override
public Pair<String, Shape> visitMultiSelectHash(MultiSelectHashExpression expression) {
throw new SmithyBuildException("Unsupported JMESPath expression");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.amazonaws.util.awsclientgenerator.transform;
import lombok.Data;
import java.text.MessageFormat;
import java.util.Stack;
import java.util.Optional;
import java.util.Stack;

import lombok.Data;
import software.amazon.smithy.utils.Pair;

@Data
Expand Down Expand Up @@ -39,7 +40,22 @@ public void addVariableInScope(String var)
{
this.getCppCode().append(MessageFormat.format("{0}auto& {1} = (*this)", this.getIndentationPrefix(),var));
}
this.varName.push(Pair.of(var, false));
this.varName.push(Pair.of(var, false));
}

public void cleanupVariablesCurrentScope()
{
while(!this.getVarName().isEmpty())
{
if(!this.getVarName().peek().right)
{
this.getVarName().pop();
}
else
{
break;
}
}
}

public void rangeBasedForLoop(String varName)
Expand Down Expand Up @@ -79,10 +95,7 @@ public void closeVariableScope()
}

public boolean isStartOfNewScope(){
return (
this.getCppCode().length() > 2 &&
this.getCppCode().substring(this.getCppCode().length() - 2).equals("{\n")
);
return (!this.getVarName().isEmpty()) && this.getVarName().peek().right;
}

public void addInScopeVariableToResult(Optional<String> accessorSuffix)
Expand Down

0 comments on commit a940f72

Please sign in to comment.