Files
ent/entc/gen/template/builder/mutation.tmpl
2021-04-10 14:56:34 +03:00

682 lines
20 KiB
Cheetah

{{/*
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.
*/}}
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
{{ define "mutation" }}
{{ $pkg := base $.Config.Package }}
{{ template "header" $ }}
import (
"context"
"fmt"
"sync"
{{- range $n := $.Nodes }}
"{{ $.Config.Package }}/{{ $n.Package }}"
{{- end }}
"entgo.io/ent"
)
const (
// Operation types.
OpCreate = ent.OpCreate
OpDelete = ent.OpDelete
OpDeleteOne = ent.OpDeleteOne
OpUpdate = ent.OpUpdate
OpUpdateOne = ent.OpUpdateOne
// Node types.
{{- range $n := $.Nodes }}
Type{{ $n.Name }} = "{{ $n.Name }}"
{{- end }}
)
{{ range $n := $.Nodes }}
{{ $mutation := $n.MutationName }}
// {{ $mutation }} represents an operation that mutates the {{ $n.Name }} nodes in the graph.
type {{ $mutation }} struct {
config
op Op
typ string
{{ $n.ID.BuilderField }} *{{ $n.ID.Type }}
{{- range $f := $n.MutationFields }}
{{ $f.BuilderField }} *{{ $f.Type }}
{{- if and $f.Type.Numeric $f.ConvertedToBasic }}
add{{ $f.BuilderField }} *{{ $f.Type }}
{{- end }}
{{- end }}
clearedFields map[string]struct{}
{{- range $e := $n.Edges }}
{{- if $e.Unique }}
{{ $e.BuilderField }} *{{ $e.Type.ID.Type }}
{{- else }}
{{ $e.BuilderField }} map[{{ $e.Type.ID.Type }}]struct{}
removed{{ $e.BuilderField }} map[{{ $e.Type.ID.Type }}]struct{}
{{- end }}
cleared{{ $e.BuilderField }} bool
{{- end }}
done bool
oldValue func(context.Context) (*{{ $n.Name }}, error)
predicates []predicate.{{ $n.Name }}
}
var _ ent.Mutation = (*{{ $mutation }})(nil)
{{ $mutationOption := print (camel $n.Name) "Option" }}
// {{ $mutationOption }} allows management of the mutation configuration using functional options.
type {{ $mutationOption }} func(*{{ $mutation }})
// new{{ $mutation }} creates new mutation for the {{ $n.Name }} entity.
func new{{ $mutation }}(c config, op Op, opts ...{{ $mutationOption }}) *{{ $mutation }} {
m := &{{ $mutation }}{
config: c,
op: op,
typ: Type{{ $n.Name }},
clearedFields: make(map[string]struct{}),
}
for _, opt := range opts {
opt(m)
}
return m
}
{{ $opt := print "with" $n.Name "ID" }}
// {{ $opt }} sets the ID field of the mutation.
func {{ $opt }}(id {{ $n.ID.Type }}) {{ $mutationOption }} {
return func(m *{{ $mutation }}) {
var (
err error
once sync.Once
value *{{ $n.Name }}
)
m.oldValue = func(ctx context.Context) (*{{ $n.Name }}, error) {
once.Do(func() {
if m.done {
err = fmt.Errorf("querying old values post mutation is not allowed")
} else {
value, err = m.Client().{{ $n.Name }}.Get(ctx, id)
}
})
return value, err
}
m.{{ $n.ID.BuilderField }} = &id
}
}
{{ $opt = print "with" $n.Name }}
// {{ $opt }} sets the old {{ $n.Name }} of the mutation.
func {{ $opt }}(node *{{ $n.Name }}) {{ $mutationOption }} {
return func(m *{{ $mutation }}) {
m.oldValue = func(context.Context) (*{{ $n.Name }}, error) {
return node, nil
}
m.{{ $n.ID.BuilderField }} = &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 {{ $mutation }}) 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 {{ $mutation }}) Tx() (*Tx, error) {
if _, ok := m.driver.(*txDriver); !ok {
return nil, fmt.Errorf("{{ $pkg }}: mutation is not running in a transaction")
}
tx := &Tx{config: m.config}
tx.init()
return tx, nil
}
{{- if $n.ID.UserDefined }}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of {{ $n.Name }} entities.
func (m *{{ $mutation }}) SetID(id {{ $n.ID.Type }}) {
m.{{ $n.ID.BuilderField }} = &id
}
{{- end }}
// ID returns the ID value in the mutation. Note that the ID
// is only available if it was provided to the builder.
func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
if m.{{ $n.ID.BuilderField }} == nil {
return
}
return *m.{{ $n.ID.BuilderField }}, true
}
{{ range $f := $n.Fields }}
{{ $const := print $n.Package "." $f.Constant }}
{{ $p := receiver $f.Type.String }}{{ if eq $p "m" }} {{ $p = "value" }} {{ end }}
{{ $func := $f.MutationSet }}
// {{ $func }} sets the "{{ $f.Name }}" field.
func (m *{{ $mutation }}) {{ $func }}({{ $p }} {{ $f.Type }}) {
m.{{ $f.BuilderField }} = &{{ $p }}
{{- /* setting numeric type override previous calls to Add. */}}
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
m.add{{ $f.BuilderField }} = nil
{{- end }}
}
// {{ $f.MutationGet }} returns the value of the "{{ $f.Name }}" field in the mutation.
func (m *{{ $mutation }}) {{ $f.MutationGet }}() (r {{ $f.Type }}, exists bool) {
v := m.{{ $f.BuilderField }}
if v == nil {
return
}
return *v, true
}
// {{ $f.MutationGetOld }} returns the old "{{ $f.Name }}" field's value of the {{ $n.Name }} entity.
// If the {{ $n.Name }} 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 *{{ $mutation }}) {{ $f.MutationGetOld }}(ctx context.Context) (v {{ if $f.NillableValue }}*{{ end }}{{ $f.Type }}, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("{{ $f.MutationGetOld }} is only allowed on UpdateOne operations")
}
if m.{{ $n.ID.BuilderField }} == nil || m.oldValue == nil {
return v, fmt.Errorf("{{ $f.MutationGetOld }} requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for {{ $f.MutationGetOld }}: %w", err)
}
return oldValue.{{ $f.StructField }}, nil
}
{{ if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
{{ $func := print "Add" $f.StructField }}
// {{ $func }} adds {{ $p }} to the "{{ $f.Name }}" field.
func (m *{{ $mutation }}) {{ $func }}({{ $p }} {{ $f.Type }}) {
if m.add{{ $f.BuilderField }} != nil {
*m.add{{ $f.BuilderField }} += {{ $p }}
} else {
m.add{{ $f.BuilderField }} = &{{ $p }}
}
}
// Added{{ $f.StructField }} returns the value that was added to the "{{ $f.Name }}" field in this mutation.
func (m *{{ $mutation }}) Added{{ $f.StructField }}() (r {{ $f.Type }}, exists bool) {
v := m.add{{ $f.BuilderField }}
if v == nil {
return
}
return *v, true
}
{{ end }}
{{ if $f.Optional }}
{{ $func := $f.MutationClear }}
// {{ $func }} clears the value of the "{{ $f.Name }}" field.
func (m *{{ $mutation }}) {{ $func }}() {
m.{{ $f.BuilderField }} = nil
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
m.add{{ $f.BuilderField }} = nil
{{- end }}
m.clearedFields[{{ $const }}] = struct{}{}
}
{{ $func = $f.MutationCleared }}
// {{ $func }} returns if the "{{ $f.Name }}" field was cleared in this mutation.
func (m *{{ $mutation }}) {{ $func }}() bool {
_, ok := m.clearedFields[{{ $const }}]
return ok
}
{{ end }}
{{ $func = $f.MutationReset }}
// {{ $func }} resets all changes to the "{{ $f.Name }}" field.
func (m *{{ $mutation }}) {{ $func }}() {
m.{{ $f.BuilderField }} = nil
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
m.add{{ $f.BuilderField }} = nil
{{- end }}
{{- if $f.Optional }}
delete(m.clearedFields, {{ $const }})
{{- end }}
}
{{ end }}
{{ range $e := $n.Edges }}
{{ $op := "add" }}{{ $idsFunc := $e.MutationAdd }}{{ if $e.Unique }}{{ $op = "set" }}{{ $idsFunc = $e.MutationSet }}{{ end }}
{{/* Check if this setter was already defined by the field-setters (e.g. edge-field with the same name). */}}
{{ $withSetGet := not $e.HasFieldSetter }}
{{ if $withSetGet }}
// {{ $idsFunc }} {{ $op }}s the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by id{{ if not $e.Unique }}s{{ end }}.
func (m *{{ $mutation }}) {{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }} {{ $e.Type.ID.Type }}) {
{{- if $e.Unique }}
m.{{ $e.BuilderField }} = &id
{{- else }}
if m.{{ $e.BuilderField }} == nil {
m.{{ $e.BuilderField }} = make(map[{{ $e.Type.ID.Type }}]struct{})
}
for i := range ids {
m.{{ $e.BuilderField }}[ids[i]] = struct{}{}
}
{{- end }}
}
{{ end }}
{{ $func := $e.MutationClear }}
// {{ $func }} clears the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity.
func (m *{{ $mutation }}) {{ $func }}() {
m.cleared{{ $e.BuilderField }} = true
}
{{ $func = $e.MutationCleared }}
// {{ $func }} reports if the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity was cleared.
func (m *{{ $mutation }}) {{ $func }}() bool {
return {{ with $e.Field }}{{ if .Optional }}m.{{ .MutationCleared }}() || {{ end }}{{ end }}m.cleared{{ $e.BuilderField }}
}
{{ if not $e.Unique }}
{{ $p := lower (printf "%.1s" $e.Type.Name) }}
// {{ $e.MutationRemove }} removes the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by IDs.
func (m *{{ $mutation }}) {{ $e.MutationRemove }}(ids ...{{ $e.Type.ID.Type }}) {
if m.removed{{ $e.BuilderField }} == nil {
m.removed{{ $e.BuilderField }} = make(map[{{ $e.Type.ID.Type }}]struct{})
}
{{- if $e.Unique }}
m.removed{{ $e.BuilderField }}[id] = struct{}{}
{{- else }}
for i := range ids {
m.removed{{ $e.BuilderField }}[ids[i]] = struct{}{}
}
{{- end }}
}
{{ $func := print "Removed" $e.StructField }}
// {{ $func }} returns the removed IDs of the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity.
func (m *{{ $mutation }}) {{ $func }}IDs() (ids []{{ $e.Type.ID.Type }}) {
for id := range m.removed{{ $e.BuilderField }} {
ids = append(ids, id)
}
return
}
{{ else if and $e.Unique $withSetGet }}
// {{ $e.StructField }}ID returns the "{{ $e.Name }}" edge ID in the mutation.
func (m *{{ $mutation }}) {{ $e.StructField }}ID() (id {{ $e.Type.ID.Type }}, exists bool) {
if m.{{ $e.BuilderField }} != nil {
return *m.{{ $e.BuilderField }}, true
}
return
}
{{ end }}
// {{ $e.StructField }}IDs returns the "{{ $e.Name }}" edge IDs in the mutation.
{{- if $e.Unique }}
// Note that IDs always returns len(IDs) <= 1 for unique edges, and you should use
// {{ $e.StructField }}ID instead. It exists only for internal usage by the builders.
{{- end }}
func (m *{{ $mutation }}) {{ $e.StructField }}IDs() (ids []{{ $e.Type.ID.Type }}) {
{{- if $e.Unique }}
if id := m.{{ $e.BuilderField }}; id != nil {
ids = append(ids, *id)
}
{{- else }}
for id := range m.{{ $e.BuilderField }} {
ids = append(ids, id)
}
{{- end}}
return
}
{{ $func = $e.MutationReset }}
// {{ $func }} resets all changes to the "{{ $e.Name }}" edge.
func (m *{{ $mutation }}) {{ $func }}() {
m.{{ $e.BuilderField }} = nil
m.cleared{{ $e.BuilderField }} = false
{{- if not $e.Unique }}
m.removed{{ $e.BuilderField }} = nil
{{- end }}
}
{{ end }}
// Op returns the operation name.
func (m *{{ $mutation }}) Op() Op {
return m.op
}
// Type returns the node type of this mutation ({{ $n.Name }}).
func (m *{{ $mutation }}) 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 *{{ $mutation }}) Fields() []string {
fields := make([]string, 0, {{ len $n.Fields }})
{{- range $f := $n.Fields }}
{{- $const := print $n.Package "." $f.Constant }}
if m.{{ $f.BuilderField }} != nil {
fields = append(fields, {{ $const }})
}
{{- end }}
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 *{{ $mutation }}) Field(name string) (ent.Value, bool) {
{{- with $n.Fields }}
switch name {
{{- range $f := $n.Fields }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
return m.{{ $f.MutationGet }}()
{{- end }}
}
{{- end }}
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 *{{ $mutation }}) OldField(ctx context.Context, name string) (ent.Value, error) {
{{- with $n.Fields }}
switch name {
{{- range $f := . }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
return m.{{ $f.MutationGetOld }}(ctx)
{{- end }}
}
{{- end }}
return nil, fmt.Errorf("unknown {{ $n.Name }} 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 *{{ $mutation }}) SetField(name string, value ent.Value) error {
switch name {
{{- range $f := $n.Fields }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
v, ok := value.({{ $f.Type }})
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.Set{{ $f.StructField }}(v)
return nil
{{- end }}
}
return fmt.Errorf("unknown {{ $n.Name }} field %s", name)
}
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *{{ $mutation }}) AddedFields() []string {
{{- if $n.HasNumeric }}
var fields []string
{{- range $f := $n.Fields }}
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
{{- $const := print $n.Package "." $f.Constant }}
if m.add{{ $f.BuilderField }} != nil {
fields = append(fields, {{ $const }})
}
{{- end }}
{{- end }}
return fields
{{- else }}
return nil
{{- end }}
}
// 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 *{{ $mutation }}) AddedField(name string) (ent.Value, bool) {
{{- if $n.HasNumeric }}
switch name {
{{- range $f := $n.Fields }}
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
return m.Added{{ $f.StructField }}()
{{- end }}
{{- end }}
}
{{- end }}
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 *{{ $mutation }}) AddField(name string, value ent.Value) error {
{{- with $n.Fields }}
switch name {
{{- range $f := $n.Fields }}
{{- if and $f.Type.Numeric $f.ConvertedToBasic (not $f.IsEdgeField) }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
v, ok := value.({{ $f.Type }})
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.Add{{ $f.StructField }}(v)
return nil
{{- end }}
{{- end }}
}
{{- end }}
return fmt.Errorf("unknown {{ $n.Name }} numeric field %s", name)
}
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *{{ $mutation }}) ClearedFields() []string {
{{- if $n.HasOptional }}
var fields []string
{{- range $f := $n.Fields }}
{{- if $f.Optional }}
{{- $const := print $n.Package "." $f.Constant }}
if m.FieldCleared({{ $const }}) {
fields = append(fields, {{ $const }})
}
{{- end }}
{{- end }}
return fields
{{- else }}
return nil
{{- end }}
}
// FieldCleared returns a boolean indicating if a field with the given name was
// cleared in this mutation.
func (m *{{ $mutation }}) 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 *{{ $mutation }}) ClearField(name string) error {
{{- if $n.HasOptional }}
switch name {
{{- range $f := $n.Fields }}
{{- if $f.Optional }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
m.Clear{{ $f.StructField }}()
return nil
{{- end }}
{{- end }}
}
{{- end }}
return fmt.Errorf("unknown {{ $n.Name }} 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 *{{ $mutation }}) ResetField(name string) error {
{{- with $n.Fields }}
switch name {
{{- range $f := $n.Fields }}
{{- $const := print $n.Package "." $f.Constant }}
case {{ $const }}:
m.{{ $f.MutationReset }}()
return nil
{{- end }}
}
{{- end }}
return fmt.Errorf("unknown {{ $n.Name }} field %s", name)
}
// AddedEdges returns all edge names that were set/added in this mutation.
func (m *{{ $mutation }}) AddedEdges() []string {
edges := make([]string, 0, {{ len $n.Edges }})
{{- range $e := $n.Edges }}
if m.{{ $e.BuilderField }} != nil {
{{- $const := print $n.Package "." $e.Constant }}
edges = append(edges, {{ $const }})
}
{{- end }}
return edges
}
// AddedIDs returns all IDs (to other nodes) that were added for the given edge
// name in this mutation.
func (m *{{ $mutation }}) AddedIDs(name string) []ent.Value {
{{- with $n.Edges }}
switch name {
{{- range $e := $n.Edges }}
{{- $const := print $n.Package "." $e.Constant }}
case {{ $const }}:
{{- if $e.Unique }}
if id := m.{{ $e.BuilderField }}; id != nil {
return []ent.Value{*id}
}
{{- else }}
ids := make([]ent.Value, 0, len(m.{{ $e.BuilderField }}))
for id := range m.{{ $e.BuilderField }} {
ids = append(ids, id)
}
return ids
{{- end }}
{{- end }}
}
{{- end }}
return nil
}
// RemovedEdges returns all edge names that were removed in this mutation.
func (m *{{ $mutation }}) RemovedEdges() []string {
edges := make([]string, 0, {{ len $n.Edges }})
{{- range $e := $n.Edges }}
{{- if not $e.Unique }}
if m.removed{{ $e.BuilderField }} != nil {
{{- $const := print $n.Package "." $e.Constant }}
edges = append(edges, {{ $const }})
}
{{- end }}
{{- end }}
return edges
}
// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with
// the given name in this mutation.
func (m *{{ $mutation }}) RemovedIDs(name string) []ent.Value {
{{- with $n.Edges }}
switch name {
{{- range $e := $n.Edges }}
{{- if not $e.Unique }}
{{- $const := print $n.Package "." $e.Constant }}
case {{ $const }}:
ids := make([]ent.Value, 0, len(m.removed{{ $e.BuilderField }}))
for id := range m.removed{{ $e.BuilderField }} {
ids = append(ids, id)
}
return ids
{{- end }}
{{- end }}
}
{{- end }}
return nil
}
// ClearedEdges returns all edge names that were cleared in this mutation.
func (m *{{ $mutation }}) ClearedEdges() []string {
edges := make([]string, 0, {{ len $n.Edges }})
{{- range $e := $n.Edges }}
if m.cleared{{ $e.BuilderField }} {
{{- $const := print $n.Package "." $e.Constant }}
edges = append(edges, {{ $const }})
}
{{- end }}
return edges
}
// EdgeCleared returns a boolean which indicates if the edge with the given name
// was cleared in this mutation.
func (m *{{ $mutation }}) EdgeCleared(name string) bool {
{{- with $n.Edges }}
switch name {
{{- range $e := $n.Edges }}
{{- $const := print $n.Package "." $e.Constant }}
case {{ $const }}:
return m.cleared{{ $e.BuilderField }}
{{- end }}
}
{{- end }}
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 *{{ $mutation }}) ClearEdge(name string) error {
{{- with $n.Edges }}
switch name {
{{- range $e := $n.Edges }}
{{- if $e.Unique }}
{{- $const := print $n.Package "." $e.Constant }}
case {{ $const }}:
m.{{ $e.MutationClear }}()
return nil
{{- end }}
{{- end }}
}
{{- end }}
return fmt.Errorf("unknown {{ $n.Name }} 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 *{{ $mutation }}) ResetEdge(name string) error {
{{- with $n.Edges }}
switch name {
{{- range $e := $n.Edges }}
{{- $const := print $n.Package "." $e.Constant }}
case {{ $const }}:
m.{{ $e.MutationReset }}()
return nil
{{- end }}
}
{{- end }}
return fmt.Errorf("unknown {{ $n.Name }} edge %s", name)
}
{{ end }}
{{ end }}