func (m *CategoryMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
	switch name {
	case category.FieldName:
		return m.OldName(ctx)
	case category.FieldDescription:
		return m.OldDescription(ctx)
	}
	return nil, fmt.Errorf("unknown Category field %s", name)
}
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.
package ent
import (
	"context"
	"fmt"
	"sync"
	"time"
	"entgo.io/contrib/entproto/internal/entprototest/ent/blogpost"
	"entgo.io/contrib/entproto/internal/entprototest/ent/category"
	"entgo.io/contrib/entproto/internal/entprototest/ent/dependsonskipped"
	"entgo.io/contrib/entproto/internal/entprototest/ent/duplicatenumbermessage"
	"entgo.io/contrib/entproto/internal/entprototest/ent/image"
	"entgo.io/contrib/entproto/internal/entprototest/ent/invalidfieldmessage"
	"entgo.io/contrib/entproto/internal/entprototest/ent/messagewithenum"
	"entgo.io/contrib/entproto/internal/entprototest/ent/messagewithfieldone"
	"entgo.io/contrib/entproto/internal/entprototest/ent/messagewithoptionals"
	"entgo.io/contrib/entproto/internal/entprototest/ent/messagewithpackagename"
	"entgo.io/contrib/entproto/internal/entprototest/ent/portal"
	"entgo.io/contrib/entproto/internal/entprototest/ent/predicate"
	"entgo.io/contrib/entproto/internal/entprototest/ent/schema"
	"entgo.io/contrib/entproto/internal/entprototest/ent/user"
	"entgo.io/contrib/entproto/internal/entprototest/ent/validmessage"
	"github.com/google/uuid"
	"entgo.io/ent"
)
const (
	OpCreate    = ent.OpCreate
	OpDelete    = ent.OpDelete
	OpDeleteOne = ent.OpDeleteOne
	OpUpdate    = ent.OpUpdate
	OpUpdateOne = ent.OpUpdateOne
	TypeBlogPost               = "BlogPost"
	TypeCategory               = "Category"
	TypeDependsOnSkipped       = "DependsOnSkipped"
	TypeDuplicateNumberMessage = "DuplicateNumberMessage"
	TypeExplicitSkippedMessage = "ExplicitSkippedMessage"
	TypeImage                  = "Image"
	TypeImplicitSkippedMessage = "ImplicitSkippedMessage"
	TypeInvalidFieldMessage    = "InvalidFieldMessage"
	TypeMessageWithEnum        = "MessageWithEnum"
	TypeMessageWithFieldOne    = "MessageWithFieldOne"
	TypeMessageWithID          = "MessageWithID"
	TypeMessageWithOptionals   = "MessageWithOptionals"
	TypeMessageWithPackageName = "MessageWithPackageName"
	TypePortal                 = "Portal"
	TypeUser                   = "User"
	TypeValidMessage           = "ValidMessage"
)
type BlogPostMutation struct {
	config
	op                Op
	typ               string
	id                *int
	title             *string
	body              *string
	external_id       *int
	addexternal_id    *int
	clearedFields     map[string]struct{}
	author            *int
	clearedauthor     bool
	categories        map[int]struct{}
	removedcategories map[int]struct{}
	clearedcategories bool
	done              bool
	oldValue          func(context.Context) (*BlogPost, error)
	predicates        []predicate.BlogPost
}
var _ ent.Mutation = (*BlogPostMutation)(nil)
type blogpostOption func(*BlogPostMutation)
func newBlogPostMutation(c config, op Op, opts ...blogpostOption) *BlogPostMutation {
	m := &BlogPostMutation{
		config:        c,
		op:            op,
		typ:           TypeBlogPost,
		clearedFields: make(map[string]struct{}),
	}
	for _, opt := range opts {
		opt(m)
	}
	return m
}
func withBlogPostID(id int) blogpostOption {
	return func(m *BlogPostMutation) {
		var (
			err   error
			once  sync.Once
			value *BlogPost
		)
		m.oldValue = func(ctx context.Context) (*BlogPost, error) {
			once.Do(func() {
				if m.done {
					err = fmt.Errorf("querying old values post mutation is not allowed")
				} else {
					value, err = m.Client().BlogPost.Get(ctx, id)
				}
			})
			return value, err
		}
		m.id = &id
	}
}
func withBlogPost(node *BlogPost) blogpostOption {
	return func(m *BlogPostMutation) {
		m.oldValue = func(context.Context) (*BlogPost, error) {
			return node, nil
		}
		m.id = &node.ID
	}
}
func (m BlogPostMutation) Client() *Client {
	client := &Client{config: m.config}
	client.init()
	return client
}
func (m BlogPostMutation) Tx() (*Tx, error) {
	if _, ok := m.driver.(*txDriver); !ok {
		return nil, fmt.Errorf("ent: mutation is not running in a transaction")
	}
	tx := &Tx{config: m.config}
	tx.init()
	return tx, nil
}
func (m *BlogPostMutation) ID() (id int, exists bool) {
	if m.id == nil {
		return
	}
	return *m.id, true
}
func (m *BlogPostMutation) SetTitle(s string) {
	m.title = &s
}
func (m *BlogPostMutation) Title() (r string, exists bool) {
	v := m.title
	if v == nil {
		return
	}
	return *v, true
}
func (m *BlogPostMutation) OldTitle(ctx context.Context) (v string, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, fmt.Errorf("OldTitle is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, fmt.Errorf("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
}
func (m *BlogPostMutation) ResetTitle() {
	m.title = nil
}
func (m *BlogPostMutation) SetBody(s string) {
	m.body = &s
}
func (m *BlogPostMutation) Body() (r string, exists bool) {
	v := m.body
	if v == nil {
		return
	}
	return *v, true
}
func (m *BlogPostMutation) OldBody(ctx context.Context) (v string, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, fmt.Errorf("OldBody is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, fmt.Errorf("OldBody requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldBody: %w", err)
	}
	return oldValue.Body, nil
}
func (m *BlogPostMutation) ResetBody() {
	m.body = nil
}
func (m *BlogPostMutation) SetExternalID(i int) {
	m.external_id = &i
	m.addexternal_id = nil
}
func (m *BlogPostMutation) ExternalID() (r int, exists bool) {
	v := m.external_id
	if v == nil {
		return
	}
	return *v, true
}
func (m *BlogPostMutation) OldExternalID(ctx context.Context) (v int, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, fmt.Errorf("OldExternalID is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, fmt.Errorf("OldExternalID requires an ID field in the mutation")
	}
	oldValue, err := m.oldValue(ctx)
	if err != nil {
		return v, fmt.Errorf("querying old value for OldExternalID: %w", err)
	}
	return oldValue.ExternalID, nil
}
func (m *BlogPostMutation) AddExternalID(i int) {
	if m.addexternal_id != nil {
		*m.addexternal_id += i
	} else {
		m.addexternal_id = &i
	}
}
func (m *BlogPostMutation) AddedExternalID() (r int, exists bool) {
	v := m.addexternal_id
	if v == nil {
		return
	}
	return *v, true
}
func (m *BlogPostMutation) ResetExternalID() {
	m.external_id = nil
	m.addexternal_id = nil
}
func (m *BlogPostMutation) SetAuthorID(id int) {
	m.author = &id
}
func (m *BlogPostMutation) ClearAuthor() {
	m.clearedauthor = true
}
func (m *BlogPostMutation) AuthorCleared() bool {
	return m.clearedauthor
}
func (m *BlogPostMutation) AuthorID() (id int, exists bool) {
	if m.author != nil {
		return *m.author, true
	}
	return
}
func (m *BlogPostMutation) AuthorIDs() (ids []int) {
	if id := m.author; id != nil {
		ids = append(ids, *id)
	}
	return
}
func (m *BlogPostMutation) ResetAuthor() {
	m.author = nil
	m.clearedauthor = false
}
func (m *BlogPostMutation) AddCategoryIDs(ids ...int) {
	if m.categories == nil {
		m.categories = make(map[int]struct{})
	}
	for i := range ids {
		m.categories[ids[i]] = struct{}{}
	}
}
func (m *BlogPostMutation) ClearCategories() {
	m.clearedcategories = true
}
func (m *BlogPostMutation) CategoriesCleared() bool {
	return m.clearedcategories
}
func (m *BlogPostMutation) RemoveCategoryIDs(ids ...int) {
	if m.removedcategories == nil {
		m.removedcategories = make(map[int]struct{})
	}
	for i := range ids {
		delete(m.categories, ids[i])
		m.removedcategories[ids[i]] = struct{}{}
	}
}
func (m *BlogPostMutation) RemovedCategoriesIDs() (ids []int) {
	for id := range m.removedcategories {
		ids = append(ids, id)
	}
	return
}
func (m *BlogPostMutation) CategoriesIDs() (ids []int) {
	for id := range m.categories {
		ids = append(ids, id)
	}
	return
}
func (m *BlogPostMutation) ResetCategories() {
	m.categories = nil
	m.clearedcategories = false
	m.removedcategories = nil
}
func (m *BlogPostMutation) Where(ps ...predicate.BlogPost) {
	m.predicates = append(m.predicates, ps...)
}
func (m *BlogPostMutation) Op() Op {
	return m.op
}
func (m *BlogPostMutation) Type() string {
	return m.typ
}
func (m *BlogPostMutation) Fields() []string {
	fields := make([]string, 0, 3)
	if m.title != nil {
		fields = append(fields, blogpost.FieldTitle)
	}
	if m.body != nil {
		fields = append(fields, blogpost.FieldBody)
	}
	if m.external_id != nil {
		fields = append(fields, blogpost.FieldExternalID)
	}
	return fields
}
func (m *BlogPostMutation) Field(name string) (ent.Value, bool) {
	switch name {
	case blogpost.FieldTitle:
		return m.Title()
	case blogpost.FieldBody:
		return m.Body()
	case blogpost.FieldExternalID:
		return m.ExternalID()
	}
	return nil, false
}
func (m *BlogPostMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
	switch name {
	case blogpost.FieldTitle:
		return m.OldTitle(ctx)
	case blogpost.FieldBody:
		return m.OldBody(ctx)
	case blogpost.FieldExternalID:
		return m.OldExternalID(ctx)
	}
	return nil, fmt.Errorf("unknown BlogPost field %s", name)
}
func (m *BlogPostMutation) SetField(name string, value ent.Value) error {
	switch name {
	case blogpost.FieldTitle:
		v, ok := value.(string)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetTitle(v)
		return nil
	case blogpost.FieldBody:
		v, ok := value.(string)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetBody(v)
		return nil
	case blogpost.FieldExternalID:
		v, ok := value.(int)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.SetExternalID(v)
		return nil
	}
	return fmt.Errorf("unknown BlogPost field %s", name)
}
func (m *BlogPostMutation) AddedFields() []string {
	var fields []string
	if m.addexternal_id != nil {
		fields = append(fields, blogpost.FieldExternalID)
	}
	return fields
}
func (m *BlogPostMutation) AddedField(name string) (ent.Value, bool) {
	switch name {
	case blogpost.FieldExternalID:
		return m.AddedExternalID()
	}
	return nil, false
}
func (m *BlogPostMutation) AddField(name string, value ent.Value) error {
	switch name {
	case blogpost.FieldExternalID:
		v, ok := value.(int)
		if !ok {
			return fmt.Errorf("unexpected type %T for field %s", value, name)
		}
		m.AddExternalID(v)
		return nil
	}
	return fmt.Errorf("unknown BlogPost numeric field %s", name)
}
func (m *BlogPostMutation) ClearedFields() []string {
	return nil
}
func (m *BlogPostMutation) FieldCleared(name string) bool {
	_, ok := m.clearedFields[name]
	return ok
}
func (m *BlogPostMutation) ClearField(name string) error {
	return fmt.Errorf("unknown BlogPost nullable field %s", name)
}
func (m *BlogPostMutation) ResetField(name string) error {
	switch name {
	case blogpost.FieldTitle:
		m.ResetTitle()
		return nil
	case blogpost.FieldBody:
		m.ResetBody()
		return nil
	case blogpost.FieldExternalID:
		m.ResetExternalID()
		return nil
	}
	return fmt.Errorf("unknown BlogPost field %s", name)
}
func (m *BlogPostMutation) AddedEdges() []string {
	edges := make([]string, 0, 2)
	if m.author != nil {
		edges = append(edges, blogpost.EdgeAuthor)
	}
	if m.categories != nil {
		edges = append(edges, blogpost.EdgeCategories)
	}
	return edges
}
func (m *BlogPostMutation) AddedIDs(name string) []ent.Value {
	switch name {
	case blogpost.EdgeAuthor:
		if id := m.author; id != nil {
			return []ent.Value{*id}
		}
	case blogpost.EdgeCategories:
		ids := make([]ent.Value, 0, len(m.categories))
		for id := range m.categories {
			ids = append(ids, id)
		}
		return ids
	}
	return nil
}
func (m *BlogPostMutation) RemovedEdges() []string {
	edges := make([]string, 0, 2)
	if m.removedcategories != nil {
		edges = append(edges, blogpost.EdgeCategories)
	}
	return edges
}
func (m *BlogPostMutation) RemovedIDs(name string) []ent.Value {
	switch name {
	case blogpost.EdgeCategories:
		ids := make([]ent.Value, 0, len(m.removedcategories))
		for id := range m.removedcategories {
			ids = append(ids, id)
		}
		return ids
	}
	return nil
}
func (m *BlogPostMutation) ClearedEdges() []string {
	edges := make([]string, 0, 2)
	if m.clearedauthor {
		edges = append(edges, blogpost.EdgeAuthor)
	}
	if m.clearedcategories {
		edges = append(edges, blogpost.EdgeCategories)
	}
	return edges
}
func (m *BlogPostMutation) EdgeCleared(name string) bool {
	switch name {
	case blogpost.EdgeAuthor:
		return m.clearedauthor
	case blogpost.EdgeCategories:
		return m.clearedcategories
	}
	return false
}
func (m *BlogPostMutation) ClearEdge(name string) error {
	switch name {
	case blogpost.EdgeAuthor:
		m.ClearAuthor()
		return nil
	}
	return fmt.Errorf("unknown BlogPost unique edge %s", name)
}
func (m *BlogPostMutation) ResetEdge(name string) error {
	switch name {
	case blogpost.EdgeAuthor:
		m.ResetAuthor()
		return nil
	case blogpost.EdgeCategories:
		m.ResetCategories()
		return nil
	}
	return fmt.Errorf("unknown BlogPost edge %s", name)
}
type CategoryMutation struct {
	config
	op                Op
	typ               string
	id                *int
	name              *string
	description       *string
	clearedFields     map[string]struct{}
	blog_posts        map[int]struct{}
	removedblog_posts map[int]struct{}
	clearedblog_posts bool
	done              bool
	oldValue          func(context.Context) (*Category, error)
	predicates        []predicate.Category
}
var _ ent.Mutation = (*CategoryMutation)(nil)
type categoryOption func(*CategoryMutation)
func newCategoryMutation(c config, op Op, opts ...categoryOption) *CategoryMutation {
	m := &CategoryMutation{
		config:        c,
		op:            op,
		typ:           TypeCategory,
		clearedFields: make(map[string]struct{}),
	}
	for _, opt := range opts {
		opt(m)
	}
	return m
}
func withCategoryID(id int) categoryOption {
	return func(m *CategoryMutation) {
		var (
			err   error
			once  sync.Once
			value *Category
		)
		m.oldValue = func(ctx context.Context) (*Category, error) {
			once.Do(func() {
				if m.done {
					err = fmt.Errorf("querying old values post mutation is not allowed")
				} else {
					value, err = m.Client().Category.Get(ctx, id)
				}
			})
			return value, err
		}
		m.id = &id
	}
}
func withCategory(node *Category) categoryOption {
	return func(m *CategoryMutation) {
		m.oldValue = func(context.Context) (*Category, error) {
			return node, nil
		}
		m.id = &node.ID
	}
}
func (m CategoryMutation) Client() *Client {
	client := &Client{config: m.config}
	client.init()
	return client
}
func (m CategoryMutation) Tx() (*Tx, error) {
	if _, ok := m.driver.(*txDriver); !ok {
		return nil, fmt.Errorf("ent: mutation is not running in a transaction")
	}
	tx := &Tx{config: m.config}
	tx.init()
	return tx, nil
}
func (m *CategoryMutation) ID() (id int, exists bool) {
	if m.id == nil {
		return
	}
	return *m.id, true
}
func (m *CategoryMutation) SetName(s string) {
	m.name = &s
}
func (m *CategoryMutation) Name() (r string, exists bool) {
	v := m.name
	if v == nil {
		return
	}
	return *v, true
}
func (m *CategoryMutation) OldName(ctx context.Context) (v string, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, fmt.Errorf("OldName is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, fmt.Errorf("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
}
func (m *CategoryMutation) ResetName() {
	m.name = nil
}
func (m *CategoryMutation) SetDescription(s string) {
	m.description = &s
}
func (m *CategoryMutation) Description() (r string, exists bool) {
	v := m.description
	if v == nil {
		return
	}
	return *v, true
}
func (m *CategoryMutation) OldDescription(ctx context.Context) (v string, err error) {
	if !m.op.Is(OpUpdateOne) {
		return v, fmt.Errorf("OldDescription is only allowed on UpdateOne operations")
	}
	if m.id == nil || m.oldValue == nil {
		return v, fmt.Errorf("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
}
func (m *CategoryMutation) ResetDescription() {
	m.description = nil
}
func (m *CategoryMutation) AddBlogPostIDs(ids ...int) {
	if m.blog_posts == nil {
		m.blog_posts = make(map[int]struct{})
	}
	for i := range ids {
		m.blog_posts[ids[i]] = struct{}{}
	}
}
func (m *CategoryMutation) ClearBlogPosts() {
	m.clearedblog_posts = true
}
func (m *CategoryMutation) BlogPostsCleared() bool {
	return m.clearedblog_posts
}
func (m *CategoryMutation) RemoveBlogPostIDs(ids ...int) {
	if m.removedblog_posts == nil {
		m.removedblog_posts = make(map[int]struct{})
	}
	for i := range ids {
		delete(m.blog_posts, ids[i])
		m.removedblog_posts[ids[i]] = struct{}{}
	}
}
func (m *CategoryMutation) RemovedBlogPostsIDs() (ids []int) {
	for id := range m.removedblog_posts {
		ids = append(ids, id)
	}
	return
}
func (m *CategoryMutation) BlogPostsIDs() (ids []int) {
	for id := range m.blog_posts {
		ids = append(ids, id)
	}
	return
}
func (m *CategoryMutation) ResetBlogPosts() {
	m.blog_posts = nil
	m.clearedblog_posts = false
	m.removedblog_posts = nil
}
func (m *CategoryMutation) Where(ps ...predicate.Category) {
	m.predicates = append(m.predicates, ps...)
}
func (m *CategoryMutation) Op() Op {
	return m.op
}
func (m *CategoryMutation) Type() string {
	return m.typ
}
func (m *CategoryMutation) Fields() []string {
	fields := make([]string, 0, 2)
	if m.name != nil {
		fields = append(fields, category.FieldName)
	}
	if m.description != nil {
		fields = append(fields, category.FieldDescription)
	}
	return fields
}
func (m *CategoryMutation) Field(name string) (ent.Value, bool) {
	switch name {
	case category.FieldName:
		return m.Name()
	case category.FieldDescription:
		return m.Description()
	}
	return nil, false
}