From eca70b8490346c178368295872e622ec193df824 Mon Sep 17 00:00:00 2001 From: Clarence Date: Thu, 13 Jan 2022 15:39:11 +0400 Subject: [PATCH] schema/field: array types are nillable (#2266) * all: type fixes * schema/field: remove array check from goType --- CONTRIBUTING.md | 2 +- entc/gen/template/builder/setter.tmpl | 2 +- entc/integration/customid/ent/blob_create.go | 16 ++++++++++ entc/integration/customid/ent/blob_update.go | 16 ++++++++++ .../integration/customid/ent/device_create.go | 8 +++++ .../customid/ent/mixinid_create.go | 8 +++++ .../customid/ent/session_create.go | 8 +++++ entc/integration/edgefield/ent/car_create.go | 8 +++++ entc/integration/ent/fieldtype_create.go | 16 ++++++++++ entc/integration/ent/fieldtype_update.go | 32 +++++++++++++++++++ entc/integration/ent/pet_create.go | 8 +++++ entc/integration/ent/pet_update.go | 16 ++++++++++ .../gremlin/ent/fieldtype_create.go | 16 ++++++++++ .../gremlin/ent/fieldtype_update.go | 32 +++++++++++++++++++ entc/integration/gremlin/ent/pet_create.go | 8 +++++ entc/integration/gremlin/ent/pet_update.go | 16 ++++++++++ .../privacy/ent/internal/schema.go | 2 +- entc/integration/privacy/ent/task_create.go | 8 +++++ entc/integration/privacy/ent/task_update.go | 16 ++++++++++ schema/field/field.go | 9 +++--- schema/field/field_test.go | 2 +- 21 files changed, 240 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 799a47ae5..4017874ea 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,7 @@ If you touch any file in `entc`, run the following command in `entc`: go generate ./... ``` -Then, in `entc/inegration` run `docker-compose` in order to spin-up all database containers: +Then, in `entc/integration` run `docker-compose` in order to spin-up all database containers: ``` docker-compose -f docker-compose.yaml up -d diff --git a/entc/gen/template/builder/setter.tmpl b/entc/gen/template/builder/setter.tmpl index acc6a4d5a..9d5b9ed87 100644 --- a/entc/gen/template/builder/setter.tmpl +++ b/entc/gen/template/builder/setter.tmpl @@ -31,7 +31,7 @@ in the LICENSE file in the root directory of this source tree. return {{ $receiver }} } - {{/* Avoid generting nillable setters for nillable types. */}} + {{/* Avoid generating nillable setters for nillable types. */}} {{ if and (not $f.Type.Nillable) (or $f.Optional $f.Default) (not (and $updater $f.UpdateDefault)) }} {{ $nillableFunc := print "SetNillable" $f.StructField }} // {{ $nillableFunc }} sets the "{{ $f.Name }}" field if the given value is not nil. diff --git a/entc/integration/customid/ent/blob_create.go b/entc/integration/customid/ent/blob_create.go index f6a0ce92f..0df7ead95 100644 --- a/entc/integration/customid/ent/blob_create.go +++ b/entc/integration/customid/ent/blob_create.go @@ -33,6 +33,14 @@ func (bc *BlobCreate) SetUUID(u uuid.UUID) *BlobCreate { return bc } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (bc *BlobCreate) SetNillableUUID(u *uuid.UUID) *BlobCreate { + if u != nil { + bc.SetUUID(*u) + } + return bc +} + // SetCount sets the "count" field. func (bc *BlobCreate) SetCount(i int) *BlobCreate { bc.mutation.SetCount(i) @@ -53,6 +61,14 @@ func (bc *BlobCreate) SetID(u uuid.UUID) *BlobCreate { return bc } +// SetNillableID sets the "id" field if the given value is not nil. +func (bc *BlobCreate) SetNillableID(u *uuid.UUID) *BlobCreate { + if u != nil { + bc.SetID(*u) + } + return bc +} + // SetParentID sets the "parent" edge to the Blob entity by ID. func (bc *BlobCreate) SetParentID(id uuid.UUID) *BlobCreate { bc.mutation.SetParentID(id) diff --git a/entc/integration/customid/ent/blob_update.go b/entc/integration/customid/ent/blob_update.go index d0625aecd..c4f112ef5 100644 --- a/entc/integration/customid/ent/blob_update.go +++ b/entc/integration/customid/ent/blob_update.go @@ -38,6 +38,14 @@ func (bu *BlobUpdate) SetUUID(u uuid.UUID) *BlobUpdate { return bu } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (bu *BlobUpdate) SetNillableUUID(u *uuid.UUID) *BlobUpdate { + if u != nil { + bu.SetUUID(*u) + } + return bu +} + // SetCount sets the "count" field. func (bu *BlobUpdate) SetCount(i int) *BlobUpdate { bu.mutation.ResetCount() @@ -332,6 +340,14 @@ func (buo *BlobUpdateOne) SetUUID(u uuid.UUID) *BlobUpdateOne { return buo } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (buo *BlobUpdateOne) SetNillableUUID(u *uuid.UUID) *BlobUpdateOne { + if u != nil { + buo.SetUUID(*u) + } + return buo +} + // SetCount sets the "count" field. func (buo *BlobUpdateOne) SetCount(i int) *BlobUpdateOne { buo.mutation.ResetCount() diff --git a/entc/integration/customid/ent/device_create.go b/entc/integration/customid/ent/device_create.go index bf729a0e5..d4f1ae1f3 100644 --- a/entc/integration/customid/ent/device_create.go +++ b/entc/integration/customid/ent/device_create.go @@ -34,6 +34,14 @@ func (dc *DeviceCreate) SetID(s schema.ID) *DeviceCreate { return dc } +// SetNillableID sets the "id" field if the given value is not nil. +func (dc *DeviceCreate) SetNillableID(s *schema.ID) *DeviceCreate { + if s != nil { + dc.SetID(*s) + } + return dc +} + // SetActiveSessionID sets the "active_session" edge to the Session entity by ID. func (dc *DeviceCreate) SetActiveSessionID(id schema.ID) *DeviceCreate { dc.mutation.SetActiveSessionID(id) diff --git a/entc/integration/customid/ent/mixinid_create.go b/entc/integration/customid/ent/mixinid_create.go index ce7138ded..72cdc6913 100644 --- a/entc/integration/customid/ent/mixinid_create.go +++ b/entc/integration/customid/ent/mixinid_create.go @@ -45,6 +45,14 @@ func (mic *MixinIDCreate) SetID(u uuid.UUID) *MixinIDCreate { return mic } +// SetNillableID sets the "id" field if the given value is not nil. +func (mic *MixinIDCreate) SetNillableID(u *uuid.UUID) *MixinIDCreate { + if u != nil { + mic.SetID(*u) + } + return mic +} + // Mutation returns the MixinIDMutation object of the builder. func (mic *MixinIDCreate) Mutation() *MixinIDMutation { return mic.mutation diff --git a/entc/integration/customid/ent/session_create.go b/entc/integration/customid/ent/session_create.go index 04c36e39a..40de65047 100644 --- a/entc/integration/customid/ent/session_create.go +++ b/entc/integration/customid/ent/session_create.go @@ -34,6 +34,14 @@ func (sc *SessionCreate) SetID(s schema.ID) *SessionCreate { return sc } +// SetNillableID sets the "id" field if the given value is not nil. +func (sc *SessionCreate) SetNillableID(s *schema.ID) *SessionCreate { + if s != nil { + sc.SetID(*s) + } + return sc +} + // SetDeviceID sets the "device" edge to the Device entity by ID. func (sc *SessionCreate) SetDeviceID(id schema.ID) *SessionCreate { sc.mutation.SetDeviceID(id) diff --git a/entc/integration/edgefield/ent/car_create.go b/entc/integration/edgefield/ent/car_create.go index 44708794d..24396fdc9 100644 --- a/entc/integration/edgefield/ent/car_create.go +++ b/entc/integration/edgefield/ent/car_create.go @@ -44,6 +44,14 @@ func (cc *CarCreate) SetID(u uuid.UUID) *CarCreate { return cc } +// SetNillableID sets the "id" field if the given value is not nil. +func (cc *CarCreate) SetNillableID(u *uuid.UUID) *CarCreate { + if u != nil { + cc.SetID(*u) + } + return cc +} + // AddRentalIDs adds the "rentals" edge to the Rental entity by IDs. func (cc *CarCreate) AddRentalIDs(ids ...int) *CarCreate { cc.mutation.AddRentalIDs(ids...) diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index eee5836aa..420316726 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -685,12 +685,28 @@ func (ftc *FieldTypeCreate) SetOptionalUUID(u uuid.UUID) *FieldTypeCreate { return ftc } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeCreate { + if u != nil { + ftc.SetOptionalUUID(*u) + } + return ftc +} + // SetNillableUUID sets the "nillable_uuid" field. func (ftc *FieldTypeCreate) SetNillableUUID(u uuid.UUID) *FieldTypeCreate { ftc.mutation.SetNillableUUID(u) return ftc } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeCreate { + if u != nil { + ftc.SetNillableUUID(*u) + } + return ftc +} + // SetStrings sets the "strings" field. func (ftc *FieldTypeCreate) SetStrings(s []string) *FieldTypeCreate { ftc.mutation.SetStrings(s) diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index 4a494ec43..7bb6626c1 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -1187,6 +1187,14 @@ func (ftu *FieldTypeUpdate) SetOptionalUUID(u uuid.UUID) *FieldTypeUpdate { return ftu } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeUpdate { + if u != nil { + ftu.SetOptionalUUID(*u) + } + return ftu +} + // ClearOptionalUUID clears the value of the "optional_uuid" field. func (ftu *FieldTypeUpdate) ClearOptionalUUID() *FieldTypeUpdate { ftu.mutation.ClearOptionalUUID() @@ -1199,6 +1207,14 @@ func (ftu *FieldTypeUpdate) SetNillableUUID(u uuid.UUID) *FieldTypeUpdate { return ftu } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeUpdate { + if u != nil { + ftu.SetNillableUUID(*u) + } + return ftu +} + // ClearNillableUUID clears the value of the "nillable_uuid" field. func (ftu *FieldTypeUpdate) ClearNillableUUID() *FieldTypeUpdate { ftu.mutation.ClearNillableUUID() @@ -3635,6 +3651,14 @@ func (ftuo *FieldTypeUpdateOne) SetOptionalUUID(u uuid.UUID) *FieldTypeUpdateOne return ftuo } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeUpdateOne { + if u != nil { + ftuo.SetOptionalUUID(*u) + } + return ftuo +} + // ClearOptionalUUID clears the value of the "optional_uuid" field. func (ftuo *FieldTypeUpdateOne) ClearOptionalUUID() *FieldTypeUpdateOne { ftuo.mutation.ClearOptionalUUID() @@ -3647,6 +3671,14 @@ func (ftuo *FieldTypeUpdateOne) SetNillableUUID(u uuid.UUID) *FieldTypeUpdateOne return ftuo } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeUpdateOne { + if u != nil { + ftuo.SetNillableUUID(*u) + } + return ftuo +} + // ClearNillableUUID clears the value of the "nillable_uuid" field. func (ftuo *FieldTypeUpdateOne) ClearNillableUUID() *FieldTypeUpdateOne { ftuo.mutation.ClearNillableUUID() diff --git a/entc/integration/ent/pet_create.go b/entc/integration/ent/pet_create.go index 986ae236d..a25dbd3cc 100644 --- a/entc/integration/ent/pet_create.go +++ b/entc/integration/ent/pet_create.go @@ -53,6 +53,14 @@ func (pc *PetCreate) SetUUID(u uuid.UUID) *PetCreate { return pc } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (pc *PetCreate) SetNillableUUID(u *uuid.UUID) *PetCreate { + if u != nil { + pc.SetUUID(*u) + } + return pc +} + // SetNickname sets the "nickname" field. func (pc *PetCreate) SetNickname(s string) *PetCreate { pc.mutation.SetNickname(s) diff --git a/entc/integration/ent/pet_update.go b/entc/integration/ent/pet_update.go index 23c8114b5..ba8516dda 100644 --- a/entc/integration/ent/pet_update.go +++ b/entc/integration/ent/pet_update.go @@ -66,6 +66,14 @@ func (pu *PetUpdate) SetUUID(u uuid.UUID) *PetUpdate { return pu } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (pu *PetUpdate) SetNillableUUID(u *uuid.UUID) *PetUpdate { + if u != nil { + pu.SetUUID(*u) + } + return pu +} + // ClearUUID clears the value of the "uuid" field. func (pu *PetUpdate) ClearUUID() *PetUpdate { pu.mutation.ClearUUID() @@ -388,6 +396,14 @@ func (puo *PetUpdateOne) SetUUID(u uuid.UUID) *PetUpdateOne { return puo } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (puo *PetUpdateOne) SetNillableUUID(u *uuid.UUID) *PetUpdateOne { + if u != nil { + puo.SetUUID(*u) + } + return puo +} + // ClearUUID clears the value of the "uuid" field. func (puo *PetUpdateOne) ClearUUID() *PetUpdateOne { puo.mutation.ClearUUID() diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index f4d6a46e3..81c3ba596 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -685,12 +685,28 @@ func (ftc *FieldTypeCreate) SetOptionalUUID(u uuid.UUID) *FieldTypeCreate { return ftc } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeCreate { + if u != nil { + ftc.SetOptionalUUID(*u) + } + return ftc +} + // SetNillableUUID sets the "nillable_uuid" field. func (ftc *FieldTypeCreate) SetNillableUUID(u uuid.UUID) *FieldTypeCreate { ftc.mutation.SetNillableUUID(u) return ftc } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftc *FieldTypeCreate) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeCreate { + if u != nil { + ftc.SetNillableUUID(*u) + } + return ftc +} + // SetStrings sets the "strings" field. func (ftc *FieldTypeCreate) SetStrings(s []string) *FieldTypeCreate { ftc.mutation.SetStrings(s) diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index 539062d7c..d414ee8c0 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -1189,6 +1189,14 @@ func (ftu *FieldTypeUpdate) SetOptionalUUID(u uuid.UUID) *FieldTypeUpdate { return ftu } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeUpdate { + if u != nil { + ftu.SetOptionalUUID(*u) + } + return ftu +} + // ClearOptionalUUID clears the value of the "optional_uuid" field. func (ftu *FieldTypeUpdate) ClearOptionalUUID() *FieldTypeUpdate { ftu.mutation.ClearOptionalUUID() @@ -1201,6 +1209,14 @@ func (ftu *FieldTypeUpdate) SetNillableUUID(u uuid.UUID) *FieldTypeUpdate { return ftu } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftu *FieldTypeUpdate) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeUpdate { + if u != nil { + ftu.SetNillableUUID(*u) + } + return ftu +} + // ClearNillableUUID clears the value of the "nillable_uuid" field. func (ftu *FieldTypeUpdate) ClearNillableUUID() *FieldTypeUpdate { ftu.mutation.ClearNillableUUID() @@ -3088,6 +3104,14 @@ func (ftuo *FieldTypeUpdateOne) SetOptionalUUID(u uuid.UUID) *FieldTypeUpdateOne return ftuo } +// SetNillableOptionalUUID sets the "optional_uuid" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableOptionalUUID(u *uuid.UUID) *FieldTypeUpdateOne { + if u != nil { + ftuo.SetOptionalUUID(*u) + } + return ftuo +} + // ClearOptionalUUID clears the value of the "optional_uuid" field. func (ftuo *FieldTypeUpdateOne) ClearOptionalUUID() *FieldTypeUpdateOne { ftuo.mutation.ClearOptionalUUID() @@ -3100,6 +3124,14 @@ func (ftuo *FieldTypeUpdateOne) SetNillableUUID(u uuid.UUID) *FieldTypeUpdateOne return ftuo } +// SetNillableNillableUUID sets the "nillable_uuid" field if the given value is not nil. +func (ftuo *FieldTypeUpdateOne) SetNillableNillableUUID(u *uuid.UUID) *FieldTypeUpdateOne { + if u != nil { + ftuo.SetNillableUUID(*u) + } + return ftuo +} + // ClearNillableUUID clears the value of the "nillable_uuid" field. func (ftuo *FieldTypeUpdateOne) ClearNillableUUID() *FieldTypeUpdateOne { ftuo.mutation.ClearNillableUUID() diff --git a/entc/integration/gremlin/ent/pet_create.go b/entc/integration/gremlin/ent/pet_create.go index 721a06811..e5ab6f061 100644 --- a/entc/integration/gremlin/ent/pet_create.go +++ b/entc/integration/gremlin/ent/pet_create.go @@ -54,6 +54,14 @@ func (pc *PetCreate) SetUUID(u uuid.UUID) *PetCreate { return pc } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (pc *PetCreate) SetNillableUUID(u *uuid.UUID) *PetCreate { + if u != nil { + pc.SetUUID(*u) + } + return pc +} + // SetNickname sets the "nickname" field. func (pc *PetCreate) SetNickname(s string) *PetCreate { pc.mutation.SetNickname(s) diff --git a/entc/integration/gremlin/ent/pet_update.go b/entc/integration/gremlin/ent/pet_update.go index 43c743307..0686d0b4b 100644 --- a/entc/integration/gremlin/ent/pet_update.go +++ b/entc/integration/gremlin/ent/pet_update.go @@ -68,6 +68,14 @@ func (pu *PetUpdate) SetUUID(u uuid.UUID) *PetUpdate { return pu } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (pu *PetUpdate) SetNillableUUID(u *uuid.UUID) *PetUpdate { + if u != nil { + pu.SetUUID(*u) + } + return pu +} + // ClearUUID clears the value of the "uuid" field. func (pu *PetUpdate) ClearUUID() *PetUpdate { pu.mutation.ClearUUID() @@ -330,6 +338,14 @@ func (puo *PetUpdateOne) SetUUID(u uuid.UUID) *PetUpdateOne { return puo } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (puo *PetUpdateOne) SetNillableUUID(u *uuid.UUID) *PetUpdateOne { + if u != nil { + puo.SetUUID(*u) + } + return puo +} + // ClearUUID clears the value of the "uuid" field. func (puo *PetUpdateOne) ClearUUID() *PetUpdateOne { puo.mutation.ClearUUID() diff --git a/entc/integration/privacy/ent/internal/schema.go b/entc/integration/privacy/ent/internal/schema.go index 646c55b2d..610c83cb9 100644 --- a/entc/integration/privacy/ent/internal/schema.go +++ b/entc/integration/privacy/ent/internal/schema.go @@ -10,4 +10,4 @@ // Package internal holds a loadable version of the latest schema. package internal -const Schema = `{"Schema":"entgo.io/ent/entc/integration/privacy/ent/schema","Package":"entgo.io/ent/entc/integration/privacy/ent","Schemas":[{"name":"Task","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"owner","type":"User","ref_name":"tasks","unique":true,"inverse":true}],"fields":[{"name":"title","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"description","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"status","type":{"Type":6,"Ident":"task.Status","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"enums":[{"N":"planned","V":"planned"},{"N":"in_progress","V":"in_progress"},{"N":"closed","V":"closed"}],"default":true,"default_value":"planned","default_kind":24,"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"uuid","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"","Nillable":true,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"hooks":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Team","config":{"Table":""},"edges":[{"name":"tasks","type":"Task","ref_name":"teams","inverse":true},{"name":"users","type":"User","ref_name":"teams","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"tasks","type":"Task"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"immutable":true,"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"age","type":{"Type":17,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["privacy","entql","schema/snapshot"]}` +const Schema = `{"Schema":"entgo.io/ent/entc/integration/privacy/ent/schema","Package":"entgo.io/ent/entc/integration/privacy/ent","Schemas":[{"name":"Task","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"owner","type":"User","ref_name":"tasks","unique":true,"inverse":true}],"fields":[{"name":"title","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"description","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"status","type":{"Type":6,"Ident":"task.Status","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"enums":[{"N":"planned","V":"planned"},{"N":"in_progress","V":"in_progress"},{"N":"closed","V":"closed"}],"default":true,"default_value":"planned","default_kind":24,"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"uuid","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"","Nillable":false,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"hooks":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Team","config":{"Table":""},"edges":[{"name":"tasks","type":"Task","ref_name":"teams","inverse":true},{"name":"users","type":"User","ref_name":"teams","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"tasks","type":"Task"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"immutable":true,"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"age","type":{"Type":17,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["privacy","entql","schema/snapshot"]}` diff --git a/entc/integration/privacy/ent/task_create.go b/entc/integration/privacy/ent/task_create.go index 30a529e16..f515d50e9 100644 --- a/entc/integration/privacy/ent/task_create.go +++ b/entc/integration/privacy/ent/task_create.go @@ -66,6 +66,14 @@ func (tc *TaskCreate) SetUUID(u uuid.UUID) *TaskCreate { return tc } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (tc *TaskCreate) SetNillableUUID(u *uuid.UUID) *TaskCreate { + if u != nil { + tc.SetUUID(*u) + } + return tc +} + // AddTeamIDs adds the "teams" edge to the Team entity by IDs. func (tc *TaskCreate) AddTeamIDs(ids ...int) *TaskCreate { tc.mutation.AddTeamIDs(ids...) diff --git a/entc/integration/privacy/ent/task_update.go b/entc/integration/privacy/ent/task_update.go index d9027e18a..c66740675 100644 --- a/entc/integration/privacy/ent/task_update.go +++ b/entc/integration/privacy/ent/task_update.go @@ -80,6 +80,14 @@ func (tu *TaskUpdate) SetUUID(u uuid.UUID) *TaskUpdate { return tu } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (tu *TaskUpdate) SetNillableUUID(u *uuid.UUID) *TaskUpdate { + if u != nil { + tu.SetUUID(*u) + } + return tu +} + // ClearUUID clears the value of the "uuid" field. func (tu *TaskUpdate) ClearUUID() *TaskUpdate { tu.mutation.ClearUUID() @@ -439,6 +447,14 @@ func (tuo *TaskUpdateOne) SetUUID(u uuid.UUID) *TaskUpdateOne { return tuo } +// SetNillableUUID sets the "uuid" field if the given value is not nil. +func (tuo *TaskUpdateOne) SetNillableUUID(u *uuid.UUID) *TaskUpdateOne { + if u != nil { + tuo.SetUUID(*u) + } + return tuo +} + // ClearUUID clears the value of the "uuid" field. func (tuo *TaskUpdateOne) ClearUUID() *TaskUpdateOne { tuo.mutation.ClearUUID() diff --git a/schema/field/field.go b/schema/field/field.go index e18530946..f945d6725 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -129,10 +129,9 @@ func UUID(name string, typ driver.Valuer) *uuidBuilder { b := &uuidBuilder{&Descriptor{ Name: name, Info: &TypeInfo{ - Type: TypeUUID, - Nillable: true, - Ident: rt.String(), - PkgPath: indirect(rt).PkgPath(), + Type: TypeUUID, + Ident: rt.String(), + PkgPath: indirect(rt).PkgPath(), }, }} b.desc.goType(typ, valueScannerType) @@ -1159,7 +1158,7 @@ func (d *Descriptor) goType(typ interface{}, expectType reflect.Type) { }, } switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Map: + case reflect.Slice, reflect.Ptr, reflect.Map: info.Nillable = true } switch pt := reflect.PtrTo(t); { diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 59634379d..f463dafa3 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -230,7 +230,7 @@ func TestBytes(t *testing.T) { assert.Equal(t, "uuid.UUID", fd.Info.Ident) assert.Equal(t, "github.com/google/uuid", fd.Info.PkgPath) assert.Equal(t, "uuid.UUID", fd.Info.String()) - assert.True(t, fd.Info.Nillable) + assert.False(t, fd.Info.Nillable) assert.True(t, fd.Info.ValueScanner()) assert.NotEmpty(t, fd.Default.(func() uuid.UUID)())