avro icon indicating copy to clipboard operation
avro copied to clipboard

undefined: avrotypegen

Open gubtos opened this issue 2 years ago • 3 comments

Given the following avro

{
    "type": "record",
    "name": "ItemEvent",
    "namespace": "search.item",
    "fields": [
        {
            "name": "id",
            "type": {
                "type": "string",
                "logicalType": "UUID"
            }
        },
        {
            "name": "modes",
            "type": {
                "type": "array",
                "items": {
                    "type": "enum",
                    "name": "mode",
                    "symbols": [
                        "SINGULAR"
                    ]
                }
            }
        }
    ]
}

And run a command like

avrogo -p is item-search.avsc 

The generated code dont imports the avrotypegen module, but uses it in the code. Given the following error:

undefined: avrotypegen compiler[UndeclaredName](https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName)

gubtos avatar Jan 08 '24 19:01 gubtos

code generated

// Code generated by avrogen. DO NOT EDIT.

package is

import (
	"fmt"
	"strconv"
)

type ItemEvent struct {
	Id    string `json:"id"`
	Modes []Mode `json:"modes"`
}

// AvroRecord implements the avro.AvroRecord interface.
func (ItemEvent) AvroRecord() avrotypegen.RecordInfo {
	return avrotypegen.RecordInfo{
		Schema: `{"fields":[{"name":"id","type":{"logicalType":"UUID","type":"string"}},{"name":"modes","type":{"items":{"name":"mode","symbols":["SINGULAR"],"type":"enum"},"type":"array"}}],"name":"search.item.ItemEvent","type":"record"}`,
		Required: []bool{
			0: true,
			1: true,
		},
	}
}

type Mode int

const (
	ModeSINGULAR Mode = iota
)

var _Mode_strings = []string{
	"SINGULAR",
}

// String returns the textual representation of Mode.
func (e Mode) String() string {
	if e < 0 || int(e) >= len(_Mode_strings) {
		return "Mode(" + strconv.FormatInt(int64(e), 10) + ")"
	}
	return _Mode_strings[e]
}

// MarshalText implements encoding.TextMarshaler
// by returning the textual representation of Mode.
func (e Mode) MarshalText() ([]byte, error) {
	if e < 0 || int(e) >= len(_Mode_strings) {
		return nil, fmt.Errorf("Mode value %d is out of bounds", e)
	}
	return []byte(_Mode_strings[e]), nil
}

// UnmarshalText implements encoding.TextUnmarshaler
// by expecting the textual representation of Mode.
func (e *Mode) UnmarshalText(data []byte) error {
	// Note for future: this could be more efficient.
	for i, s := range _Mode_strings {
		if string(data) == s {
			*e = Mode(i)
			return nil
		}
	}
	return fmt.Errorf("unknown value %q for Mode", data)
}

gubtos avatar Jan 08 '24 19:01 gubtos

Hi @gubtos, I had the same issue and found that using -tokenize solves it. Try calling avrogo -p is -tokenize item-search.avsc

caio-northfleet-neon avatar Apr 22 '24 18:04 caio-northfleet-neon

Hey @gubtos the same problem is happening to me!

I debugged locally here and noticed that the shouldImportAvroTypeGen() function is not behaving as it should. I think the problem is in the use of the sort.Search() function. I believe this function does not fit the desired purpose...

Does it make sense to propose this fix? What do you think?

thiagodpf avatar Aug 30 '24 21:08 thiagodpf