zoukankan      html  css  js  c++  java
  • python笔记49-yaml文件中变量的使用(锚点& 与 引用*)

    前言

    在yaml文件中如何引用变量?当我们在一个yaml文件中写很多测试数据时候,比如一些配置信息像用户名,邮箱,数据库配置等很多地方都会重复用到。
    重复的数据,如果不设置变量,后续维护起来就很困难。
    yaml文件里面也可以设置变量(锚点&),其它地方重复用到的话,可以用*引用

    锚点&和引用*

    对于重复的数据,可以单独写到yaml文件的开头位置,其它的地方用到的可以用*引用

    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    userinfo: &userinfo
        user: yoyo
        email: 283340479@qq.com
    
    test1:
       name: testcase 1
       data:
           <<: *userinfo
           tel: 12345678901
    
    test2:
        name: testcase 2
        data:
            <<: *userinfo
            tel: 15201234023
    

    等同于下面的数据

    userinfo:
        user: yoyo
        email: 283340479@qq.com
    
    test1:
       name: testcase 1
       data:
           user: yoyo
           email: 283340479@qq.com
           tel: 12345678901
    
    test2:
        name: testcase 2
        data:
            user: yoyo
            email: 283340479@qq.com
            tel: 15201234023
    

    &用来建立锚点(userinfo),<<表示合并到当前数据,*用来引用锚点。

    *引用value值

    上面的例子是对userinfo整体的数据,引用到其它地方了,有时候我们只想引用其中的一个值,如email的值,如何实现呢?

    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    userinfo: &userinfo
        user: yoyo
        email: &email 283340479@qq.com
    
    test3:
       name: testcase 3
       data:
           user: admin
           email: *email
           tel: 12345678901
    
    test4:
       name: testcase 5
       data:
           user: test123
           email: *email
           tel: 12345678902
    

    等同于下面的数据

    userinfo:
        user: yoyo
        email: 283340479@qq.com
    
    test3:
       name: testcase 3
       data:
           user: admin
           email: 283340479@qq.com
           tel: 12345678901
    
    test4:
       name: testcase 5
       data:
           user: test123
           email: 283340479@qq.com
           tel: 12345678902
    

    这样就可以把重复的数据,单独写到一个配置,其它地方*引用就可以了

  • 相关阅读:
    ext文件系统机制原理剖析
    win10企业版无法访问共享文件夹
    Linux 系统 CPU 的性能监控及调优
    MySQL延时复制简介
    MySQL迁移升级解决方案
    Docker 微服务教程安装WordPress
    java
    pom.xml
    sharding-jdbc
    java-MyBatis可视化代码生成工具
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/14023794.html
Copyright © 2011-2022 走看看