forked from cornelk/pulsar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
73 lines (65 loc) · 1.7 KB
/
topic.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 pulsar
import (
"errors"
"fmt"
"strings"
)
// ...
const (
publicTenant = "public"
DefaultNamespace = "default"
// TODO support partitioning partitionedTopicSuffix = "-partition-"
persistentDomain = "persistent"
nonPersistentDomain = "non-persistent"
domainSeparator = "://"
)
// Topic represents a Pulsar Topic.
type Topic struct {
Domain string
Tenant string
Namespace string
LocalName string
CompleteName string
}
// NewTopic creates a new topic struct from the given topic name.
// The topic name can be in short form or a fully qualified topic name.
func NewTopic(name string) (*Topic, error) {
if !strings.Contains(name, domainSeparator) {
// The short topic name can be:
// - <topic>
// - <property>/<namespace>/<topic>
parts := strings.Split(name, "/")
switch len(parts) {
case 3:
name = persistentDomain + domainSeparator +
name
case 1:
name = persistentDomain + domainSeparator +
publicTenant + "/" + DefaultNamespace + "/" + parts[0]
default:
return nil, errors.New("invalid topic short name format")
}
}
parts := strings.Split(name, domainSeparator)
if len(parts) != 2 {
return nil, errors.New("invalid topic domain format")
}
domain := parts[0]
if domain != persistentDomain && domain != nonPersistentDomain {
return nil, errors.New("invalid topic domain")
}
parts = strings.Split(parts[1], "/")
if len(parts) != 3 {
return nil, errors.New("invalid topic name format")
}
t := &Topic{
Domain: domain,
Tenant: parts[0],
Namespace: parts[1],
LocalName: parts[2],
CompleteName: "",
}
t.CompleteName = fmt.Sprintf("%s://%s/%s/%s", t.Domain, t.Tenant,
t.Namespace, t.LocalName)
return t, nil
}