Optional properties 编辑

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.”

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:44 次

字数:2786

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文