convert dvar

A dvar value is normally a string, which could be a formated yml content, then be converted to an object. This demo shows that you can use a flag: toObj to notate this dvar will be transformed to an object and registered to global runtime

Rules

  • you can reference to plain dvar with the string|untemplated value
  • you can reference to hiera structure of a object once it is converted
  • the new name of the dvar object will be: dvar_object
  • the flag keepName makes the dvar keep its original name as is instead of creating dvar_object
  • keepName only used and pair to toObj

Demo

source

Main task yaml file
    vars:
      student:
        name: Tom
        gender: Male
        address:
          suburb:
            name: sydney
            postcode: 2000
            CBD: yes
          school: Sydney Grammar
    dvars:
    - name: a_smart_guy
      value: |
        name: Tom
        gender: Male
        school: Sydney Grammar
      flags: [toObj]
    - name: school_address
      #    expand: 2
    
      desc: |
        it can reference to the dvar object a_smart_guy_object has been registered from last step
      value: |
        address:
          suburb:
            name: {{.student.address.suburb.name}}
            postcode: 2000
            CBD: yes
          school: {{.student.address.school}}
          tom:
            name: {{.a_smart_guy_object.name}}
      flags: [toObj]
    - name: tom
      desc: |
        in this case, we use keepName flag to make the dvar name the same name we give. So instead we have regitered tom (yml string) and tom_object (object), we now only have tom as registered object
      value: |
        name: {{.a_smart_guy_object.name}}
        gender: Male
        school: Sydney Grammar
      flags:
      - toObj
      - keepName
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - echo """a smart guy=>{{.a_smart_guy}}"""
        - echo """postcode=>{{.student.address.suburb.postcode}}"""
        - echo """school address {{.school_address}}"""
        - echo """this guy is in =>{{.a_smart_guy_object.school}} school"""
        - echo """school address object {{.school_address_object.suburb.name}}"""
        - echo """school address object -> {{.school_address_object.address.suburb.name}}"""
        - echo """tom - {{.tom}}"""
    
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 -> c0027
                 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/c0027
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "a_smart_guy_object": {
        "school": "Sydney Grammar",
        "name": "Tom",
        "gender": "Male"
      },
      "school_address": "address:\n  suburb:\n    name: sydney\n    postcode: 2000\n    CBD: yes\n  school: Sydney Grammar\n  tom:\n    name: Tom\n",
      "school_address_object": {
        "address": {
          "suburb": {
            "postcode": 2000,
            "CBD": true,
            "name": "sydney"
          },
          "school": "Sydney Grammar",
          "tom": {
            "name": "Tom"
          }
        }
      },
      "up_runtime_task_layer_number": 0,
      "tom": {
        "school": "Sydney Grammar",
        "name": "Tom",
        "gender": "Male"
      },
      "student": {
        "address": {
          "suburb": {
            "postcode": 2000,
            "cbd": true,
            "name": "sydney"
          },
          "school": "Sydney Grammar"
        },
        "name": "Tom",
        "gender": "Male"
      },
      "a_smart_guy": "name: Tom\ngender: Male\nschool: Sydney Grammar\n"
    })
    
    cmd( 1):
    echo """a smart guy=>{{.a_smart_guy}}"""
    
    -
    a smart guy=>name: Tom
    gender: Male
    school: Sydney Grammar
    
    
    -
     .. ok
    cmd( 2):
    echo """postcode=>{{.student.address.suburb.postcode}}"""
    
    -
    postcode=>2000
    
    -
     .. ok
    cmd( 3):
    echo """school address {{.school_address}}"""
    
    -
    school address address:
      suburb:
        name: sydney
        postcode: 2000
        CBD: yes
      school: Sydney Grammar
      tom:
        name: Tom
    
    
    -
     .. ok
    cmd( 4):
    echo """this guy is in =>{{.a_smart_guy_object.school}} school"""
    
    -
    this guy is in =>Sydney Grammar school
    
    -
     .. ok
    cmd( 5):
    echo """school address object {{.school_address_object.suburb.name}}"""
    
    -
    school address object <no value>
    
    -
     .. ok
    cmd( 6):
    echo """school address object -> {{.school_address_object.address.suburb.name}}"""
    
    -
    school address object -> sydney
    
    -
     .. ok
    cmd( 7):
    echo """tom - {{.tom}}"""
    
    -
    tom - map[gender:Male name:Tom school:Sydney Grammar]
    
    -
     .. ok
    . ok
    
Logs with different verbose level
Raw logs with different verbose level