-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
105 lines (98 loc) · 2 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
93
94
95
96
97
98
99
100
101
102
103
104
105
package menu
import (
"encoding/json"
"fmt"
"html/template"
"os"
)
func ExampleListItem_template() {
const snippet = `<ul>
{{- range .Items}}
<li>
{{- if .IsList}}
{{.Label}}<br>
<ol>
{{- range .Items}}
<li><a href="{{.Action}}">{{.Label}}</a></li>
{{- end}}
</ol>
{{- else}}
<a href="{{.Action}}">{{.Label}}</a>
{{- end}}
</li>
{{- end}}
</ul>`
m := NewList("Haha", []ListItem{
NewItem("About", "/about"),
NewList("Locations", []ListItem{
NewItem("Berlin", "/locations/Berlin"),
NewItem("Seoul", "/locations/Seoul"),
}),
NewItemMask("Admin", "/admin", 0x80),
NewItemMask("FAQ", "/faq", 0x01),
})
t := template.Must(template.New("snippet").Parse(snippet))
err := t.Execute(os.Stdout, m.Filtered(0x01))
if err != nil {
panic(err)
}
// Output:
// <ul>
// <li>
// <a href="/about">About</a>
// </li>
// <li>
// Locations<br>
// <ol>
// <li><a href="/locations/Berlin">Berlin</a></li>
// <li><a href="/locations/Seoul">Seoul</a></li>
// </ol>
// </li>
// <li>
// <a href="/faq">FAQ</a>
// </li>
// </ul>
}
func ExampleListItem_jSON() {
menu := NewList("", []ListItem{
NewItem("About", "/about"),
NewList("Locations", []ListItem{
NewItem("Berlin", "/locations/Berlin"),
NewItem("Seoul", "/locations/Seoul"),
}),
NewItemMask("Admin", "/admin", 0x80),
NewItemMask("FAQ", "/faq", 0x01),
})
menuJSON, err := json.MarshalIndent(menu.Filtered(0x80), "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(menuJSON))
// Output:
// {
// "label": "",
// "items": [
// {
// "label": "About",
// "action": "/about"
// },
// {
// "label": "Locations",
// "items": [
// {
// "label": "Berlin",
// "action": "/locations/Berlin"
// },
// {
// "label": "Seoul",
// "action": "/locations/Seoul"
// }
// ]
// },
// {
// "label": "Admin",
// "action": "/admin"
// }
// ]
// }
}