From f2411742aed3e3e844cf2e19da81a769133b7973 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sun, 11 Aug 2019 08:40:44 -0700 Subject: [PATCH] ent/field: force optional on nillable field Reviewed By: alexsn Differential Revision: D16757719 fbshipit-source-id: 813e10b8b4b1744e7ab68f4513722f66911cdef3 --- entc/gen/graph_test.go | 6 +++--- entc/gen/type.go | 3 +++ entc/load/bindata.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/entc/gen/graph_test.go b/entc/gen/graph_test.go index a3d298383..5b0f025fa 100644 --- a/entc/gen/graph_test.go +++ b/entc/gen/graph_test.go @@ -17,7 +17,7 @@ var ( Name: "T1", Fields: []*load.Field{ {Name: "age", Type: field.TypeInt, Optional: true}, - {Name: "expired_at", Type: field.TypeTime, Nillable: true}, + {Name: "expired_at", Type: field.TypeTime, Nillable: true, Optional: true}, {Name: "name", Type: field.TypeString, Default: true}, }, Edges: []*load.Edge{ @@ -77,7 +77,7 @@ func TestNewGraph(t *testing.T) { for i, typ := range []string{"int", "time.Time", "string"} { require.Equal(typ, t1.Fields[i].Type.String()) } - for i, optional := range []bool{true, false, false} { + for i, optional := range []bool{true, true, false} { require.Equal(optional, t1.Fields[i].Optional) } for i, nullable := range []bool{false, true, false} { @@ -157,7 +157,7 @@ func TestGraph_Gen(t *testing.T) { Name: "T1", Fields: []*load.Field{ {Name: "age", Type: field.TypeInt, Optional: true}, - {Name: "expired_at", Type: field.TypeTime, Nillable: true}, + {Name: "expired_at", Type: field.TypeTime, Nillable: true, Optional: true}, {Name: "name", Type: field.TypeString}, }, Edges: []*load.Edge{ diff --git a/entc/gen/type.go b/entc/gen/type.go index b3b26bdd8..3059ebfcc 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -110,6 +110,9 @@ func NewType(c Config, schema *load.Schema) (*Type, error) { if !f.Type.Valid() { return nil, fmt.Errorf("invalid type for field %s", f.Name) } + if f.Nillable && !f.Optional { + return nil, fmt.Errorf("nillable field %q must be optional", f.Name) + } typ.Fields = append(typ.Fields, &Field{ def: f, Name: f.Name, diff --git a/entc/load/bindata.go b/entc/load/bindata.go index 2c45e01af..36146418f 100644 --- a/entc/load/bindata.go +++ b/entc/load/bindata.go @@ -102,7 +102,7 @@ func schemaGo() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "schema.go", size: 2637, mode: os.FileMode(420), modTime: time.Unix(1565179921, 0)} + info := bindataFileInfo{name: "schema.go", size: 2637, mode: os.FileMode(420), modTime: time.Unix(1565536164, 0)} a := &asset{bytes: bytes, info: info} return a, nil }