github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/resource.go (about) 1 package kafka 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/ResourceType.java 9 type ResourceType int8 10 11 const ( 12 ResourceTypeUnknown ResourceType = 0 13 ResourceTypeAny ResourceType = 1 14 ResourceTypeTopic ResourceType = 2 15 ResourceTypeGroup ResourceType = 3 16 // See https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/config/ConfigResource.java#L36 17 ResourceTypeBroker ResourceType = 4 18 ResourceTypeCluster ResourceType = 4 19 ResourceTypeTransactionalID ResourceType = 5 20 ResourceTypeDelegationToken ResourceType = 6 21 ) 22 23 func (rt ResourceType) String() string { 24 mapping := map[ResourceType]string{ 25 ResourceTypeUnknown: "Unknown", 26 ResourceTypeAny: "Any", 27 ResourceTypeTopic: "Topic", 28 ResourceTypeGroup: "Group", 29 // Note that ResourceTypeBroker and ResourceTypeCluster have the same value. 30 // A map cannot have duplicate values so we just use the same value for both. 31 ResourceTypeCluster: "Cluster", 32 ResourceTypeTransactionalID: "Transactionalid", 33 ResourceTypeDelegationToken: "Delegationtoken", 34 } 35 s, ok := mapping[rt] 36 if !ok { 37 s = mapping[ResourceTypeUnknown] 38 } 39 return s 40 } 41 42 func (rt ResourceType) MarshalText() ([]byte, error) { 43 return []byte(rt.String()), nil 44 } 45 46 func (rt *ResourceType) UnmarshalText(text []byte) error { 47 normalized := strings.ToLower(string(text)) 48 mapping := map[string]ResourceType{ 49 "unknown": ResourceTypeUnknown, 50 "any": ResourceTypeAny, 51 "topic": ResourceTypeTopic, 52 "group": ResourceTypeGroup, 53 "broker": ResourceTypeBroker, 54 "cluster": ResourceTypeCluster, 55 "transactionalid": ResourceTypeTransactionalID, 56 "delegationtoken": ResourceTypeDelegationToken, 57 } 58 parsed, ok := mapping[normalized] 59 if !ok { 60 *rt = ResourceTypeUnknown 61 return fmt.Errorf("cannot parse %s as a ResourceType", normalized) 62 } 63 *rt = parsed 64 return nil 65 } 66 67 // https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/PatternType.java 68 type PatternType int8 69 70 const ( 71 // PatternTypeUnknown represents any PatternType which this client cannot 72 // understand. 73 PatternTypeUnknown PatternType = 0 74 // PatternTypeAny matches any resource pattern type. 75 PatternTypeAny PatternType = 1 76 // PatternTypeMatch perform pattern matching. 77 PatternTypeMatch PatternType = 2 78 // PatternTypeLiteral represents a literal name. 79 // A literal name defines the full name of a resource, e.g. topic with name 80 // 'foo', or group with name 'bob'. 81 PatternTypeLiteral PatternType = 3 82 // PatternTypePrefixed represents a prefixed name. 83 // A prefixed name defines a prefix for a resource, e.g. topics with names 84 // that start with 'foo'. 85 PatternTypePrefixed PatternType = 4 86 ) 87 88 func (pt PatternType) String() string { 89 mapping := map[PatternType]string{ 90 PatternTypeUnknown: "Unknown", 91 PatternTypeAny: "Any", 92 PatternTypeMatch: "Match", 93 PatternTypeLiteral: "Literal", 94 PatternTypePrefixed: "Prefixed", 95 } 96 s, ok := mapping[pt] 97 if !ok { 98 s = mapping[PatternTypeUnknown] 99 } 100 return s 101 } 102 103 func (pt PatternType) MarshalText() ([]byte, error) { 104 return []byte(pt.String()), nil 105 } 106 107 func (pt *PatternType) UnmarshalText(text []byte) error { 108 normalized := strings.ToLower(string(text)) 109 mapping := map[string]PatternType{ 110 "unknown": PatternTypeUnknown, 111 "any": PatternTypeAny, 112 "match": PatternTypeMatch, 113 "literal": PatternTypeLiteral, 114 "prefixed": PatternTypePrefixed, 115 } 116 parsed, ok := mapping[normalized] 117 if !ok { 118 *pt = PatternTypeUnknown 119 return fmt.Errorf("cannot parse %s as a PatternType", normalized) 120 } 121 *pt = parsed 122 return nil 123 }