-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstatic.go
106 lines (94 loc) · 2.46 KB
/
static.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
106
package proxy
import (
"context"
"melody/config"
)
const (
staticKey = "static"
staticAlwaysStrategy = "always"
staticIfSuccessStrategy = "success"
staticIfErroredStrategy = "errored"
staticIfCompleteStrategy = "complete"
staticIfIncompleteStrategy = "incomplete"
)
type staticConfig struct {
Data map[string]interface{}
Strategy string
Match func(*Response, error) bool
}
// NewStaticDataMiddleware 定义了插入静态数据中间件
func NewStaticDataMiddleware(endpoint *config.EndpointConfig) Middleware {
v, ok := getStaticConfig(endpoint.ExtraConfig)
if !ok {
return EmptyMiddleware
}
return func(proxy ...Proxy) Proxy {
if len(proxy) > 1 {
panic(ErrTooManyProxies)
}
return func(ctx context.Context, request *Request) (response *Response, e error) {
result, err := proxy[0](ctx, request)
if !v.Match(result, err) {
return result, err
}
if result == nil {
result = &Response{Data: map[string]interface{}{}}
}
for k, v := range v.Data {
result.Data[k] = v
}
return result, err
}
}
}
func getStaticConfig(extraConfig config.ExtraConfig) (staticConfig, bool) {
v, ok := extraConfig[Namespace]
if !ok {
return staticConfig{}, ok
}
e, ok := v.(map[string]interface{})
if !ok {
return staticConfig{}, ok
}
v, ok = e[staticKey]
if !ok {
return staticConfig{}, ok
}
tmp, ok := v.(map[string]interface{})
if !ok {
return staticConfig{}, ok
}
data, ok := tmp["data"].(map[string]interface{})
if !ok {
return staticConfig{}, ok
}
name, ok := tmp["strategy"].(string)
if !ok {
name = staticAlwaysStrategy
}
cfg := staticConfig{
Data: data,
Strategy: name,
Match: staticAlwaysMatch,
}
switch name {
case staticAlwaysStrategy:
cfg.Match = staticAlwaysMatch
case staticIfSuccessStrategy:
cfg.Match = staticIfSuccessMatch
case staticIfErroredStrategy:
cfg.Match = staticIfErroredMatch
case staticIfCompleteStrategy:
cfg.Match = staticIfCompleteMatch
case staticIfIncompleteStrategy:
cfg.Match = staticIfIncompleteMatch
}
return cfg, true
}
func staticAlwaysMatch(_ *Response, _ error) bool { return true }
func staticIfSuccessMatch(_ *Response, err error) bool { return err == nil }
func staticIfErroredMatch(_ *Response, err error) bool { return err != nil }
func staticIfCompleteMatch(r *Response, err error) bool {
return err == nil && r != nil && r.IsComplete
}
func staticIfIncompleteMatch(r *Response, _ error) bool { return r == nil || !r.IsComplete }