是時候使用 YAML 來做配置或資料檔案了
概述
我們做程式,經常需要用到配置資訊,回顧一下這麼多年的搬磚生涯,我記得用過多種格式的檔案來定義配置資訊,例如 ini檔案
,xml檔案
,或者現在比較流行的 json
檔案。
這些年隨著雲端計算和雲原生應用的流行,我發現一種新的做配置(甚至作為資料來源格式)的檔案格式,叫做 YAML
(發音 /ˈjæməl/ )。看這個名字,好像它又是一種新的 ML(Markup Language)
,出人意外的是據說它的這個名字的本意是指 YAML Ain't Markup Language
,當然也有人調侃地說,其實是指 Yet Another Markup Language
。 我們不管這麼多,今天主要來看看具體怎麼使用它,以及它跟之前的格式(尤其是 json
一個簡單的例子
YAML檔案一般以 .yml
作為副檔名,例如下面這個例子是介紹了我的部落格基本資訊。我們可以看到非常簡潔的語法,例如
- 在冒號(
:
) 的兩邊定義了屬性名和屬性值。 - 不同屬性,換行即可。
- 如果表示一個數組,則用
-
定義條目。 - 多行文字屬性值,用
|
定義。 - 不同的層級用縮排即可。
title: 陳希章@中國 author: 陳希章 link: https://chenxizhang.cnblogs.com description: | 這是陳希章在部落格園的主頁, 這個部落格是從2005年開始撰寫的,目前有隨筆約1400篇 tags: - 科技 - 人文 languages: - en-us - zh-cn articles: - title: 使用本地自簽名證書為 React 專案啟用 https 支援 href: https://www.cnblogs.com/chenxizhang/p/16244358.html - title: 在部落格文章中使用mermaid 定義流程圖,序列圖,甘特圖 href: https://www.cnblogs.com/chenxizhang/p/16253501.html
與其他格式的比較
我們來看一下,如果這個檔案用 json
來定義應該怎麼樣呢?
{ "title": "我的部落格", "author": "陳希章", "link": "https://chenxizhang.cnblogs.com", "description": "這是陳希章在部落格園的主頁,\n這個部落格是從2005年開始撰寫的,目前有隨筆約1400篇\n", "tags": [ "科技", "人文" ], "languages": [ "en-us", "zh-cn" ], "articles": [ { "href": "https://www.cnblogs.com/chenxizhang/p/16244358.html", "title": "使用本地自簽名證書為 React 專案啟用 https 支援" }, { "href": "https://www.cnblogs.com/chenxizhang/p/16253501.html", "title": "在部落格文章中使用mermaid 定義流程圖,序列圖,甘特圖" } ] }
簡單比較一下,同樣的資料,用 json
需要 612 個字元,而用 YAML
則需要 410個字元,節約大約200個字元呢,也就是相當於省了1/3的體積。
如果是XML
會更加繁瑣,這裡就不比較了。
就算是一直來說已經算比較簡介的 json
,為了表示一個數據結構,不得不引入很多跟內容無關的字元,包括 {
和 }
表示一個物件的開始和結束,[
和 ]
表示一個數字的開始和結束,另外不同的屬性之間還要用 ,
分開,如果有多行文字,需要用 \n
來分割。
定義schema來輔助輸入和驗證
在使用 XML
或 json
檔案格式時,我們都可以通過架構檔案(schema)來讓編輯器提供智慧提示,並且也可以在程式中對內容進行校驗。例如如果我們要驗證上面這個json
檔案,可以定義如下的schema
檔案
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://www.xizhang.com/schemas/blog",
"title": "部落格資料",
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "部落格標題"
},
"author": {
"type": "string",
"description": "作者資訊"
},
"link": {
"type": "string",
"description": "部落格連結地址"
},
"description": {
"type": "string",
"description": "部落格描述資訊,可以多行"
},
"tags": {
"type": "array",
"description": "標籤資訊,可以有多個",
"items": {
"type": "string"
}
},
"languages": {
"type": "array",
"description": "支援的語言列表",
"items": {
"type": "string"
}
},
"articles": {
"type": "array",
"description": "文章列表",
"items": {
"type": "object",
"title": "文章",
"description": "文章資訊",
"properties": {
"title": {
"type": "string",
"description": "文章標題"
},
"href": {
"type": "string",
"description": "文章連結"
}
},
"required": [
"title"
]
}
}
},
"required": [
"title",
"author",
"link",
"articles"
]
}
注意,編寫這個文件本身不難,你輸入第一行 $schema
並且選擇其中一個標準版本,例如 http://json-schema.org/draft-07/schema
之後,下面就都有智慧提示,所以不要過分依賴所謂網上很多的生成器。
如果在json
檔案中使用這個schema,語法如下
{
"$schema": "./blogschema.json",
"title": "我的部落格",
"author": "陳希章",
"link": "https://chenxizhang.cnblogs.com",
"description": "這是陳希章在部落格園的主頁,\n這個部落格是從2005年開始撰寫的,目前有隨筆約1400篇\n",
"tags": [
"科技",
"人文"
],
"languages": [
"en-us",
"zh-cn"
],
"articles": [
{
"href": "https://www.cnblogs.com/chenxizhang/p/16244358.html",
"title": "使用本地自簽名證書為 React 專案啟用 https 支援"
},
{
"href": "https://www.cnblogs.com/chenxizhang/p/16253501.html",
"title": "在部落格文章中使用mermaid 定義流程圖,序列圖,甘特圖"
}
]
}
除了用相對路徑指定schema檔案外,還可以用網路地址,例如 "$schema": "https://files.cnblogs.com/files/chenxizhang/blogschema.json"
有意思的是,YAML
也是使用json
schema來做架構驗證和智慧提示,如果你用vscode
作為編輯器,你可以安裝一個外掛如下
接下來在你的 yml
檔案的頂部插入這樣一句 # yaml-language-server:$schema=https://files.cnblogs.com/files/chenxizhang/blogschema.json
然後再進行輸入時,就會有智慧提示,包括了描述資訊等
如果你的資料資訊不完整,vscode
會有明確的提示
我們還可以讓 vscode
自動給相關檔案套用對應的schema,例如我們想讓當前目錄中所有帶有 blog
的 .yml
檔案,都自動地套用 https://files.cnblogs.com/files/chenxizhang/blogschema.json
這個schema,可以這麼做
- 在當前專案根目錄下面建立
.vscode
目錄 - 在
.vscode
目錄中建立settings.json
檔案
輸入如下的內容
{
"yaml.schemas": {
"https://files.cnblogs.com/files/chenxizhang/blogschema.json": "*blog.yml"
}
}