From 3e7270e83514003fb3f58078ccaae1131c4d9fc9 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Tue, 27 Aug 2019 03:56:44 -0700 Subject: [PATCH] ent/schema: change ent interafces for descriptor only Summary: wip Reviewed By: alexsn Differential Revision: D17052576 fbshipit-source-id: 165faca2ff02c22bb76f164d81d406ce46ab04c4 --- ent.go | 83 +++-- entc/gen/bindata.go | 4 +- entc/gen/template/meta.tmpl | 11 +- entc/integration/ent/card/card.go | 8 +- entc/integration/ent/fieldtype/fieldtype.go | 4 +- entc/integration/ent/file/file.go | 6 +- entc/integration/ent/group/group.go | 18 +- entc/integration/ent/groupinfo/groupinfo.go | 4 +- entc/integration/ent/user/user.go | 4 +- entc/integration/migrate/entv1/user/user.go | 4 +- entc/integration/migrate/entv2/user/user.go | 8 +- entc/load/bindata.go | 4 +- entc/load/schema.go | 62 ++-- schema/edge/edge.go | 96 ++--- schema/edge/edge_test.go | 87 +++-- schema/field/field.go | 170 ++++----- schema/field/field_test.go | 100 +++--- schema/field/gen/numeric.tmpl | 60 ++-- schema/field/numeric.go | 370 ++++++++++++-------- schema/index/index.go | 57 ++- schema/index/index_test.go | 30 +- 21 files changed, 665 insertions(+), 525 deletions(-) diff --git a/ent.go b/ent.go index 581eaa5b8..95109cf46 100644 --- a/ent.go +++ b/ent.go @@ -1,9 +1,10 @@ -// Package ent is an interface package for the schemas that use entc. +// Package ent is the interface between end-user schemas and entc (ent codegen). package ent import ( "github.com/facebookincubator/ent/schema/edge" "github.com/facebookincubator/ent/schema/field" + "github.com/facebookincubator/ent/schema/index" ) type ( @@ -16,46 +17,76 @@ type ( // } // Interface interface { + // Type is a dummy method, that is used in edge declaration. + // + // The Type method should be used as follows: + // + // type S struct { ent.Schema } + // + // type T struct { ent.Schema } + // + // func (T) Edges() []ent.Edge { + // return []ent.Edge{ + // edge.To("S", S.Type), + // } + // } + // Type() + // Fields returns the fields of the schema. Fields() []Field + // Edges returns the edges of the schema. Edges() []Edge + // Indexes returns the indexes of the schema. Indexes() []Index } - // Field is the interface for vertex and edges fields used by the code generation. + // A Field interface returns a field descriptor for vertex fields/properties. + // The usage for the interface is as follows: + // + // func (T) Fields() []ent.Field { + // return []ent.Field{ + // field.Int("int"), + // } + // } + // Field interface { - Tag() string - Name() string - Type() field.Type - IsUnique() bool - IsNillable() bool - IsOptional() bool - HasDefault() bool - IsImmutable() bool - Value() interface{} - Validators() []interface{} + Descriptor() *field.Descriptor } - // Edge is the interface for graph edges in the schema. It is used by the code generation. + // A Edge interface returns an edge descriptor for vertex edges. + // The usage for the interface is as follows: + // + // func (T) Edges() []ent.Edge { + // return []ent.Edge{ + // edge.To("S", S.Type), + // } + // } + // Edge interface { - Tag() string - Type() string - Name() string - RefName() string - Assoc() *edge.Edge - IsUnique() bool - IsInverse() bool - IsRequired() bool + Descriptor() *edge.Descriptor } - // Index is the interface for graph indexes in the schema. It is used by the code generation. + // A Index interface returns an index descriptor for vertex indexes. + // The usage for the interface is as follows: + // + // func (T) Indexes() []ent.Index { + // return []ent.Index{ + // index.Fields("f1", "f2"). + // Unique(), + // } + // } + // Index interface { - IsUnique() bool - EdgeNames() []string - FieldNames() []string + Descriptor() *index.Descriptor } - // Schema is the default implementation Interface. + // Schema is the default implementation for the schema Interface. + // It can be embedded in end-user schemas as follows: + // + // type T struct { + // ent.Schema + // } + // Schema struct { Interface } diff --git a/entc/gen/bindata.go b/entc/gen/bindata.go index 9ca055813..1e0c95310 100644 --- a/entc/gen/bindata.go +++ b/entc/gen/bindata.go @@ -764,7 +764,7 @@ func templateImportTmpl() (*asset, error) { return a, nil } -var _templateMetaTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x54\x5d\x6b\xeb\x38\x10\x7d\x76\x7e\xc5\xac\x71\xc1\x86\xd4\xd9\x7d\xed\x92\xa7\x2d\xcb\x16\x96\x65\xe1\x96\xbe\x94\x52\x94\x78\xd4\xa8\x55\xe4\x5c\x49\x49\x53\x84\xfe\xfb\x65\xf4\xe1\xd8\xbd\xe9\x6d\x9f\x12\x69\x66\xce\x9c\x39\x73\x2c\xe7\xa0\x43\x2e\x14\x42\xb9\x45\xcb\x4a\xf0\x7e\x36\x73\xee\x12\x5e\x85\xdd\x00\x1e\x2d\xaa\x0e\x2a\x28\xff\x67\xeb\x17\xf6\x84\x25\x54\x6d\xfa\x0b\x97\xde\xcf\x0a\xe7\xc0\xe2\x76\x27\x99\x45\x28\x37\xc8\x3a\xd4\x25\xb4\x84\xe2\x1c\x50\x6d\xc4\x1b\x25\x89\xed\xae\xd7\xb6\x84\x2a\x84\xd6\xbd\x32\x16\xea\x59\xb1\x58\xc0\xbf\x6c\x85\x12\x36\xbd\xec\x0c\xd8\x0d\x82\xb1\x5a\xa8\x27\x90\xe1\xba\x43\xd5\x5b\x3a\x52\xc4\x39\x90\xfd\x2b\x6a\xa8\xda\xff\xd8\x16\xc1\x7b\xb0\x6f\x3b\x04\xa1\x42\xb8\x63\x96\xad\x98\xc1\x76\x56\x44\xcc\x25\x94\xce\x41\xd5\xc6\x93\xf7\x65\xe8\x17\xae\x6e\xae\xdb\xbf\x88\x03\x53\x96\x60\x7e\xea\x3e\xe9\x2b\x3a\xe0\x02\x65\x77\xa6\xd1\x39\xb0\xd8\xd6\x28\xf6\x82\x31\x98\xc8\x96\x21\x5d\x33\xf5\x84\x50\x3d\xce\xa1\xe2\x70\xb5\x84\xaa\xfd\x9b\xb0\x0d\x09\x4b\x68\xb1\x13\x05\xf8\x09\x35\x88\x9e\xc9\xc7\x8c\x4f\x59\x9f\xd4\xe2\x83\x5c\x07\xd4\x16\x8f\xb0\xd3\xfd\x0e\xb5\x7d\x3b\x33\x50\x31\xe9\x30\x19\x85\x4f\x07\xa1\x35\x67\x33\x8c\x86\x32\xb6\xd7\xe4\x93\x30\xda\xb7\x74\x08\xf4\x09\xd9\x6e\x77\x92\x42\x3b\x2d\x94\xe5\x50\x76\x82\x49\x5c\xdb\xc5\x85\x59\x90\x11\x17\xeb\x34\xb1\x29\x4f\x48\xb9\xf8\x38\xb8\x29\xc2\x04\x2b\x15\x27\xc3\x35\xc1\x72\x5f\xa1\xf2\x15\x26\x07\xa6\x05\x5b\x49\x7c\xcf\xc4\x39\x10\x1c\x36\xcc\xdc\x4e\xd9\x7c\x95\xe5\xf4\x03\x11\x1c\x7a\xf2\xf3\x3f\xcc\x5c\x23\x67\x7b\x69\xe3\xe1\x8e\x49\xd1\x31\xdb\x6b\x43\x99\x07\xa6\xe9\x63\xe1\xd1\x28\x4b\x5a\x2d\xad\x8b\xa6\x5a\x6f\x70\xcb\xc0\xfb\x36\x38\x31\x2d\xc8\xf9\x64\xaa\xba\x19\xaf\x47\x9c\xf3\x5c\x64\x2d\x38\xed\x77\xc4\x22\x04\x0a\x7a\x12\xaa\x2e\x5d\x45\x4f\xa6\x84\xd0\x29\x96\x67\x5f\x76\x43\xe9\xc8\x99\xf9\xf2\xc0\xe4\x1e\x81\xf7\x3a\x7b\x73\xe4\xca\x30\x57\x1b\xfb\x4d\x60\x96\x31\x64\xee\xe9\x5e\x80\xf7\x0f\xed\x1d\xe1\xd4\x4d\x5b\x0f\xa4\x6f\xcc\xad\x08\x38\x7c\xaf\xd6\x75\x03\x83\xc0\xb1\xc9\x2d\xbd\x10\xde\x37\x71\xce\xc1\xb4\x74\x08\x8f\x5d\xc5\xdb\x91\xd8\x97\x79\x6e\xa8\x14\xb1\x8b\x33\x0f\x09\xe3\x78\x78\x7a\xae\x96\x30\x38\x88\xfa\x43\x7d\x61\x1a\x40\xad\x7b\x5d\x0e\xdd\xa7\x32\xa9\x34\xb5\x30\xc0\x48\x96\x84\x9c\xa5\x29\x27\xda\x94\x49\x1c\xb8\xb1\x54\xb0\x66\x52\x62\x07\xab\xb7\x90\xba\xda\x0b\xd9\xa1\x36\xb0\x42\xde\x6b\x04\xc3\x0e\x98\x65\x14\x1c\xf0\xfb\xbb\xe1\xfe\xc8\x4c\x8a\x31\x8f\xf3\x22\xa7\x9a\xba\xb9\xff\xfd\x21\x88\x1d\xe7\x8d\x42\x06\x25\xa5\xc1\x8f\xf0\x86\x45\xe4\x22\x70\x21\xad\x38\x9c\xc8\x5c\x7d\xd2\x37\x16\x70\x15\x32\xef\xdb\xb6\x7d\x08\xb0\xd3\xa5\x46\xa5\x33\x7a\xb0\x6b\xf2\xfa\xf3\x1c\x2a\x45\xa5\xc7\x74\x31\x51\x22\xd1\x9e\x30\x0a\x3c\x9e\x03\x8f\xfa\xc3\x56\xcd\x7c\xd4\x2a\x7d\xc7\xe1\x9c\x7e\x34\xda\xbd\x56\x30\xaa\xcf\x26\xff\x25\x71\x5a\xfe\xe3\x1c\x78\x60\x1c\x09\xd3\xe4\x39\x5c\xd0\x36\xb5\x0e\x9a\xa9\x29\x6e\xf3\x67\x88\xfc\xb6\x04\x25\xe4\xa9\x20\x13\x41\xad\xf3\x55\x1e\x39\xff\xa6\x0c\x25\xe4\x78\x02\x5f\x0f\x0b\x1e\x7f\x2a\xe3\xc7\x3e\xff\x6f\xde\xbd\x65\xf1\xef\x8f\x00\x00\x00\xff\xff\xab\xe1\xd9\x49\x5b\x08\x00\x00") +var _templateMetaTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x55\x4d\x6f\xe3\x36\x10\x3d\xcb\xbf\x62\x4a\x68\x01\x1b\x48\xe4\xf6\x9a\xc2\xa7\x06\x45\x03\x14\x45\x81\x06\xbd\x04\xc1\x82\x96\x86\x31\x77\x69\x4a\x25\x69\x6f\x02\x81\xff\xbd\x18\x7e\x48\xa2\xeb\x6c\x73\xb2\x48\x0e\xdf\xbc\x37\xf3\x86\x1e\x47\xe8\x50\x48\x8d\xc0\x8e\xe8\x38\x03\xef\x57\xab\x71\xbc\x85\x6f\xd2\x1d\x00\x5f\x1d\xea\x0e\x6a\x60\x7f\xf2\xf6\x2b\x7f\x41\x06\x75\x93\x3e\xe1\xd6\xfb\x55\x35\x8e\xe0\xf0\x38\x28\xee\x10\xd8\x01\x79\x87\x86\x41\x43\x28\xe3\x08\x74\x37\xe2\x2d\x82\xe4\x71\xe8\x8d\x63\x50\x87\xa3\xb6\xd7\xd6\xc1\x7a\x55\x6d\xb7\xf0\x3b\xdf\xa3\x82\x43\xaf\x3a\x0b\xee\x80\x60\x9d\x91\xfa\x05\x54\xd8\xee\x50\xf7\x8e\x96\x74\x32\x8e\xa0\xfa\x6f\x68\xa0\x6e\xfe\xe0\x47\x04\xef\xc1\xbd\x0d\x08\x52\x87\xe3\x8e\x3b\xbe\xe7\x16\x9b\x55\x15\x31\x77\xc0\xc6\x11\xea\x26\xae\xbc\x67\x21\x5f\xd8\x7a\xb8\x6f\x7e\x21\x0e\x5c\x3b\x82\xf9\x4f\xf6\x22\xaf\xec\x40\x48\x54\xdd\x95\x44\xd7\xc0\x62\x5a\xab\xf9\x57\x8c\x87\x89\x2c\x0b\xe1\x86\xeb\x17\x84\xfa\xf3\x0d\xd4\x02\xee\x76\x50\x37\xbf\x12\xb6\xa5\xc2\x12\x5a\xcc\x44\x07\x62\x46\x0d\x45\xcf\xe4\x63\xc4\xff\xb2\x9e\xab\x25\xa6\x72\x9d\xd1\x38\x7c\x85\xc1\xf4\x03\x1a\xf7\x76\x45\x50\x55\x64\x28\xa4\x88\x52\x08\xb5\x39\x9b\x61\x21\xca\xba\xde\x90\x4f\x82\xb4\xbf\xd2\x22\xd0\x27\x64\x77\x1c\x14\x1d\x0d\x46\x6a\x27\x80\x75\x92\x2b\x6c\xdd\xf6\x93\xdd\x92\x11\xb7\x6d\x52\x6c\xd9\x8c\x94\x2f\xbf\x4e\x6e\x8a\x30\xc1\x4a\xd5\x6c\xb8\x4d\xb0\xdc\x47\xa8\x7c\x84\xc9\x99\x1b\xc9\xf7\x0a\x2f\x99\x8c\x23\x48\x01\x07\x6e\x1f\x4b\x36\x1f\x65\x59\x0e\x88\x14\xd0\x93\x9f\x7f\xe3\xf6\x1e\x05\x3f\x29\x17\x17\x7f\x73\x25\x3b\xee\x7a\x63\x29\xf2\xcc\x0d\x0d\x8b\x88\x46\xd9\x51\x6b\xa9\x5d\xa4\xaa\x3d\xe0\x91\x83\xf7\x4d\x70\x62\x6a\xd0\xe8\x93\xa9\xd6\x9b\x65\x7b\xe4\x35\xcf\x05\xd6\xb7\x50\x77\x68\xdb\xa9\x1e\xc0\x68\xc9\x60\x3d\x70\xdb\x72\x95\x7b\xbf\x99\x2e\x64\xe2\xa2\x60\x2e\x9a\x05\xef\x18\x9a\x4d\x1b\xe0\xbd\x07\x99\x0c\x1b\x79\xd3\xae\x91\x83\xeb\x0d\x88\xde\x44\x77\x4f\x66\x0d\x72\xc9\x92\xd5\x12\x60\x17\xf7\xed\x13\x6d\x4a\xf0\xfe\xb9\xb9\x9f\x50\x48\x6f\xe1\xcd\x44\xb5\xe4\x19\x89\x25\xd5\x71\x2b\xce\x5b\x0a\x08\x04\x2e\xe9\xe7\xab\x8b\xa9\xcb\x9b\x67\xae\x4e\x18\x04\xa4\xb9\xfb\x8e\x88\x09\x66\xb7\x2c\x4b\xce\xdc\xac\x27\xbe\x0f\xf6\x51\x06\x08\x71\xd2\xed\x7a\x03\x93\x6f\x22\xfe\x23\x3d\x7c\xde\x5f\xd3\x1b\xde\xf0\xab\xbd\xa0\xab\x9a\x88\x45\xb9\x53\xc0\xf2\x3c\xbc\xa8\x77\x3b\x98\x06\x83\xf2\xc3\xfa\x93\xdd\x00\x1a\xd3\x1b\x36\x65\x2f\x2b\xa4\x93\x60\x69\x81\x53\x45\x12\x72\xae\x0a\x2b\xca\xc2\x52\x5d\xe0\xc1\xd1\x85\x96\x2b\x85\x1d\xec\xdf\x42\xe8\xfe\x24\x55\x87\xc6\xc2\x1e\x45\x6f\x10\x2c\x3f\x63\xae\xa0\x14\x80\xff\x5c\x88\xfb\x29\x33\xa9\x96\x3c\xca\xfa\xce\xe1\x4f\x3f\x3e\x87\x2a\x47\xa1\xb1\x82\xa1\x84\xca\xe2\x3b\x40\x73\x07\xf2\x25\x18\x43\x58\x75\x9e\x59\xdc\xbd\x97\x30\x46\x0a\x1d\x42\x9e\x9a\xa6\x79\x0e\x78\x65\x1b\x63\x6d\x33\x6c\xf0\x66\x1a\xda\x2f\x37\x50\x6b\xba\xfa\x9a\x36\x0a\xed\x89\x6f\x41\x25\x4c\xc6\x97\x30\x19\xeb\x77\x53\x6d\x6e\x16\xa9\xd2\x83\x14\xd6\xe9\xc7\xa0\x3b\x19\x0d\x8b\xfb\xd9\xd1\xdf\x25\x4e\xed\xfe\x7c\x03\x22\x30\x8e\x84\x49\x79\x3e\xae\xa8\x7f\xc6\xd0\xa1\xd0\x25\xee\xe6\xe7\x70\xf2\xc3\x0e\xb4\x54\xf3\x85\x4c\x04\x8d\xc9\x5b\x59\x72\xfe\x4d\x11\x5a\xaa\xa5\x02\xbf\x9e\x3a\xbb\x1c\x8e\xe5\xbf\x56\xfe\xde\x5c\x3c\xca\xf1\xf3\xdf\x00\x00\x00\xff\xff\xd8\x31\x19\x57\x24\x09\x00\x00") func templateMetaTmplBytes() ([]byte, error) { return bindataRead( @@ -779,7 +779,7 @@ func templateMetaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/meta.tmpl", size: 2139, mode: os.FileMode(420), modTime: time.Unix(1566214133, 0)} + info := bindataFileInfo{name: "template/meta.tmpl", size: 2340, mode: os.FileMode(420), modTime: time.Unix(1566835376, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/meta.tmpl b/entc/gen/template/meta.tmpl index 4cc84c265..127b86cda 100644 --- a/entc/gen/template/meta.tmpl +++ b/entc/gen/template/meta.tmpl @@ -32,20 +32,25 @@ const ( var ( fields = {{ base $.Schema }}.{{ $.Name }}{}.Fields() {{ range $i, $f := $.Fields -}} + {{- $desc := print "desc" (pascal $f.Name) -}} + {{ if or $f.HasDefault $f.Validators -}} + // {{ $desc }} is the schema descriptor for {{ $f.Name }} field. + {{ $desc }} = fields[{{ $i }}].Descriptor() + {{ end -}} {{ if $f.HasDefault }} {{- $default := $f.DefaultName -}} // {{ $default }} holds the default value for the {{ $f.Name }} field. - {{ $default }} = fields[{{ $i }}].Value().({{ if $f.IsTime }}func() {{ end }}{{ $f.Type }}) + {{ $default }} = {{ $desc }}.Default.({{ if $f.IsTime }}func() {{ end }}{{ $f.Type }}) {{ end -}} {{ with $f.Validators -}} {{ $name := $f.Validator -}} {{ $type := printf "func (%s) error" $f.Type -}} // {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save. {{ if eq $f.Validators 1 -}} - {{ $name }} = fields[{{ $i }}].Validators()[0].({{ $type }}) + {{ $name }} = {{ $desc }}.Validators[0].({{ $type }}) {{ else -}} {{ $name }} = func() {{ $type }} { - validators := fields[{{ $i }}].Validators() + validators := {{ $desc }}.Validators fns := [...]func({{ $f.Type }}) error { {{- range $j, $n := xrange $f.Validators }} validators[{{ $j }}].(func({{ $f.Type }}) error), diff --git a/entc/integration/ent/card/card.go b/entc/integration/ent/card/card.go index 85962128d..364ca143a 100644 --- a/entc/integration/ent/card/card.go +++ b/entc/integration/ent/card/card.go @@ -41,8 +41,12 @@ var Columns = []string{ var ( fields = schema.Card{}.Fields() + // descNumber is the schema descriptor for number field. + descNumber = fields[0].Descriptor() // NumberValidator is a validator for the "number" field. It is called by the builders before save. - NumberValidator = fields[0].Validators()[0].(func(string) error) + NumberValidator = descNumber.Validators[0].(func(string) error) + // descCreatedAt is the schema descriptor for created_at field. + descCreatedAt = fields[1].Descriptor() // DefaultCreatedAt holds the default value for the created_at field. - DefaultCreatedAt = fields[1].Value().(func() time.Time) + DefaultCreatedAt = descCreatedAt.Default.(func() time.Time) ) diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index 7883df6a7..cdf3d2a27 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -71,6 +71,8 @@ var Columns = []string{ var ( fields = schema.FieldType{}.Fields() + // descValidateOptionalInt32 is the schema descriptor for validate_optional_int32 field. + descValidateOptionalInt32 = fields[15].Descriptor() // ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. - ValidateOptionalInt32Validator = fields[15].Validators()[0].(func(int32) error) + ValidateOptionalInt32Validator = descValidateOptionalInt32.Validators[0].(func(int32) error) ) diff --git a/entc/integration/ent/file/file.go b/entc/integration/ent/file/file.go index 352784360..9ad55f5ba 100644 --- a/entc/integration/ent/file/file.go +++ b/entc/integration/ent/file/file.go @@ -54,8 +54,10 @@ var Columns = []string{ var ( fields = schema.File{}.Fields() + // descSize is the schema descriptor for size field. + descSize = fields[0].Descriptor() // DefaultSize holds the default value for the size field. - DefaultSize = fields[0].Value().(int) + DefaultSize = descSize.Default.(int) // SizeValidator is a validator for the "size" field. It is called by the builders before save. - SizeValidator = fields[0].Validators()[0].(func(int) error) + SizeValidator = descSize.Validators[0].(func(int) error) ) diff --git a/entc/integration/ent/group/group.go b/entc/integration/ent/group/group.go index c1f136ee9..d3716c5ad 100644 --- a/entc/integration/ent/group/group.go +++ b/entc/integration/ent/group/group.go @@ -79,17 +79,25 @@ var ( var ( fields = schema.Group{}.Fields() + // descActive is the schema descriptor for active field. + descActive = fields[0].Descriptor() // DefaultActive holds the default value for the active field. - DefaultActive = fields[0].Value().(bool) + DefaultActive = descActive.Default.(bool) + // descType is the schema descriptor for type field. + descType = fields[2].Descriptor() // TypeValidator is a validator for the "type" field. It is called by the builders before save. - TypeValidator = fields[2].Validators()[0].(func(string) error) + TypeValidator = descType.Validators[0].(func(string) error) + // descMaxUsers is the schema descriptor for max_users field. + descMaxUsers = fields[3].Descriptor() // DefaultMaxUsers holds the default value for the max_users field. - DefaultMaxUsers = fields[3].Value().(int) + DefaultMaxUsers = descMaxUsers.Default.(int) // MaxUsersValidator is a validator for the "max_users" field. It is called by the builders before save. - MaxUsersValidator = fields[3].Validators()[0].(func(int) error) + MaxUsersValidator = descMaxUsers.Validators[0].(func(int) error) + // descName is the schema descriptor for name field. + descName = fields[4].Descriptor() // NameValidator is a validator for the "name" field. It is called by the builders before save. NameValidator = func() func(string) error { - validators := fields[4].Validators() + validators := descName.Validators fns := [...]func(string) error{ validators[0].(func(string) error), validators[1].(func(string) error), diff --git a/entc/integration/ent/groupinfo/groupinfo.go b/entc/integration/ent/groupinfo/groupinfo.go index 5856b97a6..e620cabd6 100644 --- a/entc/integration/ent/groupinfo/groupinfo.go +++ b/entc/integration/ent/groupinfo/groupinfo.go @@ -39,6 +39,8 @@ var Columns = []string{ var ( fields = schema.GroupInfo{}.Fields() + // descMaxUsers is the schema descriptor for max_users field. + descMaxUsers = fields[1].Descriptor() // DefaultMaxUsers holds the default value for the max_users field. - DefaultMaxUsers = fields[1].Value().(int) + DefaultMaxUsers = descMaxUsers.Default.(int) ) diff --git a/entc/integration/ent/user/user.go b/entc/integration/ent/user/user.go index 3a588804d..46c3804fb 100644 --- a/entc/integration/ent/user/user.go +++ b/entc/integration/ent/user/user.go @@ -127,6 +127,8 @@ var ( var ( fields = schema.User{}.Fields() + // descLast is the schema descriptor for last field. + descLast = fields[2].Descriptor() // DefaultLast holds the default value for the last field. - DefaultLast = fields[2].Value().(string) + DefaultLast = descLast.Default.(string) ) diff --git a/entc/integration/migrate/entv1/user/user.go b/entc/integration/migrate/entv1/user/user.go index b5c4bb207..98750a071 100644 --- a/entc/integration/migrate/entv1/user/user.go +++ b/entc/integration/migrate/entv1/user/user.go @@ -32,6 +32,8 @@ var Columns = []string{ var ( fields = schema.User{}.Fields() + // descName is the schema descriptor for name field. + descName = fields[1].Descriptor() // NameValidator is a validator for the "name" field. It is called by the builders before save. - NameValidator = fields[1].Validators()[0].(func(string) error) + NameValidator = descName.Validators[0].(func(string) error) ) diff --git a/entc/integration/migrate/entv2/user/user.go b/entc/integration/migrate/entv2/user/user.go index 2bb95ed82..ee8148670 100644 --- a/entc/integration/migrate/entv2/user/user.go +++ b/entc/integration/migrate/entv2/user/user.go @@ -38,8 +38,12 @@ var Columns = []string{ var ( fields = schema.User{}.Fields() + // descBuffer is the schema descriptor for buffer field. + descBuffer = fields[3].Descriptor() // DefaultBuffer holds the default value for the buffer field. - DefaultBuffer = fields[3].Value().([]byte) + DefaultBuffer = descBuffer.Default.([]byte) + // descTitle is the schema descriptor for title field. + descTitle = fields[4].Descriptor() // DefaultTitle holds the default value for the title field. - DefaultTitle = fields[4].Value().(string) + DefaultTitle = descTitle.Default.(string) ) diff --git a/entc/load/bindata.go b/entc/load/bindata.go index 3e30fb046..efaa0768c 100644 --- a/entc/load/bindata.go +++ b/entc/load/bindata.go @@ -87,7 +87,7 @@ func templateMainTmpl() (*asset, error) { return a, nil } -var _schemaGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\xdd\x8e\xdb\xb6\x12\xbe\xb6\x9e\x62\x8e\x81\x2c\xa4\xc0\xd0\xde\x2b\xf0\xc5\xc1\x39\x5b\x74\x5b\x74\x53\x24\x6d\x6f\x82\x20\xa1\xa5\x91\xcd\x44\xa2\x1c\x92\xf6\x26\x59\xf8\xdd\x8b\x19\x0e\xf5\x63\x39\x1b\x20\xc1\x06\x08\x56\x9c\x3f\xce\x7c\xf3\x71\x48\xef\x55\xf9\x51\x6d\x11\x9a\x4e\x55\x49\xa2\xdb\x7d\x67\x3d\xa4\xc9\x62\x89\xa6\xec\x2a\x6d\xb6\xd7\x1f\x5c\x67\x96\xc9\x62\x59\xb7\x9e\xfe\x58\xac\x1b\x2c\xfd\x32\x49\x16\xcb\xad\xf6\xbb\xc3\x26\x2f\xbb\xf6\xba\x56\x25\x6e\xba\xee\xa3\x36\xe5\x61\xa3\x7c\x67\xaf\xd1\xb0\xfd\xf7\x6c\xae\x5d\xb9\xc3\x56\x5d\xd7\x1a\x9b\x6a\x99\x64\x49\x72\x7d\x0d\xaf\x59\x06\x16\xf7\x16\x1d\x1a\xef\x40\x19\x40\xe3\x73\x51\xf8\x9d\xf2\x70\xaf\x1c\xa7\x8d\x15\xd4\xb6\x6b\x41\x41\xd9\xb5\xfb\x46\x63\x05\x07\x87\x16\xa4\xb4\x3c\xf1\x5f\xf6\x18\x43\x3a\x6f\x0f\xa5\x87\x87\x64\x71\xa7\x5a\x04\x00\x92\x68\xb3\x05\x80\xf7\x54\x69\xb1\x34\xaa\xc5\x55\xd7\x6a\x8f\xed\xde\x7f\x59\xbe\x4f\x16\x37\xd5\x16\x1d\x00\xbc\x79\xfb\x9c\x3e\x7b\x4b\x24\xf9\xd4\xf4\x17\xaa\xc2\xb1\x29\x7f\x46\x53\xae\xee\xcc\xf6\xd6\x54\xf8\x19\x1d\xd9\xf2\x67\xb4\xd5\x41\x3e\x31\x3e\x31\x2c\x21\xe4\x1c\x95\x20\xff\x01\x50\x82\xe3\x1c\x93\x31\x2c\x8f\x00\xf3\x17\xc5\x08\xff\xb8\xc0\x9c\x05\x62\x4e\x1b\x9c\x99\xab\x2d\x7c\x33\xba\x57\xdb\xa9\xf5\x6b\xfd\x35\x06\x7f\xae\x8d\x97\x4f\xb1\x76\xfa\xeb\x59\xf0\xff\xed\x94\x75\xc8\x66\xcf\x87\xe8\x62\x5e\x06\xe5\xd4\xe3\x6f\xa3\x3f\x1d\xc2\x16\x9b\xae\x6b\xa6\x1b\x1c\x58\x39\x75\xb8\xd3\x4d\xa3\x36\x0d\x5e\x74\x30\xa2\x9c\xba\xbc\xdc\x7b\xdd\x19\xd5\x5c\x74\xe9\x44\x39\x75\xf9\x3f\xd6\xea\xd0\xf8\xcb\x69\x55\x41\x79\x46\xa4\xb6\x3d\xf8\x90\xd8\xdc\x43\x47\xe5\xd4\xe7\x1f\xd5\xe8\x8a\xce\xa0\x83\x01\xdc\xe8\x73\xec\x95\x17\x48\xc8\x47\x60\xce\x41\x16\xff\x00\x05\xd9\xef\x02\x03\xa5\x87\xdf\xe7\xde\xd4\xf0\x11\xd6\x9d\x19\x9e\xf3\xed\x15\xd6\x61\xf3\xa9\x9d\xc5\xfa\xdd\x7c\xf7\x57\x58\x0b\x35\x27\x13\xc1\x62\xfd\x0d\x8e\x49\x63\x1e\x61\xd7\xad\x39\xa2\x75\x78\x6e\xaa\x83\xf8\x7c\xfb\x4f\x07\x6d\xb1\x3a\xb3\xb5\x22\xbe\xd0\xb5\x30\x61\xe6\x6d\x0b\xf2\x1f\xe8\x5b\x70\x1c\x1a\x27\x95\xf6\x04\x7c\xa4\x52\x99\xa7\x6f\xde\x4e\x91\xfe\xf6\x38\x3d\xb7\xbc\x30\x4d\x43\x95\x77\x78\xcf\xfd\x28\x2d\x2a\x8f\x5c\xa4\x54\x44\xc1\x43\x59\xe1\xbe\x21\xd6\xa3\xa5\x1b\x29\x4f\xea\x83\x29\xa3\x6b\x8a\x3d\x9b\x33\x69\xee\x43\xb2\x30\x08\xc5\x1a\xae\x68\xf9\x90\x2c\x98\xa4\x05\x57\x89\x39\x7d\xa7\xd9\x2a\x59\x30\x23\xa3\x94\xbe\x45\xaa\xb6\x41\x48\x52\xb5\x0d\x42\xe1\x5a\x41\x42\xf9\x0e\x8a\x80\x62\xc1\xd6\xb7\x2e\xac\x82\x46\xe8\x51\x04\x8d\xac\x62\xb4\xd0\xf6\x82\x55\x71\xc5\xba\x53\xb2\xd0\x35\x70\xf6\x98\xff\xd7\xb9\xae\x4c\xb3\x17\x80\xf0\x9f\x35\x18\xdd\x50\x69\x0b\xc3\x29\xc0\x7a\x40\x20\x63\x3f\x8b\xfe\x60\x0d\x18\x14\x6c\xff\x50\xd6\xed\x54\x23\xf7\x28\x3f\x0f\xe8\xc0\xe3\xf8\x5e\xee\x41\xa5\xaf\x0e\x14\xfc\xf6\xfa\xe5\x1d\x39\x33\xc1\x4a\x65\x60\x83\x50\x21\xb9\x56\xc1\x84\x02\x88\x73\xb7\xf9\x80\xa5\x97\x3f\xd2\x95\xc9\xa6\xa9\x8b\x7b\x13\x6f\x65\xa7\x0c\xd2\x0d\xbc\x79\xbb\xf9\xe2\x71\x05\x68\x2d\xfd\xef\x6c\x46\xa5\x39\x6e\x5a\xf0\x7d\x08\x78\x6b\x53\x69\x8b\xa5\x4f\xe5\x19\xc3\x8d\x7a\x59\x4b\xe4\x2c\x93\x76\x9e\x92\x85\x70\x8c\x43\x16\x6b\x70\xaa\xc6\xc0\xc6\x68\x1b\x90\xb5\x76\x8c\x65\xc4\x4c\x37\x2b\xa8\x5b\x9f\xdf\x50\x2e\x75\xba\x94\xc4\x9f\x7d\x2a\xe0\xd9\x71\xb9\x02\xc7\xfb\x70\xf0\x00\x76\xdd\x59\x78\xb7\x82\x9a\xb6\xb2\xca\x10\x57\x03\xf5\x29\xaa\x63\xf1\x15\x6f\x4f\xeb\x11\xff\x00\xea\x11\x03\x47\x14\x24\xc5\x40\xc2\x11\x0b\x59\x11\x79\x38\xe2\x1b\xc9\xa7\x8c\x8b\x37\x51\x11\x94\xbf\x2a\x27\x02\x51\xc7\xeb\xb0\x10\xdf\xb8\x16\x75\xbc\xfa\xa2\x3a\xae\x45\xdd\x5f\x5a\x45\x50\xf7\x6b\xd1\x0f\x17\x54\x01\x0d\x9a\xb4\xce\x07\x49\x9a\xb1\xcd\x29\x59\x50\x0f\xdc\x0a\xba\x8f\x84\x50\x9d\xa7\xe1\x19\x42\x4f\x07\x9b\xbd\x20\x31\xe3\x45\xaf\x05\x6e\x22\x6b\xd2\x8c\x65\x35\x2f\x60\x0d\x57\xa4\x1e\xc2\x95\xb3\x70\xf2\xb4\x98\x84\x94\x17\x05\xd9\x95\xd1\xa0\x0f\x1c\xdf\x22\x6b\xb8\x12\x3b\x09\xef\x72\x99\x68\x6b\x50\xfb\x3d\x9a\x2a\x8d\x92\x15\xb8\x3a\x50\x21\x8c\xc1\x31\xef\x78\x5e\x3e\x25\xed\x70\xa0\x1d\xef\x1e\x58\x97\x87\x39\x3d\x4a\xf5\x26\xa4\x36\x0c\x8a\x10\x25\x3e\x58\xc7\x39\xcb\xe3\xf6\x29\xb3\xd6\xd5\xe7\x21\x6f\xc9\x41\x32\x8f\x4f\xeb\x51\xee\xb7\x31\xc9\x2b\xfe\xe2\x26\x72\x3d\x05\x50\x24\xae\x8d\xf6\x71\xc2\xbf\xd0\x96\x82\x75\xfc\x3d\x56\xc6\x63\x43\xca\xe9\xa1\x39\x4d\x86\x27\x5d\x57\xb9\xcc\xb0\xd4\x65\x32\x49\x87\x59\x02\xf7\x56\xed\x1d\x0f\xc1\x50\x75\xe4\x47\x8b\x7e\xd7\x55\x70\xaf\xfd\x0e\x2c\x96\xdd\x11\x2d\xf8\x0e\xd0\xb8\x83\x45\x30\x1d\xec\x95\xd1\x25\xbd\xdd\xa0\x0d\xe1\xb5\xd9\xca\xcc\x9c\x8d\xaa\xd9\xc0\xac\xe3\xb5\xda\xff\x78\x38\x1f\x9d\x15\xd6\x68\x81\xc2\xa5\xbc\xa6\xf6\x1d\x19\xed\x90\x0c\xdd\x22\xc7\x71\x33\x17\xe4\xbf\xbe\xd0\xc7\x58\x51\x48\x58\x5a\x7a\xe4\x93\x52\xc7\xb3\x60\x74\x13\x0e\xc8\x89\x8e\x90\x60\x37\x71\x4f\xb3\x15\x5b\x0d\x00\x06\x72\xce\xf0\x0b\xe2\x9f\x85\x6f\x7c\xe2\x66\xe8\x85\x23\x12\xc0\x23\xc3\x27\xc4\x2e\x54\x73\x01\x3a\x94\xa3\xf9\x18\x72\xa1\x88\x19\x70\xf1\x6c\xcc\xa0\x8b\x8a\x9f\x05\x6f\x7a\xf4\x67\xf0\xe9\xfe\x67\x6f\xff\xfe\x7c\x42\x04\x63\x51\x17\x30\xd4\xfd\x90\x78\x0c\xc5\x58\xcd\x80\x23\x17\xda\x3f\x24\x3c\x8c\x9f\x12\xd9\x64\x45\xb9\xd1\xb0\xf2\xf9\xef\xda\x54\x69\x06\xeb\x75\xaf\xff\xd3\x5b\x4e\x9d\x6e\x09\x9f\xdf\x34\xd8\xa6\x93\xd1\xe1\x93\x53\xf2\x6f\x00\x00\x00\xff\xff\x0b\xa3\x7d\x9f\x9d\x11\x00\x00") +var _schemaGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\x5f\x8f\xd3\xba\x12\x7f\x4e\x3e\xc5\x50\x89\x55\xb2\xaa\xd2\xfb\x1c\xd4\xa7\xcb\x5e\x89\x7b\x75\x97\x23\xe0\x9c\x17\x84\xc0\x4d\xc6\xad\x21\x71\x8a\xed\x76\x59\x56\xfd\xee\x47\x33\xb6\xf3\xa7\x29\x7b\xce\x01\x2d\x12\xda\x78\xfe\x79\xe6\x37\x3f\x8f\xdd\xbd\xa8\xbe\x88\x2d\x42\xd3\x89\x3a\x4d\x55\xbb\xef\x8c\x83\x2c\x4d\x16\xa8\xab\xae\x56\x7a\xbb\xfa\x6c\x3b\xbd\x48\x93\x85\x6c\x1d\xfd\x31\x28\x1b\xac\xdc\x22\x4d\x93\xc5\x56\xb9\xdd\x61\x53\x54\x5d\xbb\x92\xa2\xc2\x4d\xd7\x7d\x51\xba\x3a\x6c\x84\xeb\xcc\x0a\xb5\x5b\xd9\x6a\x87\xad\x58\x61\xbd\xc5\xbf\xe5\xb0\xf8\x07\x41\xa5\xc2\xa6\x5e\xa4\x79\x9a\xae\x56\xf0\x96\x65\x60\x70\x6f\xd0\xa2\x76\x16\x84\x06\xd4\xae\x08\x0a\xb7\x13\x0e\xee\x84\xe5\x3a\xb1\x06\x69\xba\x16\x04\x54\x5d\xbb\x6f\x14\xd6\x70\xb0\x68\x20\x60\x51\xa4\xee\x7e\x8f\x31\xa4\x75\xe6\x50\x39\x78\x48\x93\x5b\xd1\x22\x00\x90\x44\xe9\x2d\x00\x7c\x22\x68\xca\x85\x16\x2d\x2e\xbb\x56\x39\x6c\xf7\xee\x7e\xf1\x29\x4d\x6e\xea\x2d\x5a\x00\x78\xff\xe1\x9a\x3e\x7b\x4b\xc2\xc1\x4e\x4d\xff\x43\x55\x58\x36\xe5\xcf\x68\xca\xd5\x9d\xd9\xbe\xd2\x35\x7e\x43\x4b\xb6\xfc\x19\x6d\x95\x97\x4f\x8c\x4f\x0c\x8b\x0f\x39\x47\xc5\xcb\x7f\x02\x14\xef\x38\xc7\x64\x0c\xcb\x23\xc0\xbc\xa3\x18\xfe\x1f\x17\x58\xb0\x20\x98\xd3\x06\x67\xe6\x62\x0b\x3f\x8c\xee\xc4\x76\x6a\xfd\x56\x7d\x8f\xc1\xaf\x95\x76\xe1\x33\x58\x5b\xf5\xfd\x2c\xf8\xbf\x77\xc2\x58\x64\xb3\xeb\x21\x7a\x30\xaf\xbc\x72\xea\xf1\xbb\x56\x5f\x0f\x7e\x8b\x4d\xd7\x35\xd3\x0d\x0e\xac\x9c\x3a\xdc\xaa\xa6\x11\x9b\x06\x2f\x3a\xe8\xa0\x9c\xba\xbc\xde\x3b\xd5\x69\xd1\x5c\x74\xe9\x82\x72\xea\xf2\x12\xa5\x38\x34\xee\x72\x5a\xb5\x57\x9e\x11\xa9\x6d\x0f\xce\x27\x36\xf7\x50\x51\x39\xf5\xf9\x43\x34\xaa\xa6\x33\x68\x61\x00\x37\xfa\x1c\x7b\xe5\x05\x12\xf2\x11\x98\x73\x90\xc5\x3f\x41\x41\xf6\xbb\xc0\xc0\xd0\xc3\xbf\xe6\xde\xd4\xf0\x11\xd6\x9d\x19\x9e\xf3\xed\x0d\x4a\xbf\xf9\xd4\xce\xa0\xfc\x38\xdf\xfd\x0d\xca\x40\xcd\xc9\x44\x30\x28\x7f\xc0\xb1\xd0\x98\x47\xd8\xf5\x4a\x1f\xd1\x58\x3c\x37\x55\x5e\x7c\xbe\xfd\xd7\x83\x32\x58\x9f\xd9\x9a\x20\xbe\xd0\x35\x3f\x61\xe6\x6d\xf3\xf2\x9f\xe8\x9b\x77\x1c\x1a\x17\x2a\xed\x09\xf8\x48\xa5\x61\x9e\xbe\xff\x30\x45\xfa\xc7\xe3\xf4\xdc\xf2\xc2\x34\xf5\x55\xde\xe2\x1d\xf7\xa3\x32\x28\x1c\x72\x91\xa1\x22\x0a\xee\xcb\xe2\xaf\x1a\x6d\x65\xd4\xde\x75\xa6\x48\xe5\x41\x57\xd1\x33\xc3\x1a\xae\xc9\xa2\x78\xd9\x5b\xe4\xa1\xc9\x0f\x69\xa2\x11\xca\x35\x5c\xd1\xf2\x21\x4d\x88\x5a\xa5\xa7\x01\xd6\xc5\x3b\xb1\x5d\x92\xec\x7e\x8f\x65\x2f\x23\x36\xa6\x09\xb3\xba\x17\xd2\x82\x84\x1e\xb1\xd2\x0b\xfd\x82\xc4\x81\x07\x25\x8b\xc3\x82\xe4\xb1\xe7\x25\xc9\xe3\xc2\x2b\x64\x88\xcf\x0a\x19\xe2\x9f\xd2\x44\x49\x30\x28\x29\x65\xaf\x79\xc1\xcb\x67\x6b\xd0\xaa\xa1\x72\x12\x8d\x24\x86\x75\x5f\xbe\x41\x99\xb3\xab\x41\x77\x30\x1a\x34\x06\x64\xff\x2f\x8c\xdd\x89\x26\xdc\xa2\xfc\x9a\xa0\xe3\x8e\xe3\x5b\x59\x69\x87\x86\x2e\x79\xfa\xea\x40\xc0\x7f\xdf\xbe\xbe\x25\x67\xa6\x57\x25\x34\x6c\x08\x79\x72\xad\xbd\x09\x05\x08\xce\xdd\xe6\x33\x56\x2e\xfc\x09\x4d\x99\x6c\x9a\xd9\xb8\x37\xb1\x36\xec\x94\x43\xb6\x81\xf7\x1f\x36\xf7\x0e\x97\x80\xc6\xd0\x7f\xea\xd8\x43\x9a\x58\x6e\x95\xf7\x7d\xf0\x00\x29\x5d\x2b\x83\x95\xcb\xc2\xab\x87\xfb\xf3\x5a\x86\xc8\x79\xce\xad\xc9\xf2\x53\x9a\x04\x86\x71\xc8\x72\x0d\x56\x48\xf4\x5c\x8c\xb6\x0c\x2e\x69\x47\x68\x46\xcc\x54\xb3\x04\xd9\xba\xe2\x86\x72\x91\xd9\x22\x24\xfe\xfc\x6b\x09\xcf\x8f\x8b\x25\x58\x4f\x01\x72\xf7\x60\xcb\xce\xc0\xc7\x25\x70\xa7\x8c\xd0\xc4\x54\x4f\x7c\x8a\x2a\x6b\x12\xcb\x11\x21\xb3\x3c\x4d\x12\xcb\xd6\x57\x9c\x15\x99\x8d\x38\x06\x20\x07\x96\x8d\x08\xc9\xf2\x48\xc9\x11\x79\xbd\xdc\xd3\x77\xc4\x4a\x16\x0f\xbc\x8c\x17\x53\x19\x34\xf1\x9e\xf2\x00\xb0\x45\xbc\x20\xcb\x90\x42\xbc\x12\x49\x17\x6f\xc2\xa0\x8b\x4b\xd6\xf5\x17\x58\xc9\xba\x7e\xc9\xca\xe1\xa6\x2a\xa1\x41\x9d\xc9\xba\x18\x44\x39\x99\x9c\xd2\x84\xba\x21\xeb\x82\xdf\x0c\xcf\xd6\xf0\x2f\x46\x2e\xb1\xd2\x4b\xd6\x70\x15\x94\x13\xeb\xf8\x64\x78\xb6\x86\xc5\xa2\xf7\x88\x52\xef\x14\x56\xc1\xcf\x16\x61\x22\xad\x41\xec\xf7\xa8\xeb\x2c\x4a\x96\x60\xc3\xc9\xf1\x63\x6c\xcc\x1c\x9e\x77\x4f\x49\x1c\x1c\x88\xc3\xbb\x73\x50\x5b\xf8\x39\x3b\x4a\xf5\xc6\xa7\xd6\xcf\xba\x09\xa7\x72\x1f\x32\xbe\x3e\xc7\x05\x84\x97\xea\x53\x96\xa0\xea\x6f\x43\x11\x21\x07\x0e\x1c\x14\xaa\xfe\x36\x3b\x01\x45\x7c\x41\x8f\x4a\x7c\x15\xd3\xbf\xe2\x2f\x6e\x2a\x97\x5d\x02\xc7\xf0\x10\x90\xd4\xf7\xad\x64\x69\xe8\xe1\x98\xff\x24\x1e\xc8\x7f\x9a\x4c\x45\xba\x85\x8a\x30\x9c\x32\x9b\x87\x11\x39\x0c\x09\xb8\x33\x62\x6f\x79\xba\xf9\xfa\x23\x6d\x5a\x74\xbb\xae\x86\x3b\xe5\x76\x60\xb0\xea\x8e\x68\xc0\x75\x80\xda\x1e\x0c\x82\xee\x60\x2f\xb4\xaa\xe8\x49\x06\xad\x0f\xaf\xf4\x36\x0c\xc3\xd9\x0c\x9a\x4d\x42\x19\x6f\xcb\xfe\x37\xc1\xf9\x4c\xac\x51\xa2\x01\x0a\x97\xe5\x1e\x5d\x09\x47\xc6\xdd\x27\x93\xe5\x2f\xe0\x38\x6e\x6b\x42\xfe\xeb\x0b\x1d\x8d\x15\xf9\x84\x43\x73\x8f\xd4\x96\x30\x3b\x81\x83\xf8\x73\x73\xa2\x7e\x05\xec\x26\xee\x59\xbe\x64\xab\x01\x40\xcf\xd9\x19\x7e\x5e\xfc\xab\xf0\x8d\x0f\xe2\x0c\x3d\x7f\x72\x3c\x78\x64\xf8\x84\xd8\xf9\x6a\x2e\x40\x87\xe1\xc4\x3e\x86\x9c\x2f\x62\x06\x5c\x3c\x0b\x33\xe8\xa2\xe2\x57\xc1\x9b\x0e\x81\x19\x7c\xaa\xff\x35\xdb\x3f\x2b\x9f\x10\xc1\x58\xd4\x05\x0c\x55\x3f\x14\x1e\x43\x31\x56\x33\xe0\xc8\x85\xf6\x2f\x04\x07\xe3\x37\x42\x3e\x59\x51\x6e\x34\xb6\x5c\xf1\x3f\xa5\xeb\x2c\x87\xf5\xba\xd7\xff\xe6\x0c\xa7\x4e\xf7\x87\x2b\x6e\x1a\x6c\xb3\xc9\xe8\x70\xe9\x29\xfd\x33\x00\x00\xff\xff\x98\xa7\xb5\xe3\xa5\x11\x00\x00") func schemaGoBytes() ([]byte, error) { return bindataRead( @@ -102,7 +102,7 @@ func schemaGo() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "schema.go", size: 4509, mode: os.FileMode(420), modTime: time.Unix(1566736551, 0)} + info := bindataFileInfo{name: "schema.go", size: 4517, mode: os.FileMode(420), modTime: time.Unix(1566898372, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/load/schema.go b/entc/load/schema.go index 84e3551d7..c39e05519 100644 --- a/entc/load/schema.go +++ b/entc/load/schema.go @@ -5,6 +5,8 @@ import ( "fmt" "reflect" + "github.com/facebookincubator/ent/schema/edge" + "github.com/facebookincubator/ent" "github.com/facebookincubator/ent/schema/field" ) @@ -51,19 +53,19 @@ type Index struct { Fields []string `json:"fields,omitempty"` } -// NewEdge creates an loaded edge from schema interface. -func NewEdge(e ent.Edge) *Edge { +// NewEdge creates an loaded edge from edge descriptor. +func NewEdge(ed *edge.Descriptor) *Edge { ne := &Edge{ - Name: e.Name(), - Type: e.Type(), - Tag: e.Tag(), - RefName: e.RefName(), - Unique: e.IsUnique(), - Inverse: e.IsInverse(), - Required: e.IsRequired(), + Tag: ed.Tag, + Type: ed.Type, + Name: ed.Name, + Unique: ed.Unique, + Inverse: ed.Inverse, + Required: ed.Required, + RefName: ed.RefName, } - if e := e.Assoc(); e != nil { - ne.Ref = NewEdge(e) + if ref := ed.Ref; ref != nil { + ne.Ref = NewEdge(ref) } return ne } @@ -77,24 +79,23 @@ func MarshalSchema(schema ent.Interface) (b []byte, err error) { return nil, fmt.Errorf("schema %q: %v", s.Name, err) } for _, f := range fields { + fd := f.Descriptor() sf := &Field{ - Name: f.Name(), - Type: f.Type(), - Tag: f.Tag(), - Unique: f.IsUnique(), - Default: f.HasDefault(), - Nillable: f.IsNillable(), - Optional: f.IsOptional(), - Immutable: f.IsImmutable(), - Validators: len(f.Validators()), + Name: fd.Name, + Type: fd.Type, + Tag: fd.Tag, + Unique: fd.Unique, + Default: fd.Default != nil, + Nillable: fd.Nillable, + Optional: fd.Optional, + Immutable: fd.Immutable, + Validators: len(fd.Validators), } - if s, ok := f.(field.Sizer); ok { - size := s.Size() - sf.Size = &size + if fd.Size != 0 { + sf.Size = &fd.Size } - if c, ok := f.(field.Charseter); ok { - charset := c.Charset() - sf.Charset = &charset + if fd.Charset != "" { + sf.Charset = &fd.Charset } s.Fields = append(s.Fields, sf) } @@ -103,17 +104,18 @@ func MarshalSchema(schema ent.Interface) (b []byte, err error) { return nil, fmt.Errorf("schema %q: %v", s.Name, err) } for _, e := range edges { - s.Edges = append(s.Edges, NewEdge(e)) + s.Edges = append(s.Edges, NewEdge(e.Descriptor())) } indexes, err := safeIndexes(schema) if err != nil { return nil, fmt.Errorf("schema %q: %v", s.Name, err) } for _, idx := range indexes { + idx := idx.Descriptor() s.Indexes = append(s.Indexes, &Index{ - Edges: idx.EdgeNames(), - Fields: idx.FieldNames(), - Unique: idx.IsUnique(), + Edges: idx.Edges, + Fields: idx.Fields, + Unique: idx.Unique, }) } return json.Marshal(s) diff --git a/schema/edge/edge.go b/schema/edge/edge.go index ffe18978b..0bc7e0c88 100644 --- a/schema/edge/edge.go +++ b/schema/edge/edge.go @@ -2,60 +2,30 @@ package edge import ( "reflect" - - "github.com/facebookincubator/ent/schema/field" ) -// Edge represents an edge in the graph. -type Edge struct { - typ string - tag string - ref string - name string - unique bool - required bool - inverse bool - parent *Edge - fields []*field.Field +// A Descriptor for edge configuration. +type Descriptor struct { + Tag string // struct tag. + Type string // edge type. + Name string // edge name. + RefName string // ref name; inverse only. + Ref *Descriptor // edge reference; to/from of the same type. + Unique bool // unique edge. + Inverse bool // inverse edge. + Required bool // required on creation. } // To defines an association edge between two vertices. func To(name string, t interface{}) *assocBuilder { - return &assocBuilder{&Edge{name: name, typ: typ(t)}} + return &assocBuilder{desc: &Descriptor{Name: name, Type: typ(t)}} } // From represents a reversed-edge between two vertices that has a back-reference to its source edge. func From(name string, t interface{}) *inverseBuilder { - return &inverseBuilder{&Edge{name: name, typ: typ(t), inverse: true}} + return &inverseBuilder{desc: &Descriptor{Name: name, Type: typ(t), Inverse: true}} } -// Type returns the type of the edge. -func (e Edge) Type() string { return e.typ } - -// IsUnique returns is the edge is unique. -func (e Edge) IsUnique() bool { return e.unique } - -// AssocName returns the edge name. -func (e Edge) Name() string { return e.name } - -// IsInverse returns is the edge is inverse type. -func (e Edge) IsInverse() bool { return e.inverse } - -// Assoc returns the assoc edge of the inverse edge. -func (e Edge) Assoc() *Edge { return e.parent } - -// RefName returns the reference edge name. -func (e Edge) RefName() string { return e.ref } - -// GetFields returns the edge fields. -func (e Edge) GetFields() []*field.Field { return e.fields } - -// Tag returns the struct tag of the edge. -func (e Edge) Tag() string { return e.tag } - -// IsRequired returns is this edge is an optional edge. -func (e Edge) IsRequired() bool { return e.required } - func typ(t interface{}) string { if rt := reflect.TypeOf(t); rt.NumIn() > 0 { return rt.In(0).Name() @@ -65,38 +35,32 @@ func typ(t interface{}) string { // assocBuilder is the builder for assoc edges. type assocBuilder struct { - *Edge -} - -// Fields sets the fields of the edge. -func (b *assocBuilder) Fields(f ...*field.Field) *assocBuilder { - b.fields = f - return b + desc *Descriptor } // Unique sets the edge type to be unique. Basically, it's limited the ent to be one of the two: // one2one or one2many. one2one applied if the inverse-edge is also unique. func (b *assocBuilder) Unique() *assocBuilder { - b.unique = true + b.desc.Unique = true return b } // Required indicates that this edge is a required field on creation. // Unlike fields, edges are optional by default. func (b *assocBuilder) Required() *assocBuilder { - b.required = true + b.desc.Required = true return b } // StructTag sets the struct tag of the assoc edge. func (b *assocBuilder) StructTag(s string) *assocBuilder { - b.tag = s + b.desc.Tag = s return b } // Assoc creates an inverse-edge with the same type. func (b *assocBuilder) From(name string) *inverseBuilder { - return &inverseBuilder{&Edge{name: name, typ: b.typ, inverse: true, parent: b.Edge}} + return &inverseBuilder{desc: &Descriptor{Name: name, Type: b.desc.Type, Inverse: true, Ref: b.desc}} } // Comment used to put annotations on the schema. @@ -104,40 +68,39 @@ func (b *assocBuilder) Comment(string) *assocBuilder { return b } +// Descriptor implements the ent.Descriptor interface. +func (b *assocBuilder) Descriptor() *Descriptor { + return b.desc +} + // assocBuilder is the builder for inverse edges. type inverseBuilder struct { - *Edge + desc *Descriptor } // Ref sets the referenced-edge of this inverse edge. func (b *inverseBuilder) Ref(ref string) *inverseBuilder { - b.ref = ref - return b -} - -// Fields sets the fields of the edge. -func (b *inverseBuilder) Fields(f ...*field.Field) *inverseBuilder { - b.fields = f + b.desc.RefName = ref return b } // Unique sets the edge type to be unique. Basically, it's limited the ent to be one of the two: // one2one or one2many. one2one applied if the inverse-edge is also unique. func (b *inverseBuilder) Unique() *inverseBuilder { - b.unique = true + b.desc.Unique = true return b } // Required indicates that this edge is a required field on creation. // Unlike fields, edges are optional by default. func (b *inverseBuilder) Required() *inverseBuilder { - b.required = true + b.desc.Required = true return b } // StructTag sets the struct tag of the inverse edge. func (b *inverseBuilder) StructTag(s string) *inverseBuilder { - b.tag = s + b.desc.Tag = s return b } @@ -145,3 +108,8 @@ func (b *inverseBuilder) StructTag(s string) *inverseBuilder { func (b *inverseBuilder) Comment(string) *inverseBuilder { return b } + +// Descriptor implements the ent.Descriptor interface. +func (b *inverseBuilder) Descriptor() *Descriptor { + return b.desc +} diff --git a/schema/edge/edge_test.go b/schema/edge/edge_test.go index 612df67f1..3672c6c06 100644 --- a/schema/edge/edge_test.go +++ b/schema/edge/edge_test.go @@ -12,46 +12,69 @@ import ( func TestEdge(t *testing.T) { assert := assert.New(t) type User struct{ ent.Schema } - e := edge.To("friends", User.Type).Required() - assert.False(e.IsInverse()) - assert.Equal("User", e.Type()) - assert.Equal("friends", e.Name()) - assert.True(e.IsRequired()) + e := edge.To("friends", User.Type). + Required(). + Descriptor() + assert.False(e.Inverse) + assert.Equal("User", e.Type) + assert.Equal("friends", e.Name) + assert.True(e.Required) type Node struct{ ent.Schema } - e = edge.To("parent", Node.Type).Unique() - assert.False(e.IsInverse()) - assert.True(e.IsUnique()) - assert.Equal("Node", e.Type()) - assert.Equal("parent", e.Name()) - assert.False(e.IsRequired()) + e = edge.To("parent", Node.Type). + Unique(). + Descriptor() + assert.False(e.Inverse) + assert.True(e.Unique) + assert.Equal("Node", e.Type) + assert.Equal("parent", e.Name) + assert.False(e.Required) t.Log("m2m relation of the same type") - from := edge.To("following", User.Type).From("followers") + from := edge.To("following", User.Type). + From("followers"). + Descriptor() - assert.True(from.IsInverse()) - assert.False(from.IsUnique()) - assert.Equal("followers", from.Name()) - assert.NotNil(from.Assoc()) - assert.Equal("following", from.Assoc().Name()) - assert.False(from.Assoc().IsUnique()) + assert.True(from.Inverse) + assert.False(from.Unique) + assert.Equal("followers", from.Name) + assert.NotNil(from.Ref) + assert.Equal("following", from.Ref.Name) + assert.False(from.Ref.Unique) t.Log("o2m relation of the same type") - from = edge.To("following", User.Type).Unique().From("followers") - assert.False(from.IsUnique()) - assert.True(from.Assoc().IsUnique()) - from = edge.To("following", User.Type).From("followers").Unique() - assert.True(from.IsUnique()) - assert.False(from.Assoc().IsUnique()) + from = edge.To("following", User.Type). + Unique(). + From("followers"). + Descriptor() + assert.False(from.Unique) + assert.True(from.Ref.Unique) + from = edge.To("following", User.Type). + From("followers"). + Unique(). + Descriptor() + assert.True(from.Unique) + assert.False(from.Ref.Unique) t.Log("o2o relation of the same type") - from = edge.To("following", User.Type).Unique().From("followers").Unique() - assert.True(from.IsUnique()) - assert.True(from.Assoc().IsUnique()) + from = edge.To("following", User.Type). + Unique(). + From("followers"). + Unique(). + Descriptor() + assert.True(from.Unique) + assert.True(from.Ref.Unique) - e = edge.To("user", User.Type).StructTag(`json:"user_name,omitempty"`) - assert.Equal(`json:"user_name,omitempty"`, e.Tag()) - from = edge.To("following", User.Type).StructTag("following").From("followers").StructTag("followers") - assert.Equal("followers", from.Tag()) - assert.Equal("following", from.Assoc().Tag()) + e = edge.To("user", User.Type). + StructTag(`json:"user_name,omitempty"`). + Descriptor() + assert.Equal(`json:"user_name,omitempty"`, e.Tag) + + from = edge.To("following", User.Type). + StructTag("following"). + From("followers"). + StructTag("followers"). + Descriptor() + assert.Equal("followers", from.Tag) + assert.Equal("following", from.Ref.Tag) } diff --git a/schema/field/field.go b/schema/field/field.go index d9a406dd4..1bde7935e 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -107,84 +107,63 @@ var ( } ) -// Field represents a field on a graph vertex. -type Field struct { - typ Type - tag string - size int - name string - charset string - unique bool - nillable bool - optional bool - immutable bool - value interface{} - validators []interface{} +// A Descriptor for field configuration. +type Descriptor struct { + Tag string // struct tag. + Size int // varchar size. + Name string // field name. + Type Type // field type. + Charset string // string charset. + Unique bool // unique index of field. + Nillable bool // nillable struct field. + Optional bool // nullable field in database. + Immutable bool // create-only field. + Default interface{} // default value on create. + UpdateDefault interface{} // default value on update. + Validators []interface{} // validator functions. } // String returns a new Field with type string. -func String(name string) *stringBuilder { return &stringBuilder{Field{typ: TypeString, name: name}} } +func String(name string) *stringBuilder { + return &stringBuilder{desc: &Descriptor{Type: TypeString, Name: name}} +} // Text returns a new string field without limitation on the size. // In MySQL, it is the "longtext" type, but in SQLite and Gremlin it has not effect. func Text(name string) *stringBuilder { - return &stringBuilder{Field{typ: TypeString, name: name, size: math.MaxInt32}} + return &stringBuilder{desc: &Descriptor{Type: TypeString, Name: name, Size: math.MaxInt32}} } // Bytes returns a new Field with type bytes/buffer. // In MySQL and SQLite, it is the "BLOB" type, and it does not support for Gremlin. -func Bytes(name string) *bytesBuilder { return &bytesBuilder{Field{typ: TypeBytes, name: name}} } +func Bytes(name string) *bytesBuilder { + return &bytesBuilder{desc: &Descriptor{Type: TypeBytes, Name: name}} +} // Bool returns a new Field with type bool. -func Bool(name string) *boolBuilder { return &boolBuilder{Field{typ: TypeBool, name: name}} } +func Bool(name string) *boolBuilder { + return &boolBuilder{desc: &Descriptor{Type: TypeBool, Name: name}} +} // Time returns a new Field with type timestamp. -func Time(name string) *timeBuilder { return &timeBuilder{Field{typ: TypeTime, name: name}} } - -// Type returns the field type. -func (f Field) Type() Type { return f.typ } - -// Name returns the field name. -func (f Field) Name() string { return f.name } - -// HasDefault returns is this field has a default value. -func (f Field) HasDefault() bool { return f.value != nil } - -// Value returns the default value of the field. -func (f Field) Value() interface{} { return f.value } - -// IsNillable returns if this field is an nillable field. Basically, wraps the value with pointer. -func (f Field) IsNillable() bool { return f.nillable } - -// IsOptional returns is this field is an optional field. -func (f Field) IsOptional() bool { return f.optional } - -// IsImmutable returns is this field is an immutable field. -func (f Field) IsImmutable() bool { return f.immutable } - -// IsUnique returns is this field is a unique field. -func (f Field) IsUnique() bool { return f.unique } - -// Validators returns the field matchers. -func (f Field) Validators() []interface{} { return f.validators } - -// Tag returns the struct tag of the field. -func (f Field) Tag() string { return f.tag } +func Time(name string) *timeBuilder { + return &timeBuilder{desc: &Descriptor{Type: TypeTime, Name: name}} +} // stringBuilder is the builder for string fields. type stringBuilder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *stringBuilder) Unique() *stringBuilder { - b.unique = true + b.desc.Unique = true return b } // Match adds a regex matcher for this field. Operation fails if the regex fails. func (b *stringBuilder) Match(re *regexp.Regexp) *stringBuilder { - b.validators = append(b.validators, func(v string) error { + b.desc.Validators = append(b.desc.Validators, func(v string) error { if !re.MatchString(v) { return errors.New("value does not match validation") } @@ -196,7 +175,7 @@ func (b *stringBuilder) Match(re *regexp.Regexp) *stringBuilder { // MinLen adds a length validator for this field. // Operation fails if the length of the string is less than the given value. func (b *stringBuilder) MinLen(i int) *stringBuilder { - b.validators = append(b.validators, func(v string) error { + b.desc.Validators = append(b.desc.Validators, func(v string) error { if len(v) < i { return errors.New("value is less than the required length") } @@ -208,8 +187,8 @@ func (b *stringBuilder) MinLen(i int) *stringBuilder { // MaxLen adds a length validator for this field. // Operation fails if the length of the string is greater than the given value. func (b *stringBuilder) MaxLen(i int) *stringBuilder { - b.size = i - b.validators = append(b.validators, func(v string) error { + b.desc.Size = i + b.desc.Validators = append(b.desc.Validators, func(v string) error { if len(v) > i { return errors.New("value is less than the required length") } @@ -220,33 +199,33 @@ func (b *stringBuilder) MaxLen(i int) *stringBuilder { // Validate adds a validator for this field. Operation fails if the validation fails. func (b *stringBuilder) Validate(fn func(string) error) *stringBuilder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } // Default sets the default value of the field. func (b *stringBuilder) Default(s string) *stringBuilder { - b.value = s + b.desc.Default = s return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *stringBuilder) Nillable() *stringBuilder { - b.nillable = true + b.desc.Nillable = true return b } // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *stringBuilder) Optional() *stringBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *stringBuilder) Immutable() *stringBuilder { - b.immutable = true + b.desc.Immutable = true return b } @@ -257,46 +236,44 @@ func (b *stringBuilder) Comment(c string) *stringBuilder { // StructTag sets the struct tag of the field. func (b *stringBuilder) StructTag(s string) *stringBuilder { - b.tag = s + b.desc.Tag = s return b } // SetCharset sets the character set attribute for character fields. // For example, utf8 or utf8mb4 in MySQL. -func (b *stringBuilder) SetCharset(s string) *stringBuilder { - b.charset = s +func (b *stringBuilder) Charset(s string) *stringBuilder { + b.desc.Charset = s return b } -// Size returns the maximum size of a string. -// In SQL dialects this is parameter for varchar. -func (b stringBuilder) Size() int { return b.size } - -// Charset returns the character set of the field. -func (b stringBuilder) Charset() string { return b.charset } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *stringBuilder) Descriptor() *Descriptor { + return b.desc +} // timeBuilder is the builder for time fields. type timeBuilder struct { - Field + desc *Descriptor } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *timeBuilder) Nillable() *timeBuilder { - b.nillable = true + b.desc.Nillable = true return b } // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *timeBuilder) Optional() *timeBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *timeBuilder) Immutable() *timeBuilder { - b.immutable = true + b.desc.Immutable = true return b } @@ -307,7 +284,7 @@ func (b *timeBuilder) Comment(c string) *timeBuilder { // StructTag sets the struct tag of the field. func (b *timeBuilder) StructTag(s string) *timeBuilder { - b.tag = s + b.desc.Tag = s return b } @@ -318,38 +295,43 @@ func (b *timeBuilder) StructTag(s string) *timeBuilder { // Default(time.Now) // func (b *timeBuilder) Default(f func() time.Time) *timeBuilder { - b.value = f + b.desc.Default = f return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *timeBuilder) Descriptor() *Descriptor { + return b.desc +} + // boolBuilder is the builder for boolean fields. type boolBuilder struct { - Field + desc *Descriptor } // Default sets the default value of the field. func (b *boolBuilder) Default(v bool) *boolBuilder { - b.value = v + b.desc.Default = v return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *boolBuilder) Nillable() *boolBuilder { - b.nillable = true + b.desc.Nillable = true return b } // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *boolBuilder) Optional() *boolBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *boolBuilder) Immutable() *boolBuilder { - b.immutable = true + b.desc.Immutable = true return b } @@ -360,38 +342,43 @@ func (b *boolBuilder) Comment(c string) *boolBuilder { // StructTag sets the struct tag of the field. func (b *boolBuilder) StructTag(s string) *boolBuilder { - b.tag = s + b.desc.Tag = s return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *boolBuilder) Descriptor() *Descriptor { + return b.desc +} + // bytesBuilder is the builder for bytes fields. type bytesBuilder struct { - Field + desc *Descriptor } // Default sets the default value of the field. func (b *bytesBuilder) Default(v []byte) *bytesBuilder { - b.value = v + b.desc.Default = v return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *bytesBuilder) Nillable() *bytesBuilder { - b.nillable = true + b.desc.Nillable = true return b } // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *bytesBuilder) Optional() *bytesBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *bytesBuilder) Immutable() *bytesBuilder { - b.immutable = true + b.desc.Immutable = true return b } @@ -402,16 +389,11 @@ func (b *bytesBuilder) Comment(c string) *bytesBuilder { // StructTag sets the struct tag of the field. func (b *bytesBuilder) StructTag(s string) *bytesBuilder { - b.tag = s + b.desc.Tag = s return b } -// Charseter is the interface that wraps the Charset method. -type Charseter interface { - Charset() string -} - -// Sizer is the interface that wraps the Size method. -type Sizer interface { - Size() int +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *bytesBuilder) Descriptor() *Descriptor { + return b.desc } diff --git a/schema/field/field_test.go b/schema/field/field_test.go index ea684c5b4..34802426f 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -5,7 +5,6 @@ import ( "testing" "time" - "github.com/facebookincubator/ent" "github.com/facebookincubator/ent/schema/field" "github.com/stretchr/testify/assert" @@ -13,84 +12,95 @@ import ( func TestInt(t *testing.T) { f := field.Int("age").Positive() - assert.Equal(t, "age", f.Name()) - assert.Equal(t, field.TypeInt, f.Type()) - assert.Len(t, f.Validators(), 1) + fd := f.Descriptor() + assert.Equal(t, "age", fd.Name) + assert.Equal(t, field.TypeInt, fd.Type) + assert.Len(t, fd.Validators, 1) f = field.Int("age").Default(10).Min(10).Max(20) - assert.True(t, f.HasDefault()) - assert.Equal(t, 10, f.Value()) - assert.Len(t, f.Validators(), 2) + fd = f.Descriptor() + assert.NotNil(t, fd.Default) + assert.Equal(t, 10, fd.Default) + assert.Len(t, fd.Validators, 2) f = field.Int("age").Range(20, 40).Nillable() - assert.False(t, f.HasDefault()) - assert.True(t, f.IsNillable()) - assert.False(t, f.IsImmutable()) - assert.Len(t, f.Validators(), 1) + fd = f.Descriptor() + assert.Nil(t, fd.Default) + assert.True(t, fd.Nillable) + assert.False(t, fd.Immutable) + assert.Len(t, fd.Validators, 1) - assert.Equal(t, field.TypeInt8, field.Int8("age").Type()) - assert.Equal(t, field.TypeInt16, field.Int16("age").Type()) - assert.Equal(t, field.TypeInt32, field.Int32("age").Type()) - assert.Equal(t, field.TypeInt64, field.Int64("age").Type()) + assert.Equal(t, field.TypeInt8, field.Int8("age").Descriptor().Type) + assert.Equal(t, field.TypeInt16, field.Int16("age").Descriptor().Type) + assert.Equal(t, field.TypeInt32, field.Int32("age").Descriptor().Type) + assert.Equal(t, field.TypeInt64, field.Int64("age").Descriptor().Type) } func TestFloat(t *testing.T) { f := field.Float("age").Positive() - assert.Equal(t, "age", f.Name()) - assert.Equal(t, field.TypeFloat64, f.Type()) - assert.Len(t, f.Validators(), 1) + fd := f.Descriptor() + assert.Equal(t, "age", fd.Name) + assert.Equal(t, field.TypeFloat64, fd.Type) + assert.Len(t, fd.Validators, 1) f = field.Float("age").Min(2.5).Max(5) - assert.Len(t, f.Validators(), 2) + fd = f.Descriptor() + assert.Len(t, fd.Validators, 2) } func TestBool(t *testing.T) { f := field.Bool("active").Default(true).Immutable() - assert.Equal(t, "active", f.Name()) - assert.Equal(t, field.TypeBool, f.Type()) - assert.True(t, f.HasDefault()) - assert.True(t, f.IsImmutable()) - assert.Equal(t, true, f.Value()) + fd := f.Descriptor() + assert.Equal(t, "active", fd.Name) + assert.Equal(t, field.TypeBool, fd.Type) + assert.NotNil(t, fd.Default) + assert.True(t, fd.Immutable) + assert.Equal(t, true, fd.Default) } func TestBytes(t *testing.T) { f := field.Bytes("active").Default([]byte("{}")) - assert.Equal(t, "active", f.Name()) - assert.Equal(t, field.TypeBytes, f.Type()) - assert.True(t, f.HasDefault()) - assert.Equal(t, []byte("{}"), f.Value()) + fd := f.Descriptor() + assert.Equal(t, "active", fd.Name) + assert.Equal(t, field.TypeBytes, fd.Type) + assert.NotNil(t, fd.Default) + assert.Equal(t, []byte("{}"), fd.Default) } func TestString(t *testing.T) { re := regexp.MustCompile("[a-zA-Z0-9]") f := field.String("name").Unique().Match(re).Validate(func(string) error { return nil }) - assert.Equal(t, field.TypeString, f.Type()) - assert.Equal(t, "name", f.Name()) - assert.True(t, f.IsUnique()) - assert.Len(t, f.Validators(), 2) + fd := f.Descriptor() + assert.Equal(t, field.TypeString, fd.Type) + assert.Equal(t, "name", fd.Name) + assert.True(t, fd.Unique) + assert.Len(t, fd.Validators, 2) } func TestCharset(t *testing.T) { - var f ent.Field = field.String("name").SetCharset("utf8") - cs, ok := f.(field.Charseter) - assert.True(t, ok, "string field implements the Charseter interface") - assert.Equal(t, "utf8", cs.Charset()) + fd := field.String("name"). + Charset("utf8"). + Descriptor() + assert.Equal(t, "utf8", fd.Charset) } func TestTime(t *testing.T) { now := time.Now() - f := field.Time("created_at"). + fd := field.Time("created_at"). Default(func() time.Time { return now - }) - assert.Equal(t, "created_at", f.Name()) - assert.Equal(t, field.TypeTime, f.Type()) - assert.Equal(t, "time.Time", f.Type().String()) - assert.NotNil(t, f.Value()) - assert.Equal(t, now, f.Value().(func() time.Time)()) + }). + Descriptor() + assert.Equal(t, "created_at", fd.Name) + assert.Equal(t, field.TypeTime, fd.Type) + assert.Equal(t, "time.Time", fd.Type.String()) + assert.NotNil(t, fd.Default) + assert.Equal(t, now, fd.Default.(func() time.Time)()) } func TestField_Tag(t *testing.T) { - f := field.Bool("expired").StructTag(`json:"expired,omitempty"`) - assert.Equal(t, `json:"expired,omitempty"`, f.Tag()) + fd := field.Bool("expired"). + StructTag(`json:"expired,omitempty"`). + Descriptor() + assert.Equal(t, `json:"expired,omitempty"`, fd.Tag) } diff --git a/schema/field/gen/numeric.tmpl b/schema/field/gen/numeric.tmpl index 8086f294c..89b260b7b 100644 --- a/schema/field/gen/numeric.tmpl +++ b/schema/field/gen/numeric.tmpl @@ -8,32 +8,32 @@ import "errors" {{ range $_, $t := $.Ints }} {{ $title := title $t.String }} // {{ $title }} returns a new Field with type {{ $t }}. - func {{ $title }}(name string) *{{ $t }}Builder { return &{{ $t }}Builder{Field{typ: Type{{ $title }}, name: name}} } + func {{ $title }}(name string) *{{ $t }}Builder { return &{{ $t }}Builder{desc: &Descriptor{Type: Type{{ $title }}, Name: name}} } {{ end }} // Float returns a new Field with type float64. -func Float(name string) *float64Builder { return &float64Builder{Field{typ: TypeFloat64, name: name}} } +func Float(name string) *float64Builder { return &float64Builder{desc: &Descriptor{Type: TypeFloat64, Name: name}} } // Float32 returns a new Field with type float32. -func Float32(name string) *float32Builder { return &float32Builder{Field{typ: TypeFloat32, name: name}} } +func Float32(name string) *float32Builder { return &float32Builder{desc: &Descriptor{Type: TypeFloat32, Name: name}} } {{ range $_, $t := $.Ints }} {{ $builder := printf "%sBuilder" $t }} // {{ $builder }} is the builder for {{ $t }} field. type {{ $builder }} struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *{{ $builder }}) Unique() *{{ $builder }} { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v < i || v > j { return errors.New("value out of range") } @@ -44,7 +44,7 @@ func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v < i { return errors.New("value out of range") } @@ -55,7 +55,7 @@ func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *{{ $builder }}) Max(i {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v > i { return errors.New("value out of range") } @@ -78,14 +78,14 @@ func (b *{{ $builder }}) Positive() *{{ $builder }} { // Default sets the default value of the field. func (b *{{ $builder }}) Default(i {{ $t }}) *{{ $builder }} { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *{{ $builder }}) Nillable() *{{ $builder }} { - b.nillable = true + b.desc.Nillable = true return b } @@ -97,28 +97,33 @@ func (b *{{ $builder }}) Comment(c string) *{{ $builder }} { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *{{ $builder }}) Optional() *{{ $builder }} { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *{{ $builder }}) Immutable() *{{ $builder }} { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *{{ $builder }}) StructTag(s string) *{{ $builder }} { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *{{ $builder }}) Validate(fn func({{ $t }}) error) *{{ $builder }} { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *{{ $builder }}) Descriptor() *Descriptor { + return b.desc +} + {{ end }} {{ range $_, $t := $.Floats }} @@ -126,18 +131,18 @@ func (b *{{ $builder }}) Validate(fn func({{ $t }}) error) *{{ $builder }} { // {{ $builder }} is the builder for float fields. type {{ $builder }} struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *{{ $builder }}) Unique() *{{ $builder }} { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v < i || v > j { return errors.New("value out of range") } @@ -148,7 +153,7 @@ func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v < i { return errors.New("value out of range") } @@ -159,7 +164,7 @@ func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *{{ $builder }}) Max(i {{ $t }}) *{{ $builder }} { - b.validators = append(b.validators, func(v {{ $t }}) error { + b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error { if v > i { return errors.New("value out of range") } @@ -180,14 +185,14 @@ func (b *{{ $builder }}) Negative() *{{ $builder }} { // Default sets the default value of the field. func (b *{{ $builder }}) Default(i {{ $t }}) *{{ $builder }} { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *{{ $builder }}) Nillable() *{{ $builder }} { - b.nillable = true + b.desc.Nillable = true return b } @@ -199,27 +204,32 @@ func (b *{{ $builder }}) Comment(c string) *{{ $builder }} { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *{{ $builder }}) Optional() *{{ $builder }} { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *{{ $builder }}) Immutable() *{{ $builder }} { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *{{ $builder }}) StructTag(s string) *{{ $builder }} { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *{{ $builder }}) Validate(fn func({{ $t }}) error) *{{ $builder }} { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } + +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *{{ $builder }}) Descriptor() *Descriptor { + return b.desc +} {{ end }} {{ end }} \ No newline at end of file diff --git a/schema/field/numeric.go b/schema/field/numeric.go index 56df6f777..ad7f08635 100644 --- a/schema/field/numeric.go +++ b/schema/field/numeric.go @@ -5,55 +5,77 @@ import "errors" //go:generate go run gen/gen.go // Int returns a new Field with type int. -func Int(name string) *intBuilder { return &intBuilder{Field{typ: TypeInt, name: name}} } +func Int(name string) *intBuilder { return &intBuilder{desc: &Descriptor{Type: TypeInt, Name: name}} } // Uint returns a new Field with type uint. -func Uint(name string) *uintBuilder { return &uintBuilder{Field{typ: TypeUint, name: name}} } +func Uint(name string) *uintBuilder { + return &uintBuilder{desc: &Descriptor{Type: TypeUint, Name: name}} +} // Int8 returns a new Field with type int8. -func Int8(name string) *int8Builder { return &int8Builder{Field{typ: TypeInt8, name: name}} } +func Int8(name string) *int8Builder { + return &int8Builder{desc: &Descriptor{Type: TypeInt8, Name: name}} +} // Int16 returns a new Field with type int16. -func Int16(name string) *int16Builder { return &int16Builder{Field{typ: TypeInt16, name: name}} } +func Int16(name string) *int16Builder { + return &int16Builder{desc: &Descriptor{Type: TypeInt16, Name: name}} +} // Int32 returns a new Field with type int32. -func Int32(name string) *int32Builder { return &int32Builder{Field{typ: TypeInt32, name: name}} } +func Int32(name string) *int32Builder { + return &int32Builder{desc: &Descriptor{Type: TypeInt32, Name: name}} +} // Int64 returns a new Field with type int64. -func Int64(name string) *int64Builder { return &int64Builder{Field{typ: TypeInt64, name: name}} } +func Int64(name string) *int64Builder { + return &int64Builder{desc: &Descriptor{Type: TypeInt64, Name: name}} +} // Uint8 returns a new Field with type uint8. -func Uint8(name string) *uint8Builder { return &uint8Builder{Field{typ: TypeUint8, name: name}} } +func Uint8(name string) *uint8Builder { + return &uint8Builder{desc: &Descriptor{Type: TypeUint8, Name: name}} +} // Uint16 returns a new Field with type uint16. -func Uint16(name string) *uint16Builder { return &uint16Builder{Field{typ: TypeUint16, name: name}} } +func Uint16(name string) *uint16Builder { + return &uint16Builder{desc: &Descriptor{Type: TypeUint16, Name: name}} +} // Uint32 returns a new Field with type uint32. -func Uint32(name string) *uint32Builder { return &uint32Builder{Field{typ: TypeUint32, name: name}} } +func Uint32(name string) *uint32Builder { + return &uint32Builder{desc: &Descriptor{Type: TypeUint32, Name: name}} +} // Uint64 returns a new Field with type uint64. -func Uint64(name string) *uint64Builder { return &uint64Builder{Field{typ: TypeUint64, name: name}} } +func Uint64(name string) *uint64Builder { + return &uint64Builder{desc: &Descriptor{Type: TypeUint64, Name: name}} +} // Float returns a new Field with type float64. -func Float(name string) *float64Builder { return &float64Builder{Field{typ: TypeFloat64, name: name}} } +func Float(name string) *float64Builder { + return &float64Builder{desc: &Descriptor{Type: TypeFloat64, Name: name}} +} // Float32 returns a new Field with type float32. -func Float32(name string) *float32Builder { return &float32Builder{Field{typ: TypeFloat32, name: name}} } +func Float32(name string) *float32Builder { + return &float32Builder{desc: &Descriptor{Type: TypeFloat32, Name: name}} +} // intBuilder is the builder for int field. type intBuilder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *intBuilder) Unique() *intBuilder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *intBuilder) Range(i, j int) *intBuilder { - b.validators = append(b.validators, func(v int) error { + b.desc.Validators = append(b.desc.Validators, func(v int) error { if v < i || v > j { return errors.New("value out of range") } @@ -64,7 +86,7 @@ func (b *intBuilder) Range(i, j int) *intBuilder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *intBuilder) Min(i int) *intBuilder { - b.validators = append(b.validators, func(v int) error { + b.desc.Validators = append(b.desc.Validators, func(v int) error { if v < i { return errors.New("value out of range") } @@ -75,7 +97,7 @@ func (b *intBuilder) Min(i int) *intBuilder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *intBuilder) Max(i int) *intBuilder { - b.validators = append(b.validators, func(v int) error { + b.desc.Validators = append(b.desc.Validators, func(v int) error { if v > i { return errors.New("value out of range") } @@ -96,14 +118,14 @@ func (b *intBuilder) Negative() *intBuilder { // Default sets the default value of the field. func (b *intBuilder) Default(i int) *intBuilder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *intBuilder) Nillable() *intBuilder { - b.nillable = true + b.desc.Nillable = true return b } @@ -115,42 +137,47 @@ func (b *intBuilder) Comment(c string) *intBuilder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *intBuilder) Optional() *intBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *intBuilder) Immutable() *intBuilder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *intBuilder) StructTag(s string) *intBuilder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *intBuilder) Validate(fn func(int) error) *intBuilder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *intBuilder) Descriptor() *Descriptor { + return b.desc +} + // uintBuilder is the builder for uint field. type uintBuilder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *uintBuilder) Unique() *uintBuilder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *uintBuilder) Range(i, j uint) *uintBuilder { - b.validators = append(b.validators, func(v uint) error { + b.desc.Validators = append(b.desc.Validators, func(v uint) error { if v < i || v > j { return errors.New("value out of range") } @@ -161,7 +188,7 @@ func (b *uintBuilder) Range(i, j uint) *uintBuilder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *uintBuilder) Min(i uint) *uintBuilder { - b.validators = append(b.validators, func(v uint) error { + b.desc.Validators = append(b.desc.Validators, func(v uint) error { if v < i { return errors.New("value out of range") } @@ -172,7 +199,7 @@ func (b *uintBuilder) Min(i uint) *uintBuilder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *uintBuilder) Max(i uint) *uintBuilder { - b.validators = append(b.validators, func(v uint) error { + b.desc.Validators = append(b.desc.Validators, func(v uint) error { if v > i { return errors.New("value out of range") } @@ -188,14 +215,14 @@ func (b *uintBuilder) Positive() *uintBuilder { // Default sets the default value of the field. func (b *uintBuilder) Default(i uint) *uintBuilder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *uintBuilder) Nillable() *uintBuilder { - b.nillable = true + b.desc.Nillable = true return b } @@ -207,42 +234,47 @@ func (b *uintBuilder) Comment(c string) *uintBuilder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *uintBuilder) Optional() *uintBuilder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *uintBuilder) Immutable() *uintBuilder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *uintBuilder) StructTag(s string) *uintBuilder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *uintBuilder) Validate(fn func(uint) error) *uintBuilder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *uintBuilder) Descriptor() *Descriptor { + return b.desc +} + // int8Builder is the builder for int8 field. type int8Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *int8Builder) Unique() *int8Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *int8Builder) Range(i, j int8) *int8Builder { - b.validators = append(b.validators, func(v int8) error { + b.desc.Validators = append(b.desc.Validators, func(v int8) error { if v < i || v > j { return errors.New("value out of range") } @@ -253,7 +285,7 @@ func (b *int8Builder) Range(i, j int8) *int8Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *int8Builder) Min(i int8) *int8Builder { - b.validators = append(b.validators, func(v int8) error { + b.desc.Validators = append(b.desc.Validators, func(v int8) error { if v < i { return errors.New("value out of range") } @@ -264,7 +296,7 @@ func (b *int8Builder) Min(i int8) *int8Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *int8Builder) Max(i int8) *int8Builder { - b.validators = append(b.validators, func(v int8) error { + b.desc.Validators = append(b.desc.Validators, func(v int8) error { if v > i { return errors.New("value out of range") } @@ -285,14 +317,14 @@ func (b *int8Builder) Negative() *int8Builder { // Default sets the default value of the field. func (b *int8Builder) Default(i int8) *int8Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *int8Builder) Nillable() *int8Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -304,42 +336,47 @@ func (b *int8Builder) Comment(c string) *int8Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *int8Builder) Optional() *int8Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *int8Builder) Immutable() *int8Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *int8Builder) StructTag(s string) *int8Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *int8Builder) Validate(fn func(int8) error) *int8Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *int8Builder) Descriptor() *Descriptor { + return b.desc +} + // int16Builder is the builder for int16 field. type int16Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *int16Builder) Unique() *int16Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *int16Builder) Range(i, j int16) *int16Builder { - b.validators = append(b.validators, func(v int16) error { + b.desc.Validators = append(b.desc.Validators, func(v int16) error { if v < i || v > j { return errors.New("value out of range") } @@ -350,7 +387,7 @@ func (b *int16Builder) Range(i, j int16) *int16Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *int16Builder) Min(i int16) *int16Builder { - b.validators = append(b.validators, func(v int16) error { + b.desc.Validators = append(b.desc.Validators, func(v int16) error { if v < i { return errors.New("value out of range") } @@ -361,7 +398,7 @@ func (b *int16Builder) Min(i int16) *int16Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *int16Builder) Max(i int16) *int16Builder { - b.validators = append(b.validators, func(v int16) error { + b.desc.Validators = append(b.desc.Validators, func(v int16) error { if v > i { return errors.New("value out of range") } @@ -382,14 +419,14 @@ func (b *int16Builder) Negative() *int16Builder { // Default sets the default value of the field. func (b *int16Builder) Default(i int16) *int16Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *int16Builder) Nillable() *int16Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -401,42 +438,47 @@ func (b *int16Builder) Comment(c string) *int16Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *int16Builder) Optional() *int16Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *int16Builder) Immutable() *int16Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *int16Builder) StructTag(s string) *int16Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *int16Builder) Validate(fn func(int16) error) *int16Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *int16Builder) Descriptor() *Descriptor { + return b.desc +} + // int32Builder is the builder for int32 field. type int32Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *int32Builder) Unique() *int32Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *int32Builder) Range(i, j int32) *int32Builder { - b.validators = append(b.validators, func(v int32) error { + b.desc.Validators = append(b.desc.Validators, func(v int32) error { if v < i || v > j { return errors.New("value out of range") } @@ -447,7 +489,7 @@ func (b *int32Builder) Range(i, j int32) *int32Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *int32Builder) Min(i int32) *int32Builder { - b.validators = append(b.validators, func(v int32) error { + b.desc.Validators = append(b.desc.Validators, func(v int32) error { if v < i { return errors.New("value out of range") } @@ -458,7 +500,7 @@ func (b *int32Builder) Min(i int32) *int32Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *int32Builder) Max(i int32) *int32Builder { - b.validators = append(b.validators, func(v int32) error { + b.desc.Validators = append(b.desc.Validators, func(v int32) error { if v > i { return errors.New("value out of range") } @@ -479,14 +521,14 @@ func (b *int32Builder) Negative() *int32Builder { // Default sets the default value of the field. func (b *int32Builder) Default(i int32) *int32Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *int32Builder) Nillable() *int32Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -498,42 +540,47 @@ func (b *int32Builder) Comment(c string) *int32Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *int32Builder) Optional() *int32Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *int32Builder) Immutable() *int32Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *int32Builder) StructTag(s string) *int32Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *int32Builder) Validate(fn func(int32) error) *int32Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *int32Builder) Descriptor() *Descriptor { + return b.desc +} + // int64Builder is the builder for int64 field. type int64Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *int64Builder) Unique() *int64Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *int64Builder) Range(i, j int64) *int64Builder { - b.validators = append(b.validators, func(v int64) error { + b.desc.Validators = append(b.desc.Validators, func(v int64) error { if v < i || v > j { return errors.New("value out of range") } @@ -544,7 +591,7 @@ func (b *int64Builder) Range(i, j int64) *int64Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *int64Builder) Min(i int64) *int64Builder { - b.validators = append(b.validators, func(v int64) error { + b.desc.Validators = append(b.desc.Validators, func(v int64) error { if v < i { return errors.New("value out of range") } @@ -555,7 +602,7 @@ func (b *int64Builder) Min(i int64) *int64Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *int64Builder) Max(i int64) *int64Builder { - b.validators = append(b.validators, func(v int64) error { + b.desc.Validators = append(b.desc.Validators, func(v int64) error { if v > i { return errors.New("value out of range") } @@ -576,14 +623,14 @@ func (b *int64Builder) Negative() *int64Builder { // Default sets the default value of the field. func (b *int64Builder) Default(i int64) *int64Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *int64Builder) Nillable() *int64Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -595,42 +642,47 @@ func (b *int64Builder) Comment(c string) *int64Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *int64Builder) Optional() *int64Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *int64Builder) Immutable() *int64Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *int64Builder) StructTag(s string) *int64Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *int64Builder) Validate(fn func(int64) error) *int64Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *int64Builder) Descriptor() *Descriptor { + return b.desc +} + // uint8Builder is the builder for uint8 field. type uint8Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *uint8Builder) Unique() *uint8Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *uint8Builder) Range(i, j uint8) *uint8Builder { - b.validators = append(b.validators, func(v uint8) error { + b.desc.Validators = append(b.desc.Validators, func(v uint8) error { if v < i || v > j { return errors.New("value out of range") } @@ -641,7 +693,7 @@ func (b *uint8Builder) Range(i, j uint8) *uint8Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *uint8Builder) Min(i uint8) *uint8Builder { - b.validators = append(b.validators, func(v uint8) error { + b.desc.Validators = append(b.desc.Validators, func(v uint8) error { if v < i { return errors.New("value out of range") } @@ -652,7 +704,7 @@ func (b *uint8Builder) Min(i uint8) *uint8Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *uint8Builder) Max(i uint8) *uint8Builder { - b.validators = append(b.validators, func(v uint8) error { + b.desc.Validators = append(b.desc.Validators, func(v uint8) error { if v > i { return errors.New("value out of range") } @@ -668,14 +720,14 @@ func (b *uint8Builder) Positive() *uint8Builder { // Default sets the default value of the field. func (b *uint8Builder) Default(i uint8) *uint8Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *uint8Builder) Nillable() *uint8Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -687,42 +739,47 @@ func (b *uint8Builder) Comment(c string) *uint8Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *uint8Builder) Optional() *uint8Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *uint8Builder) Immutable() *uint8Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *uint8Builder) StructTag(s string) *uint8Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *uint8Builder) Validate(fn func(uint8) error) *uint8Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *uint8Builder) Descriptor() *Descriptor { + return b.desc +} + // uint16Builder is the builder for uint16 field. type uint16Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *uint16Builder) Unique() *uint16Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *uint16Builder) Range(i, j uint16) *uint16Builder { - b.validators = append(b.validators, func(v uint16) error { + b.desc.Validators = append(b.desc.Validators, func(v uint16) error { if v < i || v > j { return errors.New("value out of range") } @@ -733,7 +790,7 @@ func (b *uint16Builder) Range(i, j uint16) *uint16Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *uint16Builder) Min(i uint16) *uint16Builder { - b.validators = append(b.validators, func(v uint16) error { + b.desc.Validators = append(b.desc.Validators, func(v uint16) error { if v < i { return errors.New("value out of range") } @@ -744,7 +801,7 @@ func (b *uint16Builder) Min(i uint16) *uint16Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *uint16Builder) Max(i uint16) *uint16Builder { - b.validators = append(b.validators, func(v uint16) error { + b.desc.Validators = append(b.desc.Validators, func(v uint16) error { if v > i { return errors.New("value out of range") } @@ -760,14 +817,14 @@ func (b *uint16Builder) Positive() *uint16Builder { // Default sets the default value of the field. func (b *uint16Builder) Default(i uint16) *uint16Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *uint16Builder) Nillable() *uint16Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -779,42 +836,47 @@ func (b *uint16Builder) Comment(c string) *uint16Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *uint16Builder) Optional() *uint16Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *uint16Builder) Immutable() *uint16Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *uint16Builder) StructTag(s string) *uint16Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *uint16Builder) Validate(fn func(uint16) error) *uint16Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *uint16Builder) Descriptor() *Descriptor { + return b.desc +} + // uint32Builder is the builder for uint32 field. type uint32Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *uint32Builder) Unique() *uint32Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *uint32Builder) Range(i, j uint32) *uint32Builder { - b.validators = append(b.validators, func(v uint32) error { + b.desc.Validators = append(b.desc.Validators, func(v uint32) error { if v < i || v > j { return errors.New("value out of range") } @@ -825,7 +887,7 @@ func (b *uint32Builder) Range(i, j uint32) *uint32Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *uint32Builder) Min(i uint32) *uint32Builder { - b.validators = append(b.validators, func(v uint32) error { + b.desc.Validators = append(b.desc.Validators, func(v uint32) error { if v < i { return errors.New("value out of range") } @@ -836,7 +898,7 @@ func (b *uint32Builder) Min(i uint32) *uint32Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *uint32Builder) Max(i uint32) *uint32Builder { - b.validators = append(b.validators, func(v uint32) error { + b.desc.Validators = append(b.desc.Validators, func(v uint32) error { if v > i { return errors.New("value out of range") } @@ -852,14 +914,14 @@ func (b *uint32Builder) Positive() *uint32Builder { // Default sets the default value of the field. func (b *uint32Builder) Default(i uint32) *uint32Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *uint32Builder) Nillable() *uint32Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -871,42 +933,47 @@ func (b *uint32Builder) Comment(c string) *uint32Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *uint32Builder) Optional() *uint32Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *uint32Builder) Immutable() *uint32Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *uint32Builder) StructTag(s string) *uint32Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *uint32Builder) Validate(fn func(uint32) error) *uint32Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *uint32Builder) Descriptor() *Descriptor { + return b.desc +} + // uint64Builder is the builder for uint64 field. type uint64Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *uint64Builder) Unique() *uint64Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *uint64Builder) Range(i, j uint64) *uint64Builder { - b.validators = append(b.validators, func(v uint64) error { + b.desc.Validators = append(b.desc.Validators, func(v uint64) error { if v < i || v > j { return errors.New("value out of range") } @@ -917,7 +984,7 @@ func (b *uint64Builder) Range(i, j uint64) *uint64Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *uint64Builder) Min(i uint64) *uint64Builder { - b.validators = append(b.validators, func(v uint64) error { + b.desc.Validators = append(b.desc.Validators, func(v uint64) error { if v < i { return errors.New("value out of range") } @@ -928,7 +995,7 @@ func (b *uint64Builder) Min(i uint64) *uint64Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *uint64Builder) Max(i uint64) *uint64Builder { - b.validators = append(b.validators, func(v uint64) error { + b.desc.Validators = append(b.desc.Validators, func(v uint64) error { if v > i { return errors.New("value out of range") } @@ -944,14 +1011,14 @@ func (b *uint64Builder) Positive() *uint64Builder { // Default sets the default value of the field. func (b *uint64Builder) Default(i uint64) *uint64Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *uint64Builder) Nillable() *uint64Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -963,42 +1030,47 @@ func (b *uint64Builder) Comment(c string) *uint64Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *uint64Builder) Optional() *uint64Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *uint64Builder) Immutable() *uint64Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *uint64Builder) StructTag(s string) *uint64Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *uint64Builder) Validate(fn func(uint64) error) *uint64Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *uint64Builder) Descriptor() *Descriptor { + return b.desc +} + // float64Builder is the builder for float fields. type float64Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *float64Builder) Unique() *float64Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *float64Builder) Range(i, j float64) *float64Builder { - b.validators = append(b.validators, func(v float64) error { + b.desc.Validators = append(b.desc.Validators, func(v float64) error { if v < i || v > j { return errors.New("value out of range") } @@ -1009,7 +1081,7 @@ func (b *float64Builder) Range(i, j float64) *float64Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *float64Builder) Min(i float64) *float64Builder { - b.validators = append(b.validators, func(v float64) error { + b.desc.Validators = append(b.desc.Validators, func(v float64) error { if v < i { return errors.New("value out of range") } @@ -1020,7 +1092,7 @@ func (b *float64Builder) Min(i float64) *float64Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *float64Builder) Max(i float64) *float64Builder { - b.validators = append(b.validators, func(v float64) error { + b.desc.Validators = append(b.desc.Validators, func(v float64) error { if v > i { return errors.New("value out of range") } @@ -1041,14 +1113,14 @@ func (b *float64Builder) Negative() *float64Builder { // Default sets the default value of the field. func (b *float64Builder) Default(i float64) *float64Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *float64Builder) Nillable() *float64Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -1060,42 +1132,47 @@ func (b *float64Builder) Comment(c string) *float64Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *float64Builder) Optional() *float64Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *float64Builder) Immutable() *float64Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *float64Builder) StructTag(s string) *float64Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *float64Builder) Validate(fn func(float64) error) *float64Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *float64Builder) Descriptor() *Descriptor { + return b.desc +} + // float32Builder is the builder for float fields. type float32Builder struct { - Field + desc *Descriptor } // Unique makes the field unique within all vertices of this type. func (b *float32Builder) Unique() *float32Builder { - b.unique = true + b.desc.Unique = true return b } // Range adds a range validator for this field where the given value needs to be in the range of [i, j]. func (b *float32Builder) Range(i, j float32) *float32Builder { - b.validators = append(b.validators, func(v float32) error { + b.desc.Validators = append(b.desc.Validators, func(v float32) error { if v < i || v > j { return errors.New("value out of range") } @@ -1106,7 +1183,7 @@ func (b *float32Builder) Range(i, j float32) *float32Builder { // Min adds a minimum value validator for this field. Operation fails if the validator fails. func (b *float32Builder) Min(i float32) *float32Builder { - b.validators = append(b.validators, func(v float32) error { + b.desc.Validators = append(b.desc.Validators, func(v float32) error { if v < i { return errors.New("value out of range") } @@ -1117,7 +1194,7 @@ func (b *float32Builder) Min(i float32) *float32Builder { // Max adds a maximum value validator for this field. Operation fails if the validator fails. func (b *float32Builder) Max(i float32) *float32Builder { - b.validators = append(b.validators, func(v float32) error { + b.desc.Validators = append(b.desc.Validators, func(v float32) error { if v > i { return errors.New("value out of range") } @@ -1138,14 +1215,14 @@ func (b *float32Builder) Negative() *float32Builder { // Default sets the default value of the field. func (b *float32Builder) Default(i float32) *float32Builder { - b.value = i + b.desc.Default = i return b } // Nillable indicates that this field is a nillable. // Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field. func (b *float32Builder) Nillable() *float32Builder { - b.nillable = true + b.desc.Nillable = true return b } @@ -1157,24 +1234,29 @@ func (b *float32Builder) Comment(c string) *float32Builder { // Optional indicates that this field is optional on create. // Unlike edges, fields are required by default. func (b *float32Builder) Optional() *float32Builder { - b.optional = true + b.desc.Optional = true return b } // Immutable indicates that this field cannot be updated. func (b *float32Builder) Immutable() *float32Builder { - b.immutable = true + b.desc.Immutable = true return b } // StructTag sets the struct tag of the field. func (b *float32Builder) StructTag(s string) *float32Builder { - b.tag = s + b.desc.Tag = s return b } // Validate adds a validator for this field. Operation fails if the validation fails. func (b *float32Builder) Validate(fn func(float32) error) *float32Builder { - b.validators = append(b.validators, fn) + b.desc.Validators = append(b.desc.Validators, fn) return b } + +// Descriptor implements the ent.Field interface by returning its descriptor. +func (b *float32Builder) Descriptor() *Descriptor { + return b.desc +} diff --git a/schema/index/index.go b/schema/index/index.go index a3a7ad637..5a5270277 100644 --- a/schema/index/index.go +++ b/schema/index/index.go @@ -1,10 +1,15 @@ package index -// Index represents an index on a vertex columns. -type Index struct { - unique bool - edges []string - fields []string +// A Descriptor for index configuration. +type Descriptor struct { + Unique bool // unique index. + Edges []string // edge columns. + Fields []string // field columns. +} + +// Builder for indexes on vertex columns and edges in the graph. +type Builder struct { + desc *Descriptor } // Fields creates an index on the given vertex fields. @@ -23,8 +28,8 @@ type Index struct { // // } // -func Fields(fields ...string) *Index { - return &Index{fields: fields} +func Fields(fields ...string) *Builder { + return &Builder{desc: &Descriptor{Fields: fields}} } // Edges creates an index on the given vertex edge fields. @@ -39,8 +44,8 @@ func Fields(fields ...string) *Index { // // } // -func Edges(edges ...string) *Index { - return &Index{edges: edges} +func Edges(edges ...string) *Builder { + return &Builder{desc: &Descriptor{Edges: edges}} } // Fields sets the fields of the index. @@ -53,9 +58,9 @@ func Edges(edges ...string) *Index { // Unique(), // // } -func (i *Index) Fields(fields ...string) *Index { - i.fields = fields - return i +func (b *Builder) Fields(fields ...string) *Builder { + b.desc.Fields = fields + return b } // FromEdges sets the fields index to be unique under the set of edges (sub-graph). For example: @@ -68,30 +73,20 @@ func (i *Index) Fields(fields ...string) *Index { // Unique(), // } // -func (i *Index) Edges(edges ...string) *Index { - i.edges = edges - return i +func (b *Builder) Edges(edges ...string) *Builder { + b.desc.Edges = edges + return b } // Unique sets the index to be a unique index. // Note that defining a uniqueness on optional fields won't prevent // duplicates if one of the column contains NULL values. -func (i *Index) Unique() *Index { - i.unique = true - return i +func (b *Builder) Unique() *Builder { + b.desc.Unique = true + return b } -// EdgeNames returns the edge names of the given index. -func (i Index) EdgeNames() []string { - return i.edges -} - -// FieldNames returns the field names of the given index. -func (i Index) FieldNames() []string { - return i.fields -} - -// IsUnique indicates if this index is a unique index. -func (i Index) IsUnique() bool { - return i.unique +// Descriptor implements the ent.Descriptor interface. +func (b *Builder) Descriptor() *Descriptor { + return b.desc } diff --git a/schema/index/index_test.go b/schema/index/index_test.go index 397809843..c5ca52739 100644 --- a/schema/index/index_test.go +++ b/schema/index/index_test.go @@ -9,18 +9,24 @@ import ( ) func TestIndex(t *testing.T) { - idx := index.Fields("name", "address") - require.Empty(t, idx.EdgeNames()) - require.False(t, idx.IsUnique()) - require.Equal(t, []string{"name", "address"}, idx.FieldNames()) + idx := index.Fields("name", "address"). + Descriptor() + require.Empty(t, idx.Edges) + require.False(t, idx.Unique) + require.Equal(t, []string{"name", "address"}, idx.Fields) - idx = index.Fields("name", "address").Unique() - require.Empty(t, idx.EdgeNames()) - require.True(t, idx.IsUnique()) - require.Equal(t, []string{"name", "address"}, idx.FieldNames()) + idx = index.Fields("name", "address"). + Unique(). + Descriptor() + require.Empty(t, idx.Edges) + require.True(t, idx.Unique) + require.Equal(t, []string{"name", "address"}, idx.Fields) - idx = index.Fields("name", "address").Edges("parent", "type").Unique() - require.Equal(t, []string{"parent", "type"}, idx.EdgeNames()) - require.True(t, idx.IsUnique()) - require.Equal(t, []string{"name", "address"}, idx.FieldNames()) + idx = index.Fields("name", "address"). + Edges("parent", "type"). + Unique(). + Descriptor() + require.Equal(t, []string{"parent", "type"}, idx.Edges) + require.True(t, idx.Unique) + require.Equal(t, []string{"name", "address"}, idx.Fields) }