Ent: Field named "type" crashes on generation

Created on 21 Oct 2019  路  5Comments  路  Source: ent/ent

Hello, first thanks for this wonderful project !

Im hitting this issue, when one of my fields is called type, it crashes on:

[1] $ go generate ./ent                                                                                                                                                                                                                                                                                                   
format file /path/to/code/test/ent/mymodel/model.go: /path/to/code/ent/mymodel/model.go:111:18: expected type, found 'type' (and 1 more errors)
exit status 1
ent/generate.go:3: running "go": exit status 1
bug

Most helpful comment

111 fixes.

All 5 comments

That's weird as we do use this name in integration tests here. Could you post your entire schema directory?

I just debugged it a bit more and its failing on this field definition:

field.String("type").NotEmpty().MaxLen(255)

It generates the following validator code:

TypeValidator = func() func (string) error {
        validators := descType.Validators
        fns := [...]func(string) error {
                validators[0].(func(string) error),
                validators[1].(func(string) error),
        }
        return func(type string) error {
            for _, fn := range fns {
                if err := fn(type); err != nil {
                    return err
                }
            }
            return nil
        }
    }()

Ok, I'm able to reproduce. It occurs when a field named type has more than one validator (NotEmpty and MaxLen in this case).

111 fixes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clearcodecn picture clearcodecn  路  3Comments

HarikiRito picture HarikiRito  路  3Comments

ernado picture ernado  路  3Comments

metalrex100 picture metalrex100  路  5Comments

juanpabloaj picture juanpabloaj  路  3Comments