Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Invalid Code Generation with Mixin Field with ValueScanner but no ValidatorΒ #3785

@mgabeler-lee-6rs

Description

@mgabeler-lee-6rs

Given a mixin like this:

type FooMixin struct {
	mixin.Schema
}

func (FooMixin) Fields() []ent.Field {
	return []ent.Field{
		field.String("fooUrl").
			StorageKey("foo_url").
			SchemaType(/* ... */).
			GoType(new(url.URL)).
			ValueScanner(field.BinaryValueScanner[*url.URL]{}),
	}
}

The generated code does not compile. Digging in, the issue is that it doesn't generate a variable declaration due to this logic:

if f.Position != nil && f.Position.MixedIn && (f.Default || f.UpdateDefault || f.Validators > 0) {
-- there's no check there for the field having a ValueScanner. This causes this template code to skip declaring the ...MixinFieldsN variable:
{{ print $pkg "MixinFields" $i }} := {{ $pkg }}Mixin[{{ $i }}].Fields()
. However, in the looping below, it tries to reference the field if it HasValueScanner here:
{{- with $f.HasValueScanner }}

My guess at a solution:

  • The ent/entc/gen.Field type needs a flag for whether it has a value scanner ~
    Validators int
  • This needs to be copied over ~
    Validators: f.Validators,
  • The template needs to add this to the list of things that cause a field to be reported in MixedInFields ~
    if f.Position != nil && f.Position.MixedIn && (f.Default || f.UpdateDefault || f.Validators > 0) {
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

NOTE: This sounds similar to #3255, but on closer inspection they seem like independent issues AFAICT.

Current Behavior 😯

Generated code fails to compile due to attempting to reference an undeclared local variable.

Expected Behavior πŸ€”

Generated code should declare the variable so it compiles.

Steps to Reproduce πŸ•Ή

Repro repo: https://github.com/mgabeler-lee-6rs/entbug-3785
Build output:

entbug-3785$ go build -v ./...
entgo.io/bug/ent
# entgo.io/bug/ent
ent/runtime.go:6:2: "net/url" imported and not used
ent/runtime.go:10:2: "entgo.io/ent/schema/field" imported and not used
ent/runtime.go:20:17: undefined: userMixinFields0

(I think the unused import problem will go away with the fix, not sure, but I personally normally run a formatter over generated code anyways that cleans up such things for me)

Steps:

  1. Add a mixin like the above to a schema type
  2. Generate the code
  3. Try to compile it

Workaround

Add a no-op custom validator to the mixed in field so that the template declares the variable.

Your Environment 🌎

Tech Version
Go 1.21.1
Ent 0.12.4
Database PostgreSQL
Driver github.com/jackc/pgx/v5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions