[master] 7c3ab25 Allow duration types in boolean contexts

Federico G. Schwindt fgsch at lodoss.net
Fri Sep 16 12:43:06 CEST 2016


commit 7c3ab2582274d3ec735b55814620fd136dbb014e
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Fri Sep 16 11:05:15 2016 +0100

    Allow duration types in boolean contexts
    
    Polish test while here.

diff --git a/bin/varnishtest/tests/v00016.vtc b/bin/varnishtest/tests/v00016.vtc
index 009f17d..7dc818b 100644
--- a/bin/varnishtest/tests/v00016.vtc
+++ b/bin/varnishtest/tests/v00016.vtc
@@ -78,11 +78,6 @@ varnish v1 -errvcl {Unknown time unit 'k'.  Legal are 'ms', 's', 'm', 'h', 'd',
 	sub vcl_backend_response { set beresp.ttl = 1. k; }
 }
 
-varnish v1 -errvcl {Expression has type DURATION, expected BOOL} {
-	backend b { .host = "127.0.0.1"; }
-	sub vcl_backend_response { if (beresp.ttl *= 2) { } }
-}
-
 varnish v1 -errvcl {Operator > not possible on BACKEND} {
 	backend a { .host = "127.0.0.1"; }
 	backend b { .host = "127.0.0.1"; }
diff --git a/bin/varnishtest/tests/v00020.vtc b/bin/varnishtest/tests/v00020.vtc
index 3fd7e69..67388a1 100644
--- a/bin/varnishtest/tests/v00020.vtc
+++ b/bin/varnishtest/tests/v00020.vtc
@@ -58,14 +58,6 @@ varnish v1 -errvcl {Unknown token '-' when looking for DURATION} {
 	}
 }
 
-varnish v1 -errvcl {'&&' must be followed by BOOL, found DURATION.} {
-	sub vcl_recv {
-		if (req.ttl < 3s && req.ttl) {
-			set req.http.foo = vcl_recv;
-		}
-	}
-}
-
 varnish v1 -errvcl {Operator * not possible on type STRING.} {
 	sub vcl_recv {
 		set req.http.foo = "bla" * "foo";
@@ -78,13 +70,6 @@ varnish v1 -errvcl {DURATION + INT not possible.} {
 	}
 }
 
-varnish v1 -errvcl {'!' must be followed by BOOL, found DURATION.} {
-	sub vcl_backend_response {
-		if (! req.ttl) {
-		}
-	}
-}
-
 varnish v1 -errvcl {BOOL + BOOL not possible.} {
 	sub vcl_backend_response {
 		if (beresp.do_gzip + beresp.do_gunzip) {
@@ -124,16 +109,11 @@ varnish v1 -vcl {
 		set req.http.foo = req.http.foo + "bar" !~ "bar";
 
 		set req.ttl = 1s;
-	}
-}
 
+		if (req.ttl) { }
+		if (!req.ttl) { }
 
-varnish v1 -vcl {
-	import std;
-	backend b { .host = "127.0.0.1"; }
-	sub vcl_recv {
-		if (std.integer("1", 1)) {
-		}
+		if (1) { }
 	}
 }
 
@@ -251,22 +231,6 @@ varnish v1 -errvcl {DURATION + STRING not possible.} {
 	}
 }
 
-varnish v1 -errvcl {'||' must be followed by BOOL, found DURATION.} {
-	backend b { .host = "127.0.0.1"; }
-	sub vcl_backend_response {
-		if (req.url || beresp.ttl) {
-		}
-	}
-}
-
-varnish v1 -errvcl {'&&' must be followed by BOOL, found DURATION.} {
-	backend b { .host = "127.0.0.1"; }
-	sub vcl_backend_response {
-		if (req.url && beresp.ttl) {
-		}
-	}
-}
-
 varnish v1 -vcl {
 	backend b { .host = "127.0.0.1"; }
 	sub vcl_miss {
diff --git a/doc/sphinx/reference/vcl.rst b/doc/sphinx/reference/vcl.rst
index ca77e8d..4a67afd 100644
--- a/doc/sphinx/reference/vcl.rst
+++ b/doc/sphinx/reference/vcl.rst
@@ -93,7 +93,8 @@ their value.
 
 String types will evaluate to *false* if they are empty; backend types
 will evalute to *false* if they don't have a backend assigned; integer
-types will evaluate to *false* if their value is zero.
+types will evaluate to *false* if their value is zero; duration types
+will evaluate to *false* if their value is equal or less than zero.
 
 Time
 ~~~~
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 7d594cf..b1ed848 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -1200,12 +1200,12 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, vcc_type_t fmt)
 	default:
 		break;
 	}
-	if (fmt == BOOL &&
-	    ((*e)->fmt == STRING || (*e)->fmt == BACKEND ||
-	     (*e)->fmt == INT)) {
-		*e = vcc_expr_edit(BOOL, "(\v1 != 0)", *e, NULL);
+	if (fmt != BOOL)
 		return;
-	}
+	if ((*e)->fmt == STRING || (*e)->fmt == BACKEND || (*e)->fmt == INT)
+		*e = vcc_expr_edit(BOOL, "(\v1 != 0)", *e, NULL);
+	else if ((*e)->fmt == DURATION)
+		*e = vcc_expr_edit(BOOL, "(\v1 > 0)", *e, NULL);
 }
 
 /*--------------------------------------------------------------------



More information about the varnish-commit mailing list