// 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/gremlin/ent/group" "entgo.io/ent/entc/integration/gremlin/ent/groupinfo" ) // GroupInfoCreate is the builder for creating a GroupInfo entity. type GroupInfoCreate struct { config mutation *GroupInfoMutation hooks []Hook } // SetDesc sets the "desc" field. func (_c *GroupInfoCreate) SetDesc(v string) *GroupInfoCreate { _c.mutation.SetDesc(v) return _c } // SetMaxUsers sets the "max_users" field. func (_c *GroupInfoCreate) SetMaxUsers(v int) *GroupInfoCreate { _c.mutation.SetMaxUsers(v) return _c } // SetNillableMaxUsers sets the "max_users" field if the given value is not nil. func (_c *GroupInfoCreate) SetNillableMaxUsers(v *int) *GroupInfoCreate { if v != nil { _c.SetMaxUsers(*v) } return _c } // AddGroupIDs adds the "groups" edge to the Group entity by IDs. func (_c *GroupInfoCreate) AddGroupIDs(ids ...string) *GroupInfoCreate { _c.mutation.AddGroupIDs(ids...) return _c } // AddGroups adds the "groups" edges to the Group entity. func (_c *GroupInfoCreate) AddGroups(v ...*Group) *GroupInfoCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddGroupIDs(ids...) } // Mutation returns the GroupInfoMutation object of the builder. func (_c *GroupInfoCreate) Mutation() *GroupInfoMutation { return _c.mutation } // Save creates the GroupInfo in the database. func (_c *GroupInfoCreate) Save(ctx context.Context) (*GroupInfo, error) { _c.defaults() return withHooks(ctx, _c.gremlinSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *GroupInfoCreate) SaveX(ctx context.Context) *GroupInfo { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *GroupInfoCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *GroupInfoCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *GroupInfoCreate) defaults() { if _, ok := _c.mutation.MaxUsers(); !ok { v := groupinfo.DefaultMaxUsers _c.mutation.SetMaxUsers(v) } } // check runs all checks and user-defined validators on the builder. func (_c *GroupInfoCreate) check() error { if _, ok := _c.mutation.Desc(); !ok { return &ValidationError{Name: "desc", err: errors.New(`ent: missing required field "GroupInfo.desc"`)} } if _, ok := _c.mutation.MaxUsers(); !ok { return &ValidationError{Name: "max_users", err: errors.New(`ent: missing required field "GroupInfo.max_users"`)} } return nil } func (_c *GroupInfoCreate) gremlinSave(ctx context.Context) (*GroupInfo, 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 := &GroupInfo{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 *GroupInfoCreate) 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(groupinfo.Label) if value, ok := _c.mutation.Desc(); ok { v.Property(dsl.Single, groupinfo.FieldDesc, value) } if value, ok := _c.mutation.MaxUsers(); ok { v.Property(dsl.Single, groupinfo.FieldMaxUsers, value) } for _, id := range _c.mutation.GroupsIDs() { v.AddE(group.InfoLabel).From(g.V(id)).InV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(group.InfoLabel).OutV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(groupinfo.Label, group.InfoLabel, id)), }) } 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 } // GroupInfoCreateBulk is the builder for creating many GroupInfo entities in bulk. type GroupInfoCreateBulk struct { config err error builders []*GroupInfoCreate }