学习资料
链接:http://www.ruanyifeng.com/blog/2016/07/yaml.html
优势
yaml 的精髓我觉得是引用,这样可以使用面向对象的组合思想。
语法检查工具
- 在线工具:https://yaml-online-parser.appspot.com/
数据结构
YAML 支持的数据结构有三种。- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
 - 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
 - 纯量(scalars):单个的、不可再分的值
 
 
对象
animal: pets
数组
一组连词线开头的行,构成一个数组。
- Cat- Dog- Goldfish
纯量
- 字符串
 - 布尔值
 - 整数
 - 浮点数
 - Null
 - 时间
 - 日期
强制类型转化
YAML 允许使用两个感叹号,强制转换数据类型。e: !!str 123f: !!str true
引用
锚点&和别名*,可以用来引用。defaults: &defaultsadapter: postgreshost: localhostdevelopment:database: myapp_development<<: *defaultstest:database: myapp_test<<: *defaults
实战
对象的值是数组且使用了引用
email_accounts 的 value 是一个数组。数组中的元素是一个对象,这个对象使用了 「引用」。
如下写法有毛病,email不必须要的:
正确写法, 下面这种写法才是我想要的:common: &commonemail_ssl: &email_sslhost: smtp.mxhichina.comport: !!str 465password: Qwe123456_email_accounts:- email:<<: *email_sslusername: booleandatamsg@shouxin168.com- email:<<: *email_sslusername: booleandatamsg002@shouxin168.com- email:<<: *email_sslusername: booleandatamsg003@shouxin168.com- email:<<: *email_sslusername: booleandatamsg004@shouxin168.com- email:<<: *email_sslusername: booleandatamsg005@shouxin168.com
common: &commonproject_name: messageurl_prefix: /message/apilog_file: /var/log/message/message.loghmac: he23udendife4f3fe4n!mongodb_uri: mongodb://root:shouxin168_mg@dds-uf6ecf5cc83911641262-pub.mongodb.rds.aliyuncs.com:3717,dds-uf6ecf5cc83911642699-pub.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-17015893guard_developer_aes_key: vo8N6ZPawqLDhGj7guard_developer_pri_key: /etc/guard_v2/guard_develop.rsaguard_developer_uuid: 01b20490-4a1e-11e9-9eb7-247703d210dcguard_prod_url: https://guard.shouxin168.com/api/lightning/product/querylog_formatter: jsonlighting_mysql_uri: lgt:Qwe123456@tcp(rm-uf6xfyemz1085v2a4.mysql.rds.aliyuncs.com)/lightingredis_url: r-uf67b5cbffd85a24.redis.rds.aliyuncs.com:6379/6email_ssl: &email_sslhost: smtp.mxhichina.comport: !!str 465password: Qwe123456_email_accounts:-<<: *email_sslusername: booleandatamsg@shouxin168.com-<<: *email_sslusername: booleandatamsg002@shouxin168.com-<<: *email_sslusername: booleandatamsg003@shouxin168.com-<<: *email_sslusername: booleandatamsg004@shouxin168.com-<<: *email_sslusername: booleandatamsg005@shouxin168.com
 
