heetch / avro

Avro codec and code generation for Go
MIT License
85 stars 15 forks source link

undefined: avrotypegen #130

Open gubtos opened 9 months ago

gubtos commented 9 months ago

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 commented 9 months ago

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)
}
caio-northfleet-neon commented 6 months ago

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

thiagodpf commented 1 month ago

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?