-
Notifications
You must be signed in to change notification settings - Fork 188
/
key.go
65 lines (55 loc) · 1.52 KB
/
key.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
package counter
import (
"fmt"
"strings"
"github.com/eolinker/eosc"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)
var _ IKeyGenerator = (*keyGenerate)(nil)
type IKeyGenerator interface {
Key(ctx http_service.IHttpContext) string
Variables(ctx http_service.IHttpContext) (eosc.Untyped[string, string], bool)
}
func newKeyGenerate(key string) *keyGenerate {
key = strings.TrimSpace(key)
tmp := strings.Split(key, ":")
keys := make([]string, 0, len(tmp))
variables := make([]string, 0, len(tmp))
for _, t := range tmp {
t = strings.TrimSpace(t)
tLen := len(t)
if tLen > 0 {
if tLen > 1 && t[0] == '$' {
variables = append(variables, t[1:])
keys = append(keys, "%s")
} else {
keys = append(keys, t)
}
}
}
return &keyGenerate{format: strings.Join(keys, ":"), variables: variables}
}
type keyGenerate struct {
format string
// 变量列表
variables []string
}
func (k *keyGenerate) Variables(ctx http_service.IHttpContext) (eosc.Untyped[string, string], bool) {
variables := eosc.BuildUntyped[string, string]()
entry := ctx.GetEntry()
for _, v := range k.variables {
value := eosc.ReadStringFromEntry(entry, v)
if value == "" {
return nil, false
}
variables.Set(fmt.Sprintf("$%s", v), value)
}
return variables, true
}
func (k *keyGenerate) Key(ctx http_service.IHttpContext) string {
variables := make([]interface{}, 0, len(k.variables))
for _, v := range k.variables {
variables = append(variables, ctx.GetLabel(v))
}
return fmt.Sprintf(k.format, variables...)
}