reg/set an object

To utilize the best of UPcmd, you always want to access the data conviently

Let’s talk about how we can prepare the data object

Demo

source

Main task yaml file
    vars:
      school: |
        sg:
          name: sydney grammar
          state: nsw
          address: sydney
          postcode: 2000
    tasks:
    - name: task
      task:
      - func: cmd
        desc: |
          1st solution: register object in any template rending
        do:
        - name: print
          desc: |
            Use a actual cmd to register a variable does not make sense, but it is doable.
            It is not advised to do it this way
          cmd: '{{.school |reg "myschool"}}'
        - name: print
          desc: |
            please note that myschool is only a string, but not an object
          cmd: '{{.myschool}}'
        - name: print
          desc: |
            please note that below result is only a string representation of the object
            any golang template result can only be string, but not a object
          cmd: '{{.myschool | ymlToObj |objToYml}}'
        - name: print
          desc: |
            now register the object to a named var myschool_object
          cmd: '{{.myschool | ymlToObj |reg "myschool_object"}}'
        - name: print
          desc: |
            same as above, this will only print the string reprentation of the object
          cmd: '{{.myschool_object}}'
        - name: printObj
          desc: |
            same as above, this will only print the string reprentation of the object
          cmd: '{{.myschool_object}}'
        - name: printObj
          desc: |
            * IMPORTANT
            use the object var name to refer to the object in register
          cmd: myschool_object
      - func: cmd
        desc: |
          2nd solution:
          use dvar auto coversion
        dvars:
        - name: myschool2
          value: '{{.school }}'
          flags:
          - toObj
          - v
        do:
        - name: print
          cmd: |
            {{.myschool2}}
            {{.myschool2_object}}
        - name: printObj
          cmd: myschool2_object
      - func: cmd
        desc: |
          3rd solution:
          use toObj cmd
          details: https://upcmd.netlify.app/cmd-func/c0095/
        do:
        - name: toObj
          cmd:
            fromkey: school
            reg: myschool3_object
        - name: printObj
          cmd: myschool3_object
    
Main log file
    loading [Config]:  ./tests/functests/upconfig.yml
    Main config:
                 Version -> 1.0.0
                  RefDir -> ./tests/functests
                 WorkDir -> cwd
              AbsWorkDir -> /up_project/up
                TaskFile -> c0156
                 Verbose -> vvv
              ModuleName -> self
               ShellType -> /bin/sh
           MaxCallLayers -> 8
                 Timeout -> 3600000
     MaxModuelCallLayers -> 256
               EntryTask -> task
      ModRepoUsernameRef -> 
      ModRepoPasswordRef -> 
    work dir: /up_project/up
    -exec task: task
    loading [Task]:  ./tests/functests/c0156
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1: [
    1st solution: register object in any template rending
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "school": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [print: Use a actual cmd to register a variable does not make sense, but it is doable.
    It is not advised to do it this way
     ]
    |
      sg:
        name: sydney grammar
        state: nsw
        address: sydney
        postcode: 2000
    
    ~SubStep2: [print: please note that myschool is only a string, but not an object
     ]
    sg:
      name: sydney grammar
      state: nsw
      address: sydney
      postcode: 2000
    
    ~SubStep3: [print: please note that below result is only a string representation of the object
    any golang template result can only be string, but not a object
     ]
    sg:
      address: sydney
      name: sydney grammar
      postcode: 2000
      state: nsw
    
    ~SubStep4: [print: now register the object to a named var myschool_object
     ]
    sg:
      address: sydney
      name: sydney grammar
      postcode: 2000
      state: nsw
    
    ~SubStep5: [print: same as above, this will only print the string reprentation of the object
     ]
    map[sg:map[address:sydney name:sydney grammar postcode:2000 state:nsw]]
    ~SubStep6: [printObj: same as above, this will only print the string reprentation of the object
     ]
    object:
     map[sg:map[address:sydney name:sydney grammar postcode:2000 state:nsw]]: (interface {}) <nil>
    
    ~SubStep7: [printObj: * IMPORTANT
    use the object var name to refer to the object in register
     ]
    object:
     myschool_object: (*map[interface {}]interface {})({
      "sg": {
        "name": "sydney grammar",
        "state": "nsw",
        "address": "sydney",
        "postcode": 2000
      }
    })
    
    -Step2: [
    2nd solution:
    use dvar auto coversion
    ]
    dvar> myschool2:
    "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n"
    
    -
    sg:
      name: sydney grammar
      state: nsw
      address: sydney
      postcode: 2000
    
    dvar[object]> myschool2_object:
    {
      "sg": {
        "name": "sydney grammar",
        "state": "nsw",
        "address": "sydney",
        "postcode": 2000
      }
    }
    
    self: final context exec vars:
    
    (*core.Cache)({
      "myschool2": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "myschool2_object": {
        "sg": {
          "name": "sydney grammar",
          "state": "nsw",
          "address": "sydney",
          "postcode": 2000
        }
      },
      "myschool": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "myschool_object": (*map[interface {}]interface {})({
        "sg": {
          "postcode": 2000,
          "name": "sydney grammar",
          "state": "nsw",
          "address": "sydney"
        }
      }),
      "school": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [print:  ]
    sg:
      name: sydney grammar
      state: nsw
      address: sydney
      postcode: 2000
    
    map[sg:map[address:sydney name:sydney grammar postcode:2000 state:nsw]]
    
    ~SubStep2: [printObj:  ]
    object:
     myschool2_object: {
      "sg": {
        "name": "sydney grammar",
        "state": "nsw",
        "address": "sydney",
        "postcode": 2000
      }
    }
    
    -Step3: [
    3rd solution:
    use toObj cmd
    details: https://upcmd.netlify.app/cmd-func/c0095/
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "school": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "myschool": "sg:\n  name: sydney grammar\n  state: nsw\n  address: sydney\n  postcode: 2000\n",
      "myschool_object": (*map[interface {}]interface {})({
        "sg": {
          "postcode": 2000,
          "name": "sydney grammar",
          "state": "nsw",
          "address": "sydney"
        }
      }),
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [toObj:  ]
    ~SubStep2: [printObj:  ]
    object:
     myschool3_object: {
      "sg": {
        "name": "sydney grammar",
        "state": "nsw",
        "address": "sydney",
        "postcode": 2000
      }
    }
    
    
Logs with different verbose level
Raw logs with different verbose level

Where are the data object from?

The data objects come from a few sources:

  1. scopes: you can define vars and dvars in scope group
  2. global vars/dvars
  3. vars/dvars in func
  4. vars registered from with templating using reg template func
  5. vars registered using reg cmd
  6. dvar flag: toObj will also register the object to that local exec vars context

This examples shows you the point 4,5 and 6, three ways to set the vars

You can make use of vars in following cases: 1. if condition 2. any templating rendering 3. yml operation using cmd: query, ymlWrite, ymlDelete