-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
scopes.go
93 lines (78 loc) · 2.2 KB
/
scopes.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
package scopes
import (
"strings"
)
const (
SubscriptionScopes = "subscriptionScopes"
PublishingScopes = "publishingScopes"
AllowedTopics = "allowedTopics"
ProtectedTopics = "protectedTopics"
appsSeparator = ";"
appSeparator = "="
topicSeparator = ","
)
func findParamInMetadata(param string, metadata map[string]string) (string, bool) {
param = strings.ToLower(param)
for name, val := range metadata {
name = strings.ToLower(name)
if param == name {
return val, true
}
}
return "", false
}
// GetScopedTopics returns a list of scoped topics for a given application from a Pub/Sub
// Component properties.
func GetScopedTopics(scope, appID string, metadata map[string]string) []string {
var (
existM = map[string]struct{}{}
topics = []string{}
)
if val, ok := findParamInMetadata(scope, metadata); ok && val != "" {
val = strings.ReplaceAll(val, " ", "")
apps := strings.Split(val, appsSeparator)
for _, a := range apps {
appTopics := strings.Split(a, appSeparator)
if len(appTopics) < 2 {
continue
}
app := appTopics[0]
if app != appID {
continue
}
tempTopics := strings.Split(appTopics[1], topicSeparator)
for _, tempTopic := range tempTopics {
if _, ok = existM[tempTopic]; !ok {
existM[tempTopic] = struct{}{}
topics = append(topics, tempTopic)
}
}
}
}
return topics
}
func getParamTopics(param string, metadata map[string]string) []string {
var (
existM = map[string]struct{}{}
topics = []string{}
)
if val, ok := findParamInMetadata(param, metadata); ok && val != "" {
val = strings.ReplaceAll(val, " ", "")
tempTopics := strings.Split(val, topicSeparator)
for _, tempTopic := range tempTopics {
if _, ok = existM[tempTopic]; !ok {
existM[tempTopic] = struct{}{}
topics = append(topics, tempTopic)
}
}
}
return topics
}
// GetAllowedTopics return the all topics list of params allowedTopics.
func GetAllowedTopics(metadata map[string]string) []string {
return getParamTopics(AllowedTopics, metadata)
}
// GetProtectedTopics returns all topics of param protectedTopics.
func GetProtectedTopics(metadata map[string]string) []string {
return getParamTopics(ProtectedTopics, metadata)
}