YAML学习笔记——入门

 

背景

在学习Fabric的e2e_cli的例子的时候发现很多配置文件都是使用.yaml文件进行配置。虽然看起来结构很简单,但是因为不懂其语法,所以不是很确定其中的具体内容。于是打算简单学习一下。

什么是YAML

第一步,起码要知道其缩写是什么吧,HTML,超文本标记语言,XML,可扩展的标记语言,YAML是什么标记语言呢?

然后,百度告诉我下面两行内容:

YAML是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写。

在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种置标语言),

真的是简单粗暴,但是这也是我第一次见到递归缩写。然后我去百度看看有没有什么其他的递归缩写,接着我就发现了另外一个很有名的递归缩写,GNU, GNU is Not Unix,当然,GNU就是GNU,一个名字,没有其他含义了。Emacs、GCC都是GNU的产品。

为什么用YAML

话题不扯远了,为什么要用YAML呢?换句话说,YAML有什么特点吸引人呢?

我第一眼看到的e2e_cli的第一步的配置文件的时候,觉得就是简单。

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs: 
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: example.com
    CA: 
        Country: US
        Province: California
        Locality: San Francisco
    # ---------------------------------------------------------------------------
    # "Specs" - See PeerOrgs below for complete description
    # ---------------------------------------------------------------------------
    Specs: 
      - Hostname: orderer
# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs: 
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    CA: 
        Country: US
        Province: California
        Locality: San Francisco
    # ---------------------------------------------------------------------------
    # "Specs"
    # ---------------------------------------------------------------------------
    # Uncomment this section to enable the explicit definition of hosts in your
    # configuration.  Most users will want to use Template, below
    #
    # Specs is an array of Spec entries.  Each Spec entry consists of two fields:
    #   - Hostname:   (Required) The desired hostname, sans the domain.
    #   - CommonName: (Optional) Specifies the template or explicit override for
    #                 the CN.  By default, this is the template:
    #
    #                              "{{.Hostname}}.{{.Domain}}"
    #
    #                 which obtains its values from the Spec.Hostname and
    #                 Org.Domain, respectively.
    # ---------------------------------------------------------------------------
    # Specs: 
    #   - Hostname: foo # implicitly "foo.org1.example.com"
    #     CommonName: foo27.org5.example.com # overrides Hostname-based FQDN set above
    #   - Hostname: bar
    #   - Hostname: baz
    # ---------------------------------------------------------------------------
    # "Template"
    # ---------------------------------------------------------------------------
    # Allows for the definition of 1 or more hosts that are created sequentially
    # from a template. By default, this looks like "peer%d" from 0 to Count-1.
    # You may override the number of nodes (Count), the starting index (Start)
    # or the template used to construct the name (Hostname).
    #
    # Note: Template and Specs are not mutually exclusive.  You may define both
    # sections and the aggregate nodes will be created for you.  Take care with
    # name collisions
    # ---------------------------------------------------------------------------
    Template: 
      Count: 2
      # Start: 5
      # Hostname: {{.Prefix}}{{.Index}} # default
    # ---------------------------------------------------------------------------
    # "Users"
    # ---------------------------------------------------------------------------
    # Count: The number of user accounts _in addition_ to Admin
    # ---------------------------------------------------------------------------
    Users:
      Count: 1
  # ---------------------------------------------------------------------------
  # Org2: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    CA: 
        Country: US
        Province: California
        Locality: San Francisco
    Template: 
      Count: 2
    Users: 
      Count: 1

现在让我们把注释去掉,就会看到下面的内容:

OrdererOrgs: 
  - Name: Orderer
    Domain: example.com
    CA: 
      Country: US
      Province: California
      Locality: San Francisco
   Specs: 
      - Hostname: orderer
PeerOrgs: 
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    CA: 
      Country: US
      Province: California
      Locality: San Francisco
    Template: 
      Count: 2
    Users: 
      Count: 1
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    CA: 
      Country: US
      Province: California
      Locality: San Francisco
    Template: 
      Count: 2
    Users: 
      Count: 1

简单,可读性高,易处理。然后,具体有什么特点呢,专业一点的解释在下面这个简书里提到了,我就不啰嗦了。https://www.jianshu.com/p/da0659a3632b

快速入门

  • 首先是三种格式,常量值,对象和数组

比如说Country的值为US

Country: US

CA的Country的值为UC

CA: 
  Country: US

数组,PeerOrgs中有两个Org

PeerOrgs: 
  - Org1
  - Org2
  • 一些常量
Count: 1
Country: US
EnableNodeOUs: true #True TRUE true都可以,false也一样
Null: ~ #在YAML中的null使用~来表示
String: 
  -	Hehe
  -	‘hello’
  -	“world”
Date: 2019-01-16
Datetime: 2019-01-16T20:31:15+08:00 #Date加T链接时间在加上时区。
  • 注释:
#在YAML中只有单行注释

注意:

在YAML中不能有TAB键,只能用空格,2个或者4个,相同层级的要左对齐。

大小写敏感。

冒号后面要加一个空格。代表Key: Value

在数组中也是一个横线后面加一个空格。

 

见得较少的或者说复杂一点的:

  • ---表示一个文档的开始
  • …表示一个文档的结束
---
OrdererOrgs: 
  - Name: Orderer
    Domain: example.com
    CA:
      Country: US
      Province: California
      Locality: San Francisco
    Specs:
      - Hostname: orderer
…
  • !!类型转换
String: 
  - !!str 123456789
  - !!str true
  • >折叠换行,|保留换行
ZYX: >
  This is
  Zyx
ZYX: |
  This is
  Zyx

第一个结果是

This is Zyx

第二个结果是

This is

 Zyx

  • &锚点,*引用,锚点不能单独写,前面必须有对象名。
Zyx: &zyx This is zyx
Boss: *zyx

结果相当于Boss: This is zyx。

  • 锚点可以用在合并内容上
ZYX:
  - &Z {x : 1 , y : 2}

Example:
  <<: *Z
  z: 3

结果为Example = {x = 1 , y =2 , z = 3}

参考:

https://www.jianshu.com/p/3f76188c1a8a

https://www.jianshu.com/p/97222440cd08

https://www.jianshu.com/p/da0659a3632b

https://baike.baidu.com/item/YAML/1067697?fr=aladdin#reference-[1]-1479346-wrap

https://yaml.org/spec/1.2/spec.html

猜你喜欢

转载自blog.csdn.net/z695516174/article/details/86514545