mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
202 lines
5.0 KiB
Go
202 lines
5.0 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. 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/facebookincubator/ent/dialect/gremlin"
|
|
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
|
|
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/g"
|
|
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/file"
|
|
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/filetype"
|
|
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/user"
|
|
)
|
|
|
|
// FileCreate is the builder for creating a File entity.
|
|
type FileCreate struct {
|
|
config
|
|
mutation *FileMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetSize sets the size field.
|
|
func (fc *FileCreate) SetSize(i int) *FileCreate {
|
|
fc.mutation.SetSize(i)
|
|
return fc
|
|
}
|
|
|
|
// SetNillableSize sets the size field if the given value is not nil.
|
|
func (fc *FileCreate) SetNillableSize(i *int) *FileCreate {
|
|
if i != nil {
|
|
fc.SetSize(*i)
|
|
}
|
|
return fc
|
|
}
|
|
|
|
// SetName sets the name field.
|
|
func (fc *FileCreate) SetName(s string) *FileCreate {
|
|
fc.mutation.SetName(s)
|
|
return fc
|
|
}
|
|
|
|
// SetUser sets the user field.
|
|
func (fc *FileCreate) SetUser(s string) *FileCreate {
|
|
fc.mutation.SetUser(s)
|
|
return fc
|
|
}
|
|
|
|
// SetNillableUser sets the user field if the given value is not nil.
|
|
func (fc *FileCreate) SetNillableUser(s *string) *FileCreate {
|
|
if s != nil {
|
|
fc.SetUser(*s)
|
|
}
|
|
return fc
|
|
}
|
|
|
|
// SetGroup sets the group field.
|
|
func (fc *FileCreate) SetGroup(s string) *FileCreate {
|
|
fc.mutation.SetGroup(s)
|
|
return fc
|
|
}
|
|
|
|
// SetNillableGroup sets the group field if the given value is not nil.
|
|
func (fc *FileCreate) SetNillableGroup(s *string) *FileCreate {
|
|
if s != nil {
|
|
fc.SetGroup(*s)
|
|
}
|
|
return fc
|
|
}
|
|
|
|
// SetOwnerID sets the owner edge to User by id.
|
|
func (fc *FileCreate) SetOwnerID(id string) *FileCreate {
|
|
fc.mutation.SetOwnerID(id)
|
|
return fc
|
|
}
|
|
|
|
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
|
|
func (fc *FileCreate) SetNillableOwnerID(id *string) *FileCreate {
|
|
if id != nil {
|
|
fc = fc.SetOwnerID(*id)
|
|
}
|
|
return fc
|
|
}
|
|
|
|
// SetOwner sets the owner edge to User.
|
|
func (fc *FileCreate) SetOwner(u *User) *FileCreate {
|
|
return fc.SetOwnerID(u.ID)
|
|
}
|
|
|
|
// SetTypeID sets the type edge to FileType by id.
|
|
func (fc *FileCreate) SetTypeID(id string) *FileCreate {
|
|
fc.mutation.SetTypeID(id)
|
|
return fc
|
|
}
|
|
|
|
// SetNillableTypeID sets the type edge to FileType by id if the given value is not nil.
|
|
func (fc *FileCreate) SetNillableTypeID(id *string) *FileCreate {
|
|
if id != nil {
|
|
fc = fc.SetTypeID(*id)
|
|
}
|
|
return fc
|
|
}
|
|
|
|
// SetType sets the type edge to FileType.
|
|
func (fc *FileCreate) SetType(f *FileType) *FileCreate {
|
|
return fc.SetTypeID(f.ID)
|
|
}
|
|
|
|
// Save creates the File in the database.
|
|
func (fc *FileCreate) Save(ctx context.Context) (*File, error) {
|
|
if _, ok := fc.mutation.Size(); !ok {
|
|
v := file.DefaultSize
|
|
fc.mutation.SetSize(v)
|
|
}
|
|
if v, ok := fc.mutation.Size(); ok {
|
|
if err := file.SizeValidator(v); err != nil {
|
|
return nil, fmt.Errorf("ent: validator failed for field \"size\": %v", err)
|
|
}
|
|
}
|
|
if _, ok := fc.mutation.Name(); !ok {
|
|
return nil, errors.New("ent: missing required field \"name\"")
|
|
}
|
|
var (
|
|
err error
|
|
node *File
|
|
)
|
|
if len(fc.hooks) == 0 {
|
|
node, err = fc.gremlinSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*FileMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
fc.mutation = mutation
|
|
node, err = fc.gremlinSave(ctx)
|
|
return node, err
|
|
})
|
|
for i := len(fc.hooks) - 1; i >= 0; i-- {
|
|
mut = fc.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, fc.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (fc *FileCreate) SaveX(ctx context.Context) *File {
|
|
v, err := fc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (fc *FileCreate) gremlinSave(ctx context.Context) (*File, error) {
|
|
res := &gremlin.Response{}
|
|
query, bindings := fc.gremlin().Query()
|
|
if err := fc.driver.Exec(ctx, query, bindings, res); err != nil {
|
|
return nil, err
|
|
}
|
|
if err, ok := isConstantError(res); ok {
|
|
return nil, err
|
|
}
|
|
f := &File{config: fc.config}
|
|
if err := f.FromResponse(res); err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (fc *FileCreate) gremlin() *dsl.Traversal {
|
|
v := g.AddV(file.Label)
|
|
if value, ok := fc.mutation.Size(); ok {
|
|
v.Property(dsl.Single, file.FieldSize, value)
|
|
}
|
|
if value, ok := fc.mutation.Name(); ok {
|
|
v.Property(dsl.Single, file.FieldName, value)
|
|
}
|
|
if value, ok := fc.mutation.User(); ok {
|
|
v.Property(dsl.Single, file.FieldUser, value)
|
|
}
|
|
if value, ok := fc.mutation.Group(); ok {
|
|
v.Property(dsl.Single, file.FieldGroup, value)
|
|
}
|
|
for _, id := range fc.mutation.OwnerIDs() {
|
|
v.AddE(user.FilesLabel).From(g.V(id)).InV()
|
|
}
|
|
for _, id := range fc.mutation.TypeIDs() {
|
|
v.AddE(filetype.FilesLabel).From(g.V(id)).InV()
|
|
}
|
|
return v.ValueMap(true)
|
|
}
|