From 454b7cab9794d9fe0efbda29a7526cd4c9be5bff Mon Sep 17 00:00:00 2001 From: David Estes Date: Tue, 14 Mar 2023 20:08:50 -0400 Subject: [PATCH] more functions and not operator fixes --- README.md | 1 + build.gradle | 2 +- .../plugins/hcl4j/HCLBaseFunctions.java | 41 +++++++++++++++++++ .../bertramlabs/plugins/hcl4j/HCLParser.java | 8 ++++ .../bertramlabs/plugins/hcl4j/HCLLexer.jflex | 2 +- .../plugins/hcl4j/HCLParserSpec.groovy | 3 ++ 6 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18d29ce..95e63c6 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ dependencies { ## What's New +* **0.6.8** Fixed ! (not) operator and added base64encode, base64decode, textdecodebase64, textencodebase64 * **0.6.7** Fixed operators called on variables without spaces * **0.6.6** Fixed nested strings inside an interpolation syntax as well as some add conditional checks * **0.6.5** Fixed issue with same line comments sometimes causing a parser error as well as Maps with new line terminators causing issues diff --git a/build.gradle b/build.gradle index 621f9aa..881dceb 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ ext { group = 'com.bertramlabs.plugins' -version = '0.6.7' +version = '0.6.8' 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 8892ba0..be83738 100644 --- a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java +++ b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeType; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; @@ -396,6 +398,45 @@ static void registerCastingFunctions(HCLParser parser) { } return null; }); + + parser.registerFunction("base64encode", (arguments) -> { + if(arguments.size() > 0) { + String content = (String)(arguments.get(0)); + return Base64.getEncoder().encodeToString(content.getBytes(StandardCharsets.UTF_8)); + } + return null; + }); + + parser.registerFunction("base64decode", (arguments) -> { + if(arguments.size() > 0) { + String content = (String)(arguments.get(0)); + byte[] decodedBytes = Base64.getDecoder().decode(content); + return new String(decodedBytes,StandardCharsets.UTF_8); + } + return null; + }); + + + parser.registerFunction("textencodebase64", (arguments) -> { + if(arguments.size() > 1) { + String content = (String)(arguments.get(0)); + String encoding = (String)(arguments.get(1)); + return Base64.getEncoder().encodeToString(content.getBytes(Charset.forName(encoding))); + } + return null; + }); + + parser.registerFunction("textdecodebase64", (arguments) -> { + if(arguments.size() > 1) { + String content = (String)(arguments.get(0)); + String encoding = (String)(arguments.get(1)); + + byte[] decodedBytes = Base64.getDecoder().decode(content); + return new String(decodedBytes,Charset.forName(encoding)); + } + return null; + }); } + } diff --git a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java index 3c349a7..fa983fb 100644 --- a/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java +++ b/src/main/java/com/bertramlabs/plugins/hcl4j/HCLParser.java @@ -570,6 +570,14 @@ private Object processSymbolPass2(Object val, Map mapPosition) th results = false; } break; + case "!": + Object notResult = processSymbolPass2(symbol.getChildren().get(++x),nestedMap); + if(notResult == null || notResult.equals(false)) { + results = true; + } else { + results = false; + } + break; case "+": if(results instanceof String) { Object rightResult = processSymbolPass2(symbol.getChildren().get(++x),nestedMap); diff --git a/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex b/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex index 3cd710c..423a9d9 100644 --- a/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex +++ b/src/main/jflex/com/bertramlabs/plugins/hcl4j/HCLLexer.jflex @@ -376,7 +376,7 @@ MLineStart = [\<] [\<] [\ ]? {HCLAttributeName} ExprTerm = {True} | {False} | {Null} | {DigitValue} | {Identifier} | {FunctionCall} Conditional = \|\| | \&\& | \> [=]* | \< [=]* | == | \!= | [?] | \: | if -Operation = [\+\-\/\*\%] +Operation = [\+\-\/\*\%\!] Expression = {ExprTerm} | {Operation} | {Conditional} /*For Expression*/ ForObjExpr = \{ [\n\t\f\r ]* {ForIntro} diff --git a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy index 6aea1f3..8785144 100644 --- a/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy +++ b/src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy @@ -882,6 +882,7 @@ test_var = "testvar1" local_code = "localcode1" start = 1 end = 3 + maybe = true } resource "test" "me" { @@ -894,6 +895,7 @@ test_var = "testvar1" math = local.start + local.end operations = 2 + 4 / 2 operationsOrder = 2 + (4 / 2) + perhaps = ! local.maybe } ''' HCLParser parser = new HCLParser(); @@ -910,6 +912,7 @@ test_var = "testvar1" results.resource.test.me.should_not_exist == null results.resource.test.me.operations == 3 results.resource.test.me.operationsOrder == 4 + results.resource.test.me.perhaps == false }