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

     1  doc_meta: |
     2    folder: vars
     3    title: probe exisitence of path
     4    head: |
     5      When you render a template value based on a path of the var in the register, sometimes you will get <no value> as that element does not exist yet. This will appear as a warning error and ignored. However it is critial if this value will be used in workflow condition, then this might cause problem. So it is better to know if this element exist before accessing
     6  
     7    sections:
     8      - title: Demo
     9        log: yes
    10  
    11      - title: Explain
    12        content: |
    13          Below shows how to use cmd pathExisted to get a boolean result and saved to a register value, then you can use it in your workflow to decide to go ahead to access the value of the var
    14  
    15  notes:
    16    - test how to detect if a var exist or not
    17  
    18  vars:
    19    a: aaa
    20    b: bbb
    21    sydney_grammar:
    22      address: sydney
    23  
    24  tasks:
    25    -
    26      name: task
    27      task:
    28        -
    29          func: cmd
    30          desc: |
    31            access a non-exist var will not cause error or panic while you render it
    32          do:
    33            - name: print
    34              cmd: '{{.c}}'
    35        -
    36          func: cmd
    37          desc: |
    38            however if you access a non exist child element, then it will result in a warning error
    39          do:
    40            - name: print
    41              cmd: '{{.b.not_exist}}'
    42  
    43        -
    44          func: cmd
    45          do:
    46            - name: query
    47              desc: |
    48                if the sub element does not exist, eg school_name does not exit, then the query result will give the result of the closest element result, in this caes the parent value "aaa" will be return
    49              cmd:
    50                path: a.school_name
    51                reg: myschool
    52  
    53            - name: query
    54              cmd:
    55                path: sydney_grammar.address
    56                reg: school_address
    57  
    58            - name: pathExisted
    59              cmd:
    60                path: sydney_grammar.address
    61                reg: address_existed
    62  
    63            - name: pathExisted
    64              desc: |
    65                the state will be rendered as <no value>, this will be treated as not exist
    66              cmd:
    67                path: sydney_grammar.state
    68                reg: state_existed
    69  
    70            - name: pathExisted
    71              cmd:
    72                path: b.not_exist
    73                reg: varb_sub_element_existed
    74  
    75            - name: print
    76              cmd: |
    77                sydney_grammar.address exist: {{.address_existed}}
    78                sydney_grammar.state exist: {{.state_existed}}
    79                varb_sub_element_existed exist: {{.varb_sub_element_existed}}
    80  
    81        -
    82          func: cmd
    83          desc: test pathExisted in templating
    84          do:
    85            - name: print
    86              cmd: |
    87                sydney_grammar.address exist: {{ pathExisted "sydney_grammar.address" }}
    88                sydney_grammar.state exist: {{ pathExisted "sydney_grammar.state"}}
    89                varb_sub_element_existed exist: {{ pathExisted "b.not_exist"}}
    90                {{ if pathExisted "b.not_exist"}}
    91                I am happy
    92                {{ else }}
    93                I am sad
    94                {{ end }}
    95  
    96        -
    97          func: cmd
    98          do:
    99            - name: inspect
   100              cmd:
   101                - exec_vars
   102  
   103            - name: assert
   104              cmd:
   105                - '{{pathExisted "sydney_grammar.address" }}'