// 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" "encoding/json" "errors" "fmt" "net/http" "net/url" "sync" "entgo.io/ent" "entgo.io/ent/entc/integration/json/ent/schema" "entgo.io/ent/entc/integration/json/ent/user" ) const ( // Operation types. OpCreate = ent.OpCreate OpDelete = ent.OpDelete OpDeleteOne = ent.OpDeleteOne OpUpdate = ent.OpUpdate OpUpdateOne = ent.OpUpdateOne // Node types. TypeUser = "User" ) // 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()) } } // OldT returns the old "t" 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) OldT(ctx context.Context) (v *schema.T, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldT is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldT requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldT: %w", err) } return oldValue.T, nil } // OldURL returns the old "url" 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) OldURL(ctx context.Context) (v *url.URL, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldURL is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldURL requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldURL: %w", err) } return oldValue.URL, nil } // OldURLs returns the old "URLs" 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) OldURLs(ctx context.Context) (v []*url.URL, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldURLs is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldURLs requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldURLs: %w", err) } return oldValue.URLs, nil } // OldRaw returns the old "raw" 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) OldRaw(ctx context.Context) (v json.RawMessage, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldRaw is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldRaw requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldRaw: %w", err) } return oldValue.Raw, nil } // OldDirs returns the old "dirs" 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) OldDirs(ctx context.Context) (v []http.Dir, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldDirs is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldDirs requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldDirs: %w", err) } return oldValue.Dirs, nil } // OldInts returns the old "ints" 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) OldInts(ctx context.Context) (v []int, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldInts is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldInts requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldInts: %w", err) } return oldValue.Ints, nil } // OldFloats returns the old "floats" 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) OldFloats(ctx context.Context) (v []float64, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldFloats is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldFloats requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldFloats: %w", err) } return oldValue.Floats, nil } // OldStrings returns the old "strings" 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) OldStrings(ctx context.Context) (v []string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldStrings is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldStrings requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldStrings: %w", err) } return oldValue.Strings, nil } // OldIntsValidate returns the old "ints_validate" 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) OldIntsValidate(ctx context.Context) (v []int, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldIntsValidate is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldIntsValidate requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldIntsValidate: %w", err) } return oldValue.IntsValidate, nil } // OldFloatsValidate returns the old "floats_validate" 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) OldFloatsValidate(ctx context.Context) (v []float64, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldFloatsValidate is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldFloatsValidate requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldFloatsValidate: %w", err) } return oldValue.FloatsValidate, nil } // OldStringsValidate returns the old "strings_validate" 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) OldStringsValidate(ctx context.Context) (v []string, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldStringsValidate is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldStringsValidate requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldStringsValidate: %w", err) } return oldValue.StringsValidate, nil } // OldAddr returns the old "addr" 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) OldAddr(ctx context.Context) (v schema.Addr, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldAddr is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldAddr requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldAddr: %w", err) } return oldValue.Addr, nil } // OldUnknown returns the old "unknown" 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) OldUnknown(ctx context.Context) (v any, err error) { if !m.Op().Is(OpUpdateOne) { return v, errors.New("OldUnknown is only allowed on UpdateOne operations") } if _, exists := m.ID(); !exists || m.oldValue == nil { return v, errors.New("OldUnknown requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { return v, fmt.Errorf("querying old value for OldUnknown: %w", err) } return oldValue.Unknown, 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.FieldT: return m.OldT(ctx) case user.FieldURL: return m.OldURL(ctx) case user.FieldURLs: return m.OldURLs(ctx) case user.FieldRaw: return m.OldRaw(ctx) case user.FieldDirs: return m.OldDirs(ctx) case user.FieldInts: return m.OldInts(ctx) case user.FieldFloats: return m.OldFloats(ctx) case user.FieldStrings: return m.OldStrings(ctx) case user.FieldIntsValidate: return m.OldIntsValidate(ctx) case user.FieldFloatsValidate: return m.OldFloatsValidate(ctx) case user.FieldStringsValidate: return m.OldStringsValidate(ctx) case user.FieldAddr: return m.OldAddr(ctx) case user.FieldUnknown: return m.OldUnknown(ctx) } return nil, fmt.Errorf("unknown User field %s", name) }