github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0123.yml (about)

     1  doc_meta: |
     2    folder: flow-controll
     3    title: indirect reference
     4    head: |
     5      Showcase using if condition in the case of non-exist value, you will probably need to use query cmd to query element from yml string then use if condition in the next step
     6  
     7    sections:
     8      - title: Note
     9        content: |
    10          For this case, if you need to reference to a dynamic value from a yml doc rather than var object, you can use query and register it, then refernce to that registered var value for if condition
    11  
    12      - title: Demo
    13        log: yes
    14  
    15  notes:
    16    goal:
    17      - demo how to reference to a non exist element
    18      - or how to evaluate an element
    19      - add support if the element does not exist, then if condition should be false by default
    20      - use query cmd to query within yml string, then use the registered var in if evaluation
    21  
    22  vars:
    23    student:
    24      name: tom
    25      sex: male
    26  
    27    ymldoc: |
    28      student:
    29        name: tom
    30        sex: male
    31  
    32  tasks:
    33    -
    34      name: task
    35      desc: main entry
    36      task:
    37        -
    38          func: cmd
    39          do:
    40            - name: print
    41              cmd: '{{.student.name}}'
    42  
    43            - name: print
    44              cmd: '{{.student.age}}'
    45  
    46        -
    47          func: cmd
    48          do:
    49            - name: print
    50              cmd: hello tom
    51          if: '{{eq .student.name "tom"}}'
    52  
    53        -
    54          func: cmd
    55          desc: add support if the element does not exist, then if condition should be false by default
    56          do:
    57            - name: print
    58              cmd: you will not see this message
    59          if: '{{.student.age}}'
    60  
    61        -
    62          func: cmd
    63          name: query
    64          cmd:
    65            ymlkey: ymldoc
    66            path: student.age.
    67            reg: studentage
    68          flags: [ymlOnly]
    69  
    70        -
    71          func: cmd
    72          do:
    73            - name: print
    74              cmd: you will not see this message
    75          if: '{{.studentage}}'
    76  
    77