From 0933bd8662a1261db04339e337fd4b93a20015bd Mon Sep 17 00:00:00 2001 From: David Estes Date: Wed, 27 Nov 2024 15:45:50 -0500 Subject: [PATCH] adding regexall function support --- .../plugins/hcl4j/HCLBaseFunctions.java | 17 +++++++++ .../plugins/hcl4j/HCLParserSpec.groovy | 37 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java index a6ec35b..8e8a245 100644 --- a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java +++ b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java @@ -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. @@ -81,6 +83,21 @@ static void registerBaseFunctions(HCLParser parser) { return null; }); + parser.registerFunction("regexall", (arguments) -> { + if(arguments.size() > 1 && arguments.get(0) != null && arguments.get(1) != null) { + String str = 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 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) { diff --git a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy index 8ba34ba..4b7a0e2 100644 --- a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy +++ b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy @@ -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: @@ -863,6 +885,21 @@ 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") + +''' + HCLParser parser = new HCLParser(); + when: + def results = parser.parse(hcl) + then: + results.test1 == ["abcd","efgh"] + results.test2 == [] + } + void "it should handle conditional expressions"() { given: