Skip to content

Commit

Permalink
fixing hcl4j bug with \
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Feb 29, 2024
1 parent c143855 commit 630c998
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,18 @@ protected Object processValue(HCLValue value) throws HCLParserException {
}

protected Object evaluateFunctionCall(String functionName,Function functionSymbol) throws HCLParserException {
return evaluateFunctionCall(functionName,functionSymbol,null);
}
protected Object evaluateFunctionCall(String functionName,Function functionSymbol, Map<String,Object> localVars) throws HCLParserException {
if(functionRegistry.get(functionName) != null) {
HCLFunction functionMethod = functionRegistry.get(functionName);
ArrayList<Object> functionArguments = new ArrayList<>();
for(Symbol child : functionSymbol.getChildren()) {
Object elementResult = null;
if(child instanceof EvalSymbol) {
elementResult = processEvaluation((EvalSymbol) child,null);
elementResult = processEvaluation((EvalSymbol) child,null,localVars);
} else if(child instanceof Symbol) {
elementResult = processSymbol(child,result);
elementResult = processSymbol(child,result,localVars);
}
if(elementResult instanceof VariableTree) { //we were unable to evaluate a sub argument... abort function evaluation
return null; // function evaluation aborted. perhaps throw error for later based on if ignoreExceptions is on or not
Expand All @@ -969,7 +972,10 @@ protected Object evaluateFunctionCall(String functionName,Function functionSymbo


protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParserException {
// System.out.println("Evaluating Tuple");
return evaluateComputedTuple(thisTuple,null);
}
protected Object evaluateComputedTuple(ComputedTuple thisTuple, Map<String,Object> localVars) throws HCLParserException {

ArrayList computedResult = new ArrayList();
Variable indexVariable = null;
Variable valueVariable = null;
Expand All @@ -984,6 +990,7 @@ protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParser
log.warn("Computed Tuple loop found with too many variables at line {}",thisTuple.getLine());
return null;
}
System.out.println("Evaluating Tuple: " + valueVariable.getName());
ForConditional tupleConditional = null;
for(Symbol child : thisTuple.getChildren()) {
if(child instanceof ForConditional) {
Expand All @@ -1009,12 +1016,16 @@ protected Object evaluateComputedTuple(ComputedTuple thisTuple) throws HCLParser

}
if(sourceExpression.getChildren().size() > 0 && iteratorExpression.getChildren().size() > 0) {
elementResult = processSymbol(sourceExpression,result);
elementResult = processSymbol(sourceExpression,result,localVars);
if(elementResult instanceof List) {
List elementResultList = (List)elementResult;
System.out.println("Iterating over list");
for(int counter=0;counter < elementResultList.size();counter++){

Map<String,Object> stackVars = new LinkedHashMap<>();
if(localVars != null) {
stackVars = new LinkedHashMap<>(localVars);
}
if(indexVariable != null) {
stackVars.put(indexVariable.getName(),counter);
}
Expand Down Expand Up @@ -1210,14 +1221,14 @@ protected Object processEvaluation(EvalSymbol evalSymbol, Object context,Map<Str
else if(evalSymbol instanceof Function) {
Function thisFunction = (Function)evalSymbol;
if(thisFunction.getName() != null && thisFunction.getName().length() > 0) {
return evaluateFunctionCall(thisFunction.getName(),thisFunction);
return evaluateFunctionCall(thisFunction.getName(),thisFunction,stackVars);
} else {
return null;
}
} else if(evalSymbol instanceof ComputedTuple) {
//time to actually implemented a tuple loop
ComputedTuple thisTuple = (ComputedTuple) evalSymbol;
return evaluateComputedTuple(thisTuple);
return evaluateComputedTuple(thisTuple,stackVars);
}
else if(evalSymbol instanceof Variable) {
System.out.println("Evaluating Variable: " + evalSymbol.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ AssignmentExpression = [^]
\\t { string.append('\t'); }
\\n { string.append('\n'); }
\\r { string.append('\r'); }
\\ { string.append('\\'); }
\\\\ { string.append('\\'); }
}
<STRINGDOUBLE> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,14 +1212,15 @@ swagger_path_method_parameters = [for my_value in local.swagger_path_methods_spl
given:
def hcl = '''
locals {
swagger_path_methods_split = [["a","b"],["c","d"]]
swagger_path_methods_split = [[0,"a"],[0,"b"]]
json_data = {
paths = {a: {b: [1,2]}, c:{d: [3,4]]}}
paths = [{a: "test"},{b:"test2"}]
}
swagger_path_method_parameters = [for my_value in local.swagger_path_methods_split:
[for val2 in local.json_data.paths[my_value[0]][my_value[1]] : val2]
]
}
'''
HCLParser parser = new HCLParser();
when:
Expand Down

0 comments on commit 630c998

Please sign in to comment.