forked from apache/rocketmq-client-go
/
namespace.go
45 lines (38 loc) · 1.15 KB
/
namespace.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
package utils
import "strings"
const namespaceSeparator = "%"
const retryPrefix = "%RETRY%"
const dlqPrefix = "%DLQ%"
func WrapNamespace(namespace, resourceWithOutNamespace string) string {
if IsEmpty(namespace) || IsEmpty(resourceWithOutNamespace) {
return resourceWithOutNamespace
}
if isAlreadyWithNamespace(resourceWithOutNamespace, namespace) {
return resourceWithOutNamespace
}
return namespace + namespaceSeparator + resourceWithOutNamespace
}
func isAlreadyWithNamespace(resource, namespace string) bool {
if IsEmpty(namespace) || IsEmpty(resource) {
return false
}
return strings.Contains(resource, namespace+namespaceSeparator)
}
func WithoutNamespace(resource string) string {
if len(resource) == 0 {
return resource
}
resourceWithoutNamespace := ""
if strings.HasPrefix(resource, retryPrefix) {
resourceWithoutNamespace += retryPrefix
} else if strings.HasPrefix(resource, dlqPrefix) {
resourceWithoutNamespace += dlqPrefix
}
index := strings.LastIndex(resource, namespaceSeparator)
if index > 0 {
resourceWithoutNamespace += resource[index+1:]
} else {
resourceWithoutNamespace = resource
}
return resourceWithoutNamespace
}