Product Documentation

In-place interpolations

It is now possible to replace part(s) of a string using a StyleBook expression(s). When these string expressions are evaluated by the StyleBook compiler, the part of the string that uses a StyleBook expression will be replaced by value of the expression.  To include StyleBook expressions in a string, we use the following notation:

              “…%{…}%…”

where the characters enclosed between” %{ “ and “}%” form a StyleBook expression. These expressions are known as “in-place interpolations. “

For example, the string “lb-%{$parameters.appname}%-svc” is a string expression with in-place interpolation of a StyleBook expression. The value of string expression depends on the value of the interpolation expression. Consider that $parameters.appname is assigned with “app1.” Then, the string expression evaluates to lb-app1-svc. This allows the values to be not hard-coded in string expressions, but rather be evaluated based on the user-defined values.

A practical use case of in-place interpolations is to parameterize policy expressions in StyleBooks. Consider a scenario where you want to write a policy expression that checks if the HTTP URL contains a specific word, say, “jpeg”.  

For this, you write a policy expression as follows: “HTTP.REQ.URL.CONTAINS(\“jpeg\”)”.

Now, if you want to parameterize the object in the HTTP URL, you can add a string parameter in the StyleBook, say, $parameters.url-object. The policy expression should be written based on this parameter. To do that, you use string concatenation to achieve the result. The expression would look like:

              str(“HTTP.REQ.URL.CONTAINS(\”” + $parameters.url-object + “\”)”)

If $parameter.url-object is assigned “csv”, the above expression will evaluate to “HTTP.REQ.URL.CONTAINS(\“csv\”)”. However, this expression is not easy to read. To make this parameterization easy to read and understand, you can use in-place interpolations.

The expression with in-place interpolation is now:

              str(“HTTP.REQ.URL.CONTAINS(%{quotewrap($parameters.url-object)}%)”)

In the above expression, you used an interpolation expression that adds the inner-quotes around the value of the $parameters.url-object. The result of this expression is same as the above, but, it looks more intuitive and closer to the actual result.

Allowed Types Inside Interpolations

You can use expressions that generates value of following types inside interpolations: boolean, number, tcp-port, ipaddress, and string. The generated value is automatically transformed to a string when the interpolations are replaced with the result.

String expressions can have 0, 1, or more interpolations. In a sequential interpolation, different parts of the string expression can be replaced by different StyleBook expressions. For example, the string lb-%{$parameters.appname}%-%{$parameters.vip}% returns “lb-app1-1.1.1.1”, if $parameters.appname is “app1” and $parameters.vip is “1.1.1.1”

String expressions also supports nested interpolations. That is, an interpolation expression can be nested inside another interpolation expression so that the value of one expression can become an input to the second expression.

For example, consider a string “%{lb-%{$parameters.port + 1}%}%”

The internal string, “%{$parameters.port + 1}%” returns “lb-81” if $parameters.port is 80. Here this expression is nested inside another interpolation expression.

The following table describes the different types of interpolations with examples and corresponding results. The value of the parameters used in the examples are:

  • $parameters.appname: “lb1”
  • $parameters.vip: “1.1.1.1”
  • $parameters.n1: 1
  • $parameters.n2: 3

Simple interpolations

Expression Result
lb-%{$parameters.appname}%-def lb-lb1-def
   

Automatic type conversions

Expression Result
lb-%{1}% lb-1
lb-%{$parameters.vip}% lb-1.1.1.1
lb-%{true}% lb-True
   

Sequential interpolations

Expression Result
%{$parameters.appname}%-%{str($parameters.appname)}% lb1-lb1
lb-%{1}%-%{2}% lb-1-2
   

Nested interpolations

Expression Result
%{abc-%{$parameters.n1 + 1}%}% abc-2
str(“%{abc-%{$parameters.n1}%}%-%{$parameters.n2}%”) bc-1-3
   

Interpolations with quotewrap

Expression Result
str(“%{quotewrap(abcd)}%”) "abcd|
str(“%{quotewrap(https://)}%+HTTP.REQ.HOSTNAME+HTTP.REQ.URL") ”<https://"+HTTP.REQ.HOST NAME+HTTP.REQ.URL
   

Escape Characters in Interpolations

If the characters “%{“ or “}%” are part of the string, you must provide “\” as an escape character so that the StyleBook compiler does not evaluate these as interpolation tags.

Example:

str(“%{\%{ + str($parameters.vip) + }\%}%”) returns “%{1.1.1.1}%” if $parameters.vip is 1.1.1.1

The following table describes a few more expressions and their results:

Category Expression Result
Escaping interpolations str(“%{str($parameters.n1) + }\%}%”) 1}%
  lb-%{str($parameters.n1) + }\%}% lb-1}%
  ”%{str($parameters.n1) + \”\}\%\”}%” 1}%