From d76b5dc3f0a80b082af94d942a2f2c9c22e8819a Mon Sep 17 00:00:00 2001 From: David Estes Date: Wed, 2 Oct 2024 21:28:18 -0400 Subject: [PATCH] adding try, flatten methods --- build.gradle | 2 +- .../plugins/hcl4j/HCLBaseFunctions.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2974db3..20b4e83 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ ext { group = 'com.bertramlabs.plugins' -version = '0.9.2' +version = '0.9.3' ext.isReleaseVersion = !version.endsWith("SNAPSHOT") sourceCompatibility = "1.8" diff --git a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java index 9aaf142..a6ec35b 100644 --- a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java +++ b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java @@ -204,6 +204,15 @@ static void registerBaseFunctions(HCLParser parser) { return java.util.UUID.randomUUID().toString(); }); + parser.registerFunction("try", (arguments) -> { + for(Object argument : arguments) { + if(argument != null) { + return argument; + } + } + return null; + }); + registerNumericFunctions(parser); registerCollectionFunctions(parser); registerDateFunctions(parser); @@ -417,6 +426,33 @@ static void registerCollectionFunctions(HCLParser parser) { return null; }); + parser.registerFunction("flatten", (arguments) -> { + if(arguments.size() > 0) { + if(arguments.get(0) instanceof List) { + List elements = ((List)(arguments.get(0))); + ArrayList flattened = new ArrayList<>(); + flattenList(flattened,elements); + + return flattened; + } else { + return null; + } + } + return null; + }); + + + } + + private static void flattenList(ArrayList flattened, List elements) { + for(Object element : elements) { + if(element instanceof List) { + List subElements = ((List)(element)); + flattenList(flattened,subElements); + } else { + flattened.add(element); + } + } }