Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add can and regex functions #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(var.foo.boop)
}
validation {
condition = can(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"]
}
}