From 0e49dd1d9f033a839d1bca0242ea5b83dc00343a Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Wed, 2 Feb 2022 14:48:59 +0200 Subject: [PATCH] entc/gen: support local package names for generated packages The new Alias option adds package aliases (local names) for all type-packages that their import identifier conflicts with user-defined packages (i.e. GoType). --- entc/gen/graph.go | 3 +- entc/gen/template.go | 4 +- entc/gen/template/builder/create.tmpl | 4 +- entc/gen/template/builder/delete.tmpl | 2 +- entc/gen/template/builder/mutation.tmpl | 2 +- entc/gen/template/builder/query.tmpl | 4 +- entc/gen/template/builder/update.tmpl | 4 +- entc/gen/template/client.tmpl | 2 +- entc/gen/template/dialect/sql/entql.tmpl | 2 +- entc/gen/template/ent.tmpl | 4 +- entc/gen/template/migrate/schema.tmpl | 2 +- entc/gen/template/runtime.tmpl | 2 +- entc/gen/type.go | 60 +++++++++++++--- entc/integration/ent/client.go | 8 +-- entc/integration/ent/ent.go | 4 +- entc/integration/ent/entql.go | 14 ++-- entc/integration/ent/mutation.go | 45 ++++++------ entc/integration/ent/predicate/predicate.go | 2 +- entc/integration/ent/runtime.go | 19 ++--- entc/integration/ent/schema/task.go | 40 ++--------- entc/integration/ent/schema/task/priority.go | 37 ++++++++++ entc/integration/ent/task.go | 15 ++-- entc/integration/ent/task/task.go | 6 +- entc/integration/ent/task/where.go | 22 +++--- entc/integration/ent/task_create.go | 43 +++++------ entc/integration/ent/task_delete.go | 9 +-- entc/integration/ent/task_query.go | 59 +++++++-------- entc/integration/ent/task_update.go | 71 ++++++++++--------- entc/integration/gremlin/ent/client.go | 8 +-- entc/integration/gremlin/ent/mutation.go | 45 ++++++------ .../gremlin/ent/predicate/predicate.go | 2 +- entc/integration/gremlin/ent/runtime.go | 19 ++--- entc/integration/gremlin/ent/task.go | 12 ++-- entc/integration/gremlin/ent/task/task.go | 6 +- entc/integration/gremlin/ent/task/where.go | 22 +++--- entc/integration/gremlin/ent/task_create.go | 23 +++--- entc/integration/gremlin/ent/task_delete.go | 7 +- entc/integration/gremlin/ent/task_query.go | 45 ++++++------ entc/integration/gremlin/ent/task_update.go | 51 ++++++------- entc/integration/type_test.go | 27 +++---- 40 files changed, 409 insertions(+), 347 deletions(-) create mode 100644 entc/integration/ent/schema/task/priority.go diff --git a/entc/gen/graph.go b/entc/gen/graph.go index 267ec1ddb..829385110 100644 --- a/entc/gen/graph.go +++ b/entc/gen/graph.go @@ -157,6 +157,7 @@ func NewGraph(c *Config, schemas ...*load.Schema) (g *Graph, err error) { for i := range schemas { g.addIndexes(schemas[i]) } + aliases(g) g.defaults() return } @@ -203,7 +204,7 @@ func generate(g *Graph) error { ) templates, external = g.templates() for _, n := range g.Nodes { - assets.dirs = append(assets.dirs, filepath.Join(g.Config.Target, n.Package())) + assets.dirs = append(assets.dirs, filepath.Join(g.Config.Target, n.PackageDir())) for _, tmpl := range Templates { b := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(b, tmpl.Name, n); err != nil { diff --git a/entc/gen/template.go b/entc/gen/template.go index 660b42ea0..c5ce3ea7a 100644 --- a/entc/gen/template.go +++ b/entc/gen/template.go @@ -82,7 +82,7 @@ var ( { Name: "meta", Format: func(t *Type) string { - return fmt.Sprintf("%s/%s.go", t.Package(), t.Package()) + return fmt.Sprintf("%[1]s/%[1]s.go", t.PackageDir()) }, ExtendPatterns: []string{ "meta/additional/*", @@ -405,7 +405,7 @@ func (d *Dependency) defaultName() (string, error) { } func pkgf(s string) func(t *Type) string { - return func(t *Type) string { return fmt.Sprintf(s, t.Package()) } + return func(t *Type) string { return fmt.Sprintf(s, t.PackageDir()) } } // match reports if the given name matches the extended pattern. diff --git a/entc/gen/template/builder/create.tmpl b/entc/gen/template/builder/create.tmpl index c7ced4e74..62f9ecdb6 100644 --- a/entc/gen/template/builder/create.tmpl +++ b/entc/gen/template/builder/create.tmpl @@ -15,8 +15,8 @@ in the LICENSE file in the root directory of this source tree. {{ template "import" $ }} import ( - {{- range $path := $.SiblingImports }} - "{{ $path }}" + {{- range $import := $.SiblingImports }} + {{ $import.Alias }} "{{ $import.Path }}" {{- end }} ) diff --git a/entc/gen/template/builder/delete.tmpl b/entc/gen/template/builder/delete.tmpl index 8c233cf50..5802ca0f5 100644 --- a/entc/gen/template/builder/delete.tmpl +++ b/entc/gen/template/builder/delete.tmpl @@ -14,7 +14,7 @@ in the LICENSE file in the root directory of this source tree. {{ template "import" $ }} import ( - "{{ $.Config.Package }}/{{ $.Package }}" + {{ $.Alias }} "{{ $.Config.Package }}/{{ $.PackageDir }}" ) {{ $builder := $.DeleteName }} diff --git a/entc/gen/template/builder/mutation.tmpl b/entc/gen/template/builder/mutation.tmpl index a25229c97..871cbe72d 100644 --- a/entc/gen/template/builder/mutation.tmpl +++ b/entc/gen/template/builder/mutation.tmpl @@ -17,7 +17,7 @@ import ( "sync" {{- range $n := $.Nodes }} - "{{ $.Config.Package }}/{{ $n.Package }}" + {{ $n.Alias }} "{{ $.Config.Package }}/{{ $n.PackageDir }}" {{- template "import/types" $n }} {{- end }} diff --git a/entc/gen/template/builder/query.tmpl b/entc/gen/template/builder/query.tmpl index 8ee73d775..a57551a86 100644 --- a/entc/gen/template/builder/query.tmpl +++ b/entc/gen/template/builder/query.tmpl @@ -14,8 +14,8 @@ in the LICENSE file in the root directory of this source tree. {{ template "import" $ }} import ( - {{- range $path := $.SiblingImports }} - "{{ $path }}" + {{- range $import := $.SiblingImports }} + {{ $import.Alias }} "{{ $import.Path }}" {{- end }} ) diff --git a/entc/gen/template/builder/update.tmpl b/entc/gen/template/builder/update.tmpl index 81876d2ef..68be3965f 100644 --- a/entc/gen/template/builder/update.tmpl +++ b/entc/gen/template/builder/update.tmpl @@ -14,8 +14,8 @@ in the LICENSE file in the root directory of this source tree. {{ template "import" $ }} import ( - {{- range $path := $.SiblingImports }} - "{{ $path }}" + {{- range $import := $.SiblingImports }} + {{ $import.Alias }} "{{ $import.Path }}" {{- end }} ) diff --git a/entc/gen/template/client.tmpl b/entc/gen/template/client.tmpl index 3b48cb673..1e4afe555 100644 --- a/entc/gen/template/client.tmpl +++ b/entc/gen/template/client.tmpl @@ -16,7 +16,7 @@ import ( "{{ $.Config.Package }}/migrate" {{ range $n := $.Nodes }} - "{{ $n.Config.Package }}/{{ $n.Package }}" + {{ $n.Alias }} "{{ $n.Config.Package }}/{{ $n.PackageDir }}" {{- end }} "entgo.io/ent/dialect" diff --git a/entc/gen/template/dialect/sql/entql.tmpl b/entc/gen/template/dialect/sql/entql.tmpl index a28095bbb..e972e43d4 100644 --- a/entc/gen/template/dialect/sql/entql.tmpl +++ b/entc/gen/template/dialect/sql/entql.tmpl @@ -14,7 +14,7 @@ in the LICENSE file in the root directory of this source tree. import ( "{{ $.Config.Package }}/predicate" {{- range $n := $.Nodes }} - "{{ $.Config.Package }}/{{ $n.Package }}" + {{ $n.Alias }} "{{ $.Config.Package }}/{{ $n.PackageDir }}" {{- end }} "entgo.io/ent/dialect/sql" diff --git a/entc/gen/template/ent.tmpl b/entc/gen/template/ent.tmpl index 35eaeb966..cd00d473a 100644 --- a/entc/gen/template/ent.tmpl +++ b/entc/gen/template/ent.tmpl @@ -14,8 +14,8 @@ in the LICENSE file in the root directory of this source tree. {{ template "import" $ }} import ( - {{- range $path := $.SiblingImports }} - "{{ $path }}" + {{- range $import := $.SiblingImports }} + {{ $import.Alias }} "{{ $import.Path }}" {{- end }} ) diff --git a/entc/gen/template/migrate/schema.tmpl b/entc/gen/template/migrate/schema.tmpl index 083a18fd4..5e9dab604 100644 --- a/entc/gen/template/migrate/schema.tmpl +++ b/entc/gen/template/migrate/schema.tmpl @@ -14,7 +14,7 @@ in the LICENSE file in the root directory of this source tree. import ( {{ range $n := $.Nodes }} - "{{ $.Config.Package }}/{{ $n.Package }}" + {{ $n.Alias }} "{{ $.Config.Package }}/{{ $n.PackageDir }}" {{- end }} "entgo.io/ent/dialect/entsql" diff --git a/entc/gen/template/runtime.tmpl b/entc/gen/template/runtime.tmpl index 743a019b8..c6c3dfe20 100644 --- a/entc/gen/template/runtime.tmpl +++ b/entc/gen/template/runtime.tmpl @@ -72,7 +72,7 @@ import ( "{{ . }}" {{- end }} {{- range $n := $.Nodes }} - "{{ $.Config.Package }}/{{ $n.Package }}" + {{ $n.Alias }} "{{ $.Config.Package }}/{{ $n.PackageDir }}" {{- /* Import external packages */}} {{- template "import/types" $n }} {{- end }} diff --git a/entc/gen/type.go b/entc/gen/type.go index 3463a0701..a321ad0ee 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -35,6 +35,9 @@ type ( schema *load.Schema // Name holds the type/ent name. Name string + // alias, or local package name of the generated package. + // Empty means no alias. + alias string // ID holds the ID field of this type. ID *Field // Fields holds all the primitive fields of this type. @@ -269,9 +272,20 @@ func (t Type) EntSQL() *entsql.Annotation { // Package returns the package name of this node. func (t Type) Package() string { - return strings.ToLower(t.Name) + if name := t.Alias(); name != "" { + return name + } + return t.PackageDir() } +// PackageDir returns the name of the package directory. +func (t Type) PackageDir() string { return strings.ToLower(t.Name) } + +// Alias returns local package name of a type if there is one. +// A package has an alias if its generated name conflicts with +// one of the imports of the user-defined types. +func (t Type) Alias() string { return t.alias } + // Receiver returns the receiver name of this node. It makes sure the // receiver names doesn't conflict with import names. func (t Type) Receiver() string { @@ -741,19 +755,19 @@ func (t Type) MutationName() string { } // SiblingImports returns all sibling packages that are needed for the different builders. -func (t Type) SiblingImports() []string { +func (t Type) SiblingImports() []struct{ Alias, Path string } { var ( - paths = []string{path.Join(t.Config.Package, t.Package())} - seen = map[string]bool{paths[0]: true} + imports = []struct{ Alias, Path string }{{Alias: t.Alias(), Path: path.Join(t.Config.Package, t.PackageDir())}} + seen = map[string]bool{imports[0].Path: true} ) for _, e := range t.Edges { - name := path.Join(t.Config.Package, e.Type.Package()) - if !seen[name] { - seen[name] = true - paths = append(paths, name) + p := path.Join(t.Config.Package, e.Type.PackageDir()) + if !seen[p] { + seen[p] = true + imports = append(imports, struct{ Alias, Path string }{Alias: e.Type.Alias(), Path: p}) } } - return paths + return imports } // NumHooks returns the number of hooks declared in the type schema. @@ -840,7 +854,7 @@ func (t *Type) checkField(tf *Field, f *load.Field) (err error) { case f.Info.Type == field.TypeEnum: if tf.Enums, err = tf.enums(f); err == nil && !tf.HasGoType() { // Enum types should be named as follows: typepkg.Field. - f.Info.Ident = fmt.Sprintf("%s.%s", t.Package(), pascal(f.Name)) + f.Info.Ident = fmt.Sprintf("%s.%s", t.PackageDir(), pascal(f.Name)) } case tf.Validators > 0 && !tf.ConvertedToBasic(): err = fmt.Errorf("GoType %q for field %q must be converted to the basic %q type for validators", tf.Type, f.Name, tf.Type.Type) @@ -860,6 +874,32 @@ func (t Type) UnexportedForeignKeys() []*ForeignKey { return fks } +// aliases adds package aliases (local names) for all type-packages that +// their import identifier conflicts with user-defined packages (i.e. GoType). +func aliases(g *Graph) { + names := make(map[string]*Type) + for _, n := range g.Nodes { + names[n.PackageDir()] = n + } + for _, n := range g.Nodes { + for _, f := range n.Fields { + if !f.HasGoType() { + continue + } + name := f.Type.PkgName + if name == "" && f.Type.PkgPath != "" { + name = path.Base(f.Type.PkgPath) + } + // An user-defined type already uses the + // package local name. + if n, ok := names[name]; ok { + // By default, a package named "pet" will be named as "entpet". + n.alias = path.Base(g.Package) + name + } + } + } +} + // Constant returns the constant name of the field. func (f Field) Constant() string { return "Field" + pascal(f.Name) diff --git a/entc/integration/ent/client.go b/entc/integration/ent/client.go index d62920e9e..24a1ff359 100644 --- a/entc/integration/ent/client.go +++ b/entc/integration/ent/client.go @@ -25,7 +25,7 @@ import ( "entgo.io/ent/entc/integration/ent/node" "entgo.io/ent/entc/integration/ent/pet" "entgo.io/ent/entc/integration/ent/spec" - "entgo.io/ent/entc/integration/ent/task" + enttask "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/entc/integration/ent/user" "entgo.io/ent/dialect" @@ -1571,7 +1571,7 @@ func NewTaskClient(c config) *TaskClient { } // Use adds a list of mutation hooks to the hooks stack. -// A call to `Use(f, g, h)` equals to `task.Hooks(f(g(h())))`. +// A call to `Use(f, g, h)` equals to `enttask.Hooks(f(g(h())))`. func (c *TaskClient) Use(hooks ...Hook) { c.hooks.Task = append(c.hooks.Task, hooks...) } @@ -1618,7 +1618,7 @@ func (c *TaskClient) DeleteOne(t *Task) *TaskDeleteOne { // DeleteOneID returns a delete builder for the given id. func (c *TaskClient) DeleteOneID(id int) *TaskDeleteOne { - builder := c.Delete().Where(task.ID(id)) + builder := c.Delete().Where(enttask.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne return &TaskDeleteOne{builder} @@ -1633,7 +1633,7 @@ func (c *TaskClient) Query() *TaskQuery { // Get returns a Task entity by its id. func (c *TaskClient) Get(ctx context.Context, id int) (*Task, error) { - return c.Query().Where(task.ID(id)).Only(ctx) + return c.Query().Where(enttask.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. diff --git a/entc/integration/ent/ent.go b/entc/integration/ent/ent.go index 84b532d7c..068a68b73 100644 --- a/entc/integration/ent/ent.go +++ b/entc/integration/ent/ent.go @@ -24,7 +24,7 @@ import ( "entgo.io/ent/entc/integration/ent/node" "entgo.io/ent/entc/integration/ent/pet" "entgo.io/ent/entc/integration/ent/spec" - "entgo.io/ent/entc/integration/ent/task" + enttask "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/entc/integration/ent/user" ) @@ -58,7 +58,7 @@ func columnChecker(table string) func(string) error { node.Table: node.ValidColumn, pet.Table: pet.ValidColumn, spec.Table: spec.ValidColumn, - task.Table: task.ValidColumn, + enttask.Table: enttask.ValidColumn, user.Table: user.ValidColumn, } check, ok := checks[table] diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 148817b6f..9a7d328be 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -20,7 +20,7 @@ import ( "entgo.io/ent/entc/integration/ent/pet" "entgo.io/ent/entc/integration/ent/predicate" "entgo.io/ent/entc/integration/ent/spec" - "entgo.io/ent/entc/integration/ent/task" + enttask "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/entc/integration/ent/user" "entgo.io/ent/dialect/sql" @@ -282,16 +282,16 @@ var schemaGraph = func() *sqlgraph.Schema { } graph.Nodes[12] = &sqlgraph.Node{ NodeSpec: sqlgraph.NodeSpec{ - Table: task.Table, - Columns: task.Columns, + Table: enttask.Table, + Columns: enttask.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, }, Type: "Task", Fields: map[string]*sqlgraph.FieldSpec{ - task.FieldPriority: {Type: field.TypeInt, Column: task.FieldPriority}, + enttask.FieldPriority: {Type: field.TypeInt, Column: enttask.FieldPriority}, }, } graph.Nodes[13] = &sqlgraph.Node{ @@ -1849,12 +1849,12 @@ func (f *TaskFilter) Where(p entql.P) { // WhereID applies the entql int predicate on the id field. func (f *TaskFilter) WhereID(p entql.IntP) { - f.Where(p.Field(task.FieldID)) + f.Where(p.Field(enttask.FieldID)) } // WherePriority applies the entql int predicate on the priority field. func (f *TaskFilter) WherePriority(p entql.IntP) { - f.Where(p.Field(task.FieldPriority)) + f.Where(p.Field(enttask.FieldPriority)) } // addPredicate implements the predicateAdder interface. diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 5b99468f1..25be4bed2 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -29,8 +29,9 @@ import ( "entgo.io/ent/entc/integration/ent/predicate" "entgo.io/ent/entc/integration/ent/role" "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/entc/integration/ent/spec" - "entgo.io/ent/entc/integration/ent/task" + enttask "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/entc/integration/ent/user" "github.com/google/uuid" @@ -12085,8 +12086,8 @@ type TaskMutation struct { op Op typ string id *int - priority *schema.Priority - addpriority *schema.Priority + priority *task.Priority + addpriority *task.Priority clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Task, error) @@ -12192,13 +12193,13 @@ func (m *TaskMutation) IDs(ctx context.Context) ([]int, error) { } // SetPriority sets the "priority" field. -func (m *TaskMutation) SetPriority(s schema.Priority) { - m.priority = &s +func (m *TaskMutation) SetPriority(t task.Priority) { + m.priority = &t m.addpriority = nil } // Priority returns the value of the "priority" field in the mutation. -func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { +func (m *TaskMutation) Priority() (r task.Priority, exists bool) { v := m.priority if v == nil { return @@ -12209,7 +12210,7 @@ func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { // OldPriority returns the old "priority" field's value of the Task entity. // If the Task object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err error) { +func (m *TaskMutation) OldPriority(ctx context.Context) (v task.Priority, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldPriority is only allowed on UpdateOne operations") } @@ -12223,17 +12224,17 @@ func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err return oldValue.Priority, nil } -// AddPriority adds s to the "priority" field. -func (m *TaskMutation) AddPriority(s schema.Priority) { +// AddPriority adds t to the "priority" field. +func (m *TaskMutation) AddPriority(t task.Priority) { if m.addpriority != nil { - *m.addpriority += s + *m.addpriority += t } else { - m.addpriority = &s + m.addpriority = &t } } // AddedPriority returns the value that was added to the "priority" field in this mutation. -func (m *TaskMutation) AddedPriority() (r schema.Priority, exists bool) { +func (m *TaskMutation) AddedPriority() (r task.Priority, exists bool) { v := m.addpriority if v == nil { return @@ -12268,7 +12269,7 @@ func (m *TaskMutation) Type() string { func (m *TaskMutation) Fields() []string { fields := make([]string, 0, 1) if m.priority != nil { - fields = append(fields, task.FieldPriority) + fields = append(fields, enttask.FieldPriority) } return fields } @@ -12278,7 +12279,7 @@ func (m *TaskMutation) Fields() []string { // schema. func (m *TaskMutation) Field(name string) (ent.Value, bool) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.Priority() } return nil, false @@ -12289,7 +12290,7 @@ func (m *TaskMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.OldPriority(ctx) } return nil, fmt.Errorf("unknown Task field %s", name) @@ -12300,8 +12301,8 @@ func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, er // type. func (m *TaskMutation) SetField(name string, value ent.Value) error { switch name { - case task.FieldPriority: - v, ok := value.(schema.Priority) + case enttask.FieldPriority: + v, ok := value.(task.Priority) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -12316,7 +12317,7 @@ func (m *TaskMutation) SetField(name string, value ent.Value) error { func (m *TaskMutation) AddedFields() []string { var fields []string if m.addpriority != nil { - fields = append(fields, task.FieldPriority) + fields = append(fields, enttask.FieldPriority) } return fields } @@ -12326,7 +12327,7 @@ func (m *TaskMutation) AddedFields() []string { // was not set, or was not defined in the schema. func (m *TaskMutation) AddedField(name string) (ent.Value, bool) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.AddedPriority() } return nil, false @@ -12337,8 +12338,8 @@ func (m *TaskMutation) AddedField(name string) (ent.Value, bool) { // type. func (m *TaskMutation) AddField(name string, value ent.Value) error { switch name { - case task.FieldPriority: - v, ok := value.(schema.Priority) + case enttask.FieldPriority: + v, ok := value.(task.Priority) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -12371,7 +12372,7 @@ func (m *TaskMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *TaskMutation) ResetField(name string) error { switch name { - case task.FieldPriority: + case enttask.FieldPriority: m.ResetPriority() return nil } diff --git a/entc/integration/ent/predicate/predicate.go b/entc/integration/ent/predicate/predicate.go index 73a824457..316232429 100644 --- a/entc/integration/ent/predicate/predicate.go +++ b/entc/integration/ent/predicate/predicate.go @@ -46,7 +46,7 @@ type Pet func(*sql.Selector) // Spec is the predicate function for spec builders. type Spec func(*sql.Selector) -// Task is the predicate function for task builders. +// Task is the predicate function for enttask builders. type Task func(*sql.Selector) // User is the predicate function for user builders. diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index 23bde2a26..560e3f973 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -20,7 +20,8 @@ import ( "entgo.io/ent/entc/integration/ent/item" "entgo.io/ent/entc/integration/ent/pet" "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" + enttask "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/entc/integration/ent/user" ) @@ -225,14 +226,14 @@ func init() { petDescAge := petFields[0].Descriptor() // pet.DefaultAge holds the default value on creation for the age field. pet.DefaultAge = petDescAge.Default.(float64) - taskFields := schema.Task{}.Fields() - _ = taskFields - // taskDescPriority is the schema descriptor for priority field. - taskDescPriority := taskFields[0].Descriptor() - // task.DefaultPriority holds the default value on creation for the priority field. - task.DefaultPriority = schema.Priority(taskDescPriority.Default.(int)) - // task.PriorityValidator is a validator for the "priority" field. It is called by the builders before save. - task.PriorityValidator = taskDescPriority.Validators[0].(func(int) error) + enttaskFields := schema.Task{}.Fields() + _ = enttaskFields + // enttaskDescPriority is the schema descriptor for priority field. + enttaskDescPriority := enttaskFields[0].Descriptor() + // enttask.DefaultPriority holds the default value on creation for the priority field. + enttask.DefaultPriority = task.Priority(enttaskDescPriority.Default.(int)) + // enttask.PriorityValidator is a validator for the "priority" field. It is called by the builders before save. + enttask.PriorityValidator = enttaskDescPriority.Validators[0].(func(int) error) userMixin := schema.User{}.Mixin() userMixinFields0 := userMixin[0].Fields() _ = userMixinFields0 diff --git a/entc/integration/ent/schema/task.go b/entc/integration/ent/schema/task.go index a3872750e..c91b94738 100644 --- a/entc/integration/ent/schema/task.go +++ b/entc/integration/ent/schema/task.go @@ -5,10 +5,10 @@ package schema import ( - "fmt" - "entgo.io/ent" "entgo.io/ent/schema/field" + + "entgo.io/ent/entc/integration/ent/schema/task" ) // Task holds the schema definition for the Task entity. @@ -20,40 +20,10 @@ type Task struct { func (Task) Fields() []ent.Field { return []ent.Field{ field.Int("priority"). - GoType(Priority(0)). - Default(int(PriorityMid)). + GoType(task.Priority(0)). + Default(int(task.PriorityMid)). Validate(func(i int) error { - return Priority(i).Validate() + return task.Priority(i).Validate() }), } } - -type Priority int - -const ( - PriorityLow Priority = iota - PriorityMid - PriorityHigh -) - -func (p Priority) String() string { - s := "unknown" - switch p { - case PriorityLow: - s = "low" - case PriorityMid: - s = "mid" - case PriorityHigh: - s = "high" - } - return s -} - -func (p Priority) Validate() error { - switch p { - case PriorityLow, PriorityMid, PriorityHigh: - return nil - default: - return fmt.Errorf("invalid priority value: %v", p) - } -} diff --git a/entc/integration/ent/schema/task/priority.go b/entc/integration/ent/schema/task/priority.go new file mode 100644 index 000000000..9bec6c33f --- /dev/null +++ b/entc/integration/ent/schema/task/priority.go @@ -0,0 +1,37 @@ +// Copyright 2019-present Facebook Inc. All rights reserved. +// This source code is licensed under the Apache 2.0 license found +// in the LICENSE file in the root directory of this source tree. + +package task + +import "fmt" + +type Priority int + +const ( + PriorityLow Priority = iota + PriorityMid + PriorityHigh +) + +func (p Priority) String() string { + s := "unknown" + switch p { + case PriorityLow: + s = "low" + case PriorityMid: + s = "mid" + case PriorityHigh: + s = "high" + } + return s +} + +func (p Priority) Validate() error { + switch p { + case PriorityLow, PriorityMid, PriorityHigh: + return nil + default: + return fmt.Errorf("invalid priority value: %v", p) + } +} diff --git a/entc/integration/ent/task.go b/entc/integration/ent/task.go index 673673191..21a4635aa 100644 --- a/entc/integration/ent/task.go +++ b/entc/integration/ent/task.go @@ -11,8 +11,9 @@ import ( "strings" "entgo.io/ent/dialect/sql" - "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" + + enttask "entgo.io/ent/entc/integration/ent/task" ) // Task is the model entity for the Task schema. @@ -21,7 +22,7 @@ type Task struct { // ID of the ent. ID int `json:"id,omitempty"` // Priority holds the value of the "priority" field. - Priority schema.Priority `json:"priority,omitempty"` + Priority task.Priority `json:"priority,omitempty"` } // scanValues returns the types for scanning values from sql.Rows. @@ -29,7 +30,7 @@ func (*Task) scanValues(columns []string) ([]interface{}, error) { values := make([]interface{}, len(columns)) for i := range columns { switch columns[i] { - case task.FieldID, task.FieldPriority: + case enttask.FieldID, enttask.FieldPriority: values[i] = new(sql.NullInt64) default: return nil, fmt.Errorf("unexpected column %q for type Task", columns[i]) @@ -46,17 +47,17 @@ func (t *Task) assignValues(columns []string, values []interface{}) error { } for i := range columns { switch columns[i] { - case task.FieldID: + case enttask.FieldID: value, ok := values[i].(*sql.NullInt64) if !ok { return fmt.Errorf("unexpected type %T for field id", value) } t.ID = int(value.Int64) - case task.FieldPriority: + case enttask.FieldPriority: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field priority", values[i]) } else if value.Valid { - t.Priority = schema.Priority(value.Int64) + t.Priority = task.Priority(value.Int64) } } } diff --git a/entc/integration/ent/task/task.go b/entc/integration/ent/task/task.go index baef72f8d..206724d9e 100644 --- a/entc/integration/ent/task/task.go +++ b/entc/integration/ent/task/task.go @@ -4,10 +4,10 @@ // Code generated by entc, DO NOT EDIT. -package task +package enttask import ( - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" ) const ( @@ -39,7 +39,7 @@ func ValidColumn(column string) bool { var ( // DefaultPriority holds the default value on creation for the "priority" field. - DefaultPriority schema.Priority + DefaultPriority task.Priority // PriorityValidator is a validator for the "priority" field. It is called by the builders before save. PriorityValidator func(int) error ) diff --git a/entc/integration/ent/task/where.go b/entc/integration/ent/task/where.go index a29599826..7e0f07ad0 100644 --- a/entc/integration/ent/task/where.go +++ b/entc/integration/ent/task/where.go @@ -4,12 +4,12 @@ // Code generated by entc, DO NOT EDIT. -package task +package enttask import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/entc/integration/ent/predicate" - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" ) // ID filters vertices based on their ID field. @@ -96,7 +96,7 @@ func IDLTE(id int) predicate.Task { } // Priority applies equality check predicate on the "priority" field. It's identical to PriorityEQ. -func Priority(v schema.Priority) predicate.Task { +func Priority(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldPriority), vc)) @@ -104,7 +104,7 @@ func Priority(v schema.Priority) predicate.Task { } // PriorityEQ applies the EQ predicate on the "priority" field. -func PriorityEQ(v schema.Priority) predicate.Task { +func PriorityEQ(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldPriority), vc)) @@ -112,7 +112,7 @@ func PriorityEQ(v schema.Priority) predicate.Task { } // PriorityNEQ applies the NEQ predicate on the "priority" field. -func PriorityNEQ(v schema.Priority) predicate.Task { +func PriorityNEQ(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.NEQ(s.C(FieldPriority), vc)) @@ -120,7 +120,7 @@ func PriorityNEQ(v schema.Priority) predicate.Task { } // PriorityIn applies the In predicate on the "priority" field. -func PriorityIn(vs ...schema.Priority) predicate.Task { +func PriorityIn(vs ...task.Priority) predicate.Task { v := make([]interface{}, len(vs)) for i := range v { v[i] = int(vs[i]) @@ -137,7 +137,7 @@ func PriorityIn(vs ...schema.Priority) predicate.Task { } // PriorityNotIn applies the NotIn predicate on the "priority" field. -func PriorityNotIn(vs ...schema.Priority) predicate.Task { +func PriorityNotIn(vs ...task.Priority) predicate.Task { v := make([]interface{}, len(vs)) for i := range v { v[i] = int(vs[i]) @@ -154,7 +154,7 @@ func PriorityNotIn(vs ...schema.Priority) predicate.Task { } // PriorityGT applies the GT predicate on the "priority" field. -func PriorityGT(v schema.Priority) predicate.Task { +func PriorityGT(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.GT(s.C(FieldPriority), vc)) @@ -162,7 +162,7 @@ func PriorityGT(v schema.Priority) predicate.Task { } // PriorityGTE applies the GTE predicate on the "priority" field. -func PriorityGTE(v schema.Priority) predicate.Task { +func PriorityGTE(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.GTE(s.C(FieldPriority), vc)) @@ -170,7 +170,7 @@ func PriorityGTE(v schema.Priority) predicate.Task { } // PriorityLT applies the LT predicate on the "priority" field. -func PriorityLT(v schema.Priority) predicate.Task { +func PriorityLT(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.LT(s.C(FieldPriority), vc)) @@ -178,7 +178,7 @@ func PriorityLT(v schema.Priority) predicate.Task { } // PriorityLTE applies the LTE predicate on the "priority" field. -func PriorityLTE(v schema.Priority) predicate.Task { +func PriorityLTE(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(s *sql.Selector) { s.Where(sql.LTE(s.C(FieldPriority), vc)) diff --git a/entc/integration/ent/task_create.go b/entc/integration/ent/task_create.go index 80aa322b2..565bd69a0 100644 --- a/entc/integration/ent/task_create.go +++ b/entc/integration/ent/task_create.go @@ -13,9 +13,10 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" - "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/schema/field" + + enttask "entgo.io/ent/entc/integration/ent/task" ) // TaskCreate is the builder for creating a Task entity. @@ -27,15 +28,15 @@ type TaskCreate struct { } // SetPriority sets the "priority" field. -func (tc *TaskCreate) SetPriority(s schema.Priority) *TaskCreate { - tc.mutation.SetPriority(s) +func (tc *TaskCreate) SetPriority(t task.Priority) *TaskCreate { + tc.mutation.SetPriority(t) return tc } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tc *TaskCreate) SetNillablePriority(s *schema.Priority) *TaskCreate { - if s != nil { - tc.SetPriority(*s) +func (tc *TaskCreate) SetNillablePriority(t *task.Priority) *TaskCreate { + if t != nil { + tc.SetPriority(*t) } return tc } @@ -112,7 +113,7 @@ func (tc *TaskCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (tc *TaskCreate) defaults() { if _, ok := tc.mutation.Priority(); !ok { - v := task.DefaultPriority + v := enttask.DefaultPriority tc.mutation.SetPriority(v) } } @@ -123,7 +124,7 @@ func (tc *TaskCreate) check() error { return &ValidationError{Name: "priority", err: errors.New(`ent: missing required field "Task.priority"`)} } if v, ok := tc.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -147,10 +148,10 @@ func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) { var ( _node = &Task{config: tc.config} _spec = &sqlgraph.CreateSpec{ - Table: task.Table, + Table: enttask.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, } ) @@ -159,7 +160,7 @@ func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, - Column: task.FieldPriority, + Column: enttask.FieldPriority, }) _node.Priority = value } @@ -218,20 +219,20 @@ type ( ) // SetPriority sets the "priority" field. -func (u *TaskUpsert) SetPriority(v schema.Priority) *TaskUpsert { - u.Set(task.FieldPriority, v) +func (u *TaskUpsert) SetPriority(v task.Priority) *TaskUpsert { + u.Set(enttask.FieldPriority, v) return u } // UpdatePriority sets the "priority" field to the value that was provided on create. func (u *TaskUpsert) UpdatePriority() *TaskUpsert { - u.SetExcluded(task.FieldPriority) + u.SetExcluded(enttask.FieldPriority) return u } // AddPriority adds v to the "priority" field. -func (u *TaskUpsert) AddPriority(v schema.Priority) *TaskUpsert { - u.Add(task.FieldPriority, v) +func (u *TaskUpsert) AddPriority(v task.Priority) *TaskUpsert { + u.Add(enttask.FieldPriority, v) return u } @@ -278,14 +279,14 @@ func (u *TaskUpsertOne) Update(set func(*TaskUpsert)) *TaskUpsertOne { } // SetPriority sets the "priority" field. -func (u *TaskUpsertOne) SetPriority(v schema.Priority) *TaskUpsertOne { +func (u *TaskUpsertOne) SetPriority(v task.Priority) *TaskUpsertOne { return u.Update(func(s *TaskUpsert) { s.SetPriority(v) }) } // AddPriority adds v to the "priority" field. -func (u *TaskUpsertOne) AddPriority(v schema.Priority) *TaskUpsertOne { +func (u *TaskUpsertOne) AddPriority(v task.Priority) *TaskUpsertOne { return u.Update(func(s *TaskUpsert) { s.AddPriority(v) }) @@ -503,14 +504,14 @@ func (u *TaskUpsertBulk) Update(set func(*TaskUpsert)) *TaskUpsertBulk { } // SetPriority sets the "priority" field. -func (u *TaskUpsertBulk) SetPriority(v schema.Priority) *TaskUpsertBulk { +func (u *TaskUpsertBulk) SetPriority(v task.Priority) *TaskUpsertBulk { return u.Update(func(s *TaskUpsert) { s.SetPriority(v) }) } // AddPriority adds v to the "priority" field. -func (u *TaskUpsertBulk) AddPriority(v schema.Priority) *TaskUpsertBulk { +func (u *TaskUpsertBulk) AddPriority(v task.Priority) *TaskUpsertBulk { return u.Update(func(s *TaskUpsert) { s.AddPriority(v) }) diff --git a/entc/integration/ent/task_delete.go b/entc/integration/ent/task_delete.go index b769b8d03..9c53fa05e 100644 --- a/entc/integration/ent/task_delete.go +++ b/entc/integration/ent/task_delete.go @@ -13,8 +13,9 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/ent/predicate" - "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/schema/field" + + enttask "entgo.io/ent/entc/integration/ent/task" ) // TaskDelete is the builder for deleting a Task entity. @@ -74,10 +75,10 @@ func (td *TaskDelete) ExecX(ctx context.Context) int { func (td *TaskDelete) sqlExec(ctx context.Context) (int, error) { _spec := &sqlgraph.DeleteSpec{ Node: &sqlgraph.NodeSpec{ - Table: task.Table, + Table: enttask.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, }, } @@ -103,7 +104,7 @@ func (tdo *TaskDeleteOne) Exec(ctx context.Context) error { case err != nil: return err case n == 0: - return &NotFoundError{task.Label} + return &NotFoundError{enttask.Label} default: return nil } diff --git a/entc/integration/ent/task_query.go b/entc/integration/ent/task_query.go index 4c1f73ba6..b646da522 100644 --- a/entc/integration/ent/task_query.go +++ b/entc/integration/ent/task_query.go @@ -16,8 +16,9 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/ent/predicate" - "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/schema/field" + + enttask "entgo.io/ent/entc/integration/ent/task" ) // TaskQuery is the builder for querying Task entities. @@ -74,7 +75,7 @@ func (tq *TaskQuery) First(ctx context.Context) (*Task, error) { return nil, err } if len(nodes) == 0 { - return nil, &NotFoundError{task.Label} + return nil, &NotFoundError{enttask.Label} } return nodes[0], nil } @@ -96,7 +97,7 @@ func (tq *TaskQuery) FirstID(ctx context.Context) (id int, err error) { return } if len(ids) == 0 { - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} return } return ids[0], nil @@ -123,9 +124,9 @@ func (tq *TaskQuery) Only(ctx context.Context) (*Task, error) { case 1: return nodes[0], nil case 0: - return nil, &NotFoundError{task.Label} + return nil, &NotFoundError{enttask.Label} default: - return nil, &NotSingularError{task.Label} + return nil, &NotSingularError{enttask.Label} } } @@ -150,9 +151,9 @@ func (tq *TaskQuery) OnlyID(ctx context.Context) (id int, err error) { case 1: id = ids[0] case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: - err = &NotSingularError{task.Label} + err = &NotSingularError{enttask.Label} } return } @@ -186,7 +187,7 @@ func (tq *TaskQuery) AllX(ctx context.Context) []*Task { // IDs executes the query and returns a list of Task IDs. func (tq *TaskQuery) IDs(ctx context.Context) ([]int, error) { var ids []int - if err := tq.Select(task.FieldID).Scan(ctx, &ids); err != nil { + if err := tq.Select(enttask.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil @@ -259,12 +260,12 @@ func (tq *TaskQuery) Clone() *TaskQuery { // Example: // // var v []struct { -// Priority schema.Priority `json:"priority,omitempty"` +// Priority task.Priority `json:"priority,omitempty"` // Count int `json:"count,omitempty"` // } // // client.Task.Query(). -// GroupBy(task.FieldPriority). +// GroupBy(enttask.FieldPriority). // Aggregate(ent.Count()). // Scan(ctx, &v) // @@ -286,11 +287,11 @@ func (tq *TaskQuery) GroupBy(field string, fields ...string) *TaskGroupBy { // Example: // // var v []struct { -// Priority schema.Priority `json:"priority,omitempty"` +// Priority task.Priority `json:"priority,omitempty"` // } // // client.Task.Query(). -// Select(task.FieldPriority). +// Select(enttask.FieldPriority). // Scan(ctx, &v) // func (tq *TaskQuery) Select(fields ...string) *TaskSelect { @@ -300,7 +301,7 @@ func (tq *TaskQuery) Select(fields ...string) *TaskSelect { func (tq *TaskQuery) prepareQuery(ctx context.Context) error { for _, f := range tq.fields { - if !task.ValidColumn(f) { + if !enttask.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } } @@ -366,11 +367,11 @@ func (tq *TaskQuery) sqlExist(ctx context.Context) (bool, error) { func (tq *TaskQuery) querySpec() *sqlgraph.QuerySpec { _spec := &sqlgraph.QuerySpec{ Node: &sqlgraph.NodeSpec{ - Table: task.Table, - Columns: task.Columns, + Table: enttask.Table, + Columns: enttask.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, }, From: tq.sql, @@ -381,9 +382,9 @@ func (tq *TaskQuery) querySpec() *sqlgraph.QuerySpec { } if fields := tq.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) - _spec.Node.Columns = append(_spec.Node.Columns, task.FieldID) + _spec.Node.Columns = append(_spec.Node.Columns, enttask.FieldID) for i := range fields { - if fields[i] != task.FieldID { + if fields[i] != enttask.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } @@ -413,10 +414,10 @@ func (tq *TaskQuery) querySpec() *sqlgraph.QuerySpec { func (tq *TaskQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(tq.driver.Dialect()) - t1 := builder.Table(task.Table) + t1 := builder.Table(enttask.Table) columns := tq.fields if len(columns) == 0 { - columns = task.Columns + columns = enttask.Columns } selector := builder.Select(t1.Columns(columns...)...).From(t1) if tq.sql != nil { @@ -544,7 +545,7 @@ func (tgb *TaskGroupBy) String(ctx context.Context) (_ string, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Strings returned %d results when one was expected", len(v)) } @@ -593,7 +594,7 @@ func (tgb *TaskGroupBy) Int(ctx context.Context) (_ int, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Ints returned %d results when one was expected", len(v)) } @@ -642,7 +643,7 @@ func (tgb *TaskGroupBy) Float64(ctx context.Context) (_ float64, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Float64s returned %d results when one was expected", len(v)) } @@ -691,7 +692,7 @@ func (tgb *TaskGroupBy) Bool(ctx context.Context) (_ bool, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Bools returned %d results when one was expected", len(v)) } @@ -709,7 +710,7 @@ func (tgb *TaskGroupBy) BoolX(ctx context.Context) bool { func (tgb *TaskGroupBy) sqlScan(ctx context.Context, v interface{}) error { for _, f := range tgb.fields { - if !task.ValidColumn(f) { + if !enttask.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)} } } @@ -799,7 +800,7 @@ func (ts *TaskSelect) String(ctx context.Context) (_ string, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Strings returned %d results when one was expected", len(v)) } @@ -846,7 +847,7 @@ func (ts *TaskSelect) Int(ctx context.Context) (_ int, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Ints returned %d results when one was expected", len(v)) } @@ -893,7 +894,7 @@ func (ts *TaskSelect) Float64(ctx context.Context) (_ float64, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Float64s returned %d results when one was expected", len(v)) } @@ -940,7 +941,7 @@ func (ts *TaskSelect) Bool(ctx context.Context) (_ bool, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Bools returned %d results when one was expected", len(v)) } diff --git a/entc/integration/ent/task_update.go b/entc/integration/ent/task_update.go index 426ba4a8e..61f56dcdc 100644 --- a/entc/integration/ent/task_update.go +++ b/entc/integration/ent/task_update.go @@ -14,9 +14,10 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/ent/predicate" - "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/schema/field" + + enttask "entgo.io/ent/entc/integration/ent/task" ) // TaskUpdate is the builder for updating Task entities. @@ -33,23 +34,23 @@ func (tu *TaskUpdate) Where(ps ...predicate.Task) *TaskUpdate { } // SetPriority sets the "priority" field. -func (tu *TaskUpdate) SetPriority(s schema.Priority) *TaskUpdate { +func (tu *TaskUpdate) SetPriority(t task.Priority) *TaskUpdate { tu.mutation.ResetPriority() - tu.mutation.SetPriority(s) + tu.mutation.SetPriority(t) return tu } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tu *TaskUpdate) SetNillablePriority(s *schema.Priority) *TaskUpdate { - if s != nil { - tu.SetPriority(*s) +func (tu *TaskUpdate) SetNillablePriority(t *task.Priority) *TaskUpdate { + if t != nil { + tu.SetPriority(*t) } return tu } -// AddPriority adds s to the "priority" field. -func (tu *TaskUpdate) AddPriority(s schema.Priority) *TaskUpdate { - tu.mutation.AddPriority(s) +// AddPriority adds t to the "priority" field. +func (tu *TaskUpdate) AddPriority(t task.Priority) *TaskUpdate { + tu.mutation.AddPriority(t) return tu } @@ -121,7 +122,7 @@ func (tu *TaskUpdate) ExecX(ctx context.Context) { // check runs all checks and user-defined validators on the builder. func (tu *TaskUpdate) check() error { if v, ok := tu.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -131,11 +132,11 @@ func (tu *TaskUpdate) check() error { func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ - Table: task.Table, - Columns: task.Columns, + Table: enttask.Table, + Columns: enttask.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, }, } @@ -150,19 +151,19 @@ func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, - Column: task.FieldPriority, + Column: enttask.FieldPriority, }) } if value, ok := tu.mutation.AddedPriority(); ok { _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, - Column: task.FieldPriority, + Column: enttask.FieldPriority, }) } if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{err.Error(), err} } @@ -180,23 +181,23 @@ type TaskUpdateOne struct { } // SetPriority sets the "priority" field. -func (tuo *TaskUpdateOne) SetPriority(s schema.Priority) *TaskUpdateOne { +func (tuo *TaskUpdateOne) SetPriority(t task.Priority) *TaskUpdateOne { tuo.mutation.ResetPriority() - tuo.mutation.SetPriority(s) + tuo.mutation.SetPriority(t) return tuo } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tuo *TaskUpdateOne) SetNillablePriority(s *schema.Priority) *TaskUpdateOne { - if s != nil { - tuo.SetPriority(*s) +func (tuo *TaskUpdateOne) SetNillablePriority(t *task.Priority) *TaskUpdateOne { + if t != nil { + tuo.SetPriority(*t) } return tuo } -// AddPriority adds s to the "priority" field. -func (tuo *TaskUpdateOne) AddPriority(s schema.Priority) *TaskUpdateOne { - tuo.mutation.AddPriority(s) +// AddPriority adds t to the "priority" field. +func (tuo *TaskUpdateOne) AddPriority(t task.Priority) *TaskUpdateOne { + tuo.mutation.AddPriority(t) return tuo } @@ -275,7 +276,7 @@ func (tuo *TaskUpdateOne) ExecX(ctx context.Context) { // check runs all checks and user-defined validators on the builder. func (tuo *TaskUpdateOne) check() error { if v, ok := tuo.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -285,11 +286,11 @@ func (tuo *TaskUpdateOne) check() error { func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ - Table: task.Table, - Columns: task.Columns, + Table: enttask.Table, + Columns: enttask.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, - Column: task.FieldID, + Column: enttask.FieldID, }, }, } @@ -300,12 +301,12 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) _spec.Node.ID.Value = id if fields := tuo.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) - _spec.Node.Columns = append(_spec.Node.Columns, task.FieldID) + _spec.Node.Columns = append(_spec.Node.Columns, enttask.FieldID) for _, f := range fields { - if !task.ValidColumn(f) { + if !enttask.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } - if f != task.FieldID { + if f != enttask.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, f) } } @@ -321,14 +322,14 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, - Column: task.FieldPriority, + Column: enttask.FieldPriority, }) } if value, ok := tuo.mutation.AddedPriority(); ok { _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, - Column: task.FieldPriority, + Column: enttask.FieldPriority, }) } _node = &Task{config: tuo.config} @@ -336,7 +337,7 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) _spec.ScanValues = _node.scanValues if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{err.Error(), err} } diff --git a/entc/integration/gremlin/ent/client.go b/entc/integration/gremlin/ent/client.go index 74a041d12..e0d7db354 100644 --- a/entc/integration/gremlin/ent/client.go +++ b/entc/integration/gremlin/ent/client.go @@ -24,7 +24,7 @@ import ( "entgo.io/ent/entc/integration/gremlin/ent/node" "entgo.io/ent/entc/integration/gremlin/ent/pet" "entgo.io/ent/entc/integration/gremlin/ent/spec" - "entgo.io/ent/entc/integration/gremlin/ent/task" + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" "entgo.io/ent/entc/integration/gremlin/ent/user" "entgo.io/ent/dialect" @@ -1464,7 +1464,7 @@ func NewTaskClient(c config) *TaskClient { } // Use adds a list of mutation hooks to the hooks stack. -// A call to `Use(f, g, h)` equals to `task.Hooks(f(g(h())))`. +// A call to `Use(f, g, h)` equals to `enttask.Hooks(f(g(h())))`. func (c *TaskClient) Use(hooks ...Hook) { c.hooks.Task = append(c.hooks.Task, hooks...) } @@ -1511,7 +1511,7 @@ func (c *TaskClient) DeleteOne(t *Task) *TaskDeleteOne { // DeleteOneID returns a delete builder for the given id. func (c *TaskClient) DeleteOneID(id string) *TaskDeleteOne { - builder := c.Delete().Where(task.ID(id)) + builder := c.Delete().Where(enttask.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne return &TaskDeleteOne{builder} @@ -1526,7 +1526,7 @@ func (c *TaskClient) Query() *TaskQuery { // Get returns a Task entity by its id. func (c *TaskClient) Get(ctx context.Context, id string) (*Task, error) { - return c.Query().Where(task.ID(id)).Only(ctx) + return c.Query().Where(enttask.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 6314b7032..df8197592 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -18,6 +18,7 @@ import ( "entgo.io/ent/entc/integration/ent/role" "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/entc/integration/gremlin/ent/card" "entgo.io/ent/entc/integration/gremlin/ent/comment" "entgo.io/ent/entc/integration/gremlin/ent/fieldtype" @@ -30,7 +31,7 @@ import ( "entgo.io/ent/entc/integration/gremlin/ent/pet" "entgo.io/ent/entc/integration/gremlin/ent/predicate" "entgo.io/ent/entc/integration/gremlin/ent/spec" - "entgo.io/ent/entc/integration/gremlin/ent/task" + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" "entgo.io/ent/entc/integration/gremlin/ent/user" "github.com/google/uuid" @@ -12085,8 +12086,8 @@ type TaskMutation struct { op Op typ string id *string - priority *schema.Priority - addpriority *schema.Priority + priority *task.Priority + addpriority *task.Priority clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Task, error) @@ -12192,13 +12193,13 @@ func (m *TaskMutation) IDs(ctx context.Context) ([]string, error) { } // SetPriority sets the "priority" field. -func (m *TaskMutation) SetPriority(s schema.Priority) { - m.priority = &s +func (m *TaskMutation) SetPriority(t task.Priority) { + m.priority = &t m.addpriority = nil } // Priority returns the value of the "priority" field in the mutation. -func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { +func (m *TaskMutation) Priority() (r task.Priority, exists bool) { v := m.priority if v == nil { return @@ -12209,7 +12210,7 @@ func (m *TaskMutation) Priority() (r schema.Priority, exists bool) { // OldPriority returns the old "priority" field's value of the Task entity. // If the Task object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err error) { +func (m *TaskMutation) OldPriority(ctx context.Context) (v task.Priority, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldPriority is only allowed on UpdateOne operations") } @@ -12223,17 +12224,17 @@ func (m *TaskMutation) OldPriority(ctx context.Context) (v schema.Priority, err return oldValue.Priority, nil } -// AddPriority adds s to the "priority" field. -func (m *TaskMutation) AddPriority(s schema.Priority) { +// AddPriority adds t to the "priority" field. +func (m *TaskMutation) AddPriority(t task.Priority) { if m.addpriority != nil { - *m.addpriority += s + *m.addpriority += t } else { - m.addpriority = &s + m.addpriority = &t } } // AddedPriority returns the value that was added to the "priority" field in this mutation. -func (m *TaskMutation) AddedPriority() (r schema.Priority, exists bool) { +func (m *TaskMutation) AddedPriority() (r task.Priority, exists bool) { v := m.addpriority if v == nil { return @@ -12268,7 +12269,7 @@ func (m *TaskMutation) Type() string { func (m *TaskMutation) Fields() []string { fields := make([]string, 0, 1) if m.priority != nil { - fields = append(fields, task.FieldPriority) + fields = append(fields, enttask.FieldPriority) } return fields } @@ -12278,7 +12279,7 @@ func (m *TaskMutation) Fields() []string { // schema. func (m *TaskMutation) Field(name string) (ent.Value, bool) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.Priority() } return nil, false @@ -12289,7 +12290,7 @@ func (m *TaskMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.OldPriority(ctx) } return nil, fmt.Errorf("unknown Task field %s", name) @@ -12300,8 +12301,8 @@ func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, er // type. func (m *TaskMutation) SetField(name string, value ent.Value) error { switch name { - case task.FieldPriority: - v, ok := value.(schema.Priority) + case enttask.FieldPriority: + v, ok := value.(task.Priority) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -12316,7 +12317,7 @@ func (m *TaskMutation) SetField(name string, value ent.Value) error { func (m *TaskMutation) AddedFields() []string { var fields []string if m.addpriority != nil { - fields = append(fields, task.FieldPriority) + fields = append(fields, enttask.FieldPriority) } return fields } @@ -12326,7 +12327,7 @@ func (m *TaskMutation) AddedFields() []string { // was not set, or was not defined in the schema. func (m *TaskMutation) AddedField(name string) (ent.Value, bool) { switch name { - case task.FieldPriority: + case enttask.FieldPriority: return m.AddedPriority() } return nil, false @@ -12337,8 +12338,8 @@ func (m *TaskMutation) AddedField(name string) (ent.Value, bool) { // type. func (m *TaskMutation) AddField(name string, value ent.Value) error { switch name { - case task.FieldPriority: - v, ok := value.(schema.Priority) + case enttask.FieldPriority: + v, ok := value.(task.Priority) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -12371,7 +12372,7 @@ func (m *TaskMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *TaskMutation) ResetField(name string) error { switch name { - case task.FieldPriority: + case enttask.FieldPriority: m.ResetPriority() return nil } diff --git a/entc/integration/gremlin/ent/predicate/predicate.go b/entc/integration/gremlin/ent/predicate/predicate.go index ab08f4f79..7c3371a71 100644 --- a/entc/integration/gremlin/ent/predicate/predicate.go +++ b/entc/integration/gremlin/ent/predicate/predicate.go @@ -46,7 +46,7 @@ type Pet func(*dsl.Traversal) // Spec is the predicate function for spec builders. type Spec func(*dsl.Traversal) -// Task is the predicate function for task builders. +// Task is the predicate function for enttask builders. type Task func(*dsl.Traversal) // User is the predicate function for user builders. diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 2715af4cf..902ef796d 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -13,6 +13,7 @@ import ( "time" "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/entc/integration/gremlin/ent/card" "entgo.io/ent/entc/integration/gremlin/ent/fieldtype" "entgo.io/ent/entc/integration/gremlin/ent/file" @@ -20,7 +21,7 @@ import ( "entgo.io/ent/entc/integration/gremlin/ent/groupinfo" "entgo.io/ent/entc/integration/gremlin/ent/item" "entgo.io/ent/entc/integration/gremlin/ent/pet" - "entgo.io/ent/entc/integration/gremlin/ent/task" + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" "entgo.io/ent/entc/integration/gremlin/ent/user" ) @@ -225,14 +226,14 @@ func init() { petDescAge := petFields[0].Descriptor() // pet.DefaultAge holds the default value on creation for the age field. pet.DefaultAge = petDescAge.Default.(float64) - taskFields := schema.Task{}.Fields() - _ = taskFields - // taskDescPriority is the schema descriptor for priority field. - taskDescPriority := taskFields[0].Descriptor() - // task.DefaultPriority holds the default value on creation for the priority field. - task.DefaultPriority = schema.Priority(taskDescPriority.Default.(int)) - // task.PriorityValidator is a validator for the "priority" field. It is called by the builders before save. - task.PriorityValidator = taskDescPriority.Validators[0].(func(int) error) + enttaskFields := schema.Task{}.Fields() + _ = enttaskFields + // enttaskDescPriority is the schema descriptor for priority field. + enttaskDescPriority := enttaskFields[0].Descriptor() + // enttask.DefaultPriority holds the default value on creation for the priority field. + enttask.DefaultPriority = task.Priority(enttaskDescPriority.Default.(int)) + // enttask.PriorityValidator is a validator for the "priority" field. It is called by the builders before save. + enttask.PriorityValidator = enttaskDescPriority.Validators[0].(func(int) error) userMixin := schema.User{}.Mixin() userMixinFields0 := userMixin[0].Fields() _ = userMixinFields0 diff --git a/entc/integration/gremlin/ent/task.go b/entc/integration/gremlin/ent/task.go index 144e589b9..3f1e60c2f 100644 --- a/entc/integration/gremlin/ent/task.go +++ b/entc/integration/gremlin/ent/task.go @@ -11,7 +11,7 @@ import ( "strings" "entgo.io/ent/dialect/gremlin" - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" ) // Task is the model entity for the Task schema. @@ -20,7 +20,7 @@ type Task struct { // ID of the ent. ID string `json:"id,omitempty"` // Priority holds the value of the "priority" field. - Priority schema.Priority `json:"priority,omitempty"` + Priority task.Priority `json:"priority,omitempty"` } // FromResponse scans the gremlin response data into Task. @@ -30,8 +30,8 @@ func (t *Task) FromResponse(res *gremlin.Response) error { return err } var scant struct { - ID string `json:"id,omitempty"` - Priority schema.Priority `json:"priority,omitempty"` + ID string `json:"id,omitempty"` + Priority task.Priority `json:"priority,omitempty"` } if err := vmap.Decode(&scant); err != nil { return err @@ -80,8 +80,8 @@ func (t *Tasks) FromResponse(res *gremlin.Response) error { return err } var scant []struct { - ID string `json:"id,omitempty"` - Priority schema.Priority `json:"priority,omitempty"` + ID string `json:"id,omitempty"` + Priority task.Priority `json:"priority,omitempty"` } if err := vmap.Decode(&scant); err != nil { return err diff --git a/entc/integration/gremlin/ent/task/task.go b/entc/integration/gremlin/ent/task/task.go index f1944064b..d1708ee6c 100644 --- a/entc/integration/gremlin/ent/task/task.go +++ b/entc/integration/gremlin/ent/task/task.go @@ -4,10 +4,10 @@ // Code generated by entc, DO NOT EDIT. -package task +package enttask import ( - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" ) const ( @@ -21,7 +21,7 @@ const ( var ( // DefaultPriority holds the default value on creation for the "priority" field. - DefaultPriority schema.Priority + DefaultPriority task.Priority // PriorityValidator is a validator for the "priority" field. It is called by the builders before save. PriorityValidator func(int) error ) diff --git a/entc/integration/gremlin/ent/task/where.go b/entc/integration/gremlin/ent/task/where.go index 10c8add0e..c4dce931a 100644 --- a/entc/integration/gremlin/ent/task/where.go +++ b/entc/integration/gremlin/ent/task/where.go @@ -4,13 +4,13 @@ // Code generated by entc, DO NOT EDIT. -package task +package enttask import ( "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/p" - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/entc/integration/gremlin/ent/predicate" ) @@ -86,7 +86,7 @@ func IDLTE(id string) predicate.Task { } // Priority applies equality check predicate on the "priority" field. It's identical to PriorityEQ. -func Priority(v schema.Priority) predicate.Task { +func Priority(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.EQ(vc)) @@ -94,7 +94,7 @@ func Priority(v schema.Priority) predicate.Task { } // PriorityEQ applies the EQ predicate on the "priority" field. -func PriorityEQ(v schema.Priority) predicate.Task { +func PriorityEQ(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.EQ(vc)) @@ -102,7 +102,7 @@ func PriorityEQ(v schema.Priority) predicate.Task { } // PriorityNEQ applies the NEQ predicate on the "priority" field. -func PriorityNEQ(v schema.Priority) predicate.Task { +func PriorityNEQ(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.NEQ(vc)) @@ -110,7 +110,7 @@ func PriorityNEQ(v schema.Priority) predicate.Task { } // PriorityIn applies the In predicate on the "priority" field. -func PriorityIn(vs ...schema.Priority) predicate.Task { +func PriorityIn(vs ...task.Priority) predicate.Task { v := make([]interface{}, len(vs)) for i := range v { v[i] = int(vs[i]) @@ -121,7 +121,7 @@ func PriorityIn(vs ...schema.Priority) predicate.Task { } // PriorityNotIn applies the NotIn predicate on the "priority" field. -func PriorityNotIn(vs ...schema.Priority) predicate.Task { +func PriorityNotIn(vs ...task.Priority) predicate.Task { v := make([]interface{}, len(vs)) for i := range v { v[i] = int(vs[i]) @@ -132,7 +132,7 @@ func PriorityNotIn(vs ...schema.Priority) predicate.Task { } // PriorityGT applies the GT predicate on the "priority" field. -func PriorityGT(v schema.Priority) predicate.Task { +func PriorityGT(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.GT(vc)) @@ -140,7 +140,7 @@ func PriorityGT(v schema.Priority) predicate.Task { } // PriorityGTE applies the GTE predicate on the "priority" field. -func PriorityGTE(v schema.Priority) predicate.Task { +func PriorityGTE(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.GTE(vc)) @@ -148,7 +148,7 @@ func PriorityGTE(v schema.Priority) predicate.Task { } // PriorityLT applies the LT predicate on the "priority" field. -func PriorityLT(v schema.Priority) predicate.Task { +func PriorityLT(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.LT(vc)) @@ -156,7 +156,7 @@ func PriorityLT(v schema.Priority) predicate.Task { } // PriorityLTE applies the LTE predicate on the "priority" field. -func PriorityLTE(v schema.Priority) predicate.Task { +func PriorityLTE(v task.Priority) predicate.Task { vc := int(v) return predicate.Task(func(t *dsl.Traversal) { t.Has(Label, FieldPriority, p.LTE(vc)) diff --git a/entc/integration/gremlin/ent/task_create.go b/entc/integration/gremlin/ent/task_create.go index 9753d05b0..4192e5333 100644 --- a/entc/integration/gremlin/ent/task_create.go +++ b/entc/integration/gremlin/ent/task_create.go @@ -14,8 +14,9 @@ import ( "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/g" - "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/gremlin/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" + + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" ) // TaskCreate is the builder for creating a Task entity. @@ -26,15 +27,15 @@ type TaskCreate struct { } // SetPriority sets the "priority" field. -func (tc *TaskCreate) SetPriority(s schema.Priority) *TaskCreate { - tc.mutation.SetPriority(s) +func (tc *TaskCreate) SetPriority(t task.Priority) *TaskCreate { + tc.mutation.SetPriority(t) return tc } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tc *TaskCreate) SetNillablePriority(s *schema.Priority) *TaskCreate { - if s != nil { - tc.SetPriority(*s) +func (tc *TaskCreate) SetNillablePriority(t *task.Priority) *TaskCreate { + if t != nil { + tc.SetPriority(*t) } return tc } @@ -111,7 +112,7 @@ func (tc *TaskCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (tc *TaskCreate) defaults() { if _, ok := tc.mutation.Priority(); !ok { - v := task.DefaultPriority + v := enttask.DefaultPriority tc.mutation.SetPriority(v) } } @@ -122,7 +123,7 @@ func (tc *TaskCreate) check() error { return &ValidationError{Name: "priority", err: errors.New(`ent: missing required field "Task.priority"`)} } if v, ok := tc.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -146,9 +147,9 @@ func (tc *TaskCreate) gremlinSave(ctx context.Context) (*Task, error) { } func (tc *TaskCreate) gremlin() *dsl.Traversal { - v := g.AddV(task.Label) + v := g.AddV(enttask.Label) if value, ok := tc.mutation.Priority(); ok { - v.Property(dsl.Single, task.FieldPriority, value) + v.Property(dsl.Single, enttask.FieldPriority, value) } return v.ValueMap(true) } diff --git a/entc/integration/gremlin/ent/task_delete.go b/entc/integration/gremlin/ent/task_delete.go index 09754dd71..aeca52fe4 100644 --- a/entc/integration/gremlin/ent/task_delete.go +++ b/entc/integration/gremlin/ent/task_delete.go @@ -15,7 +15,8 @@ import ( "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" "entgo.io/ent/entc/integration/gremlin/ent/predicate" - "entgo.io/ent/entc/integration/gremlin/ent/task" + + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" ) // TaskDelete is the builder for deleting a Task entity. @@ -82,7 +83,7 @@ func (td *TaskDelete) gremlinExec(ctx context.Context) (int, error) { } func (td *TaskDelete) gremlin() *dsl.Traversal { - t := g.V().HasLabel(task.Label) + t := g.V().HasLabel(enttask.Label) for _, p := range td.mutation.predicates { p(t) } @@ -101,7 +102,7 @@ func (tdo *TaskDeleteOne) Exec(ctx context.Context) error { case err != nil: return err case n == 0: - return &NotFoundError{task.Label} + return &NotFoundError{enttask.Label} default: return nil } diff --git a/entc/integration/gremlin/ent/task_query.go b/entc/integration/gremlin/ent/task_query.go index 240e84552..118bb93bb 100644 --- a/entc/integration/gremlin/ent/task_query.go +++ b/entc/integration/gremlin/ent/task_query.go @@ -17,7 +17,8 @@ import ( "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" "entgo.io/ent/entc/integration/gremlin/ent/predicate" - "entgo.io/ent/entc/integration/gremlin/ent/task" + + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" ) // TaskQuery is the builder for querying Task entities. @@ -73,7 +74,7 @@ func (tq *TaskQuery) First(ctx context.Context) (*Task, error) { return nil, err } if len(nodes) == 0 { - return nil, &NotFoundError{task.Label} + return nil, &NotFoundError{enttask.Label} } return nodes[0], nil } @@ -95,7 +96,7 @@ func (tq *TaskQuery) FirstID(ctx context.Context) (id string, err error) { return } if len(ids) == 0 { - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} return } return ids[0], nil @@ -122,9 +123,9 @@ func (tq *TaskQuery) Only(ctx context.Context) (*Task, error) { case 1: return nodes[0], nil case 0: - return nil, &NotFoundError{task.Label} + return nil, &NotFoundError{enttask.Label} default: - return nil, &NotSingularError{task.Label} + return nil, &NotSingularError{enttask.Label} } } @@ -149,9 +150,9 @@ func (tq *TaskQuery) OnlyID(ctx context.Context) (id string, err error) { case 1: id = ids[0] case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: - err = &NotSingularError{task.Label} + err = &NotSingularError{enttask.Label} } return } @@ -185,7 +186,7 @@ func (tq *TaskQuery) AllX(ctx context.Context) []*Task { // IDs executes the query and returns a list of Task IDs. func (tq *TaskQuery) IDs(ctx context.Context) ([]string, error) { var ids []string - if err := tq.Select(task.FieldID).Scan(ctx, &ids); err != nil { + if err := tq.Select(enttask.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil @@ -258,12 +259,12 @@ func (tq *TaskQuery) Clone() *TaskQuery { // Example: // // var v []struct { -// Priority schema.Priority `json:"priority,omitempty"` +// Priority task.Priority `json:"priority,omitempty"` // Count int `json:"count,omitempty"` // } // // client.Task.Query(). -// GroupBy(task.FieldPriority). +// GroupBy(enttask.FieldPriority). // Aggregate(ent.Count()). // Scan(ctx, &v) // @@ -285,11 +286,11 @@ func (tq *TaskQuery) GroupBy(field string, fields ...string) *TaskGroupBy { // Example: // // var v []struct { -// Priority schema.Priority `json:"priority,omitempty"` +// Priority task.Priority `json:"priority,omitempty"` // } // // client.Task.Query(). -// Select(task.FieldPriority). +// Select(enttask.FieldPriority). // Scan(ctx, &v) // func (tq *TaskQuery) Select(fields ...string) *TaskSelect { @@ -351,7 +352,7 @@ func (tq *TaskQuery) gremlinExist(ctx context.Context) (bool, error) { } func (tq *TaskQuery) gremlinQuery(context.Context) *dsl.Traversal { - v := g.V().HasLabel(task.Label) + v := g.V().HasLabel(enttask.Label) if tq.gremlin != nil { v = tq.gremlin.Clone() } @@ -444,7 +445,7 @@ func (tgb *TaskGroupBy) String(ctx context.Context) (_ string, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Strings returned %d results when one was expected", len(v)) } @@ -493,7 +494,7 @@ func (tgb *TaskGroupBy) Int(ctx context.Context) (_ int, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Ints returned %d results when one was expected", len(v)) } @@ -542,7 +543,7 @@ func (tgb *TaskGroupBy) Float64(ctx context.Context) (_ float64, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Float64s returned %d results when one was expected", len(v)) } @@ -591,7 +592,7 @@ func (tgb *TaskGroupBy) Bool(ctx context.Context) (_ bool, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskGroupBy.Bools returned %d results when one was expected", len(v)) } @@ -698,7 +699,7 @@ func (ts *TaskSelect) String(ctx context.Context) (_ string, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Strings returned %d results when one was expected", len(v)) } @@ -745,7 +746,7 @@ func (ts *TaskSelect) Int(ctx context.Context) (_ int, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Ints returned %d results when one was expected", len(v)) } @@ -792,7 +793,7 @@ func (ts *TaskSelect) Float64(ctx context.Context) (_ float64, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Float64s returned %d results when one was expected", len(v)) } @@ -839,7 +840,7 @@ func (ts *TaskSelect) Bool(ctx context.Context) (_ bool, err error) { case 1: return v[0], nil case 0: - err = &NotFoundError{task.Label} + err = &NotFoundError{enttask.Label} default: err = fmt.Errorf("ent: TaskSelect.Bools returned %d results when one was expected", len(v)) } @@ -861,7 +862,7 @@ func (ts *TaskSelect) gremlinScan(ctx context.Context, v interface{}) error { res = &gremlin.Response{} ) if len(ts.fields) == 1 { - if ts.fields[0] != task.FieldID { + if ts.fields[0] != enttask.FieldID { traversal = ts.gremlin.Values(ts.fields...) } else { traversal = ts.gremlin.ID() diff --git a/entc/integration/gremlin/ent/task_update.go b/entc/integration/gremlin/ent/task_update.go index 5e6ba90c4..7252486e6 100644 --- a/entc/integration/gremlin/ent/task_update.go +++ b/entc/integration/gremlin/ent/task_update.go @@ -15,9 +15,10 @@ import ( "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" - "entgo.io/ent/entc/integration/ent/schema" + "entgo.io/ent/entc/integration/ent/schema/task" "entgo.io/ent/entc/integration/gremlin/ent/predicate" - "entgo.io/ent/entc/integration/gremlin/ent/task" + + enttask "entgo.io/ent/entc/integration/gremlin/ent/task" ) // TaskUpdate is the builder for updating Task entities. @@ -34,23 +35,23 @@ func (tu *TaskUpdate) Where(ps ...predicate.Task) *TaskUpdate { } // SetPriority sets the "priority" field. -func (tu *TaskUpdate) SetPriority(s schema.Priority) *TaskUpdate { +func (tu *TaskUpdate) SetPriority(t task.Priority) *TaskUpdate { tu.mutation.ResetPriority() - tu.mutation.SetPriority(s) + tu.mutation.SetPriority(t) return tu } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tu *TaskUpdate) SetNillablePriority(s *schema.Priority) *TaskUpdate { - if s != nil { - tu.SetPriority(*s) +func (tu *TaskUpdate) SetNillablePriority(t *task.Priority) *TaskUpdate { + if t != nil { + tu.SetPriority(*t) } return tu } -// AddPriority adds s to the "priority" field. -func (tu *TaskUpdate) AddPriority(s schema.Priority) *TaskUpdate { - tu.mutation.AddPriority(s) +// AddPriority adds t to the "priority" field. +func (tu *TaskUpdate) AddPriority(t task.Priority) *TaskUpdate { + tu.mutation.AddPriority(t) return tu } @@ -122,7 +123,7 @@ func (tu *TaskUpdate) ExecX(ctx context.Context) { // check runs all checks and user-defined validators on the builder. func (tu *TaskUpdate) check() error { if v, ok := tu.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -142,7 +143,7 @@ func (tu *TaskUpdate) gremlinSave(ctx context.Context) (int, error) { } func (tu *TaskUpdate) gremlin() *dsl.Traversal { - v := g.V().HasLabel(task.Label) + v := g.V().HasLabel(enttask.Label) for _, p := range tu.mutation.predicates { p(v) } @@ -150,10 +151,10 @@ func (tu *TaskUpdate) gremlin() *dsl.Traversal { trs []*dsl.Traversal ) if value, ok := tu.mutation.Priority(); ok { - v.Property(dsl.Single, task.FieldPriority, value) + v.Property(dsl.Single, enttask.FieldPriority, value) } if value, ok := tu.mutation.AddedPriority(); ok { - v.Property(dsl.Single, task.FieldPriority, __.Union(__.Values(task.FieldPriority), __.Constant(value)).Sum()) + v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum()) } v.Count() trs = append(trs, v) @@ -169,23 +170,23 @@ type TaskUpdateOne struct { } // SetPriority sets the "priority" field. -func (tuo *TaskUpdateOne) SetPriority(s schema.Priority) *TaskUpdateOne { +func (tuo *TaskUpdateOne) SetPriority(t task.Priority) *TaskUpdateOne { tuo.mutation.ResetPriority() - tuo.mutation.SetPriority(s) + tuo.mutation.SetPriority(t) return tuo } // SetNillablePriority sets the "priority" field if the given value is not nil. -func (tuo *TaskUpdateOne) SetNillablePriority(s *schema.Priority) *TaskUpdateOne { - if s != nil { - tuo.SetPriority(*s) +func (tuo *TaskUpdateOne) SetNillablePriority(t *task.Priority) *TaskUpdateOne { + if t != nil { + tuo.SetPriority(*t) } return tuo } -// AddPriority adds s to the "priority" field. -func (tuo *TaskUpdateOne) AddPriority(s schema.Priority) *TaskUpdateOne { - tuo.mutation.AddPriority(s) +// AddPriority adds t to the "priority" field. +func (tuo *TaskUpdateOne) AddPriority(t task.Priority) *TaskUpdateOne { + tuo.mutation.AddPriority(t) return tuo } @@ -264,7 +265,7 @@ func (tuo *TaskUpdateOne) ExecX(ctx context.Context) { // check runs all checks and user-defined validators on the builder. func (tuo *TaskUpdateOne) check() error { if v, ok := tuo.mutation.Priority(); ok { - if err := task.PriorityValidator(int(v)); err != nil { + if err := enttask.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)} } } @@ -297,10 +298,10 @@ func (tuo *TaskUpdateOne) gremlin(id string) *dsl.Traversal { trs []*dsl.Traversal ) if value, ok := tuo.mutation.Priority(); ok { - v.Property(dsl.Single, task.FieldPriority, value) + v.Property(dsl.Single, enttask.FieldPriority, value) } if value, ok := tuo.mutation.AddedPriority(); ok { - v.Property(dsl.Single, task.FieldPriority, __.Union(__.Values(task.FieldPriority), __.Constant(value)).Sum()) + v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum()) } if len(tuo.fields) > 0 { fields := make([]interface{}, 0, len(tuo.fields)+1) diff --git a/entc/integration/type_test.go b/entc/integration/type_test.go index eadd65fd4..62c0693d1 100644 --- a/entc/integration/type_test.go +++ b/entc/integration/type_test.go @@ -18,7 +18,8 @@ import ( "entgo.io/ent/entc/integration/ent/fieldtype" "entgo.io/ent/entc/integration/ent/role" "entgo.io/ent/entc/integration/ent/schema" - "entgo.io/ent/entc/integration/ent/task" + "entgo.io/ent/entc/integration/ent/schema/task" + enttask "entgo.io/ent/entc/integration/ent/task" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -209,21 +210,21 @@ func Types(t *testing.T, client *ent.Client) { require.False(ft.DeletedAt.Time.IsZero()) err = client.Task.CreateBulk( - client.Task.Create().SetPriority(schema.PriorityLow), - client.Task.Create().SetPriority(schema.PriorityMid), - client.Task.Create().SetPriority(schema.PriorityHigh), + client.Task.Create().SetPriority(task.PriorityLow), + client.Task.Create().SetPriority(task.PriorityMid), + client.Task.Create().SetPriority(task.PriorityHigh), ).Exec(ctx) require.NoError(err) - err = client.Task.Create().SetPriority(schema.Priority(10)).Exec(ctx) + err = client.Task.Create().SetPriority(task.Priority(10)).Exec(ctx) require.Error(err) - tasks := client.Task.Query().Order(ent.Asc(task.FieldPriority)).AllX(ctx) - require.Equal(schema.PriorityLow, tasks[0].Priority) - require.Equal(schema.PriorityMid, tasks[1].Priority) - require.Equal(schema.PriorityHigh, tasks[2].Priority) + tasks := client.Task.Query().Order(ent.Asc(enttask.FieldPriority)).AllX(ctx) + require.Equal(task.PriorityLow, tasks[0].Priority) + require.Equal(task.PriorityMid, tasks[1].Priority) + require.Equal(task.PriorityHigh, tasks[2].Priority) - tasks = client.Task.Query().Order(ent.Desc(task.FieldPriority)).AllX(ctx) - require.Equal(schema.PriorityLow, tasks[2].Priority) - require.Equal(schema.PriorityMid, tasks[1].Priority) - require.Equal(schema.PriorityHigh, tasks[0].Priority) + tasks = client.Task.Query().Order(ent.Desc(enttask.FieldPriority)).AllX(ctx) + require.Equal(task.PriorityLow, tasks[2].Priority) + require.Equal(task.PriorityMid, tasks[1].Priority) + require.Equal(task.PriorityHigh, tasks[0].Priority) }