-
Notifications
You must be signed in to change notification settings - Fork 14
/
sync.go
73 lines (58 loc) · 1.44 KB
/
sync.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
package eval
import (
"net/http"
"sync"
"github.com/zclconf/go-cty/cty"
"github.com/avenga/couper/config/request"
)
const TokenRequestPrefix = "_tr_"
type SyncedVariables struct {
items sync.Map
}
func NewSyncedVariables() *SyncedVariables {
return &SyncedVariables{
items: sync.Map{},
}
}
type syncPair struct {
name string
bereq, beresp cty.Value
tokenRequest bool
}
// Set finalized cty req/resp pair.
func (sv *SyncedVariables) Set(beresp *http.Response) {
ctx := beresp.Request.Context()
name, bereqV, berespV := newBerespValues(ctx, true, beresp)
if tr, ok := ctx.Value(request.TokenRequest).(string); ok && tr != "" {
name = TokenRequestPrefix + name
}
sv.items.Store(name, &syncPair{
name: name,
bereq: bereqV,
beresp: berespV,
})
}
func (sv *SyncedVariables) Sync(variables map[string]cty.Value) {
var bereqs, beresps map[string]cty.Value
if brs, ok := variables[BackendRequests]; ok {
bereqs = brs.AsValueMap()
}
if brps, ok := variables[BackendResponses]; ok {
beresps = brps.AsValueMap()
}
sv.items.Range(func(key, value interface{}) bool {
p := value.(*syncPair)
if bereqs == nil {
bereqs = make(map[string]cty.Value)
}
name := key.(string)
bereqs[name] = p.bereq
if beresps == nil {
beresps = make(map[string]cty.Value)
}
beresps[name] = p.beresp
return true
})
variables[BackendRequests] = cty.ObjectVal(bereqs)
variables[BackendResponses] = cty.ObjectVal(beresps)
}