You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/examples/blog/internal/data/ent/mutation.go

1732 lines
48 KiB

// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent/article"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent/comment"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent/predicate"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent/tag"
"entgo.io/ent"
)
const (
// Operation types.
OpCreate = ent.OpCreate
OpDelete = ent.OpDelete
OpDeleteOne = ent.OpDeleteOne
OpUpdate = ent.OpUpdate
OpUpdateOne = ent.OpUpdateOne
// Node types.
TypeArticle = "Article"
TypeComment = "Comment"
TypeTag = "Tag"
)
// ArticleMutation represents an operation that mutates the Article nodes in the graph.
type ArticleMutation struct {
config
op Op
typ string
id *int64
title *string
content *string
created_at *time.Time
updated_at *time.Time
clearedFields map[string]struct{}
comments map[int64]struct{}
removedcomments map[int64]struct{}
clearedcomments bool
tags map[int64]struct{}
removedtags map[int64]struct{}
clearedtags bool
done bool
oldValue func(context.Context) (*Article, error)
predicates []predicate.Article
}
var _ ent.Mutation = (*ArticleMutation)(nil)
// articleOption allows management of the mutation configuration using functional options.
type articleOption func(*ArticleMutation)
// newArticleMutation creates new mutation for the Article entity.
func newArticleMutation(c config, op Op, opts ...articleOption) *ArticleMutation {
m := &ArticleMutation{
config: c,
op: op,
typ: TypeArticle,
clearedFields: make(map[string]struct{}),
}
for _, opt := range opts {
opt(m)
}
return m
}
// withArticleID sets the ID field of the mutation.
func withArticleID(id int64) articleOption {
return func(m *ArticleMutation) {
var (
err error
once sync.Once
value *Article
)
m.oldValue = func(ctx context.Context) (*Article, error) {
once.Do(func() {
if m.done {
err = fmt.Errorf("querying old values post mutation is not allowed")
} else {
value, err = m.Client().Article.Get(ctx, id)
}
})
return value, err
}
m.id = &id
}
}
// withArticle sets the old Article of the mutation.
func withArticle(node *Article) articleOption {
return func(m *ArticleMutation) {
m.oldValue = func(context.Context) (*Article, 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 ArticleMutation) 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 ArticleMutation) 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
}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of Article entities.
func (m *ArticleMutation) SetID(id int64) {
m.id = &id
}
// ID returns the ID value in the mutation. Note that the ID
// is only available if it was provided to the builder.
func (m *ArticleMutation) ID() (id int64, exists bool) {
if m.id == nil {
return
}
return *m.id, true
}
// SetTitle sets the "title" field.
func (m *ArticleMutation) SetTitle(s string) {
m.title = &s
}
// Title returns the value of the "title" field in the mutation.
func (m *ArticleMutation) Title() (r string, exists bool) {
v := m.title
if v == nil {
return
}
return *v, true
}
// OldTitle returns the old "title" field's value of the Article entity.
// If the Article 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 *ArticleMutation) 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
}
// ResetTitle resets all changes to the "title" field.
func (m *ArticleMutation) ResetTitle() {
m.title = nil
}
// SetContent sets the "content" field.
func (m *ArticleMutation) SetContent(s string) {
m.content = &s
}
// Content returns the value of the "content" field in the mutation.
func (m *ArticleMutation) Content() (r string, exists bool) {
v := m.content
if v == nil {
return
}
return *v, true
}
// OldContent returns the old "content" field's value of the Article entity.
// If the Article 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 *ArticleMutation) OldContent(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldContent is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldContent requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldContent: %w", err)
}
return oldValue.Content, nil
}
// ResetContent resets all changes to the "content" field.
func (m *ArticleMutation) ResetContent() {
m.content = nil
}
// SetCreatedAt sets the "created_at" field.
func (m *ArticleMutation) SetCreatedAt(t time.Time) {
m.created_at = &t
}
// CreatedAt returns the value of the "created_at" field in the mutation.
func (m *ArticleMutation) CreatedAt() (r time.Time, exists bool) {
v := m.created_at
if v == nil {
return
}
return *v, true
}
// OldCreatedAt returns the old "created_at" field's value of the Article entity.
// If the Article 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 *ArticleMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldCreatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldCreatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err)
}
return oldValue.CreatedAt, nil
}
// ResetCreatedAt resets all changes to the "created_at" field.
func (m *ArticleMutation) ResetCreatedAt() {
m.created_at = nil
}
// SetUpdatedAt sets the "updated_at" field.
func (m *ArticleMutation) SetUpdatedAt(t time.Time) {
m.updated_at = &t
}
// UpdatedAt returns the value of the "updated_at" field in the mutation.
func (m *ArticleMutation) UpdatedAt() (r time.Time, exists bool) {
v := m.updated_at
if v == nil {
return
}
return *v, true
}
// OldUpdatedAt returns the old "updated_at" field's value of the Article entity.
// If the Article 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 *ArticleMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldUpdatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldUpdatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err)
}
return oldValue.UpdatedAt, nil
}
// ResetUpdatedAt resets all changes to the "updated_at" field.
func (m *ArticleMutation) ResetUpdatedAt() {
m.updated_at = nil
}
// AddCommentIDs adds the "comments" edge to the Comment entity by ids.
func (m *ArticleMutation) AddCommentIDs(ids ...int64) {
if m.comments == nil {
m.comments = make(map[int64]struct{})
}
for i := range ids {
m.comments[ids[i]] = struct{}{}
}
}
// ClearComments clears the "comments" edge to the Comment entity.
func (m *ArticleMutation) ClearComments() {
m.clearedcomments = true
}
// CommentsCleared returns if the "comments" edge to the Comment entity was cleared.
func (m *ArticleMutation) CommentsCleared() bool {
return m.clearedcomments
}
// RemoveCommentIDs removes the "comments" edge to the Comment entity by IDs.
func (m *ArticleMutation) RemoveCommentIDs(ids ...int64) {
if m.removedcomments == nil {
m.removedcomments = make(map[int64]struct{})
}
for i := range ids {
m.removedcomments[ids[i]] = struct{}{}
}
}
// RemovedComments returns the removed IDs of the "comments" edge to the Comment entity.
func (m *ArticleMutation) RemovedCommentsIDs() (ids []int64) {
for id := range m.removedcomments {
ids = append(ids, id)
}
return
}
// CommentsIDs returns the "comments" edge IDs in the mutation.
func (m *ArticleMutation) CommentsIDs() (ids []int64) {
for id := range m.comments {
ids = append(ids, id)
}
return
}
// ResetComments resets all changes to the "comments" edge.
func (m *ArticleMutation) ResetComments() {
m.comments = nil
m.clearedcomments = false
m.removedcomments = nil
}
// AddTagIDs adds the "tags" edge to the Tag entity by ids.
func (m *ArticleMutation) AddTagIDs(ids ...int64) {
if m.tags == nil {
m.tags = make(map[int64]struct{})
}
for i := range ids {
m.tags[ids[i]] = struct{}{}
}
}
// ClearTags clears the "tags" edge to the Tag entity.
func (m *ArticleMutation) ClearTags() {
m.clearedtags = true
}
// TagsCleared returns if the "tags" edge to the Tag entity was cleared.
func (m *ArticleMutation) TagsCleared() bool {
return m.clearedtags
}
// RemoveTagIDs removes the "tags" edge to the Tag entity by IDs.
func (m *ArticleMutation) RemoveTagIDs(ids ...int64) {
if m.removedtags == nil {
m.removedtags = make(map[int64]struct{})
}
for i := range ids {
m.removedtags[ids[i]] = struct{}{}
}
}
// RemovedTags returns the removed IDs of the "tags" edge to the Tag entity.
func (m *ArticleMutation) RemovedTagsIDs() (ids []int64) {
for id := range m.removedtags {
ids = append(ids, id)
}
return
}
// TagsIDs returns the "tags" edge IDs in the mutation.
func (m *ArticleMutation) TagsIDs() (ids []int64) {
for id := range m.tags {
ids = append(ids, id)
}
return
}
// ResetTags resets all changes to the "tags" edge.
func (m *ArticleMutation) ResetTags() {
m.tags = nil
m.clearedtags = false
m.removedtags = nil
}
// Op returns the operation name.
func (m *ArticleMutation) Op() Op {
return m.op
}
// Type returns the node type of this mutation (Article).
func (m *ArticleMutation) Type() string {
return m.typ
}
// Fields returns all fields that were changed during this mutation. Note that in
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *ArticleMutation) Fields() []string {
fields := make([]string, 0, 4)
if m.title != nil {
fields = append(fields, article.FieldTitle)
}
if m.content != nil {
fields = append(fields, article.FieldContent)
}
if m.created_at != nil {
fields = append(fields, article.FieldCreatedAt)
}
if m.updated_at != nil {
fields = append(fields, article.FieldUpdatedAt)
}
return fields
}
// Field returns the value of a field with the given name. The second boolean
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *ArticleMutation) Field(name string) (ent.Value, bool) {
switch name {
case article.FieldTitle:
return m.Title()
case article.FieldContent:
return m.Content()
case article.FieldCreatedAt:
return m.CreatedAt()
case article.FieldUpdatedAt:
return m.UpdatedAt()
}
return nil, false
}
// 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 *ArticleMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case article.FieldTitle:
return m.OldTitle(ctx)
case article.FieldContent:
return m.OldContent(ctx)
case article.FieldCreatedAt:
return m.OldCreatedAt(ctx)
case article.FieldUpdatedAt:
return m.OldUpdatedAt(ctx)
}
return nil, fmt.Errorf("unknown Article field %s", name)
}
// SetField sets the value of a field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *ArticleMutation) SetField(name string, value ent.Value) error {
switch name {
case article.FieldTitle:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetTitle(v)
return nil
case article.FieldContent:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetContent(v)
return nil
case article.FieldCreatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetCreatedAt(v)
return nil
case article.FieldUpdatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetUpdatedAt(v)
return nil
}
return fmt.Errorf("unknown Article field %s", name)
}
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *ArticleMutation) AddedFields() []string {
return nil
}
// AddedField returns the numeric value that was incremented/decremented on a field
// with the given name. The second boolean return value indicates that this field
// was not set, or was not defined in the schema.
func (m *ArticleMutation) AddedField(name string) (ent.Value, bool) {
return nil, false
}
// AddField adds the value to the field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *ArticleMutation) AddField(name string, value ent.Value) error {
switch name {
}
return fmt.Errorf("unknown Article numeric field %s", name)
}
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *ArticleMutation) ClearedFields() []string {
return nil
}
// FieldCleared returns a boolean indicating if a field with the given name was
// cleared in this mutation.
func (m *ArticleMutation) FieldCleared(name string) bool {
_, ok := m.clearedFields[name]
return ok
}
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *ArticleMutation) ClearField(name string) error {
return fmt.Errorf("unknown Article nullable field %s", name)
}
// ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema.
func (m *ArticleMutation) ResetField(name string) error {
switch name {
case article.FieldTitle:
m.ResetTitle()
return nil
case article.FieldContent:
m.ResetContent()
return nil
case article.FieldCreatedAt:
m.ResetCreatedAt()
return nil
case article.FieldUpdatedAt:
m.ResetUpdatedAt()
return nil
}
return fmt.Errorf("unknown Article field %s", name)
}
// AddedEdges returns all edge names that were set/added in this mutation.
func (m *ArticleMutation) AddedEdges() []string {
edges := make([]string, 0, 2)
if m.comments != nil {
edges = append(edges, article.EdgeComments)
}
if m.tags != nil {
edges = append(edges, article.EdgeTags)
}
return edges
}
// AddedIDs returns all IDs (to other nodes) that were added for the given edge
// name in this mutation.
func (m *ArticleMutation) AddedIDs(name string) []ent.Value {
switch name {
case article.EdgeComments:
ids := make([]ent.Value, 0, len(m.comments))
for id := range m.comments {
ids = append(ids, id)
}
return ids
case article.EdgeTags:
ids := make([]ent.Value, 0, len(m.tags))
for id := range m.tags {
ids = append(ids, id)
}
return ids
}
return nil
}
// RemovedEdges returns all edge names that were removed in this mutation.
func (m *ArticleMutation) RemovedEdges() []string {
edges := make([]string, 0, 2)
if m.removedcomments != nil {
edges = append(edges, article.EdgeComments)
}
if m.removedtags != nil {
edges = append(edges, article.EdgeTags)
}
return edges
}
// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with
// the given name in this mutation.
func (m *ArticleMutation) RemovedIDs(name string) []ent.Value {
switch name {
case article.EdgeComments:
ids := make([]ent.Value, 0, len(m.removedcomments))
for id := range m.removedcomments {
ids = append(ids, id)
}
return ids
case article.EdgeTags:
ids := make([]ent.Value, 0, len(m.removedtags))
for id := range m.removedtags {
ids = append(ids, id)
}
return ids
}
return nil
}
// ClearedEdges returns all edge names that were cleared in this mutation.
func (m *ArticleMutation) ClearedEdges() []string {
edges := make([]string, 0, 2)
if m.clearedcomments {
edges = append(edges, article.EdgeComments)
}
if m.clearedtags {
edges = append(edges, article.EdgeTags)
}
return edges
}
// EdgeCleared returns a boolean which indicates if the edge with the given name
// was cleared in this mutation.
func (m *ArticleMutation) EdgeCleared(name string) bool {
switch name {
case article.EdgeComments:
return m.clearedcomments
case article.EdgeTags:
return m.clearedtags
}
return false
}
// ClearEdge clears the value of the edge with the given name. It returns an error
// if that edge is not defined in the schema.
func (m *ArticleMutation) ClearEdge(name string) error {
switch name {
}
return fmt.Errorf("unknown Article unique edge %s", name)
}
// ResetEdge resets all changes to the edge with the given name in this mutation.
// It returns an error if the edge is not defined in the schema.
func (m *ArticleMutation) ResetEdge(name string) error {
switch name {
case article.EdgeComments:
m.ResetComments()
return nil
case article.EdgeTags:
m.ResetTags()
return nil
}
return fmt.Errorf("unknown Article edge %s", name)
}
// CommentMutation represents an operation that mutates the Comment nodes in the graph.
type CommentMutation struct {
config
op Op
typ string
id *int64
name *string
content *string
created_at *time.Time
updated_at *time.Time
clearedFields map[string]struct{}
post *int64
clearedpost bool
done bool
oldValue func(context.Context) (*Comment, error)
predicates []predicate.Comment
}
var _ ent.Mutation = (*CommentMutation)(nil)
// commentOption allows management of the mutation configuration using functional options.
type commentOption func(*CommentMutation)
// newCommentMutation creates new mutation for the Comment entity.
func newCommentMutation(c config, op Op, opts ...commentOption) *CommentMutation {
m := &CommentMutation{
config: c,
op: op,
typ: TypeComment,
clearedFields: make(map[string]struct{}),
}
for _, opt := range opts {
opt(m)
}
return m
}
// withCommentID sets the ID field of the mutation.
func withCommentID(id int64) commentOption {
return func(m *CommentMutation) {
var (
err error
once sync.Once
value *Comment
)
m.oldValue = func(ctx context.Context) (*Comment, error) {
once.Do(func() {
if m.done {
err = fmt.Errorf("querying old values post mutation is not allowed")
} else {
value, err = m.Client().Comment.Get(ctx, id)
}
})
return value, err
}
m.id = &id
}
}
// withComment sets the old Comment of the mutation.
func withComment(node *Comment) commentOption {
return func(m *CommentMutation) {
m.oldValue = func(context.Context) (*Comment, 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 CommentMutation) 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 CommentMutation) 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
}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of Comment entities.
func (m *CommentMutation) SetID(id int64) {
m.id = &id
}
// ID returns the ID value in the mutation. Note that the ID
// is only available if it was provided to the builder.
func (m *CommentMutation) ID() (id int64, exists bool) {
if m.id == nil {
return
}
return *m.id, true
}
// SetName sets the "name" field.
func (m *CommentMutation) SetName(s string) {
m.name = &s
}
// Name returns the value of the "name" field in the mutation.
func (m *CommentMutation) Name() (r string, exists bool) {
v := m.name
if v == nil {
return
}
return *v, true
}
// OldName returns the old "name" field's value of the Comment entity.
// If the Comment 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 *CommentMutation) 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
}
// ResetName resets all changes to the "name" field.
func (m *CommentMutation) ResetName() {
m.name = nil
}
// SetContent sets the "content" field.
func (m *CommentMutation) SetContent(s string) {
m.content = &s
}
// Content returns the value of the "content" field in the mutation.
func (m *CommentMutation) Content() (r string, exists bool) {
v := m.content
if v == nil {
return
}
return *v, true
}
// OldContent returns the old "content" field's value of the Comment entity.
// If the Comment 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 *CommentMutation) OldContent(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldContent is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldContent requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldContent: %w", err)
}
return oldValue.Content, nil
}
// ResetContent resets all changes to the "content" field.
func (m *CommentMutation) ResetContent() {
m.content = nil
}
// SetCreatedAt sets the "created_at" field.
func (m *CommentMutation) SetCreatedAt(t time.Time) {
m.created_at = &t
}
// CreatedAt returns the value of the "created_at" field in the mutation.
func (m *CommentMutation) CreatedAt() (r time.Time, exists bool) {
v := m.created_at
if v == nil {
return
}
return *v, true
}
// OldCreatedAt returns the old "created_at" field's value of the Comment entity.
// If the Comment 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 *CommentMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldCreatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldCreatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err)
}
return oldValue.CreatedAt, nil
}
// ResetCreatedAt resets all changes to the "created_at" field.
func (m *CommentMutation) ResetCreatedAt() {
m.created_at = nil
}
// SetUpdatedAt sets the "updated_at" field.
func (m *CommentMutation) SetUpdatedAt(t time.Time) {
m.updated_at = &t
}
// UpdatedAt returns the value of the "updated_at" field in the mutation.
func (m *CommentMutation) UpdatedAt() (r time.Time, exists bool) {
v := m.updated_at
if v == nil {
return
}
return *v, true
}
// OldUpdatedAt returns the old "updated_at" field's value of the Comment entity.
// If the Comment 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 *CommentMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldUpdatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldUpdatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err)
}
return oldValue.UpdatedAt, nil
}
// ResetUpdatedAt resets all changes to the "updated_at" field.
func (m *CommentMutation) ResetUpdatedAt() {
m.updated_at = nil
}
// SetPostID sets the "post" edge to the Article entity by id.
func (m *CommentMutation) SetPostID(id int64) {
m.post = &id
}
// ClearPost clears the "post" edge to the Article entity.
func (m *CommentMutation) ClearPost() {
m.clearedpost = true
}
// PostCleared returns if the "post" edge to the Article entity was cleared.
func (m *CommentMutation) PostCleared() bool {
return m.clearedpost
}
// PostID returns the "post" edge ID in the mutation.
func (m *CommentMutation) PostID() (id int64, exists bool) {
if m.post != nil {
return *m.post, true
}
return
}
// PostIDs returns the "post" edge IDs in the mutation.
// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use
// PostID instead. It exists only for internal usage by the builders.
func (m *CommentMutation) PostIDs() (ids []int64) {
if id := m.post; id != nil {
ids = append(ids, *id)
}
return
}
// ResetPost resets all changes to the "post" edge.
func (m *CommentMutation) ResetPost() {
m.post = nil
m.clearedpost = false
}
// Op returns the operation name.
func (m *CommentMutation) Op() Op {
return m.op
}
// Type returns the node type of this mutation (Comment).
func (m *CommentMutation) Type() string {
return m.typ
}
// Fields returns all fields that were changed during this mutation. Note that in
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *CommentMutation) Fields() []string {
fields := make([]string, 0, 4)
if m.name != nil {
fields = append(fields, comment.FieldName)
}
if m.content != nil {
fields = append(fields, comment.FieldContent)
}
if m.created_at != nil {
fields = append(fields, comment.FieldCreatedAt)
}
if m.updated_at != nil {
fields = append(fields, comment.FieldUpdatedAt)
}
return fields
}
// Field returns the value of a field with the given name. The second boolean
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *CommentMutation) Field(name string) (ent.Value, bool) {
switch name {
case comment.FieldName:
return m.Name()
case comment.FieldContent:
return m.Content()
case comment.FieldCreatedAt:
return m.CreatedAt()
case comment.FieldUpdatedAt:
return m.UpdatedAt()
}
return nil, false
}
// 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 *CommentMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case comment.FieldName:
return m.OldName(ctx)
case comment.FieldContent:
return m.OldContent(ctx)
case comment.FieldCreatedAt:
return m.OldCreatedAt(ctx)
case comment.FieldUpdatedAt:
return m.OldUpdatedAt(ctx)
}
return nil, fmt.Errorf("unknown Comment field %s", name)
}
// SetField sets the value of a field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *CommentMutation) SetField(name string, value ent.Value) error {
switch name {
case comment.FieldName:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetName(v)
return nil
case comment.FieldContent:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetContent(v)
return nil
case comment.FieldCreatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetCreatedAt(v)
return nil
case comment.FieldUpdatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetUpdatedAt(v)
return nil
}
return fmt.Errorf("unknown Comment field %s", name)
}
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *CommentMutation) AddedFields() []string {
return nil
}
// AddedField returns the numeric value that was incremented/decremented on a field
// with the given name. The second boolean return value indicates that this field
// was not set, or was not defined in the schema.
func (m *CommentMutation) AddedField(name string) (ent.Value, bool) {
return nil, false
}
// AddField adds the value to the field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *CommentMutation) AddField(name string, value ent.Value) error {
switch name {
}
return fmt.Errorf("unknown Comment numeric field %s", name)
}
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *CommentMutation) ClearedFields() []string {
return nil
}
// FieldCleared returns a boolean indicating if a field with the given name was
// cleared in this mutation.
func (m *CommentMutation) FieldCleared(name string) bool {
_, ok := m.clearedFields[name]
return ok
}
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *CommentMutation) ClearField(name string) error {
return fmt.Errorf("unknown Comment nullable field %s", name)
}
// ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema.
func (m *CommentMutation) ResetField(name string) error {
switch name {
case comment.FieldName:
m.ResetName()
return nil
case comment.FieldContent:
m.ResetContent()
return nil
case comment.FieldCreatedAt:
m.ResetCreatedAt()
return nil
case comment.FieldUpdatedAt:
m.ResetUpdatedAt()
return nil
}
return fmt.Errorf("unknown Comment field %s", name)
}
// AddedEdges returns all edge names that were set/added in this mutation.
func (m *CommentMutation) AddedEdges() []string {
edges := make([]string, 0, 1)
if m.post != nil {
edges = append(edges, comment.EdgePost)
}
return edges
}
// AddedIDs returns all IDs (to other nodes) that were added for the given edge
// name in this mutation.
func (m *CommentMutation) AddedIDs(name string) []ent.Value {
switch name {
case comment.EdgePost:
if id := m.post; id != nil {
return []ent.Value{*id}
}
}
return nil
}
// RemovedEdges returns all edge names that were removed in this mutation.
func (m *CommentMutation) RemovedEdges() []string {
edges := make([]string, 0, 1)
return edges
}
// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with
// the given name in this mutation.
func (m *CommentMutation) RemovedIDs(name string) []ent.Value {
switch name {
}
return nil
}
// ClearedEdges returns all edge names that were cleared in this mutation.
func (m *CommentMutation) ClearedEdges() []string {
edges := make([]string, 0, 1)
if m.clearedpost {
edges = append(edges, comment.EdgePost)
}
return edges
}
// EdgeCleared returns a boolean which indicates if the edge with the given name
// was cleared in this mutation.
func (m *CommentMutation) EdgeCleared(name string) bool {
switch name {
case comment.EdgePost:
return m.clearedpost
}
return false
}
// ClearEdge clears the value of the edge with the given name. It returns an error
// if that edge is not defined in the schema.
func (m *CommentMutation) ClearEdge(name string) error {
switch name {
case comment.EdgePost:
m.ClearPost()
return nil
}
return fmt.Errorf("unknown Comment unique edge %s", name)
}
// ResetEdge resets all changes to the edge with the given name in this mutation.
// It returns an error if the edge is not defined in the schema.
func (m *CommentMutation) ResetEdge(name string) error {
switch name {
case comment.EdgePost:
m.ResetPost()
return nil
}
return fmt.Errorf("unknown Comment edge %s", name)
}
// TagMutation represents an operation that mutates the Tag nodes in the graph.
type TagMutation struct {
config
op Op
typ string
id *int64
slug *string
name *string
created_at *time.Time
updated_at *time.Time
clearedFields map[string]struct{}
posts map[int64]struct{}
removedposts map[int64]struct{}
clearedposts bool
done bool
oldValue func(context.Context) (*Tag, error)
predicates []predicate.Tag
}
var _ ent.Mutation = (*TagMutation)(nil)
// tagOption allows management of the mutation configuration using functional options.
type tagOption func(*TagMutation)
// newTagMutation creates new mutation for the Tag entity.
func newTagMutation(c config, op Op, opts ...tagOption) *TagMutation {
m := &TagMutation{
config: c,
op: op,
typ: TypeTag,
clearedFields: make(map[string]struct{}),
}
for _, opt := range opts {
opt(m)
}
return m
}
// withTagID sets the ID field of the mutation.
func withTagID(id int64) tagOption {
return func(m *TagMutation) {
var (
err error
once sync.Once
value *Tag
)
m.oldValue = func(ctx context.Context) (*Tag, error) {
once.Do(func() {
if m.done {
err = fmt.Errorf("querying old values post mutation is not allowed")
} else {
value, err = m.Client().Tag.Get(ctx, id)
}
})
return value, err
}
m.id = &id
}
}
// withTag sets the old Tag of the mutation.
func withTag(node *Tag) tagOption {
return func(m *TagMutation) {
m.oldValue = func(context.Context) (*Tag, 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 TagMutation) 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 TagMutation) 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
}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of Tag entities.
func (m *TagMutation) SetID(id int64) {
m.id = &id
}
// ID returns the ID value in the mutation. Note that the ID
// is only available if it was provided to the builder.
func (m *TagMutation) ID() (id int64, exists bool) {
if m.id == nil {
return
}
return *m.id, true
}
// SetSlug sets the "slug" field.
func (m *TagMutation) SetSlug(s string) {
m.slug = &s
}
// Slug returns the value of the "slug" field in the mutation.
func (m *TagMutation) Slug() (r string, exists bool) {
v := m.slug
if v == nil {
return
}
return *v, true
}
// OldSlug returns the old "slug" field's value of the Tag entity.
// If the Tag 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 *TagMutation) OldSlug(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldSlug is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldSlug requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldSlug: %w", err)
}
return oldValue.Slug, nil
}
// ResetSlug resets all changes to the "slug" field.
func (m *TagMutation) ResetSlug() {
m.slug = nil
}
// SetName sets the "name" field.
func (m *TagMutation) SetName(s string) {
m.name = &s
}
// Name returns the value of the "name" field in the mutation.
func (m *TagMutation) Name() (r string, exists bool) {
v := m.name
if v == nil {
return
}
return *v, true
}
// OldName returns the old "name" field's value of the Tag entity.
// If the Tag 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 *TagMutation) 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
}
// ResetName resets all changes to the "name" field.
func (m *TagMutation) ResetName() {
m.name = nil
}
// SetCreatedAt sets the "created_at" field.
func (m *TagMutation) SetCreatedAt(t time.Time) {
m.created_at = &t
}
// CreatedAt returns the value of the "created_at" field in the mutation.
func (m *TagMutation) CreatedAt() (r time.Time, exists bool) {
v := m.created_at
if v == nil {
return
}
return *v, true
}
// OldCreatedAt returns the old "created_at" field's value of the Tag entity.
// If the Tag 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 *TagMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldCreatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldCreatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err)
}
return oldValue.CreatedAt, nil
}
// ResetCreatedAt resets all changes to the "created_at" field.
func (m *TagMutation) ResetCreatedAt() {
m.created_at = nil
}
// SetUpdatedAt sets the "updated_at" field.
func (m *TagMutation) SetUpdatedAt(t time.Time) {
m.updated_at = &t
}
// UpdatedAt returns the value of the "updated_at" field in the mutation.
func (m *TagMutation) UpdatedAt() (r time.Time, exists bool) {
v := m.updated_at
if v == nil {
return
}
return *v, true
}
// OldUpdatedAt returns the old "updated_at" field's value of the Tag entity.
// If the Tag 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 *TagMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldUpdatedAt is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldUpdatedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err)
}
return oldValue.UpdatedAt, nil
}
// ResetUpdatedAt resets all changes to the "updated_at" field.
func (m *TagMutation) ResetUpdatedAt() {
m.updated_at = nil
}
// AddPostIDs adds the "posts" edge to the Article entity by ids.
func (m *TagMutation) AddPostIDs(ids ...int64) {
if m.posts == nil {
m.posts = make(map[int64]struct{})
}
for i := range ids {
m.posts[ids[i]] = struct{}{}
}
}
// ClearPosts clears the "posts" edge to the Article entity.
func (m *TagMutation) ClearPosts() {
m.clearedposts = true
}
// PostsCleared returns if the "posts" edge to the Article entity was cleared.
func (m *TagMutation) PostsCleared() bool {
return m.clearedposts
}
// RemovePostIDs removes the "posts" edge to the Article entity by IDs.
func (m *TagMutation) RemovePostIDs(ids ...int64) {
if m.removedposts == nil {
m.removedposts = make(map[int64]struct{})
}
for i := range ids {
m.removedposts[ids[i]] = struct{}{}
}
}
// RemovedPosts returns the removed IDs of the "posts" edge to the Article entity.
func (m *TagMutation) RemovedPostsIDs() (ids []int64) {
for id := range m.removedposts {
ids = append(ids, id)
}
return
}
// PostsIDs returns the "posts" edge IDs in the mutation.
func (m *TagMutation) PostsIDs() (ids []int64) {
for id := range m.posts {
ids = append(ids, id)
}
return
}
// ResetPosts resets all changes to the "posts" edge.
func (m *TagMutation) ResetPosts() {
m.posts = nil
m.clearedposts = false
m.removedposts = nil
}
// Op returns the operation name.
func (m *TagMutation) Op() Op {
return m.op
}
// Type returns the node type of this mutation (Tag).
func (m *TagMutation) Type() string {
return m.typ
}
// Fields returns all fields that were changed during this mutation. Note that in
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *TagMutation) Fields() []string {
fields := make([]string, 0, 4)
if m.slug != nil {
fields = append(fields, tag.FieldSlug)
}
if m.name != nil {
fields = append(fields, tag.FieldName)
}
if m.created_at != nil {
fields = append(fields, tag.FieldCreatedAt)
}
if m.updated_at != nil {
fields = append(fields, tag.FieldUpdatedAt)
}
return fields
}
// Field returns the value of a field with the given name. The second boolean
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *TagMutation) Field(name string) (ent.Value, bool) {
switch name {
case tag.FieldSlug:
return m.Slug()
case tag.FieldName:
return m.Name()
case tag.FieldCreatedAt:
return m.CreatedAt()
case tag.FieldUpdatedAt:
return m.UpdatedAt()
}
return nil, false
}
// 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 *TagMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case tag.FieldSlug:
return m.OldSlug(ctx)
case tag.FieldName:
return m.OldName(ctx)
case tag.FieldCreatedAt:
return m.OldCreatedAt(ctx)
case tag.FieldUpdatedAt:
return m.OldUpdatedAt(ctx)
}
return nil, fmt.Errorf("unknown Tag field %s", name)
}
// SetField sets the value of a field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *TagMutation) SetField(name string, value ent.Value) error {
switch name {
case tag.FieldSlug:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetSlug(v)
return nil
case tag.FieldName:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetName(v)
return nil
case tag.FieldCreatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetCreatedAt(v)
return nil
case tag.FieldUpdatedAt:
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetUpdatedAt(v)
return nil
}
return fmt.Errorf("unknown Tag field %s", name)
}
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *TagMutation) AddedFields() []string {
return nil
}
// AddedField returns the numeric value that was incremented/decremented on a field
// with the given name. The second boolean return value indicates that this field
// was not set, or was not defined in the schema.
func (m *TagMutation) AddedField(name string) (ent.Value, bool) {
return nil, false
}
// AddField adds the value to the field with the given name. It returns an error if
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *TagMutation) AddField(name string, value ent.Value) error {
switch name {
}
return fmt.Errorf("unknown Tag numeric field %s", name)
}
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *TagMutation) ClearedFields() []string {
return nil
}
// FieldCleared returns a boolean indicating if a field with the given name was
// cleared in this mutation.
func (m *TagMutation) FieldCleared(name string) bool {
_, ok := m.clearedFields[name]
return ok
}
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *TagMutation) ClearField(name string) error {
return fmt.Errorf("unknown Tag nullable field %s", name)
}
// ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema.
func (m *TagMutation) ResetField(name string) error {
switch name {
case tag.FieldSlug:
m.ResetSlug()
return nil
case tag.FieldName:
m.ResetName()
return nil
case tag.FieldCreatedAt:
m.ResetCreatedAt()
return nil
case tag.FieldUpdatedAt:
m.ResetUpdatedAt()
return nil
}
return fmt.Errorf("unknown Tag field %s", name)
}
// AddedEdges returns all edge names that were set/added in this mutation.
func (m *TagMutation) AddedEdges() []string {
edges := make([]string, 0, 1)
if m.posts != nil {
edges = append(edges, tag.EdgePosts)
}
return edges
}
// AddedIDs returns all IDs (to other nodes) that were added for the given edge
// name in this mutation.
func (m *TagMutation) AddedIDs(name string) []ent.Value {
switch name {
case tag.EdgePosts:
ids := make([]ent.Value, 0, len(m.posts))
for id := range m.posts {
ids = append(ids, id)
}
return ids
}
return nil
}
// RemovedEdges returns all edge names that were removed in this mutation.
func (m *TagMutation) RemovedEdges() []string {
edges := make([]string, 0, 1)
if m.removedposts != nil {
edges = append(edges, tag.EdgePosts)
}
return edges
}
// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with
// the given name in this mutation.
func (m *TagMutation) RemovedIDs(name string) []ent.Value {
switch name {
case tag.EdgePosts:
ids := make([]ent.Value, 0, len(m.removedposts))
for id := range m.removedposts {
ids = append(ids, id)
}
return ids
}
return nil
}
// ClearedEdges returns all edge names that were cleared in this mutation.
func (m *TagMutation) ClearedEdges() []string {
edges := make([]string, 0, 1)
if m.clearedposts {
edges = append(edges, tag.EdgePosts)
}
return edges
}
// EdgeCleared returns a boolean which indicates if the edge with the given name
// was cleared in this mutation.
func (m *TagMutation) EdgeCleared(name string) bool {
switch name {
case tag.EdgePosts:
return m.clearedposts
}
return false
}
// ClearEdge clears the value of the edge with the given name. It returns an error
// if that edge is not defined in the schema.
func (m *TagMutation) ClearEdge(name string) error {
switch name {
}
return fmt.Errorf("unknown Tag unique edge %s", name)
}
// ResetEdge resets all changes to the edge with the given name in this mutation.
// It returns an error if the edge is not defined in the schema.
func (m *TagMutation) ResetEdge(name string) error {
switch name {
case tag.EdgePosts:
m.ResetPosts()
return nil
}
return fmt.Errorf("unknown Tag edge %s", name)
}