avro icon indicating copy to clipboard operation
avro copied to clipboard

Feature request: generate enums

Open EduardoLaranjo opened this issue 1 year ago • 4 comments

Hi,

I've noticed that the enums are not being generated in the schema. Is it possible to enable the generation of enums?

Thank you

EduardoLaranjo avatar Aug 07 '24 14:08 EduardoLaranjo

Can you give an example of what you would expect it to generate?

nrwiersma avatar Aug 07 '24 15:08 nrwiersma

hi @nrwiersma thanks for prompt reply

So with gogen-avro any schema with an enum is gonna be generated like so:

{
    "name": "status",
    "type": {
        "name": "status",
        "type": "enum",
        "symbols": [
        "undefined",
        "pending",
        "confirmed",
        "failed"
        ]
    }
}
type Status int32

const (
	StatusUndefined Status = 0
	StatusPending   Status = 1
	StatusConfirmed Status = 2
	StatusFailed    Status = 3
)

func (e Status) String() string {
	switch e {
	case StatusUndefined:
		return "undefined"
	case StatusPending:
		return "pending"
	case StatusConfirmed:
		return "confirmed"
	case StatusFailed:
		return "failed"
	}
	return "unknown"
}

The approach taken to generate the enum is not important to me. It could be like the example above or a group of string constants. The important thing is to have access to the values in the generated code without requiring me to search through the Avro schema.

Thanks Eduardo

EduardoLaranjo avatar Aug 08 '24 13:08 EduardoLaranjo

hi @nrwiersma

I'm happy to-do this work if it's ok

I just need some guidelines for what do we want

EduardoLaranjo avatar Aug 22 '24 10:08 EduardoLaranjo

Hey. You are welcome to take a crack at it. Your proposed typed enum is good with me, but you need to consider that using the name directly is likely to cause conflicts, so making it unique is going to be the challenge.

nrwiersma avatar Aug 22 '24 15:08 nrwiersma