1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// 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 entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/facebook/ent/dialect/gremlin"
"github.com/facebook/ent/dialect/gremlin/graph/dsl"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/__"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/g"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/p"
"github.com/facebook/ent/entc/integration/gremlin/ent/pet"
"github.com/facebook/ent/entc/integration/gremlin/ent/user"
"github.com/google/uuid"
)
// PetCreate is the builder for creating a Pet entity.
type PetCreate struct {
config
mutation *PetMutation
hooks []Hook
}
// SetName sets the "name" field.
func (pc *PetCreate) SetName(s string) *PetCreate {
pc.mutation.SetName(s)
return pc
}
// SetUUID sets the "uuid" field.
func (pc *PetCreate) SetUUID(u uuid.UUID) *PetCreate {
pc.mutation.SetUUID(u)
return pc
}
// SetTeamID sets the "team" edge to the User entity by ID.
func (pc *PetCreate) SetTeamID(id string) *PetCreate {
pc.mutation.SetTeamID(id)
return pc
}
// SetNillableTeamID sets the "team" edge to the User entity by ID if the given value is not nil.
func (pc *PetCreate) SetNillableTeamID(id *string) *PetCreate {
if id != nil {
pc = pc.SetTeamID(*id)
}
return pc
}
// SetTeam sets the "team" edge to the User entity.
func (pc *PetCreate) SetTeam(u *User) *PetCreate {
return pc.SetTeamID(u.ID)
}
// SetOwnerID sets the "owner" edge to the User entity by ID.
func (pc *PetCreate) SetOwnerID(id string) *PetCreate {
pc.mutation.SetOwnerID(id)
return pc
}
// SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil.
func (pc *PetCreate) SetNillableOwnerID(id *string) *PetCreate {
if id != nil {
pc = pc.SetOwnerID(*id)
}
return pc
}
// SetOwner sets the "owner" edge to the User entity.
func (pc *PetCreate) SetOwner(u *User) *PetCreate {
return pc.SetOwnerID(u.ID)
}
// Mutation returns the PetMutation object of the builder.
func (pc *PetCreate) Mutation() *PetMutation {
return pc.mutation
}
// Save creates the Pet in the database.
func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) {
var (
err error
node *Pet
)
if len(pc.hooks) == 0 {
if err = pc.check(); err != nil {
return nil, err
}
node, err = pc.gremlinSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PetMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = pc.check(); err != nil {
return nil, err
}
pc.mutation = mutation
node, err = pc.gremlinSave(ctx)
mutation.done = true
return node, err
})
for i := len(pc.hooks) - 1; i >= 0; i-- {
mut = pc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, pc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (pc *PetCreate) SaveX(ctx context.Context) *Pet {
v, err := pc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// check runs all checks and user-defined validators on the builder.
func (pc *PetCreate) check() error {
if _, ok := pc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
}
return nil
}
func (pc *PetCreate) gremlinSave(ctx context.Context) (*Pet, error) {
res := &gremlin.Response{}
query, bindings := pc.gremlin().Query()
if err := pc.driver.Exec(ctx, query, bindings, res); err != nil {
return nil, err
}
if err, ok := isConstantError(res); ok {
return nil, err
}
pe := &Pet{config: pc.config}
if err := pe.FromResponse(res); err != nil {
return nil, err
}
return pe, nil
}
func (pc *PetCreate) gremlin() *dsl.Traversal {
type constraint struct {
pred *dsl.Traversal // constraint predicate.
test *dsl.Traversal // test matches and its constant.
}
constraints := make([]*constraint, 0, 1)
v := g.AddV(pet.Label)
if value, ok := pc.mutation.Name(); ok {
v.Property(dsl.Single, pet.FieldName, value)
}
if value, ok := pc.mutation.UUID(); ok {
v.Property(dsl.Single, pet.FieldUUID, value)
}
for _, id := range pc.mutation.TeamIDs() {
v.AddE(user.TeamLabel).From(g.V(id)).InV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.TeamLabel).OutV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(pet.Label, user.TeamLabel, id)),
})
}
for _, id := range pc.mutation.OwnerIDs() {
v.AddE(user.PetsLabel).From(g.V(id)).InV()
}
if len(constraints) == 0 {
return v.ValueMap(true)
}
tr := constraints[0].pred.Coalesce(constraints[0].test, v.ValueMap(true))
for _, cr := range constraints[1:] {
tr = cr.pred.Coalesce(cr.test, tr)
}
return tr
}
// PetCreateBulk is the builder for creating many Pet entities in bulk.
type PetCreateBulk struct {
config
builders []*PetCreate
}
|