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

     1  doc_meta: |
     2    folder: object-oriented
     3    title: reg/set an object
     4    head: |
     5      To utilize the best of UPcmd, you always want to access the data conviently
     6  
     7      Let's talk about how we can prepare the data object
     8  
     9    sections:
    10      - title: Demo
    11        log: yes
    12  
    13      - title: Where are the data object from?
    14        content: |
    15          The data objects come from a few sources:
    16  
    17          1. scopes: you can define vars and dvars in scope group
    18          2. global vars/dvars
    19          3. vars/dvars in func
    20          4. vars registered from with templating using reg template func
    21          5. vars registered using reg cmd
    22          6. dvar flag: toObj will also register the object to that local exec vars context
    23  
    24          This examples shows you the point 4,5 and 6, three ways to set the vars
    25  
    26          You can make use of vars in following cases:
    27          1. if condition
    28          2. any templating rendering
    29          3. yml operation using cmd: query, ymlWrite, ymlDelete
    30  
    31  notes:
    32    - show new way to register object from template
    33    -
    34  
    35  vars:
    36    school: |
    37      sg:
    38        name: sydney grammar
    39        state: nsw
    40        address: sydney
    41        postcode: 2000
    42  
    43  tasks:
    44    -
    45      name: task
    46      task:
    47        -
    48          func: cmd
    49          desc: |
    50            1st solution: register object in any template rending
    51          do:
    52            - name: print
    53              desc: |
    54                Use a actual cmd to register a variable does not make sense, but it is doable.
    55                It is not advised to do it this way
    56              cmd: '{{.school |reg "myschool"}}'
    57            - name: print
    58              desc: |
    59                please note that myschool is only a string, but not an object
    60              cmd: '{{.myschool}}'
    61            - name: print
    62              desc: |
    63                please note that below result is only a string representation of the object
    64                any golang template result can only be string, but not a object
    65              cmd: '{{.myschool | ymlToObj |objToYml}}'
    66  
    67            - name: print
    68              desc: |
    69                now register the object to a named var myschool_object
    70              cmd: '{{.myschool | ymlToObj |reg "myschool_object"}}'
    71  
    72            - name: print
    73              desc: |
    74                same as above, this will only print the string reprentation of the object
    75              cmd: '{{.myschool_object}}'
    76  
    77            - name: printObj
    78              desc: |
    79                same as above, this will only print the string reprentation of the object
    80              cmd: '{{.myschool_object}}'
    81  
    82            - name: printObj
    83              desc: |
    84                * IMPORTANT
    85                use the object var name to refer to the object in register
    86              cmd: myschool_object
    87  
    88        -
    89          func: cmd
    90          desc: |
    91            2nd solution:
    92            use dvar auto coversion
    93          dvars:
    94            - name: myschool2
    95              value: '{{.school }}'
    96              flags:
    97                - toObj
    98                - v
    99          do:
   100            - name: print
   101              cmd: |
   102                {{.myschool2}}
   103                {{.myschool2_object}}
   104            - name: printObj
   105              cmd: myschool2_object
   106  
   107        -
   108          func: cmd
   109          desc: |
   110            3rd solution:
   111            use toObj cmd
   112            details: https://upcmd.netlify.app/cmd-func/c0095/
   113          do:
   114            - name: toObj
   115              cmd:
   116                fromkey: school
   117                reg: myschool3_object
   118  
   119            - name: printObj
   120              cmd: myschool3_object
   121