Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sarama.CreateAclsRequest: Ignoring TextMarshaler & TextUnmarshaler for members #2888

Open
Go-Giter opened this issue May 4, 2024 · 1 comment

Comments

@Go-Giter
Copy link

Go-Giter commented May 4, 2024

Description

I have a need to marshal / unmarshal the contents of an &sarama.CreateAclsRequest. Unfortunately when marshaling a populated struct the fields permissiontype, resourcetype, resourcepatterntype, & operation come out as integers despite implementing encoding.TextMarshaler & encoding.TextUnmarshaler. The integer values aren't very meaningful to a human.

Versions

| Sarama | Kafka | Go |
| v1.43.2 | N/A | 1.22.2 |

Configuration
package main

import (
	"fmt"

	"github.com/IBM/sarama"

	"gopkg.in/yaml.v3"
)

func main() {
	car := []*sarama.CreateAclsRequest{
		{
			Version: int16(3),
			AclCreations: []*sarama.AclCreation{
				{
					Resource: sarama.Resource{
						ResourceType:        sarama.AclResourceTopic,
						ResourceName:        "test.topic",
						ResourcePatternType: sarama.AclPatternLiteral,
					},
					Acl: sarama.Acl{
						Principal:      "test-user",
						Host:           "test-host",
						Operation:      sarama.AclOperationRead,
						PermissionType: sarama.AclPermissionAllow,
					},
				},
			},
		},
	}

	b, err := yaml.Marshal(car)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", b)
}
Logs
- version: 3
  aclcreations:
    - resource:
        resourcetype: 2
        resourcename: test.topic
        resourcepatterntype: 3
      acl:
        principal: test-user
        host: test-host
        operation: 3
        permissiontype: 3
Additional Context

The receiver types on the encoding.TextMarshal and encoding.Unmarshal interfaces are pointers, while the types within the aforementioned struct are values. Also relevant is this go-yaml issue, though IMO this isn't really a bug, but just the way Go works.

Perhaps I am missing the actual issue, but should we convert the struct to contain pointers to these integer types?

@puellanivis
Copy link
Contributor

The given code works fine if using json.Marshal:

[
  {
    "Version": 3,
    "AclCreations": [
      {
        "ResourceType": "Topic",
        "ResourceName": "test.topic",
        "ResourcePatternType": "Literal",
        "Principal": "test-user",
        "Host": "test-host",
        "Operation": "Read",
        "PermissionType": "Allow"
      }
    ]
  }
]

So, I think in this case this is probably an issue with the go-yaml package.

No, I don’t think it is appropriate to convert these structs to use pointers to work around a bug in a yaml package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants