// 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. // Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "sync" "entgo.io/ent" "entgo.io/ent/entc/integration/privacy/ent/task" "entgo.io/ent/entc/integration/privacy/ent/team" "entgo.io/ent/entc/integration/privacy/ent/user" "github.com/google/uuid" ) const ( // Operation types. OpCreate = ent.OpCreate OpDelete = ent.OpDelete OpDeleteOne = ent.OpDeleteOne OpUpdate = ent.OpUpdate OpUpdateOne = ent.OpUpdateOne // Node types. TypeTask = "Task" TypeTeam = "Team" TypeUser = "User" ) // TaskMutation represents an operation that mutates the Task nodes in the graph. type TaskMutation struct { task.Mutation config id *int done bool oldValue func(context.Context) (*Task, error) } var _ ent.Mutation = (*TaskMutation)(nil) // taskOption allows management of the mutation configuration using functional options. type taskOption func(*TaskMutation) // newTaskMutation creates new mutation for the Task entity. func newTaskMutation(c config, op Op, opts ...taskOption) *TaskMutation { m := &TaskMutation{ Mutation: *task.NewMutation(op), config: c, } for _, opt := range opts { opt(m) } return m } // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. func (m *TaskMutation) ID() (id int, exists bool) { if m.id == nil { return } return *m.id, true } // withTaskID sets the ID field of the mutation. func withTaskID(id int) taskOption { return func(m *TaskMutation) { var ( err error once sync.Once value *Task ) m.oldValue = func(ctx context.Context) (*Task, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { value, err = m.Client().Task.Get(ctx, id) } }) return value, err } m.id = &id } } // withTask sets the old Task of the mutation. func withTask(node *Task) taskOption { return func(m *TaskMutation) { m.oldValue = func(context.Context) (*Task, error) { return node, nil } m.id = &node.ID } } // Client returns a new `ent.Client` from the mutation. If the mutation was // executed in a transaction (ent.Tx), a transactional client is returned. func (m TaskMutation) Client() *Client { client := &Client{config: m.config} client.init() return client } // Tx returns an `ent.Tx` for mutations that were executed in transactions; // it returns an error otherwise. func (m TaskMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("ent: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() return tx, nil } // IDs queries the database and returns the entity ids that match the mutation's predicate. // That means, if the mutation is applied within a transaction with an isolation level such // as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated // or updated by the mutation. func (m *TaskMutation) IDs(ctx context.Context) ([]int, error) { switch { case m.Op().Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() if exists { return []int{id}, nil } fallthrough case m.Op().Is(OpUpdate | OpDelete): return m.Client().Task.Query().Where(m.Predicates()...).IDs(ctx) default: return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op()) } } // OldTitle returns the old "title" 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) OldTitle(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldTitle is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldTitle requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldTitle: %w", err) } return oldValue.Title, nil } // OldDescription returns the old "description" 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) OldDescription(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldDescription is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldDescription requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldDescription: %w", err) } return oldValue.Description, nil } // OldStatus returns the old "status" 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) OldStatus(ctx context.Context) (v task.Status, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldStatus is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldStatus requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldStatus: %w", err) } return oldValue.Status, nil } // OldUUID returns the old "uuid" 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) OldUUID(ctx context.Context) (v uuid.UUID, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldUUID is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldUUID requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldUUID: %w", err) } return oldValue.UUID, nil } // OldField returns the old value of the field from the database. An error is // returned if the mutation operation is not UpdateOne, or the query to the // database failed. func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { case task.FieldTitle: return m.OldTitle(ctx) case task.FieldDescription: return m.OldDescription(ctx) case task.FieldStatus: return m.OldStatus(ctx) case task.FieldUUID: return m.OldUUID(ctx) } return nil, fmt.Errorf("unknown Task field %s", name) } // TeamMutation represents an operation that mutates the Team nodes in the graph. type TeamMutation struct { team.Mutation config id *int done bool oldValue func(context.Context) (*Team, error) } var _ ent.Mutation = (*TeamMutation)(nil) // teamOption allows management of the mutation configuration using functional options. type teamOption func(*TeamMutation) // newTeamMutation creates new mutation for the Team entity. func newTeamMutation(c config, op Op, opts ...teamOption) *TeamMutation { m := &TeamMutation{ Mutation: *team.NewMutation(op), config: c, } for _, opt := range opts { opt(m) } return m } // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. func (m *TeamMutation) ID() (id int, exists bool) { if m.id == nil { return } return *m.id, true } // withTeamID sets the ID field of the mutation. func withTeamID(id int) teamOption { return func(m *TeamMutation) { var ( err error once sync.Once value *Team ) m.oldValue = func(ctx context.Context) (*Team, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { value, err = m.Client().Team.Get(ctx, id) } }) return value, err } m.id = &id } } // withTeam sets the old Team of the mutation. func withTeam(node *Team) teamOption { return func(m *TeamMutation) { m.oldValue = func(context.Context) (*Team, error) { return node, nil } m.id = &node.ID } } // Client returns a new `ent.Client` from the mutation. If the mutation was // executed in a transaction (ent.Tx), a transactional client is returned. func (m TeamMutation) Client() *Client { client := &Client{config: m.config} client.init() return client } // Tx returns an `ent.Tx` for mutations that were executed in transactions; // it returns an error otherwise. func (m TeamMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("ent: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() return tx, nil } // IDs queries the database and returns the entity ids that match the mutation's predicate. // That means, if the mutation is applied within a transaction with an isolation level such // as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated // or updated by the mutation. func (m *TeamMutation) IDs(ctx context.Context) ([]int, error) { switch { case m.Op().Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() if exists { return []int{id}, nil } fallthrough case m.Op().Is(OpUpdate | OpDelete): return m.Client().Team.Query().Where(m.Predicates()...).IDs(ctx) default: return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op()) } } // OldName returns the old "name" field's value of the Team entity. // If the Team 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 *TeamMutation) OldName(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldName is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldName: %w", err) } return oldValue.Name, nil } // OldField returns the old value of the field from the database. An error is // returned if the mutation operation is not UpdateOne, or the query to the // database failed. func (m *TeamMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { case team.FieldName: return m.OldName(ctx) } return nil, fmt.Errorf("unknown Team field %s", name) } // UserMutation represents an operation that mutates the User nodes in the graph. type UserMutation struct { user.Mutation config id *int done bool oldValue func(context.Context) (*User, error) } var _ ent.Mutation = (*UserMutation)(nil) // userOption allows management of the mutation configuration using functional options. type userOption func(*UserMutation) // newUserMutation creates new mutation for the User entity. func newUserMutation(c config, op Op, opts ...userOption) *UserMutation { m := &UserMutation{ Mutation: *user.NewMutation(op), config: c, } for _, opt := range opts { opt(m) } return m } // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. func (m *UserMutation) ID() (id int, exists bool) { if m.id == nil { return } return *m.id, true } // withUserID sets the ID field of the mutation. func withUserID(id int) userOption { return func(m *UserMutation) { var ( err error once sync.Once value *User ) m.oldValue = func(ctx context.Context) (*User, error) { once.Do(func() { if m.done { err = errors.New("querying old values post mutation is not allowed") } else { value, err = m.Client().User.Get(ctx, id) } }) return value, err } m.id = &id } } // withUser sets the old User of the mutation. func withUser(node *User) userOption { return func(m *UserMutation) { m.oldValue = func(context.Context) (*User, error) { return node, nil } m.id = &node.ID } } // Client returns a new `ent.Client` from the mutation. If the mutation was // executed in a transaction (ent.Tx), a transactional client is returned. func (m UserMutation) Client() *Client { client := &Client{config: m.config} client.init() return client } // Tx returns an `ent.Tx` for mutations that were executed in transactions; // it returns an error otherwise. func (m UserMutation) Tx() (*Tx, error) { if _, ok := m.driver.(*txDriver); !ok { return nil, errors.New("ent: mutation is not running in a transaction") } tx := &Tx{config: m.config} tx.init() return tx, nil } // IDs queries the database and returns the entity ids that match the mutation's predicate. // That means, if the mutation is applied within a transaction with an isolation level such // as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated // or updated by the mutation. func (m *UserMutation) IDs(ctx context.Context) ([]int, error) { switch { case m.Op().Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() if exists { return []int{id}, nil } fallthrough case m.Op().Is(OpUpdate | OpDelete): return m.Client().User.Query().Where(m.Predicates()...).IDs(ctx) default: return nil, fmt.Errorf("IDs is not allowed on %s operations", m.Op()) } } // OldName returns the old "name" field's value of the User entity. // If the User 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 *UserMutation) OldName(ctx context.Context) (v string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldName is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldName requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldName: %w", err) } return oldValue.Name, nil } // OldAge returns the old "age" field's value of the User entity. // If the User 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 *UserMutation) OldAge(ctx context.Context) (v uint, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldAge is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldAge requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldAge: %w", err) } return oldValue.Age, nil } // OldField returns the old value of the field from the database. An error is // returned if the mutation operation is not UpdateOne, or the query to the // database failed. func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { case user.FieldName: return m.OldName(ctx) case user.FieldAge: return m.OldAge(ctx) } return nil, fmt.Errorf("unknown User field %s", name) }