Skip to content

Commit

Permalink
nested tuples work
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Feb 29, 2024
1 parent 630c998 commit c922c23
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Using gradle one can include the hcl4j dependency like so:

```groovy
dependencies {
compile "com.bertramlabs.plugins:hcl4j:0.8.0"
compile "com.bertramlabs.plugins:hcl4j:0.9.0"
}
```

## What's New

* **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.
* **0.7.7** HCL Periods in attribute names referenced from Unicodes ID_Continue, fixed.
* **0.7.6** SLF4j 1.7.36 upgrade to reduce CVE's
Expand Down
6 changes: 3 additions & 3 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.8.0'
version = '0.9.0'

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")
sourceCompatibility = "1.8"
Expand All @@ -51,11 +51,11 @@ dependencies {
api 'com.fasterxml.jackson.core:jackson-core:2.14.1'
api "org.slf4j:slf4j-parent:1.7.36"
api "org.slf4j:slf4j-api:1.7.36"
api 'org.apache.httpcomponents:httpclient:4.5.13'
api 'org.apache.httpcomponents:httpclient:4.5.14'
api 'org.apache.httpcomponents:httpcore:4.4.15'
api 'org.apache.httpcomponents:httpmime:4.5.13'
api group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4'
api 'commons-net:commons-net:3.7.2'
api 'commons-net:commons-net:3.9.0'
testImplementation platform("org.spockframework:spock-bom:2.3-groovy-4.0")
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-junit4" // you can remove this if your code does not rely on old JUnit 4 rules
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,62 @@ static void registerCollectionFunctions(HCLParser parser) {
return null;
});

parser.registerFunction("alltrue", (arguments) -> {
if(arguments.size() > 0) {
if(arguments.get(0) instanceof List) {
List<Object> elements = ((List<Object>)(arguments.get(0)));
if(elements.size() > 0) {
Boolean allTrue = true;
for(Object element : elements) {
if(element instanceof Boolean) {
if(!(Boolean)element) {
allTrue = false;
break;
}
} else if(element instanceof String) {
if(!element.equals("true")) {
allTrue = false;
break;
}
} else {
allTrue = false;
break;
}
}
return allTrue;
} else {
return true;
}
} else {
return true;
}
}
return true;
});


parser.registerFunction("anytrue", (arguments) -> {
if(arguments.size() > 0) {
if(arguments.get(0) instanceof List) {
List<Object> elements = ((List<Object>)(arguments.get(0)));
if(elements.size() > 0) {
for(Object element : elements) {
if(element instanceof Boolean) {
if((Boolean)element) {
return true;
}
} else if(element instanceof String) {
if(element.equals("true")) {
return true;
}
}
}
}
}
}
return false;
});

parser.registerFunction("lookup", (arguments) -> {
if(arguments.size() > 0) {
if(arguments.get(0) instanceof Map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,10 +1184,26 @@ swagger_path_method_parameters = [for i,my_value in [0,1,2,3]: "${i}:${my_value
then:
results.locals["swagger_path_method_parameters"] == ["0:1.0","1:2.0","2:3.0","3:4.0"]
}

void "it should handle nested for tuples"() {
given:
def hcl = '''
locals {
my_array = [for my_value in [1,2,3,4]:
[for sub_value in ["a","b","c","d"]: "${sub_value}${my_value}"]]
}
'''
HCLParser parser = new HCLParser();
when:
def results = parser.parse(hcl)
println results
then:
results.locals["my_array"] != null
}

void "it should handle nested complex for tuples"() {
given:
def hcl = '''
locals {
swagger_path_methods_split = [["a","b"],["c","d"]]
json_data = {
Expand Down

0 comments on commit c922c23

Please sign in to comment.