forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
123 lines (112 loc) · 3.65 KB
/
resource.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package kafka
import (
"fmt"
"strings"
)
// https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/ResourceType.java
type ResourceType int8
const (
ResourceTypeUnknown ResourceType = 0
ResourceTypeAny ResourceType = 1
ResourceTypeTopic ResourceType = 2
ResourceTypeGroup ResourceType = 3
// See https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/config/ConfigResource.java#L36
ResourceTypeBroker ResourceType = 4
ResourceTypeCluster ResourceType = 4
ResourceTypeTransactionalID ResourceType = 5
ResourceTypeDelegationToken ResourceType = 6
)
func (rt ResourceType) String() string {
mapping := map[ResourceType]string{
ResourceTypeUnknown: "Unknown",
ResourceTypeAny: "Any",
ResourceTypeTopic: "Topic",
ResourceTypeGroup: "Group",
// Note that ResourceTypeBroker and ResourceTypeCluster have the same value.
// A map cannot have duplicate values so we just use the same value for both.
ResourceTypeCluster: "Cluster",
ResourceTypeTransactionalID: "Transactionalid",
ResourceTypeDelegationToken: "Delegationtoken",
}
s, ok := mapping[rt]
if !ok {
s = mapping[ResourceTypeUnknown]
}
return s
}
func (rt ResourceType) MarshalText() ([]byte, error) {
return []byte(rt.String()), nil
}
func (rt *ResourceType) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]ResourceType{
"unknown": ResourceTypeUnknown,
"any": ResourceTypeAny,
"topic": ResourceTypeTopic,
"group": ResourceTypeGroup,
"broker": ResourceTypeBroker,
"cluster": ResourceTypeCluster,
"transactionalid": ResourceTypeTransactionalID,
"delegationtoken": ResourceTypeDelegationToken,
}
parsed, ok := mapping[normalized]
if !ok {
*rt = ResourceTypeUnknown
return fmt.Errorf("cannot parse %s as a ResourceType", normalized)
}
*rt = parsed
return nil
}
// https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/PatternType.java
type PatternType int8
const (
// PatternTypeUnknown represents any PatternType which this client cannot
// understand.
PatternTypeUnknown PatternType = 0
// PatternTypeAny matches any resource pattern type.
PatternTypeAny PatternType = 1
// PatternTypeMatch perform pattern matching.
PatternTypeMatch PatternType = 2
// PatternTypeLiteral represents a literal name.
// A literal name defines the full name of a resource, e.g. topic with name
// 'foo', or group with name 'bob'.
PatternTypeLiteral PatternType = 3
// PatternTypePrefixed represents a prefixed name.
// A prefixed name defines a prefix for a resource, e.g. topics with names
// that start with 'foo'.
PatternTypePrefixed PatternType = 4
)
func (pt PatternType) String() string {
mapping := map[PatternType]string{
PatternTypeUnknown: "Unknown",
PatternTypeAny: "Any",
PatternTypeMatch: "Match",
PatternTypeLiteral: "Literal",
PatternTypePrefixed: "Prefixed",
}
s, ok := mapping[pt]
if !ok {
s = mapping[PatternTypeUnknown]
}
return s
}
func (pt PatternType) MarshalText() ([]byte, error) {
return []byte(pt.String()), nil
}
func (pt *PatternType) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]PatternType{
"unknown": PatternTypeUnknown,
"any": PatternTypeAny,
"match": PatternTypeMatch,
"literal": PatternTypeLiteral,
"prefixed": PatternTypePrefixed,
}
parsed, ok := mapping[normalized]
if !ok {
*pt = PatternTypeUnknown
return fmt.Errorf("cannot parse %s as a PatternType", normalized)
}
*pt = parsed
return nil
}