datatemplate

Sometime you may need to do some adhoc message transformation in an elegant way, you would like to implementate your internal code or logic based on a interface / contract rather than external direct mapping. In such a scenario, it is better we could use a mapping layer to sheld your code suffering the impact of external change. We can use datatemplate for this purpose. In datatemplate definition, you map your own data structure/object with external vars/objects, then in your rendered value, you use the reference to your own object/key/value etc to generate the value required for your own business

Relavant

templating using datakey and datapath

references

Demo

source

Main task yaml file
    goal:
    - showcase the dvar to render value using sub element of an var rather the global
      var name space
    - extension to c0082 and c0098
    vars:
      student:
        name: Tom
        gender: Male
        school: Sydney Grammar
      nsw:
        sydney:
          sgschool:
            student:
              name: Grace
              gender: Female
              school: MLC
    tasks:
    - name: task
      task:
      - func: cmd
        desc: use datatemplate as datasource
        dvars:
        - name: student_info
          value: 'my name is:{{.name}} and I am in {{.school}}'
          datatemplate: |
            name: {{.nsw.sydney.sgschool.student.name}}
            gender: {{.nsw.sydney.sgschool.student.gender}}
            school: {{.nsw.sydney.sgschool.student.school}}
          flags: [vvv]
        do:
        - name: print
          cmd: '{{.student_info}}'
      - func: cmd
        desc: use datatemplate as datasource
        dvars:
        - name: student_info
          value: 'my name is:{{.student.name}} and I am in {{.school.name}}'
          datatemplate: |
            student:
              name: {{.nsw.sydney.sgschool.student.name}}
              gender: {{.nsw.sydney.sgschool.student.gender}}
            school:
              name: {{.nsw.sydney.sgschool.student.school}}
          flags: [vvv]
        do:
        - name: print
          cmd: '{{.student_info}}'
    
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 -> c0099
                 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/c0099
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1: [: use datatemplate as datasource ]
    dvar> student_info:
    "my name is:Grace and I am in MLC"
    
    -
    my name is:Grace and I am in MLC
    self: final context exec vars:
    
    (*core.Cache)({
      "student_info": "my name is:Grace and I am in MLC",
      "student": {
        "gender": "Male",
        "school": "Sydney Grammar",
        "name": "Tom"
      },
      "nsw": {
        "sydney": {
          "sgschool": {
            "student": {
              "gender": "Female",
              "school": "MLC",
              "name": "Grace"
            }
          }
        }
      },
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [print:  ]
    my name is:Grace and I am in MLC
    -Step2: [: use datatemplate as datasource ]
    dvar> student_info:
    "my name is:Grace and I am in MLC"
    
    -
    my name is:Grace and I am in MLC
    self: final context exec vars:
    
    (*core.Cache)({
      "nsw": {
        "sydney": {
          "sgschool": {
            "student": {
              "gender": "Female",
              "school": "MLC",
              "name": "Grace"
            }
          }
        }
      },
      "student": {
        "gender": "Male",
        "school": "Sydney Grammar",
        "name": "Tom"
      },
      "up_runtime_task_layer_number": 0,
      "student_info": "my name is:Grace and I am in MLC"
    })
    
    ~SubStep1: [print:  ]
    my name is:Grace and I am in MLC
    
Logs with different verbose level
Raw logs with different verbose level