velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1842835 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java test/resources/templates/compare/range.cmp test/resources/templates/range.vm
Date Thu, 04 Oct 2018 17:40:35 GMT
Author: cbrisson
Date: Thu Oct  4 17:40:34 2018
New Revision: 1842835

URL: http://svn.apache.org/viewvc?rev=1842835&view=rev
Log:
[VELOCITY-895] Implicit conversion to numbers in integer range, plus testcase

Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java
    velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/compare/range.cmp
    velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/range.vm

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java?rev=1842835&r1=1842834&r2=1842835&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIntegerRange.java
Thu Oct  4 17:40:34 2018
@@ -23,6 +23,7 @@ import org.apache.velocity.context.Inter
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.TemplateInitException;
 import org.apache.velocity.runtime.parser.Parser;
+import org.apache.velocity.util.DuckType;
 import org.apache.velocity.util.StringUtils;
 
 import java.util.ArrayList;
@@ -95,14 +96,29 @@ public class ASTIntegerRange extends Sim
         }
 
         /*
-         *  if not a Number, not much we can do either
+         *  if not a Number, try to convert
+         */
+
+        try
+        {
+            left = DuckType.asNumber(left);
+        }
+        catch (NumberFormatException nfe) {}
+
+        try
+        {
+            right = DuckType.asNumber(right);
+        }
+        catch (NumberFormatException nfe) {}
+
+        /*
+         *  if still not a Number, nothing we can do
          */
 
         if ( !( left instanceof Number )  || !( right instanceof Number ))
         {
             log.error((!(left instanceof Number) ? "Left" : "Right")
-                           + " side of range operator is not a valid type. "
-                           + "Currently only integers (1,2,3...) and the Number type are
supported. "
+                           + " side of range operator is not convertible to a Number. "
                            + StringUtils.formatFileString(this));
             return null;
         }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/compare/range.cmp
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/compare/range.cmp?rev=1842835&r1=1842834&r2=1842835&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/compare/range.cmp
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/compare/range.cmp
Thu Oct  4 17:40:34 2018
@@ -23,6 +23,9 @@
 refs $a=1 $b=5 [$a..$b]
  1  2  3  4  5 
 -----
+refs $a=1 $b="5" [$a.."$b"]
+ 1  2  3  4  5 
+-----
 [$a.. 7]
  1  2  3  4  5  6  7 
 -----

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/range.vm
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/range.vm?rev=1842835&r1=1842834&r2=1842835&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/range.vm (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/templates/range.vm Thu Oct
 4 17:40:34 2018
@@ -44,6 +44,12 @@ refs \$a=$a \$b=$b [\$a..\$b]
 #foreach($i in [$a..$b]) $i #end
 
 -----
+#set($a = 1)
+#set($b = "5")
+refs \$a=$a \$b="$b" [\$a.."\$b"]
+#foreach($i in [$a..$b]) $i #end
+
+-----
 [\$a.. 7]
 #foreach($i in [$a.. 7]) $i #end
 



Mime
View raw message