-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Added | string #5585
base: develop
Are you sure you want to change the base?
Added | string #5585
Conversation
@anna-geller I've submitted a pull request adding a StringFilter to the Kestra project that converts numbers to strings. Please review and suggest improvements if any . |
@MilosPaunovic can you please review this PR !! |
Nope, Java is not my strong suit. @Skraye will take a look when he gets the chance. |
package io.kestra.core.runners.pebble.filters; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a test file given that it is in the test folder?
The contents look to be duplicate as present in the above file.
@@ -56,7 +56,8 @@ public List<BinaryOperator> getBinaryOperators() { | |||
List<BinaryOperator> operators = new ArrayList<>(); | |||
|
|||
operators.add(new BinaryOperatorImpl("??", 120, NullCoalescingExpression::new, NORMAL, Associativity.LEFT)); | |||
operators.add(new BinaryOperatorImpl("???", 120, UndefinedCoalescingExpression::new, NORMAL, Associativity.LEFT)); | |||
operators.add( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noise, remove it
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class StringFilter implements Filter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As Shruti said, this is a duplicate of the class, please remove this file and implement a proper test
@Override | ||
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, | ||
int lineNumber) throws PebbleException { | ||
if (input == null) { | ||
return null; | ||
} | ||
|
||
// Handle numeric types (Integer, Long, Float, Double, BigDecimal, BigInteger) | ||
if (input instanceof Number) { | ||
return input.toString(); // Convert numeric input to string | ||
} | ||
|
||
// Handle string inputs that represent numbers (parse them as numbers and return | ||
// their string form) | ||
if (input instanceof String) { | ||
try { | ||
return new BigDecimal((String) input).toString(); // Convert the string representation of a number to | ||
// string form | ||
} catch (NumberFormatException e) { | ||
throw new PebbleException(null, | ||
"'string' filter expects a valid number. Received an invalid numeric string: " + input, | ||
lineNumber, self.getName()); | ||
} | ||
} | ||
|
||
// If the input is not a number or numeric string, throw an exception | ||
throw new PebbleException(null, | ||
"'string' filter expects a Number (INT, FLOAT, DOUBLE, etc.) or numeric String. Received: " | ||
+ input.getClass().getName(), | ||
lineNumber, self.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is good, but you focused too much on numbers :
- When the filter is applied on a String, it should not fail, it just does nothing
- The filter string could be applied to anything, as every Java object has the method
.toString()
Line 35 you're taking a String input, you cast int to a number to then cast it to String, it does not make sense
What changes are being made and why?
Added a new StringFilter to the Kestra project, which converts numeric inputs and string representations of numbers into their string form. This feature enhances template flexibility by allowing users to handle numeric values as strings easily.
This PR closes #5528 .
How the changes have been QAed?
Example flow code demonstrating the use of the string filter in a Pebble template
template: |
This example shows how the new StringFilter behaves with different input types.