Skip to content

Commit

Permalink
Add can and regex functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-y committed Jan 8, 2025
1 parent 1882d95 commit 9e2f42b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
28 changes: 25 additions & 3 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ static void registerBaseFunctions(HCLParser parser) {
return null;
});

parser.registerFunction("regex", (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
if(matcher.find()) {
return matcher.group();
}
}
return null;
});

parser.registerFunction("contains", (arguments) -> {
if(arguments.size() == 2) {
if(arguments.get(0) instanceof Collection) {
Expand Down Expand Up @@ -136,7 +149,6 @@ static void registerBaseFunctions(HCLParser parser) {
} catch(ClassCastException ex) {
return null;
}

} else {
return null; //Invalid Function Spec
}
Expand Down Expand Up @@ -230,6 +242,16 @@ static void registerBaseFunctions(HCLParser parser) {
return null;
});

parser.registerFunction("can", (arguments) -> {
for(Object argument : arguments) {
if(argument != null) {
return true;
}
}
return false;
});


registerNumericFunctions(parser);
registerCollectionFunctions(parser);
registerDateFunctions(parser);
Expand Down Expand Up @@ -530,7 +552,7 @@ static void registerCastingFunctions(HCLParser parser) {
byte[] decodedBytes = Base64.getDecoder().decode(content);
return new String(decodedBytes,StandardCharsets.UTF_8);
}

}
return null;
});
Expand All @@ -556,7 +578,7 @@ static void registerCastingFunctions(HCLParser parser) {
byte[] decodedBytes = Base64.getDecoder().decode(content);
return new String(decodedBytes,Charset.forName(encoding));
}

}
return null;
});
Expand Down
36 changes: 32 additions & 4 deletions src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ default = ["subnet-72b9162b"]
def results = parser.parse(hcl)
then:
results.resource.aws_instance.test.tags.Date != null

}


Expand Down Expand Up @@ -1777,7 +1777,35 @@ variable "server_id" {

}

void "should parse can and regex"() {
given:
def hcl = '''
variable "server_id" {
type = string
description = "Server ID"
default = "abcabcabc"
validation {
condition = can(regex("^(dev|prod)$", var.server_id))
}
validation {
condition = can(regex("ab", var.server_id))
}
validation {
condition = regex("ab", var.server_id)
}
validation {
condition = regexall("ab", var.server_id)
}
}'''
HCLParser parser = new HCLParser();
when:
def results = parser.parse(hcl)
then:
results.containsKey('variable')
results.variable.server_id.validation.size() == 4
results.variable.server_id.validation[0].condition == false
results.variable.server_id.validation[1].condition == true
results.variable.server_id.validation[2].condition == "ab"
results.variable.server_id.validation[3].condition == ["ab","ab","ab"]
}
}



0 comments on commit 9e2f42b

Please sign in to comment.