Skip to content

Commit

Permalink
adding try, flatten methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Oct 3, 2024
1 parent 448e2cd commit d76b5dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 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.9.2'
version = '0.9.3'

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")
sourceCompatibility = "1.8"
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Object> elements = ((List<Object>)(arguments.get(0)));
ArrayList<Object> flattened = new ArrayList<>();
flattenList(flattened,elements);

return flattened;
} else {
return null;
}
}
return null;
});


}

private static void flattenList(ArrayList<Object> flattened, List<Object> elements) {
for(Object element : elements) {
if(element instanceof List) {
List<Object> subElements = ((List<Object>)(element));
flattenList(flattened,subElements);
} else {
flattened.add(element);
}
}

}

Expand Down

0 comments on commit d76b5dc

Please sign in to comment.