Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@
* 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
* abs - returns the absolute value of first argument, rest is ignored
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ protected Optional<Object> applyList( final List<Object> argList ) {
}
}

public static final class decode extends Function.ListFunction {
@Override
protected Optional<Object> applyList(final List<Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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",
Expand Down