Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wnm3 committed Nov 8, 2023
1 parent f826799 commit 3c9604e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
1 change: 1 addition & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding//target/generated-sources/antlr4=UTF-8
encoding/<project>=UTF-8
6 changes: 3 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.lambda.genericSignature=do not generate
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.shareCommonFinallyBlocks=disabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
Expand Down Expand Up @@ -169,7 +169,7 @@ org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.storeAnnotations=disabled
org.eclipse.jdt.core.compiler.taskCaseSensitive=enabled
org.eclipse.jdt.core.compiler.taskPriorities=NORMAL,HIGH,NORMAL
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>11</source>
<target>11</target>
<additionalClasspathElements/>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.api.jsonata4java.expressions.EvaluateRuntimeException;
import com.api.jsonata4java.expressions.ExpressionsVisitor;
import com.api.jsonata4java.expressions.RegularExpression;
import com.api.jsonata4java.expressions.generated.MappingExpressionParser.ExprContext;
import com.api.jsonata4java.expressions.generated.MappingExpressionParser.Function_callContext;
import com.api.jsonata4java.expressions.utils.Constants;
import com.api.jsonata4java.expressions.utils.FunctionUtils;
Expand Down Expand Up @@ -64,19 +65,26 @@
*/
public class SplitFunction extends FunctionBase {

public static String ERR_BAD_CONTEXT = String.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_SPLIT);
public static String ERR_ARG1BADTYPE = String.format(Constants.ERR_MSG_ARG1_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG2BADTYPE = String.format(Constants.ERR_MSG_ARG2_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG3BADTYPE = String.format(Constants.ERR_MSG_ARG3_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG4BADTYPE = String.format(Constants.ERR_MSG_ARG4_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_BAD_CONTEXT = String
.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_SPLIT);
public static String ERR_ARG1BADTYPE = String
.format(Constants.ERR_MSG_ARG1_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG2BADTYPE = String
.format(Constants.ERR_MSG_ARG2_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG3BADTYPE = String
.format(Constants.ERR_MSG_ARG3_BAD_TYPE, Constants.FUNCTION_SPLIT);
public static String ERR_ARG4BADTYPE = String
.format(Constants.ERR_MSG_ARG4_BAD_TYPE, Constants.FUNCTION_SPLIT);

public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContext ctx) {
public JsonNode invoke(ExpressionsVisitor expressionVisitor,
Function_callContext ctx) {
// Create the variable to return
JsonNode result = null;

// Retrieve the number of arguments
JsonNode argString = JsonNodeFactory.instance.nullNode();
boolean useContext = FunctionUtils.useContextVariable(this, ctx, getSignature());
boolean useContext = FunctionUtils.useContextVariable(this, ctx,
getSignature());
int argCount = getArgumentCount(ctx);
if (useContext) {
argString = FunctionUtils.getContextVariable(expressionVisitor);
Expand All @@ -90,20 +98,34 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
// Make sure that we have the right number of arguments
if (argCount >= 1 && argCount <= 3) {
if (!useContext) {
argString = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, 0);
argString = FunctionUtils
.getValuesListExpression(expressionVisitor, ctx, 0);
}
if (argCount < 2) {
if (argString == null || argString.isTextual()) {
throw new EvaluateRuntimeException(ERR_BAD_CONTEXT);
}
throw new EvaluateRuntimeException(ERR_ARG1BADTYPE);
}
// check to see if we got a textual reference
final JsonNode argSeparator = FunctionUtils.getValuesListExpression(expressionVisitor, ctx,
useContext ? 0 : 1);
JsonNode argSeparator = null;
ExprContext exprCtx = ctx.exprValues().exprList()
.expr(useContext ? 0 : 1);
if ("$".equals(exprCtx.getChild(0).toString())) {
argSeparator = FunctionUtils.getValuesListExpression(
expressionVisitor, ctx, useContext ? 1 : 2);
if (useContext) {
// since we used context the $ reference is superfluous
argCount--;
}
} else {
// check to see if we got a textual reference
argSeparator = FunctionUtils.getValuesListExpression(
expressionVisitor, ctx, useContext ? 0 : 1);
}
int limit = -1; // assume unlimited
// Make sure that the separator is not null
if (argSeparator == null || !(argSeparator.isTextual() || argSeparator instanceof POJONode)) {
if (argSeparator == null || !(argSeparator.isTextual()
|| argSeparator instanceof POJONode)) {
if (argString == null) {
if (useContext) {
throw new EvaluateRuntimeException(ERR_BAD_CONTEXT);
Expand All @@ -123,13 +145,15 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
final RegularExpression regex = argSeparator instanceof POJONode
? (RegularExpression) ((POJONode) argSeparator).getPojo()
: null;
final String separator = regex != null ? regex.toString() : argSeparator.textValue();
final String separator = regex != null ? regex.toString()
: argSeparator.textValue();

if (argCount == 3) {
final JsonNode argLimit = FunctionUtils.getValuesListExpression(expressionVisitor, ctx,
useContext ? 1 : 2);
final JsonNode argLimit = FunctionUtils.getValuesListExpression(
expressionVisitor, ctx, useContext ? 1 : 2);

// Check to see if we have an optional limit argument we check it
// Check to see if we have an optional limit argument we check
// it
if (argLimit != null) {
if (argLimit.isNumber() && argLimit.asInt() >= 0) {
limit = argLimit.asInt();
Expand All @@ -140,16 +164,17 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
}

/*
* Split the string using a simple String::split... but do not specify the
* limit. This is because the String::split function in Java behaves differently
* to the Javascript String::split function... and the JSONata $split function
* is defined to behave like the Javascript version.
* Split the string using a simple String::split... but do not
* specify the limit. This is because the String::split function in
* Java behaves differently to the Javascript String::split
* function... and the JSONata $split function is defined to behave
* like the Javascript version.
*
* If the limit is zero, we do not add any strings in the output array to the
* ArrayNode object.
* If the limit is zero, we do not add any strings in the output
* array to the ArrayNode object.
*
* If the limit is non-zero, we add up to the specified number of strings to the
* ArrayNode object.
* If the limit is non-zero, we add up to the specified number of
* strings to the ArrayNode object.
*/
result = JsonNodeFactory.instance.arrayNode();
if (!str.isEmpty()) {
Expand All @@ -168,7 +193,8 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
} // FOR
}
} else {
throw new EvaluateRuntimeException(argCount == 0 ? ERR_BAD_CONTEXT : ERR_ARG4BADTYPE);
throw new EvaluateRuntimeException(
argCount == 0 ? ERR_BAD_CONTEXT : ERR_ARG4BADTYPE);
}

return result;
Expand All @@ -186,7 +212,8 @@ public int getMinArgs() {

@Override
public String getSignature() {
// accepts a string (or context variable), a string or function, an optional
// accepts a string (or context variable), a string or function, an
// optional
// number, returns an array of strings
return "<s-(sf)n?:a<s>>";
}
Expand Down
14 changes: 14 additions & 0 deletions testfiles/dataobject_l.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"predictions": [
{
"fields": [
"Response_message_field"
],
"values": [
[
"#Inventory #Signature"
]
]
}
]
}

0 comments on commit 3c9604e

Please sign in to comment.