-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
92 lines (80 loc) · 1.52 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2022 Michael J. Fromberger. All Rights Reserved.
package tomledit_test
import (
"fmt"
"log"
"os"
"strings"
"github.com/creachadair/tomledit"
"github.com/creachadair/tomledit/parser"
)
func ExampleParse() {
doc, err := tomledit.Parse(strings.NewReader(`# Example config
verbose=true
# A commented section
[commented]
x = 3 # line comment
# a commented mapping
y = true
`))
if err != nil {
log.Fatalf("Parse: %v", err)
}
// Scan through the parsed document printing out all the keys defined in it,
// in their order of occurrence.
doc.Scan(func(key parser.Key, _ *tomledit.Entry) bool {
fmt.Println(key)
return true
})
// Output:
//
// verbose
// commented
// commented.x
// commented.y
}
func ExampleDocument_First() {
doc, err := tomledit.Parse(strings.NewReader(`[foo]
bar . baz = "frob"
# end
`))
if err != nil {
log.Fatalf("Parse: %v", err)
}
// Print the first item with the given key. The default string output for an
// item is for human consumption and is not valid TOML.
fmt.Println(doc.First("foo", "bar", "baz"))
// Output:
// [foo] :: bar.baz = "frob"
}
func ExampleFormatter() {
doc, err := tomledit.Parse(strings.NewReader(`# A
b='c'
[q."r"]
# D
e.f=true
g=false
# h
i={j=1,k=2} # L
`))
if err != nil {
log.Fatalf("Parse: %v", err)
}
if err := tomledit.Format(os.Stdout, doc); err != nil {
log.Fatalf("Format: %v", err)
}
// Output:
//
// # A
// b = 'c'
//
// [q.r]
//
// # D
// e.f = true
// g = false
//
// # h
// i = {j = 1, k = 2} # L
//
}