-
Notifications
You must be signed in to change notification settings - Fork 4
/
keyval.go
63 lines (51 loc) · 1.32 KB
/
keyval.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
// Content managed by Project Forge, see [projectforge.md] for details.
package util
import (
"fmt"
"strings"
"golang.org/x/exp/slices"
)
type KeyValInt struct {
Key string `json:"key" db:"key"`
Count int `json:"val" db:"val"`
}
func (k KeyValInt) String() string {
return fmt.Sprintf("%s: %d", k.Key, k.Count)
}
type KeyValInts []*KeyValInt
func (k KeyValInts) ToMap() map[string]int {
ret := make(map[string]int, len(k))
for _, x := range k {
ret[x.Key] = x.Count
}
return ret
}
func (k KeyValInts) String() string {
ret := make([]string, 0, len(k))
for _, x := range k {
ret = append(ret, x.String())
}
return strings.Join(ret, ", ")
}
type KeyTypeDesc struct {
Key string `json:"key"`
Type string `json:"type"`
Description string `json:"description"`
}
func (k *KeyTypeDesc) Array(key string) []string {
return []string{strings.ReplaceAll("`"+k.Key+"`", "{key}", key), k.Type, strings.ReplaceAll(k.Description, "{key}", key)}
}
type KeyTypeDescs []*KeyTypeDesc
func (k KeyTypeDescs) Sort() {
slices.SortFunc(k, func(l *KeyTypeDesc, r *KeyTypeDesc) bool {
return strings.ToLower(l.Key) < strings.ToLower(r.Key)
})
}
func (k KeyTypeDescs) Array(key string) [][]string {
k.Sort()
ret := make([][]string, 0, len(k))
for _, x := range k {
ret = append(ret, x.Array(key))
}
return ret
}