// 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 ent, DO NOT EDIT. package ent import ( "context" "errors" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" "entgo.io/ent/dialect/gremlin/graph/dsl/p" "entgo.io/ent/entc/integration/ent/schema" "entgo.io/ent/entc/integration/gremlin/ent/document" ) // DocumentCreate is the builder for creating a Document entity. type DocumentCreate struct { config mutation *DocumentMutation hooks []Hook } // SetName sets the "name" field. func (_c *DocumentCreate) SetName(v string) *DocumentCreate { _c.mutation.SetName(v) return _c } // SetAttachment sets the "attachment" field. func (_c *DocumentCreate) SetAttachment(v []byte) *DocumentCreate { _c.mutation.SetAttachment(v) return _c } // SetMetadata sets the "metadata" field. func (_c *DocumentCreate) SetMetadata(v []byte) *DocumentCreate { _c.mutation.SetMetadata(v) return _c } // SetNillableMetadata sets the "metadata" field if the given value is not nil. func (_c *DocumentCreate) SetNillableMetadata(v *[]byte) *DocumentCreate { if v != nil { _c.SetMetadata(*v) } return _c } // SetPayload sets the "payload" field. func (_c *DocumentCreate) SetPayload(v *schema.DocPayload) *DocumentCreate { _c.mutation.SetPayload(v) return _c } // SetDescription sets the "description" field. func (_c *DocumentCreate) SetDescription(v string) *DocumentCreate { _c.mutation.SetDescription(v) return _c } // SetNillableDescription sets the "description" field if the given value is not nil. func (_c *DocumentCreate) SetNillableDescription(v *string) *DocumentCreate { if v != nil { _c.SetDescription(*v) } return _c } // Mutation returns the DocumentMutation object of the builder. func (_c *DocumentCreate) Mutation() *DocumentMutation { return _c.mutation } // Save creates the Document in the database. func (_c *DocumentCreate) Save(ctx context.Context) (*Document, error) { return withHooks(ctx, _c.gremlinSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *DocumentCreate) SaveX(ctx context.Context) *Document { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *DocumentCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *DocumentCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (_c *DocumentCreate) check() error { if _, ok := _c.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Document.name"`)} } if _, ok := _c.mutation.Attachment(); !ok { return &ValidationError{Name: "attachment", err: errors.New(`ent: missing required field "Document.attachment"`)} } return nil } func (_c *DocumentCreate) gremlinSave(ctx context.Context) (*Document, error) { if err := _c.check(); err != nil { return nil, err } res := &gremlin.Response{} query, bindings := _c.gremlin().Query() if err := _c.driver.Exec(ctx, query, bindings, res); err != nil { return nil, err } if err, ok := isConstantError(res); ok { return nil, err } rnode := &Document{config: _c.config} if err := rnode.FromResponse(res); err != nil { return nil, err } _c.mutation.id = &rnode.ID _c.mutation.done = true return rnode, nil } func (_c *DocumentCreate) 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(document.Label) if value, ok := _c.mutation.Name(); ok { constraints = append(constraints, &constraint{ pred: g.V().Has(document.Label, document.FieldName, value).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(document.Label, document.FieldName, value)), }) v.Property(dsl.Single, document.FieldName, value) } 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 } // DocumentCreateBulk is the builder for creating many Document entities in bulk. type DocumentCreateBulk struct { config err error builders []*DocumentCreate }