StyleBook configuration

Optional properties

Sometimes, a property of a component takes its value from an expression, which can be a simple expression such as a parameter reference, or a more complex one. Setting this property value is optional in the component. You can choose to set the property value only if the expression returns an actual value, else you can choose not to set this property.

For example, consider that one of the properties you want to set is the lbmethod (load balancing algorithm) of a component whose type is ns::lbvserver. The value of the property lbmethod is taken from a parameter value provided by the user, as shown below:

components

  -

      name: lbvserver_comp

      type: ns::lbvserver

      properties:

        name: $parameters.lb-appname + "-lb"

        servicetype: $parameters.lb-service-type

        ipv46: $parameters.lb-virtual-ip

        port: 80

        lbmethod: $parameters.lb-advanced.algorithm
<!--NeedCopy-->

Now, consider that the parameter lb-advanced.algorithm is an optional parameter. And, if the user does not provide a value for this parameter because it is optional, the expression $parameters.lb-advanced.algorithm evaluates to blank value. Therefore, an invalid value is passed for the lbmethod property. In order to avoid such a situation, you can annotate the property as optional by suffixing its name with “?” as follows:

components

   -

     name: lbvserver_comp

     type: ns::lbvserver

     properties:

       name: $parameters.lb-appname + "-lb"

       servicetype: $parameters.lb-service-type

       ipv46: $parameters.lb-virtual-ip

       port: 80

       lbmethod?: $parameters.lb-advanced.algorithm
<!--NeedCopy-->

The use of “?” omits the property if the expression on the right evaluates to nothing, which would be equivalent, in this case, to a component defined as follows:

components

  -

    name: lbvserver_comp

    type: ns::lbvserver

    properties:

      name: $parameters.lb-appname + "-lb"

      servicetype: $parameters.lb-service-type

      ipv46: $parameters.lb-virtual-ip

      port: 80
<!--NeedCopy-->

Because lbmethod is optional, omitting it still makes a valid component. Note that lbmethod might take its default value if one is defined in its type “ns::lbvserver.”

Optional properties

In this article