Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#295 Proposal for extension of function fromMillis #297

Merged
merged 5 commits into from
Jan 31, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* (c) Copyright 2018, 2019 IBM Corporation
* 1 New Orchard Road,
* Armonk, New York, 10504-1722
* United States
* +1 914 499 1900
* support: Nathaniel Mills [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.api.jsonata4java.expressions.functions;

import com.api.jsonata4java.expressions.EvaluateRuntimeException;
import com.api.jsonata4java.expressions.ExpressionsVisitor;
import com.api.jsonata4java.expressions.generated.MappingExpressionParser.Function_callContext;
import com.api.jsonata4java.expressions.utils.Constants;
import com.api.jsonata4java.expressions.utils.DateTimeUtils;
import com.api.jsonata4java.expressions.utils.FunctionUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.TextNode;

/**
* From http://docs.jsonata.org/string-functions.html:
*
* $fromMillis(number)
*
* Convert a number representing milliseconds since the Unix Epoch (1 January,
* 1970 UTC) to a timestamp string in the ISO 8601 format.
*
* Examples
*
* $fromMillis(1510067557121)=="2017-11-07T15:12:37.121Z"
*
*/
public class FromMillisZonedFunction extends FunctionBase {

public static String ERR_BAD_CONTEXT = String.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_FROM_MILLIS);
public static String ERR_ARG1BADTYPE = String.format(Constants.ERR_MSG_ARG1_BAD_TYPE,
Constants.FUNCTION_FROM_MILLIS);
public static String ERR_ARG2BADTYPE = String.format(Constants.ERR_MSG_ARG2_BAD_TYPE,
Constants.FUNCTION_FROM_MILLIS);

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

// Retrieve the number of arguments
JsonNode argNumber = JsonNodeFactory.instance.nullNode();
boolean useContext = FunctionUtils.useContextVariable(this, ctx, getSignature());
int argCount = getArgumentCount(ctx);
if (useContext) {
argNumber = FunctionUtils.getContextVariable(expressionVisitor);
if (argNumber != null && argNumber.isNull() == false) {
argCount++;
} else {
useContext = false;
}
}

// Make sure that we have the right number of arguments
if (argCount >= 1 && argCount <= 3) {
if (!useContext) {
argNumber = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, 0);
}
JsonNode picture = JsonNodeFactory.instance.nullNode();
if (argCount >= 2) {
picture = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, useContext ? 0 : 1);
}
JsonNode timezone = JsonNodeFactory.instance.nullNode();
if (argCount == 3) {
timezone = FunctionUtils.getValuesListExpression(expressionVisitor, ctx, useContext ? 1 : 2);
}
if (argNumber == null) {
return null;
}
if (argNumber.isNumber()) {
final Long millis = argNumber.asLong();
String pictureStr = null;
if (picture != null && picture.isNull() == false) {
pictureStr = picture.asText();
}
String timezoneStr = null;
if (timezone != null && timezone.isNull() == false) {
timezoneStr = timezone.asText();
}
result = new TextNode(DateTimeUtils.formatDateTimeFromZoneId(millis, pictureStr, timezoneStr));
} else {
throw new EvaluateRuntimeException(ERR_ARG1BADTYPE);
}
} else {
throw new EvaluateRuntimeException(ERR_BAD_CONTEXT);
}

return result;
}

@Override
public int getMaxArgs() {
return 3;
}

@Override
public int getMinArgs() {
return 0; // account for context variable
}

@Override
public String getSignature() {
// accepts a number (or context variable), an optional string, another optional
// string, returns a number
return "<n-s?s?:s>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.api.jsonata4java.expressions.functions.FormatBaseFunction;
import com.api.jsonata4java.expressions.functions.FormatNumberFunction;
import com.api.jsonata4java.expressions.functions.FromMillisFunction;
import com.api.jsonata4java.expressions.functions.FromMillisZonedFunction;
import com.api.jsonata4java.expressions.functions.FunctionBase;
import com.api.jsonata4java.expressions.functions.JoinFunction;
import com.api.jsonata4java.expressions.functions.KeysFunction;
Expand Down Expand Up @@ -111,6 +112,7 @@ public class Constants implements Serializable {
public static final String FUNCTION_REPLACE = "$replace";
public static final String FUNCTION_NOW = "$now";
public static final String FUNCTION_FROM_MILLIS = "$fromMillis";
public static final String FUNCTION_FROM_MILLIS_ZONED = "$fromMillisZoned";
public static final String FUNCTION_FORMAT_NUMBER = "$formatNumber";
public static final String FUNCTION_FORMAT_BASE = "$formatBase";
public static final String FUNCTION_BASE64_ENCODE = "$base64encode";
Expand Down Expand Up @@ -193,6 +195,7 @@ public class Constants implements Serializable {
FUNCTIONS.put(FUNCTION_REPLACE, new ReplaceFunction());
FUNCTIONS.put(FUNCTION_NOW, new NowFunction());
FUNCTIONS.put(FUNCTION_FROM_MILLIS, new FromMillisFunction());
FUNCTIONS.put(FUNCTION_FROM_MILLIS_ZONED, new FromMillisZonedFunction());
FUNCTIONS.put(FUNCTION_FORMAT_NUMBER, new FormatNumberFunction());
FUNCTIONS.put(FUNCTION_FORMAT_BASE, new FormatBaseFunction());
FUNCTIONS.put(FUNCTION_BASE64_ENCODE, new Base64EncodeFunction());
Expand Down
Loading
Loading