aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/go-ini/ini/README_ZH.md
diff options
context:
space:
mode:
authorJake Champlin <jake.champlin.27@gmail.com>2017-06-06 12:40:07 -0400
committerJake Champlin <jake.champlin.27@gmail.com>2017-06-06 12:40:07 -0400
commitbae9f6d2fd5eb5bc80929bd393932b23f14d7c93 (patch)
treeca9ab12a7d78b1fc27a8f734729081357ce6d252 /vendor/github.com/go-ini/ini/README_ZH.md
parent254c495b6bebab3fb72a243c4bce858d79e6ee99 (diff)
downloadterraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.gz
terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.zst
terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.zip
Initial transfer of provider code
Diffstat (limited to 'vendor/github.com/go-ini/ini/README_ZH.md')
-rw-r--r--vendor/github.com/go-ini/ini/README_ZH.md721
1 files changed, 721 insertions, 0 deletions
diff --git a/vendor/github.com/go-ini/ini/README_ZH.md b/vendor/github.com/go-ini/ini/README_ZH.md
new file mode 100644
index 0000000..3b4fb66
--- /dev/null
+++ b/vendor/github.com/go-ini/ini/README_ZH.md
@@ -0,0 +1,721 @@
1本包提供了 Go 语言中读写 INI 文件的功能。
2
3## 功能特性
4
5- 支持覆盖加载多个数据源(`[]byte`、文件和 `io.ReadCloser`)
6- 支持递归读取键值
7- 支持读取父子分区
8- 支持读取自增键名
9- 支持读取多行的键值
10- 支持大量辅助方法
11- 支持在读取时直接转换为 Go 语言类型
12- 支持读取和 **写入** 分区和键的注释
13- 轻松操作分区、键值和注释
14- 在保存文件时分区和键值会保持原有的顺序
15
16## 下载安装
17
18使用一个特定版本:
19
20 go get gopkg.in/ini.v1
21
22使用最新版:
23
24 go get github.com/go-ini/ini
25
26如需更新请添加 `-u` 选项。
27
28### 测试安装
29
30如果您想要在自己的机器上运行测试,请使用 `-t` 标记:
31
32 go get -t gopkg.in/ini.v1
33
34如需更新请添加 `-u` 选项。
35
36## 开始使用
37
38### 从数据源加载
39
40一个 **数据源** 可以是 `[]byte` 类型的原始数据,`string` 类型的文件路径或 `io.ReadCloser`。您可以加载 **任意多个** 数据源。如果您传递其它类型的数据源,则会直接返回错误。
41
42```go
43cfg, err := ini.Load([]byte("raw data"), "filename", ioutil.NopCloser(bytes.NewReader([]byte("some other data"))))
44```
45
46或者从一个空白的文件开始:
47
48```go
49cfg := ini.Empty()
50```
51
52当您在一开始无法决定需要加载哪些数据源时,仍可以使用 **Append()** 在需要的时候加载它们。
53
54```go
55err := cfg.Append("other file", []byte("other raw data"))
56```
57
58当您想要加载一系列文件,但是不能够确定其中哪些文件是不存在的,可以通过调用函数 `LooseLoad` 来忽略它们(`Load` 会因为文件不存在而返回错误):
59
60```go
61cfg, err := ini.LooseLoad("filename", "filename_404")
62```
63
64更牛逼的是,当那些之前不存在的文件在重新调用 `Reload` 方法的时候突然出现了,那么它们会被正常加载。
65
66#### 忽略键名的大小写
67
68有时候分区和键的名称大小写混合非常烦人,这个时候就可以通过 `InsensitiveLoad` 将所有分区和键名在读取里强制转换为小写:
69
70```go
71cfg, err := ini.InsensitiveLoad("filename")
72//...
73
74// sec1 和 sec2 指向同一个分区对象
75sec1, err := cfg.GetSection("Section")
76sec2, err := cfg.GetSection("SecTIOn")
77
78// key1 和 key2 指向同一个键对象
79key1, err := cfg.GetKey("Key")
80key2, err := cfg.GetKey("KeY")
81```
82
83#### 类似 MySQL 配置中的布尔值键
84
85MySQL 的配置文件中会出现没有具体值的布尔类型的键:
86
87```ini
88[mysqld]
89...
90skip-host-cache
91skip-name-resolve
92```
93
94默认情况下这被认为是缺失值而无法完成解析,但可以通过高级的加载选项对它们进行处理:
95
96```go
97cfg, err := LoadSources(LoadOptions{AllowBooleanKeys: true}, "my.cnf"))
98```
99
100这些键的值永远为 `true`,且在保存到文件时也只会输出键名。
101
102#### 关于注释
103
104下述几种情况的内容将被视为注释:
105
1061. 所有以 `#` 或 `;` 开头的行
1072. 所有在 `#` 或 `;` 之后的内容
1083. 分区标签后的文字 (即 `[分区名]` 之后的内容)
109
110如果你希望使用包含 `#` 或 `;` 的值,请使用 ``` ` ``` 或 ``` """ ``` 进行包覆。
111
112### 操作分区(Section)
113
114获取指定分区:
115
116```go
117section, err := cfg.GetSection("section name")
118```
119
120如果您想要获取默认分区,则可以用空字符串代替分区名:
121
122```go
123section, err := cfg.GetSection("")
124```
125
126当您非常确定某个分区是存在的,可以使用以下简便方法:
127
128```go
129section := cfg.Section("section name")
130```
131
132如果不小心判断错了,要获取的分区其实是不存在的,那会发生什么呢?没事的,它会自动创建并返回一个对应的分区对象给您。
133
134创建一个分区:
135
136```go
137err := cfg.NewSection("new section")
138```
139
140获取所有分区对象或名称:
141
142```go
143sections := cfg.Sections()
144names := cfg.SectionStrings()
145```
146
147### 操作键(Key)
148
149获取某个分区下的键:
150
151```go
152key, err := cfg.Section("").GetKey("key name")
153```
154
155和分区一样,您也可以直接获取键而忽略错误处理:
156
157```go
158key := cfg.Section("").Key("key name")
159```
160
161判断某个键是否存在:
162
163```go
164yes := cfg.Section("").HasKey("key name")
165```
166
167创建一个新的键:
168
169```go
170err := cfg.Section("").NewKey("name", "value")
171```
172
173获取分区下的所有键或键名:
174
175```go
176keys := cfg.Section("").Keys()
177names := cfg.Section("").KeyStrings()
178```
179
180获取分区下的所有键值对的克隆:
181
182```go
183hash := cfg.Section("").KeysHash()
184```
185
186### 操作键值(Value)
187
188获取一个类型为字符串(string)的值:
189
190```go
191val := cfg.Section("").Key("key name").String()
192```
193
194获取值的同时通过自定义函数进行处理验证:
195
196```go
197val := cfg.Section("").Key("key name").Validate(func(in string) string {
198 if len(in) == 0 {
199 return "default"
200 }
201 return in
202})
203```
204
205如果您不需要任何对值的自动转变功能(例如递归读取),可以直接获取原值(这种方式性能最佳):
206
207```go
208val := cfg.Section("").Key("key name").Value()
209```
210
211判断某个原值是否存在:
212
213```go
214yes := cfg.Section("").HasValue("test value")
215```
216
217获取其它类型的值:
218
219```go
220// 布尔值的规则:
221// true 当值为:1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On
222// false 当值为:0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off
223v, err = cfg.Section("").Key("BOOL").Bool()
224v, err = cfg.Section("").Key("FLOAT64").Float64()
225v, err = cfg.Section("").Key("INT").Int()
226v, err = cfg.Section("").Key("INT64").Int64()
227v, err = cfg.Section("").Key("UINT").Uint()
228v, err = cfg.Section("").Key("UINT64").Uint64()
229v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339)
230v, err = cfg.Section("").Key("TIME").Time() // RFC3339
231
232v = cfg.Section("").Key("BOOL").MustBool()
233v = cfg.Section("").Key("FLOAT64").MustFloat64()
234v = cfg.Section("").Key("INT").MustInt()
235v = cfg.Section("").Key("INT64").MustInt64()
236v = cfg.Section("").Key("UINT").MustUint()
237v = cfg.Section("").Key("UINT64").MustUint64()
238v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339)
239v = cfg.Section("").Key("TIME").MustTime() // RFC3339
240
241// 由 Must 开头的方法名允许接收一个相同类型的参数来作为默认值,
242// 当键不存在或者转换失败时,则会直接返回该默认值。
243// 但是,MustString 方法必须传递一个默认值。
244
245v = cfg.Seciont("").Key("String").MustString("default")
246v = cfg.Section("").Key("BOOL").MustBool(true)
247v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25)
248v = cfg.Section("").Key("INT").MustInt(10)
249v = cfg.Section("").Key("INT64").MustInt64(99)
250v = cfg.Section("").Key("UINT").MustUint(3)
251v = cfg.Section("").Key("UINT64").MustUint64(6)
252v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now())
253v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339
254```
255
256如果我的值有好多行怎么办?
257
258```ini
259[advance]
260ADDRESS = """404 road,
261NotFound, State, 5000
262Earth"""
263```
264
265嗯哼?小 case!
266
267```go
268cfg.Section("advance").Key("ADDRESS").String()
269
270/* --- start ---
271404 road,
272NotFound, State, 5000
273Earth
274------ end --- */
275```
276
277赞爆了!那要是我属于一行的内容写不下想要写到第二行怎么办?
278
279```ini
280[advance]
281two_lines = how about \
282 continuation lines?
283lots_of_lines = 1 \
284 2 \
285 3 \
286 4
287```
288
289简直是小菜一碟!
290
291```go
292cfg.Section("advance").Key("two_lines").String() // how about continuation lines?
293cfg.Section("advance").Key("lots_of_lines").String() // 1 2 3 4
294```
295
296可是我有时候觉得两行连在一起特别没劲,怎么才能不自动连接两行呢?
297
298```go
299cfg, err := ini.LoadSources(ini.LoadOptions{
300 IgnoreContinuation: true,
301}, "filename")
302```
303
304哇靠给力啊!
305
306需要注意的是,值两侧的单引号会被自动剔除:
307
308```ini
309foo = "some value" // foo: some value
310bar = 'some value' // bar: some value
311```
312
313这就是全部了?哈哈,当然不是。
314
315#### 操作键值的辅助方法
316
317获取键值时设定候选值:
318
319```go
320v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"})
321v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75})
322v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30})
323v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30})
324v = cfg.Section("").Key("UINT").InUint(4, []int{3, 6, 9})
325v = cfg.Section("").Key("UINT64").InUint64(8, []int64{3, 6, 9})
326v = cfg.Section("").Key("TIME").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time1, time2, time3})
327v = cfg.Section("").Key("TIME").InTime(time.Now(), []time.Time{time1, time2, time3}) // RFC3339
328```
329
330如果获取到的值不是候选值的任意一个,则会返回默认值,而默认值不需要是候选值中的一员。
331
332验证获取的值是否在指定范围内:
333
334```go
335vals = cfg.Section("").Key("FLOAT64").RangeFloat64(0.0, 1.1, 2.2)
336vals = cfg.Section("").Key("INT").RangeInt(0, 10, 20)
337vals = cfg.Section("").Key("INT64").RangeInt64(0, 10, 20)
338vals = cfg.Section("").Key("UINT").RangeUint(0, 3, 9)
339vals = cfg.Section("").Key("UINT64").RangeUint64(0, 3, 9)
340vals = cfg.Section("").Key("TIME").RangeTimeFormat(time.RFC3339, time.Now(), minTime, maxTime)
341vals = cfg.Section("").Key("TIME").RangeTime(time.Now(), minTime, maxTime) // RFC3339
342```
343
344##### 自动分割键值到切片(slice)
345
346当存在无效输入时,使用零值代替:
347
348```go
349// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
350// Input: how, 2.2, are, you -> [0.0 2.2 0.0 0.0]
351vals = cfg.Section("").Key("STRINGS").Strings(",")
352vals = cfg.Section("").Key("FLOAT64S").Float64s(",")
353vals = cfg.Section("").Key("INTS").Ints(",")
354vals = cfg.Section("").Key("INT64S").Int64s(",")
355vals = cfg.Section("").Key("UINTS").Uints(",")
356vals = cfg.Section("").Key("UINT64S").Uint64s(",")
357vals = cfg.Section("").Key("TIMES").Times(",")
358```
359
360从结果切片中剔除无效输入:
361
362```go
363// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
364// Input: how, 2.2, are, you -> [2.2]
365vals = cfg.Section("").Key("FLOAT64S").ValidFloat64s(",")
366vals = cfg.Section("").Key("INTS").ValidInts(",")
367vals = cfg.Section("").Key("INT64S").ValidInt64s(",")
368vals = cfg.Section("").Key("UINTS").ValidUints(",")
369vals = cfg.Section("").Key("UINT64S").ValidUint64s(",")
370vals = cfg.Section("").Key("TIMES").ValidTimes(",")
371```
372
373当存在无效输入时,直接返回错误:
374
375```go
376// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
377// Input: how, 2.2, are, you -> error
378vals = cfg.Section("").Key("FLOAT64S").StrictFloat64s(",")
379vals = cfg.Section("").Key("INTS").StrictInts(",")
380vals = cfg.Section("").Key("INT64S").StrictInt64s(",")
381vals = cfg.Section("").Key("UINTS").StrictUints(",")
382vals = cfg.Section("").Key("UINT64S").StrictUint64s(",")
383vals = cfg.Section("").Key("TIMES").StrictTimes(",")
384```
385
386### 保存配置
387
388终于到了这个时刻,是时候保存一下配置了。
389
390比较原始的做法是输出配置到某个文件:
391
392```go
393// ...
394err = cfg.SaveTo("my.ini")
395err = cfg.SaveToIndent("my.ini", "\t")
396```
397
398另一个比较高级的做法是写入到任何实现 `io.Writer` 接口的对象中:
399
400```go
401// ...
402cfg.WriteTo(writer)
403cfg.WriteToIndent(writer, "\t")
404```
405
406默认情况下,空格将被用于对齐键值之间的等号以美化输出结果,以下代码可以禁用该功能:
407
408```go
409ini.PrettyFormat = false
410```
411
412## 高级用法
413
414### 递归读取键值
415
416在获取所有键值的过程中,特殊语法 `%(<name>)s` 会被应用,其中 `<name>` 可以是相同分区或者默认分区下的键名。字符串 `%(<name>)s` 会被相应的键值所替代,如果指定的键不存在,则会用空字符串替代。您可以最多使用 99 层的递归嵌套。
417
418```ini
419NAME = ini
420
421[author]
422NAME = Unknwon
423GITHUB = https://github.com/%(NAME)s
424
425[package]
426FULL_NAME = github.com/go-ini/%(NAME)s
427```
428
429```go
430cfg.Section("author").Key("GITHUB").String() // https://github.com/Unknwon
431cfg.Section("package").Key("FULL_NAME").String() // github.com/go-ini/ini
432```
433
434### 读取父子分区
435
436您可以在分区名称中使用 `.` 来表示两个或多个分区之间的父子关系。如果某个键在子分区中不存在,则会去它的父分区中再次寻找,直到没有父分区为止。
437
438```ini
439NAME = ini
440VERSION = v1
441IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
442
443[package]
444CLONE_URL = https://%(IMPORT_PATH)s
445
446[package.sub]
447```
448
449```go
450cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1
451```
452
453#### 获取上级父分区下的所有键名
454
455```go
456cfg.Section("package.sub").ParentKeys() // ["CLONE_URL"]
457```
458
459### 无法解析的分区
460
461如果遇到一些比较特殊的分区,它们不包含常见的键值对,而是没有固定格式的纯文本,则可以使用 `LoadOptions.UnparsableSections` 进行处理:
462
463```go
464cfg, err := LoadSources(LoadOptions{UnparseableSections: []string{"COMMENTS"}}, `[COMMENTS]
465<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
466
467body := cfg.Section("COMMENTS").Body()
468
469/* --- start ---
470<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
471------ end --- */
472```
473
474### 读取自增键名
475
476如果数据源中的键名为 `-`,则认为该键使用了自增键名的特殊语法。计数器从 1 开始,并且分区之间是相互独立的。
477
478```ini
479[features]
480-: Support read/write comments of keys and sections
481-: Support auto-increment of key names
482-: Support load multiple files to overwrite key values
483```
484
485```go
486cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"}
487```
488
489### 映射到结构
490
491想要使用更加面向对象的方式玩转 INI 吗?好主意。
492
493```ini
494Name = Unknwon
495age = 21
496Male = true
497Born = 1993-01-01T20:17:05Z
498
499[Note]
500Content = Hi is a good man!
501Cities = HangZhou, Boston
502```
503
504```go
505type Note struct {
506 Content string
507 Cities []string
508}
509
510type Person struct {
511 Name string
512 Age int `ini:"age"`
513 Male bool
514 Born time.Time
515 Note
516 Created time.Time `ini:"-"`
517}
518
519func main() {
520 cfg, err := ini.Load("path/to/ini")
521 // ...
522 p := new(Person)
523 err = cfg.MapTo(p)
524 // ...
525
526 // 一切竟可以如此的简单。
527 err = ini.MapTo(p, "path/to/ini")
528 // ...
529
530 // 嗯哼?只需要映射一个分区吗?
531 n := new(Note)
532 err = cfg.Section("Note").MapTo(n)
533 // ...
534}
535```
536
537结构的字段怎么设置默认值呢?很简单,只要在映射之前对指定字段进行赋值就可以了。如果键未找到或者类型错误,该值不会发生改变。
538
539```go
540// ...
541p := &Person{
542 Name: "Joe",
543}
544// ...
545```
546
547这样玩 INI 真的好酷啊!然而,如果不能还给我原来的配置文件,有什么卵用?
548
549### 从结构反射
550
551可是,我有说不能吗?
552
553```go
554type Embeded struct {
555 Dates []time.Time `delim:"|"`
556 Places []string `ini:"places,omitempty"`
557 None []int `ini:",omitempty"`
558}
559
560type Author struct {
561 Name string `ini:"NAME"`
562 Male bool
563 Age int
564 GPA float64
565 NeverMind string `ini:"-"`
566 *Embeded
567}
568
569func main() {
570 a := &Author{"Unknwon", true, 21, 2.8, "",
571 &Embeded{
572 []time.Time{time.Now(), time.Now()},
573 []string{"HangZhou", "Boston"},
574 []int{},
575 }}
576 cfg := ini.Empty()
577 err = ini.ReflectFrom(cfg, a)
578 // ...
579}
580```
581
582瞧瞧,奇迹发生了。
583
584```ini
585NAME = Unknwon
586Male = true
587Age = 21
588GPA = 2.8
589
590[Embeded]
591Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
592places = HangZhou,Boston
593```
594
595#### 名称映射器(Name Mapper)
596
597为了节省您的时间并简化代码,本库支持类型为 [`NameMapper`](https://gowalker.org/gopkg.in/ini.v1#NameMapper) 的名称映射器,该映射器负责结构字段名与分区名和键名之间的映射。
598
599目前有 2 款内置的映射器:
600
601- `AllCapsUnderscore`:该映射器将字段名转换至格式 `ALL_CAPS_UNDERSCORE` 后再去匹配分区名和键名。
602- `TitleUnderscore`:该映射器将字段名转换至格式 `title_underscore` 后再去匹配分区名和键名。
603
604使用方法:
605
606```go
607type Info struct{
608 PackageName string
609}
610
611func main() {
612 err = ini.MapToWithMapper(&Info{}, ini.TitleUnderscore, []byte("package_name=ini"))
613 // ...
614
615 cfg, err := ini.Load([]byte("PACKAGE_NAME=ini"))
616 // ...
617 info := new(Info)
618 cfg.NameMapper = ini.AllCapsUnderscore
619 err = cfg.MapTo(info)
620 // ...
621}
622```
623
624使用函数 `ini.ReflectFromWithMapper` 时也可应用相同的规则。
625
626#### 值映射器(Value Mapper)
627
628值映射器允许使用一个自定义函数自动展开值的具体内容,例如:运行时获取环境变量:
629
630```go
631type Env struct {
632 Foo string `ini:"foo"`
633}
634
635func main() {
636 cfg, err := ini.Load([]byte("[env]\nfoo = ${MY_VAR}\n")
637 cfg.ValueMapper = os.ExpandEnv
638 // ...
639 env := &Env{}
640 err = cfg.Section("env").MapTo(env)
641}
642```
643
644本例中,`env.Foo` 将会是运行时所获取到环境变量 `MY_VAR` 的值。
645
646#### 映射/反射的其它说明
647
648任何嵌入的结构都会被默认认作一个不同的分区,并且不会自动产生所谓的父子分区关联:
649
650```go
651type Child struct {
652 Age string
653}
654
655type Parent struct {
656 Name string
657 Child
658}
659
660type Config struct {
661 City string
662 Parent
663}
664```
665
666示例配置文件:
667
668```ini
669City = Boston
670
671[Parent]
672Name = Unknwon
673
674[Child]
675Age = 21
676```
677
678很好,但是,我就是要嵌入结构也在同一个分区。好吧,你爹是李刚!
679
680```go
681type Child struct {
682 Age string
683}
684
685type Parent struct {
686 Name string
687 Child `ini:"Parent"`
688}
689
690type Config struct {
691 City string
692 Parent
693}
694```
695
696示例配置文件:
697
698```ini
699City = Boston
700
701[Parent]
702Name = Unknwon
703Age = 21
704```
705
706## 获取帮助
707
708- [API 文档](https://gowalker.org/gopkg.in/ini.v1)
709- [创建工单](https://github.com/go-ini/ini/issues/new)
710
711## 常见问题
712
713### 字段 `BlockMode` 是什么?
714
715默认情况下,本库会在您进行读写操作时采用锁机制来确保数据时间。但在某些情况下,您非常确定只进行读操作。此时,您可以通过设置 `cfg.BlockMode = false` 来将读操作提升大约 **50-70%** 的性能。
716
717### 为什么要写另一个 INI 解析库?
718
719许多人都在使用我的 [goconfig](https://github.com/Unknwon/goconfig) 来完成对 INI 文件的操作,但我希望使用更加 Go 风格的代码。并且当您设置 `cfg.BlockMode = false` 时,会有大约 **10-30%** 的性能提升。
720
721为了做出这些改变,我必须对 API 进行破坏,所以新开一个仓库是最安全的做法。除此之外,本库直接使用 `gopkg.in` 来进行版本化发布。(其实真相是导入路径更短了)