tpalfy commented on code in PR #5888: URL: https://github.com/apache/nifi/pull/5888#discussion_r856318685
########## nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/MicrosCastEvaluator.java: ########## @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.attribute.expression.language.evaluation.cast; + +import org.apache.nifi.attribute.expression.language.EvaluationContext; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.InstantQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.expression.AttributeExpression; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +public class MicrosCastEvaluator extends NumberEvaluator { Review Comment: Instead of this and `MicroCastEvaluator` might suggest a single class, something like this: ```java public class EpochTimeEvaluator extends NumberEvaluator { private final ChronoUnit chronoUnit; private final Evaluator<?> subjectEvaluator; public TimeEvaluator(final ChronoUnit chronoUnit, final Evaluator<?> subjectEvaluator) { this.chronoUnit = chronoUnit; this.subjectEvaluator = subjectEvaluator; } @Override public QueryResult<Number> evaluate(EvaluationContext evaluationContext) { final QueryResult<?> result = subjectEvaluator.evaluate(evaluationContext); if (result.getValue() == null) { return new NumberQueryResult(null); } if (result.getResultType() == AttributeExpression.ResultType.INSTANT) { final Instant instant = ((InstantQueryResult) result).getValue(); long time = chronoUnit.between(Instant.EPOCH, instant); return new NumberQueryResult(time); } return new NumberQueryResult(null); } @Override public Evaluator<?> getSubjectEvaluator() { return subjectEvaluator; } } ``` ########## nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/compile/ExpressionCompiler.java: ########## @@ -559,6 +572,18 @@ private DateEvaluator toDateEvaluator(final Evaluator<?> evaluator, final String return new DateCastEvaluator(evaluator); } + private InstantEvaluator toInstantEvaluator(final Evaluator<?> evaluator) { + return toInstantEvaluator(evaluator, null); + } + + private InstantEvaluator toInstantEvaluator(final Evaluator<?> evaluator, final String location) { Review Comment: It seems the `location` is not used. The other similar methods are trying to assess what the upstream result type (basically the subject type) can be used here and throws an exception if an invalid one is encountered. The location is used in the thrown exception. Is there a reason this is different? ########## nifi-docs/src/main/asciidoc/expression-language-guide.adoc: ########## @@ -2233,8 +2233,40 @@ In order to run the correct method, the parameter types must be correct. The Exp |============================================================================ +[[formatInstant]] +[.function] +=== formatInstant + +*Description*: [.description]#Formats a number or string as a date/time according to the format specified by the argument. +The argument must be a String that is a valid Java DateTimeFormatter format. The Subject is expected to be a Number that +represents the number of milliseconds since Midnight GMT on January 1, 1970 or a String with one of the following +DateTimeFormatter syntaxes:# + +- _ISO_OFFSET_ such as '2011-12-03T10:15:30+01:00' +- _ISO_INSTANT_ such as '2011-12-03T10:15:30Z' +- _RFC_1123_ such as 'Thu, 01 Dec 1994 16:00:00 GMT' +*Subject Type*: [.subject]#Number# Review Comment: ```suggestion *Subject Type*: [.subject]#Number or Date/Time# ``` ########## nifi-docs/src/main/asciidoc/expression-language-guide.adoc: ########## @@ -2233,8 +2233,40 @@ In order to run the correct method, the parameter types must be correct. The Exp |============================================================================ +[[formatInstant]] +[.function] +=== formatInstant + +*Description*: [.description]#Formats a number or string as a date/time according to the format specified by the argument. +The argument must be a String that is a valid Java DateTimeFormatter format. The Subject is expected to be a Number that +represents the number of milliseconds since Midnight GMT on January 1, 1970 or a String with one of the following +DateTimeFormatter syntaxes:# Review Comment: A bit confusing, would rather just go with ```suggestion *Description*: [.description]#Formats a number or a date/time according to the format and time zone specified by the arguments. The first argument must be a String that is a valid Java DateTimeFormatter format. The second argument must be a String that is a valid time zone. The Subject is expected to be a Number that represents the number of milliseconds since Midnight GMT on January 1, 1970 or a String with one of the following DateTimeFormatter syntaxes:# ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
