Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/gradle/org.slf4j-slf4j-api-2.0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom authored Nov 28, 2024
2 parents 9668871 + 567e048 commit a8c02ef
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ Using gradle one can include the hcl4j dependency like so:

```groovy
dependencies {
compile "com.bertramlabs.plugins:hcl4j:0.9.1"
compile "com.bertramlabs.plugins:hcl4j:0.9.4"
}
```

## What's New

* **0.9.6** Function result accessor with . or []
* **0.9.5** Adding support for regexall method
* **0.9.4** Fixing concurrent modification exception on data lookup
* **0.9.3** Added missing methods for array manipulation such as flatten and try
* **0.9.1** Remove annoying Nested Map Debug Log
* **0.9.0** HCL Tuple for loop nested improvements. String escapes fixed. anytrue and alltrue methods added.
* **0.8.0** HCL For Loop Tuples now evaluated.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ext {


group = 'com.bertramlabs.plugins'
version = '0.9.3'
version = '0.9.6'

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")
sourceCompatibility = "1.8"
Expand All @@ -53,7 +53,7 @@ dependencies {
api "commons-logging:commons-logging:1.3.3"
api 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
api 'com.fasterxml.jackson.core:jackson-annotations:2.17.1'
api 'com.fasterxml.jackson.core:jackson-core:2.17.1'
api 'com.fasterxml.jackson.core:jackson-core:2.18.1'
api "org.slf4j:slf4j-api:2.0.16"
api 'org.apache.httpcomponents:httpclient:4.5.14'
api 'org.apache.httpcomponents:httpcore:4.4.16'
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Provides implementations of the common Terraform Base HCL Functions that are commonly used.
Expand Down Expand Up @@ -81,6 +83,21 @@ static void registerBaseFunctions(HCLParser parser) {
return null;
});

parser.registerFunction("regexall", (arguments) -> {
if(arguments.size() > 1 && arguments.get(0) != null ) {
String str = arguments.get(1) != null ? arguments.get(1).toString() : "";
Pattern regex = Pattern.compile(arguments.get(0).toString());
Matcher matcher = regex.matcher(str);
//convert match list to array of results
ArrayList<String> results = new ArrayList<>();
while(matcher.find()) {
results.add(matcher.group());
}
return results;
}
return null;
});

parser.registerFunction("contains", (arguments) -> {
if(arguments.size() == 2) {
if(arguments.get(0) instanceof Collection) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public Map<String, Object> parse(Reader reader, Boolean ignoreParserExceptions)
intermediateHclParserExceptionHandling(() -> processSymbolPass2(result.get("data"), result), ignoreParserExceptions);

}
for (String key : result.keySet()) {
for (String key : result.keySet().toArray(new String[0])) {
intermediateHclParserExceptionHandling(() -> {
if (!Objects.equals(key, "variable") && !Objects.equals(key, "data") && !Objects.equals(key, "locals")) {
processSymbolPass2(result.get(key), result);
Expand Down Expand Up @@ -1110,9 +1110,15 @@ protected Object processEvaluation(EvalSymbol evalSymbol, Object context,Map<Str
if(evalSymbol instanceof VariableTree) {
for(int x = 0;x<evalSymbol.getChildren().size();x++) {
Symbol child = evalSymbol.getChildren().get(x);
// System.out.println("Processing Child: " + child.getName() + " child.class" + child.getClass().getName() + " line: " + child.getLine() + "col: " + child.getColumn());
if(child instanceof Variable && evalSymbol.getChildren().size() > x+1 && evalSymbol.getChildren().get(x+1) instanceof Function) {
//This may not be necessary anymore but is there to catch a function traversal potential error
return evaluateFunctionCall(child.getName(),(Function)(evalSymbol.getChildren().get(x+1)));
return evaluateFunctionCall(child.getName(), (Function) (evalSymbol.getChildren().get(x + 1)));
} else if(child instanceof Function) {
context = evaluateFunctionCall(child.getName(), (Function) child,stackVars);
if(context == null) { //function call failed
return null;
}
}else if(child instanceof Variable) {
if(context == null && x == 0) {
switch(child.getName()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ AssignmentExpression = [^]

<EVALUATEDEXPRESSION> {

{FunctionCall} {startFunction(); }
{FunctionCall} {startVariableTree(); }
{IdentifierTree} { startVariableTree(); }
\} { yypushback(yylength()); exitAttribute(true); }
{LineTerminator} { if(currentBlock instanceof HCLAttribute) {exitAttribute(true); }}
Expand All @@ -559,7 +559,7 @@ AssignmentExpression = [^]
}

<VARIABLETREE> {
{FunctionCall} {startFunction(); }
{FunctionCall} { startFunction(); }
{Identifier} { currentBlock.appendChild(new Variable(yytext(),yyline,yycolumn,yychar)); }
{WholeNumber} { startArray(); currentBlock.appendChild(new HCLValue("number",yytext(),yyline,yycolumn,yychar));exitAttribute(true); }
\[ { startArray(); }
Expand Down Expand Up @@ -594,7 +594,7 @@ AssignmentExpression = [^]
}

<FORTUPLEEXPRESSION> {
: { ((ComputedTuple)currentBlock).appendChild(new ForSource(yyline,yycolumn,yychar)) ; yybegin(HCLATTRIBUTEVALUE); }
: { ((ComputedTuple)currentBlock).appendChild(new ForSource(yyline,yycolumn,yychar)) ; yybegin(HCLATTRIBUTEVALUE); System.out.println("Starting Tuple"); }
[\]] { exitAttribute(true); }
{IfPrimitive} { startForConditional(); }
{Conditional} { yybegin(HCLATTRIBUTEVALUE); yypushback(yylength()); }
Expand Down
55 changes: 55 additions & 0 deletions src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ test = {"list": [1,2,3,[4,5,6]], name: "David Estes", info: { firstName: "David"
results.variable.test.list.size() == 4
}

// void "should handle references of resource by array" () {
//
// given:
// def hcl = '''
//resource "something" "here" {
// count = 1
// name = "bobby"
// }
//
//resource "something" "there" {
// count = 1
// name = resource.something.here[0].name
//}
//'''
// HCLParser parser = new HCLParser();
// when:
// def results = parser.parse(hcl)
//
// then:
// results.resource.something.there.name == "bobby"
//}


void "should handle multiple locals{} blocks for context"() {
given:
Expand Down Expand Up @@ -863,6 +885,23 @@ test3 = substr("hello world", 6, 10)
results.test3 == "world"
}

void "regexall should function correctly"() {
given:
def hcl = '''
test1 = regexall("[a-z]+", "1234abcd5678efgh9")
test2 = regexall("[a-z]+", "123456789")
test3 = regexall("[a-z]+", null)
'''
HCLParser parser = new HCLParser();
when:
def results = parser.parse(hcl)
then:
results.test1 == ["abcd","efgh"]
results.test2 == []
results.test3 == []
}


void "it should handle conditional expressions"() {
given:
Expand Down Expand Up @@ -1153,6 +1192,22 @@ swagger_path_method_parameters = [for my_value in [0,1,2,3]: my_value + 1 ]
results.locals["swagger_path_method_parameters"] == [1,2,3,4]
}

void "it should handle nested arrays in for tuples"() {
given:
def hcl = '''
locals {
my_array = [{name: "abc1"},{name: "abc2"},{name: "abc3"}]
my_result = [for res in local.my_array : regexall("[0-9]+$", res.name)[0]]
}
'''
HCLParser parser = new HCLParser();
when:
def results = parser.parse(hcl)
println results
then:
results.locals["my_result"] == ["1","2","3"]
}


void "it should handle conditional for tuples"() {
given:
Expand Down

0 comments on commit a8c02ef

Please sign in to comment.