One of the most powerful features of StyleBook is the use of expressions. You can use StyleBooks expressions in various scenarios to compute dynamic values. The example below shows an expression to concatenate a parameter value with a literal string.
$parameters.appname + “-mon”
This expression retrieves the parameter named appname, and concatenates it with the string “-mon”.
The following types of expressions are supported:
- Addition (+)
- Substraction (-)
- Multiplication (*)
- Division (/)
- Modulo (%)
- Adding two numbers: $parameters.a + $parameters.b
- Multiplying two numbers: $parameters.a * 10
- Finding the remainder after division of one number by another:
15 % 10 results in 5
- Concatenate two strings (+)
Concatenate two strings: str(“app-“) + $parameters.appname
Merges two lists (+)
Concatenate two lists: $parameters.external-servers + $parameters.internal-servers
If $parameters.ports-1 is [80, 81] and $parameters.port-2 is [81, 82], then $parameters.ports-1 + $parameters.ports-2 results as a list [80, 81, 81, 82]
== : Tests if two operands are equal and returns true if they are equal, else returns false.
!= : Tests if two operands are different and returns true if they are different, else returns false.
> : Returns true if the first operand is greater than the second operand, else returns false.
>= : Returns true if the first operand is greater than or equal to the second operand, else returns false.
< : Returns true if the first operand is lesser than the second operand, else returns false.
<= : Returns true if the first operand is lesser than or equal to the second operand, else returns false.
- Use of Equality operator: $parameters.name = = “abcd”
- Use of Inequality operator: $parameters.name != “default”
- Examples for other relational operators
- 10 > 9
- 10 >= 10
- 0 < 9
- 10 <= 9
- 10 == 10
- 10 != 1
Logical (Boolean) Expressions
and: The logical ‘and’ operator. If both operands are true, the result is true, else it is false.
or: The logical ‘or’ operator. If one of the operands is true, the result is true, else it is false.
not: The unary operator. If the operand is true, the result is false, and vice-versa.
in: Tests whether the first argument is a substring of the second argument
in: Tests if an item is part of a list
You can type cast expressions where strings can be converted into numbers and numbers can be converted to strings. Similarly, a tcp-port can be cast to a number, and an IP address can be cast to a string.
You must use a delimiter before and after any operator. You can use the following delimiters:
Before an operator: space, tab, comma, (, ), [, ]
After an operator: space, tab, (, [
abc + def
100 % 10
10 > 9
Expression Type Validation
StyleBook engine now allows for stronger type checking during compile time, that is, the expressions used while writing the StyleBook are validated during the import of StyleBook itself rather than while creating the configuration pack.
All references to parameters, substitutions, components, properties of components, outputs of components, user-defined variables (repeat-item, repeat-index, arguments to substitution functions,) and so on are all validated for their existence and types.
Example of Type Checks:
In the following example, the expected type of port property of lbvserver StyleBook is tcp-port. In Citrix Application Delivery Management (ADM) earlier releases, the StyleBook compiler computed the value as a string and the StyleBook was imported and executed. Now, type validations happen at compile-time (import-time). The compiler finds that string and tcp-port are not compatible types and therefore, the StyleBook compiler throws an error and fails import or migration of the StyleBook.
You should now declare this as a number for the compiler to successfully compile this StyleBook.
Example of Flagging Invalid Expressions:
In earlier releases, when an invalid expression was assigned to a property name, the compiler did not detect invalid expressions and allowed the StyleBooks to be imported into Citrix ADM. Now if this StyleBook is imported to Citrix ADM, the compiler will identify such invalid expressions and flag it. As a result, the StyleBook will not be imported to Citrix ADM.
In this example, the expression assigned to name property in lb-sg-binding-comp component is: $components.lbvserver-comp.properties.lbvservername. However, there is no property called lbvservername in component lbvserver-comp. In earlier Citrix ADM releases, the compiler would have allowed this expression and successfully imported it. The actual failure would happen when a user wants to create a configuration pack using this StyleBook. However now, this kind of error is identified during import and the StyleBook is not imported to Citrix ADM. You must manually correct such errors and import the StyleBooks.
Items of a list can be accessed now by indexing them directly:
|$components.test-lbs||Refers to the first item in test-lbs component|
|$components.test-lbs.properties.p1||Refers to property p1 of the first item in test-lbs component|
|$components.lbcomps.outputs.servicegroups.properties.servicegroupname||Refers to property servicegroupname of the second item in servicegroups component, which is an output from the first item of lbcomps component|