-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.go
More file actions
103 lines (80 loc) · 2.99 KB
/
Copy pathresolver.go
File metadata and controls
103 lines (80 loc) · 2.99 KB
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
package client
func checkLocalCriteria(snapshot *Snapshot, switcher *Switcher) (ResultDetail, error) {
if snapshot == nil {
return ResultDetail{}, newLocalCriteriaError("Snapshot not loaded. Try to use 'Client.load_snapshot()'")
}
return checkLocalDomain(snapshot, switcher)
}
func checkLocalDomain(snapshot *Snapshot, switcher *Switcher) (ResultDetail, error) {
if !snapshot.Domain.Activated {
return ResultDetail{Result: false, Reason: "Domain is disabled", Metadata: map[string]any{}}, nil
}
return checkLocalGroup(snapshot.Domain.Groups, switcher)
}
func checkLocalGroup(groups []SnapshotGroup, switcher *Switcher) (ResultDetail, error) {
key := switcher.key
for _, group := range groups {
for _, config := range group.Configs {
if config.Key != key {
continue
}
if !group.Activated {
return ResultDetail{Result: false, Reason: "Group disabled", Metadata: map[string]any{}}, nil
}
return checkLocalConfig(config, switcher)
}
}
return ResultDetail{}, newLocalCriteriaError("Config with key '%s' not found in the snapshot", switcher.key)
}
func checkLocalConfig(config SnapshotConfig, switcher *Switcher) (ResultDetail, error) {
if !config.Activated {
return ResultDetail{Result: false, Reason: "Config disabled", Metadata: map[string]any{}}, nil
}
if config.Relay != nil && config.Relay.Activated && switcher.client.Context().Options.RestrictRelay {
return ResultDetail{Result: false, Reason: "Config has relay enabled", Metadata: map[string]any{}}, nil
}
return checkLocalStrategies(config.Strategies, switcher.entries)
}
func checkLocalStrategies(strategies []SnapshotStrategy, entries []criteriaEntry) (ResultDetail, error) {
activeStrategies := 0
for _, strategy := range strategies {
if !strategy.Activated {
continue
}
activeStrategies++
if len(entries) == 0 {
return ResultDetail{Result: false, Reason: "Strategy '" + strategy.Strategy + "' did not receive any input", Metadata: map[string]any{}}, nil
}
entry, ok := findCriteriaEntry(entries, strategy.Strategy)
if !ok || !processLocalStrategy(strategy, entry.Input) {
return ResultDetail{Result: false, Reason: "Strategy '" + strategy.Strategy + "' does not agree", Metadata: map[string]any{}}, nil
}
}
return ResultDetail{Result: true, Reason: "Success", Metadata: map[string]any{}}, nil
}
func findCriteriaEntry(entries []criteriaEntry, strategy string) (criteriaEntry, bool) {
for _, entry := range entries {
if entry.Strategy == strategy {
return entry, true
}
}
return criteriaEntry{}, false
}
func checkLocalSwitchers(snapshot *Snapshot, switcherKeys []string) error {
if snapshot == nil {
return newLocalSwitcherError(switcherKeys)
}
found := make(map[string]struct{})
for _, group := range snapshot.Domain.Groups {
for _, config := range group.Configs {
found[config.Key] = struct{}{}
}
}
missing := make([]string, 0)
for _, key := range switcherKeys {
if _, ok := found[key]; !ok {
missing = append(missing, key)
}
}
return newLocalSwitcherError(missing)
}