Skip to content

Commit

Permalink
Merge pull request #1541 from izolight/marshalText
Browse files Browse the repository at this point in the history
add String, (Un)MarshalText for acl types.
  • Loading branch information
bai authored Mar 18, 2021
2 parents f8d4ccd + ae1b8f0 commit 41df78d
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
180 changes: 180 additions & 0 deletions acl_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package sarama

import (
"fmt"
"strings"
)

type (
AclOperation int

Expand Down Expand Up @@ -27,6 +32,61 @@ const (
AclOperationIdempotentWrite
)

func (a *AclOperation) String() string {
mapping := map[AclOperation]string{
AclOperationUnknown: "Unknown",
AclOperationAny: "Any",
AclOperationAll: "All",
AclOperationRead: "Read",
AclOperationWrite: "Write",
AclOperationCreate: "Create",
AclOperationDelete: "Delete",
AclOperationAlter: "Alter",
AclOperationDescribe: "Describe",
AclOperationClusterAction: "ClusterAction",
AclOperationDescribeConfigs: "DescribeConfigs",
AclOperationAlterConfigs: "AlterConfigs",
AclOperationIdempotentWrite: "IdempotentWrite",
}
s, ok := mapping[*a]
if !ok {
s = mapping[AclOperationUnknown]
}
return s
}

//MarshalText returns the text form of the AclOperation (name without prefix)
func (a *AclOperation) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

//UnmarshalText takes a text reprentation of the operation and converts it to an AclOperation
func (a *AclOperation) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]AclOperation{
"unknown": AclOperationUnknown,
"any": AclOperationAny,
"all": AclOperationAll,
"read": AclOperationRead,
"write": AclOperationWrite,
"create": AclOperationCreate,
"delete": AclOperationDelete,
"alter": AclOperationAlter,
"describe": AclOperationDescribe,
"clusteraction": AclOperationClusterAction,
"describeconfigs": AclOperationDescribeConfigs,
"alterconfigs": AclOperationAlterConfigs,
"idempotentwrite": AclOperationIdempotentWrite,
}
ao, ok := mapping[normalized]
if !ok {
*a = AclOperationUnknown
return fmt.Errorf("no acl operation with name %s", normalized)
}
*a = ao
return nil
}

// ref: https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/acl/AclPermissionType.java
const (
AclPermissionUnknown AclPermissionType = iota
Expand All @@ -35,6 +95,44 @@ const (
AclPermissionAllow
)

func (a *AclPermissionType) String() string {
mapping := map[AclPermissionType]string{
AclPermissionUnknown: "Unknown",
AclPermissionAny: "Any",
AclPermissionDeny: "Deny",
AclPermissionAllow: "Allow",
}
s, ok := mapping[*a]
if !ok {
s = mapping[AclPermissionUnknown]
}
return s
}

//MarshalText returns the text form of the AclPermissionType (name without prefix)
func (a *AclPermissionType) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

//UnmarshalText takes a text reprentation of the permission type and converts it to an AclPermissionType
func (a *AclPermissionType) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]AclPermissionType{
"unknown": AclPermissionUnknown,
"any": AclPermissionAny,
"deny": AclPermissionDeny,
"allow": AclPermissionAllow,
}

apt, ok := mapping[normalized]
if !ok {
*a = AclPermissionUnknown
return fmt.Errorf("no acl permission with name %s", normalized)
}
*a = apt
return nil
}

// ref: https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/ResourceType.java
const (
AclResourceUnknown AclResourceType = iota
Expand All @@ -45,6 +143,48 @@ const (
AclResourceTransactionalID
)

func (a *AclResourceType) String() string {
mapping := map[AclResourceType]string{
AclResourceUnknown: "Unknown",
AclResourceAny: "Any",
AclResourceTopic: "Topic",
AclResourceGroup: "Group",
AclResourceCluster: "Cluster",
AclResourceTransactionalID: "TransactionalID",
}
s, ok := mapping[*a]
if !ok {
s = mapping[AclResourceUnknown]
}
return s
}

//MarshalText returns the text form of the AclResourceType (name without prefix)
func (a *AclResourceType) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

//UnmarshalText takes a text reprentation of the resource type and converts it to an AclResourceType
func (a *AclResourceType) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]AclResourceType{
"unknown": AclResourceUnknown,
"any": AclResourceAny,
"topic": AclResourceTopic,
"group": AclResourceGroup,
"cluster": AclResourceCluster,
"transactionalid": AclResourceTransactionalID,
}

art, ok := mapping[normalized]
if !ok {
*a = AclResourceUnknown
return fmt.Errorf("no acl resource with name %s", normalized)
}
*a = art
return nil
}

// ref: https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/resource/PatternType.java
const (
AclPatternUnknown AclResourcePatternType = iota
Expand All @@ -53,3 +193,43 @@ const (
AclPatternLiteral
AclPatternPrefixed
)

func (a *AclResourcePatternType) String() string {
mapping := map[AclResourcePatternType]string{
AclPatternUnknown: "Unknown",
AclPatternAny: "Any",
AclPatternMatch: "Match",
AclPatternLiteral: "Literal",
AclPatternPrefixed: "Prefixed",
}
s, ok := mapping[*a]
if !ok {
s = mapping[AclPatternUnknown]
}
return s
}

//MarshalText returns the text form of the AclResourcePatternType (name without prefix)
func (a *AclResourcePatternType) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

//UnmarshalText takes a text reprentation of the resource pattern type and converts it to an AclResourcePatternType
func (a *AclResourcePatternType) UnmarshalText(text []byte) error {
normalized := strings.ToLower(string(text))
mapping := map[string]AclResourcePatternType{
"unknown": AclPatternUnknown,
"any": AclPatternAny,
"match": AclPatternMatch,
"literal": AclPatternLiteral,
"prefixed": AclPatternPrefixed,
}

arpt, ok := mapping[normalized]
if !ok {
*a = AclPatternUnknown
return fmt.Errorf("no acl resource pattern with name %s", normalized)
}
*a = arpt
return nil
}
71 changes: 71 additions & 0 deletions acl_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package sarama

import (
"testing"
)

func TestAclOperationTextMarshal(t *testing.T) {
for i := AclOperationUnknown; i <= AclOperationIdempotentWrite; i++ {
text, err := i.MarshalText()
if err != nil {
t.Errorf("couldn't marshal %d to text: %s", i, err)
}
var got AclOperation
err = got.UnmarshalText(text)
if err != nil {
t.Errorf("couldn't unmarshal %s to acl operation: %s", text, err)
}
if got != i {
t.Errorf("got %d, want %d", got, i)
}
}
}

func TestAclPermissionTypeTextMarshal(t *testing.T) {
for i := AclPermissionUnknown; i <= AclPermissionAllow; i++ {
text, err := i.MarshalText()
if err != nil {
t.Errorf("couldn't marshal %d to text: %s", i, err)
}
var got AclPermissionType
err = got.UnmarshalText(text)
if err != nil {
t.Errorf("couldn't unmarshal %s to acl permission: %s", text, err)
}
if got != i {
t.Errorf("got %d, want %d", got, i)
}
}
}
func TestAclResourceTypeTextMarshal(t *testing.T) {
for i := AclResourceUnknown; i <= AclResourceTransactionalID; i++ {
text, err := i.MarshalText()
if err != nil {
t.Errorf("couldn't marshal %d to text: %s", i, err)
}
var got AclResourceType
err = got.UnmarshalText(text)
if err != nil {
t.Errorf("couldn't unmarshal %s to acl resource: %s", text, err)
}
if got != i {
t.Errorf("got %d, want %d", got, i)
}
}
}
func TestAclResourcePatternTypeTextMarshal(t *testing.T) {
for i := AclPatternUnknown; i <= AclPatternPrefixed; i++ {
text, err := i.MarshalText()
if err != nil {
t.Errorf("couldn't marshal %d to text: %s", i, err)
}
var got AclResourcePatternType
err = got.UnmarshalText(text)
if err != nil {
t.Errorf("couldn't unmarshal %s to acl resource pattern: %s", text, err)
}
if got != i {
t.Errorf("got %d, want %d", got, i)
}
}
}

0 comments on commit 41df78d

Please sign in to comment.