-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
69 lines (62 loc) · 1.38 KB
/
funcs.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
/*
* Copyright (c) 2023 Mikhail Knyazhev <markus621@gmail.com>. All rights reserved.
* Use of this source code is governed by a BSD-3-Clause license that can be found in the LICENSE file.
*/
package tmpl
import (
"strings"
"text/template"
)
func TemplateFuncMap(a Action, s State) template.FuncMap {
return template.FuncMap{
"key": KeyFunc(a, s),
"key_or_default": KeyOrDefaultFunc(a, s),
"tree": TreeFunc(a, s),
}
}
func KeyFunc(a Action, s State) func(string) string {
return func(key string) string {
if !s.IsPrepared() {
a.Query(key)
return ""
}
if value := a.Keys().Get(key); value != nil {
return *value
}
return ""
}
}
func KeyOrDefaultFunc(a Action, s State) func(string, string) string {
return func(key string, def string) string {
if !s.IsPrepared() {
a.Query(key)
return def
}
if value := a.Keys().Get(key); value != nil {
return *value
}
return def
}
}
type TreeModel struct {
Key string
Value string
}
func TreeFunc(a Action, s State) func(string) []TreeModel {
return func(prefix string) []TreeModel {
data := make([]TreeModel, 0, 10)
if !s.IsPrepared() {
a.Query(prefix)
return data
}
a.Keys().Each(func(key string, value *string) {
if strings.HasPrefix(key, prefix) && value != nil {
data = append(data, TreeModel{
Key: key,
Value: *value,
})
}
})
return data
}
}