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

     1  doc_meta: |
     2    folder: flow-controll
     3    title: non-exist value if
     4    head: |
     5      Showcase using if condition in the case of non-exist value
     6  
     7    sections:
     8      - title: Supported true/false values
     9        content: |
    10          If a reference to a variable or sub elment does not exist, it will be false by default
    11  
    12      - title: Demo
    13        log: yes
    14  
    15    related:
    16      refs:
    17        - title: advanced usage of break in call func
    18          link: ../../flow-controll/c0121
    19        - title: use break to exit from loop
    20          link: ../../loop/c0125
    21  
    22  notes:
    23    goal:
    24      - demo how to reference to a non exist element
    25      - or how to evaluate an element
    26      - add support if the element does not exist, then if condition should be false by default
    27  
    28  vars:
    29    student:
    30      name: tom
    31      sex: male
    32  
    33  tasks:
    34    -
    35      name: task
    36      desc: main entry
    37      task:
    38        -
    39          func: cmd
    40          do:
    41            - name: print
    42              cmd: '{{.student.name}}'
    43  
    44            - name: print
    45              cmd: '{{.student.age}}'
    46  
    47  #          - name: print
    48  #            cmd: '{{.student.age | len}}'
    49  
    50        -
    51          func: cmd
    52          do:
    53            - name: print
    54              cmd: hello tom
    55          if: '{{eq .student.name "tom"}}'
    56  
    57        -
    58          func: cmd
    59          desc: add support if the element does not exist, then if condition should be false by default
    60          dvars:
    61            - name: student_age
    62              value: '.student.age'
    63          do:
    64            - name: assert
    65              desc: |
    66                this cmd will not evaluated as it will not reach here
    67                the below evaluate will cause issue, as age does not exist
    68                always map to a dvar first
    69              cmd: '{{eq .student.age "None"}}'
    70  
    71            - name: print
    72              cmd: '{{.student.age}}'
    73  
    74            - name: print
    75              cmd: |
    76                you will not see this message
    77          if: '{{eq .student_age "None"}}'