oasis-open / tosca-community-contributions

OASIS TC Open Repository: Manages TOSCA profiles, tests, and templates that are maintained by the TOSCA community. They are intended to be used as examples to help developers get started with TOSCA and to test compliance of TOSCA implementations with the standard.
https://github.com/oasis-open/tosca-community-contributions
Apache License 2.0
39 stars 25 forks source link

Proposal: magic words as shortcuts for function calls with no arguments #133

Closed tliron closed 1 year ago

tliron commented 2 years ago

In today's ad hoc discussion we came up with the following:

We like the idea of supporting "magic words" generally in TOSCA as a friendly syntactical shortcut

A "magic word" is a string that is not a YAML map key beginning with "$" (unescaped). It is equivalent to a function call with no arguments (or an empty seq).

For strings that are map keys beginning with "$" (unescaped) the same rules apply as we have previously defined them: we require that the map have only one key (the function name), otherwise it would be a syntax error, and that key's value is the argument or seq of arguments.

In the following example $magic is a function with no arguments that returns an integer:

node_types:

  MyNode1:
    properties:
      prop1:
        type: integer
      prop2:
        type: list
        entry_schema: integer
      prop3:
        type: map
        entry_schema: integer

service_template:

  node_templates:

    my-node:
      type: MyNode1
      properties:
        prop1: $magic
        prop2: [ $magic, $magic, $magic ]
        prop3:
          key1: $magic
          key2: $magic

If we want to use a function call with no arguments as YAML keys, we have to use the full syntax instead of magic words. Here $keygen returns a GUID string:

    my-node:
      type: MyNode2
      properties:
        hints:
          { $keygen: [] }: 123

(There is a potential problem in the example above in situations with multiple identical keys in YAML. See issue #134)

lauwers commented 1 year ago

This proposal has been accepted and is documented in Section 5.4.14 of https://docs.oasis-open.org/tosca/TOSCA/v2.0/csd05/TOSCA-v2.0-csd05.html