diff --git a/entc/integration/ent/example_test.go b/entc/integration/ent/example_test.go index 1af5980fe..c48a7aa24 100644 --- a/entc/integration/ent/example_test.go +++ b/entc/integration/ent/example_test.go @@ -202,6 +202,7 @@ func ExampleGroup() { log.Println("file created:", f0) u1 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -404,6 +405,7 @@ func ExampleUser() { log.Println("group created:", gr3) u4 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -414,6 +416,7 @@ func ExampleUser() { log.Println("user created:", u4) u6 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -429,6 +432,7 @@ func ExampleUser() { log.Println("pet created:", pe7) u8 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -439,6 +443,7 @@ func ExampleUser() { log.Println("user created:", u8) u10 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -451,6 +456,7 @@ func ExampleUser() { // create user vertex with its edges. u := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index 5abcb5f39..748fdc9f9 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -271,6 +271,7 @@ var ( // UsersColumns holds the columns for the "users" table. UsersColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "optional_int", Type: field.TypeInt, Nullable: true}, {Name: "age", Type: field.TypeInt}, {Name: "name", Type: field.TypeString}, {Name: "last", Type: field.TypeString, Default: user.DefaultLast}, @@ -289,21 +290,21 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "users_groups_blocked", - Columns: []*schema.Column{UsersColumns[7]}, + Columns: []*schema.Column{UsersColumns[8]}, RefColumns: []*schema.Column{GroupsColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "users_users_spouse", - Columns: []*schema.Column{UsersColumns[8]}, + Columns: []*schema.Column{UsersColumns[9]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, }, { Symbol: "users_users_parent", - Columns: []*schema.Column{UsersColumns[9]}, + Columns: []*schema.Column{UsersColumns[10]}, RefColumns: []*schema.Column{UsersColumns[0]}, OnDelete: schema.SetNull, diff --git a/entc/integration/ent/schema/user.go b/entc/integration/ent/schema/user.go index 6a1d0d591..fe925ba48 100644 --- a/entc/integration/ent/schema/user.go +++ b/entc/integration/ent/schema/user.go @@ -15,6 +15,12 @@ type User struct { ent.Schema } +func (User) Mixin() []ent.Mixin { + return []ent.Mixin{ + UserMixin{}, + } +} + // Fields of the user. func (User) Fields() []ent.Field { return []ent.Field{ @@ -50,3 +56,15 @@ func (User) Edges() []ent.Edge { edge.To("parent", User.Type).Unique().From("children"), } } + +// UserMixin composes create/update time mixin. +type UserMixin struct{} + +// Fields of the time mixin. +func (UserMixin) Fields() []ent.Field { + return []ent.Field{ + field.Int("optional_int"). + Optional(). + Positive(), + } +} diff --git a/entc/integration/ent/user.go b/entc/integration/ent/user.go index 712e2a9b4..2a8fb5dc0 100644 --- a/entc/integration/ent/user.go +++ b/entc/integration/ent/user.go @@ -20,6 +20,8 @@ type User struct { config `graphql:"-" json:"-"` // ID of the ent. ID string `json:"id,omitempty"` + // OptionalInt holds the value of the "optional_int" field. + OptionalInt int `json:"optional_int,omitempty"` // Age holds the value of the "age" field. Age int `json:"age,omitempty"` // Name holds the value of the "name" field. @@ -37,6 +39,7 @@ type User struct { // scanValues returns the types for scanning values from sql.Rows. func (*User) scanValues() []interface{} { return []interface{}{ + &sql.NullInt64{}, &sql.NullInt64{}, &sql.NullInt64{}, &sql.NullString{}, @@ -60,32 +63,37 @@ func (u *User) assignValues(values ...interface{}) error { u.ID = strconv.FormatInt(value.Int64, 10) values = values[1:] if value, ok := values[0].(*sql.NullInt64); !ok { - return fmt.Errorf("unexpected type %T for field age", values[0]) + return fmt.Errorf("unexpected type %T for field optional_int", values[0]) + } else if value.Valid { + u.OptionalInt = int(value.Int64) + } + if value, ok := values[1].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field age", values[1]) } else if value.Valid { u.Age = int(value.Int64) } - if value, ok := values[1].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field name", values[1]) + if value, ok := values[2].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[2]) } else if value.Valid { u.Name = value.String } - if value, ok := values[2].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field last", values[2]) + if value, ok := values[3].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field last", values[3]) } else if value.Valid { u.Last = value.String } - if value, ok := values[3].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field nickname", values[3]) + if value, ok := values[4].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field nickname", values[4]) } else if value.Valid { u.Nickname = value.String } - if value, ok := values[4].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field phone", values[4]) + if value, ok := values[5].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field phone", values[5]) } else if value.Valid { u.Phone = value.String } - if value, ok := values[5].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field password", values[5]) + if value, ok := values[6].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field password", values[6]) } else if value.Valid { u.Password = value.String } @@ -170,6 +178,8 @@ func (u *User) String() string { var builder strings.Builder builder.WriteString("User(") builder.WriteString(fmt.Sprintf("id=%v", u.ID)) + builder.WriteString(", optional_int=") + builder.WriteString(fmt.Sprintf("%v", u.OptionalInt)) builder.WriteString(", age=") builder.WriteString(fmt.Sprintf("%v", u.Age)) builder.WriteString(", name=") diff --git a/entc/integration/ent/user/user.go b/entc/integration/ent/user/user.go index 3d60f8f6b..e0098090b 100644 --- a/entc/integration/ent/user/user.go +++ b/entc/integration/ent/user/user.go @@ -7,6 +7,7 @@ package user import ( + "github.com/facebookincubator/ent" "github.com/facebookincubator/ent/entc/integration/ent/schema" ) @@ -15,6 +16,8 @@ const ( Label = "user" // FieldID holds the string denoting the id field in the database. FieldID = "id" + // FieldOptionalInt holds the string denoting the optional_int vertex property in the database. + FieldOptionalInt = "optional_int" // FieldAge holds the string denoting the age vertex property in the database. FieldAge = "age" // FieldName holds the string denoting the name vertex property in the database. @@ -86,6 +89,7 @@ const ( // Columns holds all SQL columns are user fields. var Columns = []string{ FieldID, + FieldOptionalInt, FieldAge, FieldName, FieldLast, @@ -110,8 +114,17 @@ var ( ) var ( + mixin = schema.User{}.Mixin() + mixinFields = [...][]ent.Field{ + mixin[0].Fields(), + } fields = schema.User{}.Fields() + // descOptionalInt is the schema descriptor for optional_int field. + descOptionalInt = mixinFields[0][0].Descriptor() + // OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save. + OptionalIntValidator = descOptionalInt.Validators[0].(func(int) error) + // descLast is the schema descriptor for last field. descLast = fields[2].Descriptor() // DefaultLast holds the default value on creation for the last field. diff --git a/entc/integration/ent/user/where.go b/entc/integration/ent/user/where.go index 611aa14c6..df8f1b5a5 100644 --- a/entc/integration/ent/user/where.go +++ b/entc/integration/ent/user/where.go @@ -114,6 +114,14 @@ func IDLTE(id string) predicate.User { ) } +// OptionalInt applies equality check predicate on the "optional_int" field. It's identical to OptionalIntEQ. +func OptionalInt(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldOptionalInt), v)) + }, + ) +} + // Age applies equality check predicate on the "age" field. It's identical to AgeEQ. func Age(v int) predicate.User { return predicate.User(func(s *sql.Selector) { @@ -162,6 +170,106 @@ func Password(v string) predicate.User { ) } +// OptionalIntEQ applies the EQ predicate on the "optional_int" field. +func OptionalIntEQ(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntNEQ applies the NEQ predicate on the "optional_int" field. +func OptionalIntNEQ(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntIn applies the In predicate on the "optional_int" field. +func OptionalIntIn(vs ...int) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(vs) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.In(s.C(FieldOptionalInt), v...)) + }, + ) +} + +// OptionalIntNotIn applies the NotIn predicate on the "optional_int" field. +func OptionalIntNotIn(vs ...int) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(s *sql.Selector) { + // if not arguments were provided, append the FALSE constants, + // since we can't apply "IN ()". This will make this predicate falsy. + if len(vs) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.NotIn(s.C(FieldOptionalInt), v...)) + }, + ) +} + +// OptionalIntGT applies the GT predicate on the "optional_int" field. +func OptionalIntGT(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntGTE applies the GTE predicate on the "optional_int" field. +func OptionalIntGTE(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntLT applies the LT predicate on the "optional_int" field. +func OptionalIntLT(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntLTE applies the LTE predicate on the "optional_int" field. +func OptionalIntLTE(v int) predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldOptionalInt), v)) + }, + ) +} + +// OptionalIntIsNil applies the IsNil predicate on the "optional_int" field. +func OptionalIntIsNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldOptionalInt))) + }, + ) +} + +// OptionalIntNotNil applies the NotNil predicate on the "optional_int" field. +func OptionalIntNotNil() predicate.User { + return predicate.User(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldOptionalInt))) + }, + ) +} + // AgeEQ applies the EQ predicate on the "age" field. func AgeEQ(v int) predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/entc/integration/ent/user_create.go b/entc/integration/ent/user_create.go index df67dfbfa..08111b2c1 100644 --- a/entc/integration/ent/user_create.go +++ b/entc/integration/ent/user_create.go @@ -9,6 +9,7 @@ package ent import ( "context" "errors" + "fmt" "strconv" "github.com/facebookincubator/ent/dialect/sql/sqlgraph" @@ -23,23 +24,38 @@ import ( // UserCreate is the builder for creating a User entity. type UserCreate struct { config - age *int - name *string - last *string - nickname *string - phone *string - password *string - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} + optional_int *int + age *int + name *string + last *string + nickname *string + phone *string + password *string + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} +} + +// SetOptionalInt sets the optional_int field. +func (uc *UserCreate) SetOptionalInt(i int) *UserCreate { + uc.optional_int = &i + return uc +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uc *UserCreate) SetNillableOptionalInt(i *int) *UserCreate { + if i != nil { + uc.SetOptionalInt(*i) + } + return uc } // SetAge sets the age field. @@ -340,6 +356,11 @@ func (uc *UserCreate) SetParent(u *User) *UserCreate { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { + if uc.optional_int != nil { + if err := user.OptionalIntValidator(*uc.optional_int); err != nil { + return nil, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if uc.age == nil { return nil, errors.New("ent: missing required field \"age\"") } @@ -385,6 +406,14 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { }, } ) + if value := uc.optional_int; value != nil { + spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: *value, + Column: user.FieldOptionalInt, + }) + u.OptionalInt = *value + } if value := uc.age; value != nil { spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt, diff --git a/entc/integration/ent/user_query.go b/entc/integration/ent/user_query.go index 252771257..96d6b8caa 100644 --- a/entc/integration/ent/user_query.go +++ b/entc/integration/ent/user_query.go @@ -366,12 +366,12 @@ func (uq *UserQuery) Clone() *UserQuery { // Example: // // var v []struct { -// Age int `json:"age,omitempty"` +// OptionalInt int `json:"optional_int,omitempty"` // Count int `json:"count,omitempty"` // } // // client.User.Query(). -// GroupBy(user.FieldAge). +// GroupBy(user.FieldOptionalInt). // Aggregate(ent.Count()). // Scan(ctx, &v) // @@ -387,11 +387,11 @@ func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { // Example: // // var v []struct { -// Age int `json:"age,omitempty"` +// OptionalInt int `json:"optional_int,omitempty"` // } // // client.User.Query(). -// Select(user.FieldAge). +// Select(user.FieldOptionalInt). // Scan(ctx, &v) // func (uq *UserQuery) Select(field string, fields ...string) *UserSelect { diff --git a/entc/integration/ent/user_update.go b/entc/integration/ent/user_update.go index 597a32a8d..95b0e5cdb 100644 --- a/entc/integration/ent/user_update.go +++ b/entc/integration/ent/user_update.go @@ -9,6 +9,7 @@ package ent import ( "context" "errors" + "fmt" "strconv" "github.com/facebookincubator/ent/dialect/sql" @@ -25,39 +26,42 @@ import ( // UserUpdate is the builder for updating User entities. type UserUpdate struct { config - age *int - addage *int - name *string - last *string - nickname *string - clearnickname bool - phone *string - clearphone bool - password *string - clearpassword bool - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} - clearedCard bool - removedPets map[string]struct{} - removedFiles map[string]struct{} - removedGroups map[string]struct{} - removedFriends map[string]struct{} - removedFollowers map[string]struct{} - removedFollowing map[string]struct{} - clearedTeam bool - clearedSpouse bool - removedChildren map[string]struct{} - clearedParent bool - predicates []predicate.User + optional_int *int + addoptional_int *int + clearoptional_int bool + age *int + addage *int + name *string + last *string + nickname *string + clearnickname bool + phone *string + clearphone bool + password *string + clearpassword bool + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} + clearedCard bool + removedPets map[string]struct{} + removedFiles map[string]struct{} + removedGroups map[string]struct{} + removedFriends map[string]struct{} + removedFollowers map[string]struct{} + removedFollowing map[string]struct{} + clearedTeam bool + clearedSpouse bool + removedChildren map[string]struct{} + clearedParent bool + predicates []predicate.User } // Where adds a new predicate for the builder. @@ -66,6 +70,38 @@ func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate { return uu } +// SetOptionalInt sets the optional_int field. +func (uu *UserUpdate) SetOptionalInt(i int) *UserUpdate { + uu.optional_int = &i + uu.addoptional_int = nil + return uu +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uu *UserUpdate) SetNillableOptionalInt(i *int) *UserUpdate { + if i != nil { + uu.SetOptionalInt(*i) + } + return uu +} + +// AddOptionalInt adds i to optional_int. +func (uu *UserUpdate) AddOptionalInt(i int) *UserUpdate { + if uu.addoptional_int == nil { + uu.addoptional_int = &i + } else { + *uu.addoptional_int += i + } + return uu +} + +// ClearOptionalInt clears the value of optional_int. +func (uu *UserUpdate) ClearOptionalInt() *UserUpdate { + uu.optional_int = nil + uu.clearoptional_int = true + return uu +} + // SetAge sets the age field. func (uu *UserUpdate) SetAge(i int) *UserUpdate { uu.age = &i @@ -560,6 +596,11 @@ func (uu *UserUpdate) ClearParent() *UserUpdate { // Save executes the query and returns the number of rows/vertices matched by this operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { + if uu.optional_int != nil { + if err := user.OptionalIntValidator(*uu.optional_int); err != nil { + return 0, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if len(uu.card) > 1 { return 0, errors.New("ent: multiple assignments on a unique edge \"card\"") } @@ -615,6 +656,26 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } } } + if value := uu.optional_int; value != nil { + spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: *value, + Column: user.FieldOptionalInt, + }) + } + if value := uu.addoptional_int; value != nil { + spec.Fields.Add = append(spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: *value, + Column: user.FieldOptionalInt, + }) + } + if uu.clearoptional_int { + spec.Fields.Clear = append(spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: user.FieldOptionalInt, + }) + } if value := uu.age; value != nil { spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, @@ -1172,39 +1233,74 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { // UserUpdateOne is the builder for updating a single User entity. type UserUpdateOne struct { config - id string - age *int - addage *int - name *string - last *string - nickname *string - clearnickname bool - phone *string - clearphone bool - password *string - clearpassword bool - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} - clearedCard bool - removedPets map[string]struct{} - removedFiles map[string]struct{} - removedGroups map[string]struct{} - removedFriends map[string]struct{} - removedFollowers map[string]struct{} - removedFollowing map[string]struct{} - clearedTeam bool - clearedSpouse bool - removedChildren map[string]struct{} - clearedParent bool + id string + optional_int *int + addoptional_int *int + clearoptional_int bool + age *int + addage *int + name *string + last *string + nickname *string + clearnickname bool + phone *string + clearphone bool + password *string + clearpassword bool + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} + clearedCard bool + removedPets map[string]struct{} + removedFiles map[string]struct{} + removedGroups map[string]struct{} + removedFriends map[string]struct{} + removedFollowers map[string]struct{} + removedFollowing map[string]struct{} + clearedTeam bool + clearedSpouse bool + removedChildren map[string]struct{} + clearedParent bool +} + +// SetOptionalInt sets the optional_int field. +func (uuo *UserUpdateOne) SetOptionalInt(i int) *UserUpdateOne { + uuo.optional_int = &i + uuo.addoptional_int = nil + return uuo +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableOptionalInt(i *int) *UserUpdateOne { + if i != nil { + uuo.SetOptionalInt(*i) + } + return uuo +} + +// AddOptionalInt adds i to optional_int. +func (uuo *UserUpdateOne) AddOptionalInt(i int) *UserUpdateOne { + if uuo.addoptional_int == nil { + uuo.addoptional_int = &i + } else { + *uuo.addoptional_int += i + } + return uuo +} + +// ClearOptionalInt clears the value of optional_int. +func (uuo *UserUpdateOne) ClearOptionalInt() *UserUpdateOne { + uuo.optional_int = nil + uuo.clearoptional_int = true + return uuo } // SetAge sets the age field. @@ -1701,6 +1797,11 @@ func (uuo *UserUpdateOne) ClearParent() *UserUpdateOne { // Save executes the query and returns the updated entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { + if uuo.optional_int != nil { + if err := user.OptionalIntValidator(*uuo.optional_int); err != nil { + return nil, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if len(uuo.card) > 1 { return nil, errors.New("ent: multiple assignments on a unique edge \"card\"") } @@ -1750,6 +1851,26 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) { }, }, } + if value := uuo.optional_int; value != nil { + spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: *value, + Column: user.FieldOptionalInt, + }) + } + if value := uuo.addoptional_int; value != nil { + spec.Fields.Add = append(spec.Fields.Add, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Value: *value, + Column: user.FieldOptionalInt, + }) + } + if uuo.clearoptional_int { + spec.Fields.Clear = append(spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeInt, + Column: user.FieldOptionalInt, + }) + } if value := uuo.age; value != nil { spec.Fields.Set = append(spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, diff --git a/entc/integration/gremlin/ent/example_test.go b/entc/integration/gremlin/ent/example_test.go index 83d832f62..b86721e50 100644 --- a/entc/integration/gremlin/ent/example_test.go +++ b/entc/integration/gremlin/ent/example_test.go @@ -202,6 +202,7 @@ func ExampleGroup() { log.Println("file created:", f0) u1 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -404,6 +405,7 @@ func ExampleUser() { log.Println("group created:", gr3) u4 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -414,6 +416,7 @@ func ExampleUser() { log.Println("user created:", u4) u6 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -429,6 +432,7 @@ func ExampleUser() { log.Println("pet created:", pe7) u8 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -439,6 +443,7 @@ func ExampleUser() { log.Println("user created:", u8) u10 := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). @@ -451,6 +456,7 @@ func ExampleUser() { // create user vertex with its edges. u := client.User. Create(). + SetOptionalInt(1). SetAge(1). SetName("string"). SetLast("string"). diff --git a/entc/integration/gremlin/ent/user.go b/entc/integration/gremlin/ent/user.go index 5d57f91b1..270641d84 100644 --- a/entc/integration/gremlin/ent/user.go +++ b/entc/integration/gremlin/ent/user.go @@ -19,6 +19,8 @@ type User struct { config `graphql:"-" json:"-"` // ID of the ent. ID string `json:"id,omitempty"` + // OptionalInt holds the value of the "optional_int" field. + OptionalInt int `json:"optional_int,omitempty"` // Age holds the value of the "age" field. Age int `json:"age,omitempty"` // Name holds the value of the "name" field. @@ -40,18 +42,20 @@ func (u *User) FromResponse(res *gremlin.Response) error { return err } var scanu struct { - ID string `json:"id,omitempty"` - Age int `json:"age,omitempty"` - Name string `json:"name,omitempty"` - Last string `json:"last,omitempty"` - Nickname string `json:"nickname,omitempty"` - Phone string `json:"phone,omitempty"` - Password string `json:"password,omitempty"` + ID string `json:"id,omitempty"` + OptionalInt int `json:"optional_int,omitempty"` + Age int `json:"age,omitempty"` + Name string `json:"name,omitempty"` + Last string `json:"last,omitempty"` + Nickname string `json:"nickname,omitempty"` + Phone string `json:"phone,omitempty"` + Password string `json:"password,omitempty"` } if err := vmap.Decode(&scanu); err != nil { return err } u.ID = scanu.ID + u.OptionalInt = scanu.OptionalInt u.Age = scanu.Age u.Name = scanu.Name u.Last = scanu.Last @@ -139,6 +143,8 @@ func (u *User) String() string { var builder strings.Builder builder.WriteString("User(") builder.WriteString(fmt.Sprintf("id=%v", u.ID)) + builder.WriteString(", optional_int=") + builder.WriteString(fmt.Sprintf("%v", u.OptionalInt)) builder.WriteString(", age=") builder.WriteString(fmt.Sprintf("%v", u.Age)) builder.WriteString(", name=") @@ -170,26 +176,28 @@ func (u *Users) FromResponse(res *gremlin.Response) error { return err } var scanu []struct { - ID string `json:"id,omitempty"` - Age int `json:"age,omitempty"` - Name string `json:"name,omitempty"` - Last string `json:"last,omitempty"` - Nickname string `json:"nickname,omitempty"` - Phone string `json:"phone,omitempty"` - Password string `json:"password,omitempty"` + ID string `json:"id,omitempty"` + OptionalInt int `json:"optional_int,omitempty"` + Age int `json:"age,omitempty"` + Name string `json:"name,omitempty"` + Last string `json:"last,omitempty"` + Nickname string `json:"nickname,omitempty"` + Phone string `json:"phone,omitempty"` + Password string `json:"password,omitempty"` } if err := vmap.Decode(&scanu); err != nil { return err } for _, v := range scanu { *u = append(*u, &User{ - ID: v.ID, - Age: v.Age, - Name: v.Name, - Last: v.Last, - Nickname: v.Nickname, - Phone: v.Phone, - Password: v.Password, + ID: v.ID, + OptionalInt: v.OptionalInt, + Age: v.Age, + Name: v.Name, + Last: v.Last, + Nickname: v.Nickname, + Phone: v.Phone, + Password: v.Password, }) } return nil diff --git a/entc/integration/gremlin/ent/user/user.go b/entc/integration/gremlin/ent/user/user.go index f1debfe0a..bfcaac389 100644 --- a/entc/integration/gremlin/ent/user/user.go +++ b/entc/integration/gremlin/ent/user/user.go @@ -7,6 +7,7 @@ package user import ( + "github.com/facebookincubator/ent" "github.com/facebookincubator/ent/entc/integration/ent/schema" ) @@ -15,6 +16,8 @@ const ( Label = "user" // FieldID holds the string denoting the id field in the database. FieldID = "id" + // FieldOptionalInt holds the string denoting the optional_int vertex property in the database. + FieldOptionalInt = "optional_int" // FieldAge holds the string denoting the age vertex property in the database. FieldAge = "age" // FieldName holds the string denoting the name vertex property in the database. @@ -53,8 +56,17 @@ const ( ) var ( + mixin = schema.User{}.Mixin() + mixinFields = [...][]ent.Field{ + mixin[0].Fields(), + } fields = schema.User{}.Fields() + // descOptionalInt is the schema descriptor for optional_int field. + descOptionalInt = mixinFields[0][0].Descriptor() + // OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save. + OptionalIntValidator = descOptionalInt.Validators[0].(func(int) error) + // descLast is the schema descriptor for last field. descLast = fields[2].Descriptor() // DefaultLast holds the default value on creation for the last field. diff --git a/entc/integration/gremlin/ent/user/where.go b/entc/integration/gremlin/ent/user/where.go index 28f25ffb3..b7fa91fd1 100644 --- a/entc/integration/gremlin/ent/user/where.go +++ b/entc/integration/gremlin/ent/user/where.go @@ -94,6 +94,14 @@ func IDLTE(id string) predicate.User { ) } +// OptionalInt applies equality check predicate on the "optional_int" field. It's identical to OptionalIntEQ. +func OptionalInt(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.EQ(v)) + }, + ) +} + // Age applies equality check predicate on the "age" field. It's identical to AgeEQ. func Age(v int) predicate.User { return predicate.User(func(t *dsl.Traversal) { @@ -142,6 +150,94 @@ func Password(v string) predicate.User { ) } +// OptionalIntEQ applies the EQ predicate on the "optional_int" field. +func OptionalIntEQ(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.EQ(v)) + }, + ) +} + +// OptionalIntNEQ applies the NEQ predicate on the "optional_int" field. +func OptionalIntNEQ(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.NEQ(v)) + }, + ) +} + +// OptionalIntIn applies the In predicate on the "optional_int" field. +func OptionalIntIn(vs ...int) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.Within(v...)) + }, + ) +} + +// OptionalIntNotIn applies the NotIn predicate on the "optional_int" field. +func OptionalIntNotIn(vs ...int) predicate.User { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.Without(v...)) + }, + ) +} + +// OptionalIntGT applies the GT predicate on the "optional_int" field. +func OptionalIntGT(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.GT(v)) + }, + ) +} + +// OptionalIntGTE applies the GTE predicate on the "optional_int" field. +func OptionalIntGTE(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.GTE(v)) + }, + ) +} + +// OptionalIntLT applies the LT predicate on the "optional_int" field. +func OptionalIntLT(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.LT(v)) + }, + ) +} + +// OptionalIntLTE applies the LTE predicate on the "optional_int" field. +func OptionalIntLTE(v int) predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.Has(Label, FieldOptionalInt, p.LTE(v)) + }, + ) +} + +// OptionalIntIsNil applies the IsNil predicate on the "optional_int" field. +func OptionalIntIsNil() predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldOptionalInt) + }, + ) +} + +// OptionalIntNotNil applies the NotNil predicate on the "optional_int" field. +func OptionalIntNotNil() predicate.User { + return predicate.User(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldOptionalInt) + }, + ) +} + // AgeEQ applies the EQ predicate on the "age" field. func AgeEQ(v int) predicate.User { return predicate.User(func(t *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/user_create.go b/entc/integration/gremlin/ent/user_create.go index b5f4c094a..1efa16380 100644 --- a/entc/integration/gremlin/ent/user_create.go +++ b/entc/integration/gremlin/ent/user_create.go @@ -9,6 +9,7 @@ package ent import ( "context" "errors" + "fmt" "github.com/facebookincubator/ent/dialect/gremlin" "github.com/facebookincubator/ent/dialect/gremlin/graph/dsl" @@ -21,23 +22,38 @@ import ( // UserCreate is the builder for creating a User entity. type UserCreate struct { config - age *int - name *string - last *string - nickname *string - phone *string - password *string - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} + optional_int *int + age *int + name *string + last *string + nickname *string + phone *string + password *string + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} +} + +// SetOptionalInt sets the optional_int field. +func (uc *UserCreate) SetOptionalInt(i int) *UserCreate { + uc.optional_int = &i + return uc +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uc *UserCreate) SetNillableOptionalInt(i *int) *UserCreate { + if i != nil { + uc.SetOptionalInt(*i) + } + return uc } // SetAge sets the age field. @@ -338,6 +354,11 @@ func (uc *UserCreate) SetParent(u *User) *UserCreate { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { + if uc.optional_int != nil { + if err := user.OptionalIntValidator(*uc.optional_int); err != nil { + return nil, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if uc.age == nil { return nil, errors.New("ent: missing required field \"age\"") } @@ -395,6 +416,9 @@ func (uc *UserCreate) gremlin() *dsl.Traversal { } constraints := make([]*constraint, 0, 8) v := g.AddV(user.Label) + if uc.optional_int != nil { + v.Property(dsl.Single, user.FieldOptionalInt, *uc.optional_int) + } if uc.age != nil { v.Property(dsl.Single, user.FieldAge, *uc.age) } diff --git a/entc/integration/gremlin/ent/user_query.go b/entc/integration/gremlin/ent/user_query.go index a8f72cb17..1607d22b1 100644 --- a/entc/integration/gremlin/ent/user_query.go +++ b/entc/integration/gremlin/ent/user_query.go @@ -318,12 +318,12 @@ func (uq *UserQuery) Clone() *UserQuery { // Example: // // var v []struct { -// Age int `json:"age,omitempty"` +// OptionalInt int `json:"optional_int,omitempty"` // Count int `json:"count,omitempty"` // } // // client.User.Query(). -// GroupBy(user.FieldAge). +// GroupBy(user.FieldOptionalInt). // Aggregate(ent.Count()). // Scan(ctx, &v) // @@ -339,11 +339,11 @@ func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { // Example: // // var v []struct { -// Age int `json:"age,omitempty"` +// OptionalInt int `json:"optional_int,omitempty"` // } // // client.User.Query(). -// Select(user.FieldAge). +// Select(user.FieldOptionalInt). // Scan(ctx, &v) // func (uq *UserQuery) Select(field string, fields ...string) *UserSelect { diff --git a/entc/integration/gremlin/ent/user_update.go b/entc/integration/gremlin/ent/user_update.go index 92d837f09..180f21e74 100644 --- a/entc/integration/gremlin/ent/user_update.go +++ b/entc/integration/gremlin/ent/user_update.go @@ -9,6 +9,7 @@ package ent import ( "context" "errors" + "fmt" "github.com/facebookincubator/ent/dialect/gremlin" "github.com/facebookincubator/ent/dialect/gremlin/graph/dsl" @@ -22,39 +23,42 @@ import ( // UserUpdate is the builder for updating User entities. type UserUpdate struct { config - age *int - addage *int - name *string - last *string - nickname *string - clearnickname bool - phone *string - clearphone bool - password *string - clearpassword bool - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} - clearedCard bool - removedPets map[string]struct{} - removedFiles map[string]struct{} - removedGroups map[string]struct{} - removedFriends map[string]struct{} - removedFollowers map[string]struct{} - removedFollowing map[string]struct{} - clearedTeam bool - clearedSpouse bool - removedChildren map[string]struct{} - clearedParent bool - predicates []predicate.User + optional_int *int + addoptional_int *int + clearoptional_int bool + age *int + addage *int + name *string + last *string + nickname *string + clearnickname bool + phone *string + clearphone bool + password *string + clearpassword bool + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} + clearedCard bool + removedPets map[string]struct{} + removedFiles map[string]struct{} + removedGroups map[string]struct{} + removedFriends map[string]struct{} + removedFollowers map[string]struct{} + removedFollowing map[string]struct{} + clearedTeam bool + clearedSpouse bool + removedChildren map[string]struct{} + clearedParent bool + predicates []predicate.User } // Where adds a new predicate for the builder. @@ -63,6 +67,38 @@ func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate { return uu } +// SetOptionalInt sets the optional_int field. +func (uu *UserUpdate) SetOptionalInt(i int) *UserUpdate { + uu.optional_int = &i + uu.addoptional_int = nil + return uu +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uu *UserUpdate) SetNillableOptionalInt(i *int) *UserUpdate { + if i != nil { + uu.SetOptionalInt(*i) + } + return uu +} + +// AddOptionalInt adds i to optional_int. +func (uu *UserUpdate) AddOptionalInt(i int) *UserUpdate { + if uu.addoptional_int == nil { + uu.addoptional_int = &i + } else { + *uu.addoptional_int += i + } + return uu +} + +// ClearOptionalInt clears the value of optional_int. +func (uu *UserUpdate) ClearOptionalInt() *UserUpdate { + uu.optional_int = nil + uu.clearoptional_int = true + return uu +} + // SetAge sets the age field. func (uu *UserUpdate) SetAge(i int) *UserUpdate { uu.age = &i @@ -557,6 +593,11 @@ func (uu *UserUpdate) ClearParent() *UserUpdate { // Save executes the query and returns the number of rows/vertices matched by this operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { + if uu.optional_int != nil { + if err := user.OptionalIntValidator(*uu.optional_int); err != nil { + return 0, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if len(uu.card) > 1 { return 0, errors.New("ent: multiple assignments on a unique edge \"card\"") } @@ -622,6 +663,12 @@ func (uu *UserUpdate) gremlin() *dsl.Traversal { trs []*dsl.Traversal ) + if value := uu.optional_int; value != nil { + v.Property(dsl.Single, user.FieldOptionalInt, *value) + } + if value := uu.addoptional_int; value != nil { + v.Property(dsl.Single, user.FieldOptionalInt, __.Union(__.Values(user.FieldOptionalInt), __.Constant(*value)).Sum()) + } if value := uu.age; value != nil { v.Property(dsl.Single, user.FieldAge, *value) } @@ -652,6 +699,9 @@ func (uu *UserUpdate) gremlin() *dsl.Traversal { v.Property(dsl.Single, user.FieldPassword, *value) } var properties []interface{} + if uu.clearoptional_int { + properties = append(properties, user.FieldOptionalInt) + } if uu.clearnickname { properties = append(properties, user.FieldNickname) } @@ -787,39 +837,74 @@ func (uu *UserUpdate) gremlin() *dsl.Traversal { // UserUpdateOne is the builder for updating a single User entity. type UserUpdateOne struct { config - id string - age *int - addage *int - name *string - last *string - nickname *string - clearnickname bool - phone *string - clearphone bool - password *string - clearpassword bool - card map[string]struct{} - pets map[string]struct{} - files map[string]struct{} - groups map[string]struct{} - friends map[string]struct{} - followers map[string]struct{} - following map[string]struct{} - team map[string]struct{} - spouse map[string]struct{} - children map[string]struct{} - parent map[string]struct{} - clearedCard bool - removedPets map[string]struct{} - removedFiles map[string]struct{} - removedGroups map[string]struct{} - removedFriends map[string]struct{} - removedFollowers map[string]struct{} - removedFollowing map[string]struct{} - clearedTeam bool - clearedSpouse bool - removedChildren map[string]struct{} - clearedParent bool + id string + optional_int *int + addoptional_int *int + clearoptional_int bool + age *int + addage *int + name *string + last *string + nickname *string + clearnickname bool + phone *string + clearphone bool + password *string + clearpassword bool + card map[string]struct{} + pets map[string]struct{} + files map[string]struct{} + groups map[string]struct{} + friends map[string]struct{} + followers map[string]struct{} + following map[string]struct{} + team map[string]struct{} + spouse map[string]struct{} + children map[string]struct{} + parent map[string]struct{} + clearedCard bool + removedPets map[string]struct{} + removedFiles map[string]struct{} + removedGroups map[string]struct{} + removedFriends map[string]struct{} + removedFollowers map[string]struct{} + removedFollowing map[string]struct{} + clearedTeam bool + clearedSpouse bool + removedChildren map[string]struct{} + clearedParent bool +} + +// SetOptionalInt sets the optional_int field. +func (uuo *UserUpdateOne) SetOptionalInt(i int) *UserUpdateOne { + uuo.optional_int = &i + uuo.addoptional_int = nil + return uuo +} + +// SetNillableOptionalInt sets the optional_int field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillableOptionalInt(i *int) *UserUpdateOne { + if i != nil { + uuo.SetOptionalInt(*i) + } + return uuo +} + +// AddOptionalInt adds i to optional_int. +func (uuo *UserUpdateOne) AddOptionalInt(i int) *UserUpdateOne { + if uuo.addoptional_int == nil { + uuo.addoptional_int = &i + } else { + *uuo.addoptional_int += i + } + return uuo +} + +// ClearOptionalInt clears the value of optional_int. +func (uuo *UserUpdateOne) ClearOptionalInt() *UserUpdateOne { + uuo.optional_int = nil + uuo.clearoptional_int = true + return uuo } // SetAge sets the age field. @@ -1316,6 +1401,11 @@ func (uuo *UserUpdateOne) ClearParent() *UserUpdateOne { // Save executes the query and returns the updated entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { + if uuo.optional_int != nil { + if err := user.OptionalIntValidator(*uuo.optional_int); err != nil { + return nil, fmt.Errorf("ent: validator failed for field \"optional_int\": %v", err) + } + } if len(uuo.card) > 1 { return nil, errors.New("ent: multiple assignments on a unique edge \"card\"") } @@ -1382,6 +1472,12 @@ func (uuo *UserUpdateOne) gremlin(id string) *dsl.Traversal { trs []*dsl.Traversal ) + if value := uuo.optional_int; value != nil { + v.Property(dsl.Single, user.FieldOptionalInt, *value) + } + if value := uuo.addoptional_int; value != nil { + v.Property(dsl.Single, user.FieldOptionalInt, __.Union(__.Values(user.FieldOptionalInt), __.Constant(*value)).Sum()) + } if value := uuo.age; value != nil { v.Property(dsl.Single, user.FieldAge, *value) } @@ -1412,6 +1508,9 @@ func (uuo *UserUpdateOne) gremlin(id string) *dsl.Traversal { v.Property(dsl.Single, user.FieldPassword, *value) } var properties []interface{} + if uuo.clearoptional_int { + properties = append(properties, user.FieldOptionalInt) + } if uuo.clearnickname { properties = append(properties, user.FieldNickname) }