From 7971c9125774458d1c176360810bea2d8c90d3f0 Mon Sep 17 00:00:00 2001 From: Wolfgang Colsman Date: Sun, 22 Mar 2020 19:08:06 -0400 Subject: [PATCH] add-decode-function --- .../java/com/bazaarvoice/jolt/Modifier.java | 1 + .../jolt/modifier/function/Function.java | 2 ++ .../jolt/modifier/function/Strings.java | 22 +++++++++++++++++++ .../json/modifier/functions/stringsTests.json | 10 +++++++++ 4 files changed, 35 insertions(+) diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java index e334bc72..f5f90384 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java @@ -44,6 +44,7 @@ public abstract class Modifier implements SpecDriven, ContextualTransform { STOCK_FUNCTIONS.put( "toLower", new Strings.toLowerCase() ); STOCK_FUNCTIONS.put( "toUpper", new Strings.toUpperCase() ); STOCK_FUNCTIONS.put( "concat", new Strings.concat() ); + STOCK_FUNCTIONS.put( "decode", new Strings.decode() ); STOCK_FUNCTIONS.put( "join", new Strings.join() ); STOCK_FUNCTIONS.put( "split", new Strings.split() ); STOCK_FUNCTIONS.put( "substring", new Strings.substring() ); diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Function.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Function.java index f76fe58d..d4f7c192 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Function.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Function.java @@ -64,6 +64,7 @@ * toLower - returns toLower value of toString() value of first arg, rest is ignored * toUpper - returns toUpper value of toString() value of first arg, rest is ignored * concat - concatenate all given arguments' toString() values + * decode - decode all given arguments' values similar to a SQL decode * * min - returns the min of all numbers provided in the arguments, non-numbers are ignored * max - returns the max of all numbers provided in the arguments, non-numbers are ignored @@ -71,6 +72,7 @@ * toInteger - returns the intValue() value of first argument if its numeric, rest is ignored * toDouble - returns the doubleValue() value of first argument if its numeric, rest is ignored * toLong - returns the longValue() value of first argument if its numeric, rest is ignored + * toBoolean - returns the booleanValue() value of first argument if its boolean, rest is ignored * * All of these functions returns Optional.EMPTY if unsuccessful, which results in a no-op when performing * the actual write in the json doc. diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java index 3af56224..989e0f59 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java @@ -78,6 +78,28 @@ protected Optional applyList( final List argList ) { } } + public static final class decode extends Function.ListFunction { + @Override + protected Optional applyList(final List argList) { + if (argList.size() > 0) { + Object value = argList.get(0); + for (int index = 1; index < argList.size() - 1; index += 2) { + Object test = argList.get(index); + if (java.util.Objects.equals(value, test)) { + Object result = argList.get(index + 1); + return Optional.of(result); + } + } + // default + if (argList.size() % 2 == 0) { + Object result = argList.get(argList.size() - 1); + return Optional.of(result); + } + } + return Optional.of(null); + } + } + public static final class substring extends Function.ListFunction { @Override diff --git a/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json b/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json index 47aa33e5..243ef8a1 100644 --- a/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json +++ b/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json @@ -26,6 +26,11 @@ "basic": "=concat(@(2,lower.leading) , ' ' , @(2,lower.trailing))", "parens": "=concat(@(2,lower.leading) , ' (', @(2,lower.trailing), ')')" }, + "decode": { + "decodeTrue": "=decode('1' , '1' , 'true', '0', 'false')", + "decodeFalse": "=decode('0' , '1' , 'true', '0', 'false')", + "decodeDefault": "=decode('0' , '1' , 'true', 'false')" + }, "substring": { "basic1": "=substring(@(2,string), 0, 9)", "basic2": "=substring(@(2,string), 4, 9)", @@ -83,6 +88,11 @@ "basic": "the quick brown fox jumped over the lazy dog", "parens": "the quick brown fox (jumped over the lazy dog)" }, + "decode": { + "decodeTrue": "true", + "decodeFalse": "false", + "decodeDefault": "false" + }, "substring": { "basic1": "the QuIcK", "basic2": "QuIcK",