snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
13
entc/integration/ent/schema/api.go
Normal file
13
entc/integration/ent/schema/api.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import "entgo.io/ent"
|
||||
|
||||
// Api represents an example schema with wrong usage of acronym (e.g., API).
|
||||
// nolint:stylecheck
|
||||
type Api struct {
|
||||
ent.Schema
|
||||
}
|
||||
22
entc/integration/ent/schema/builder.go
Normal file
22
entc/integration/ent/schema/builder.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import "entgo.io/ent"
|
||||
|
||||
// Builder holds the schema definition for the Builder entity.
|
||||
type Builder struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Builder.
|
||||
func (Builder) Fields() []ent.Field {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Edges of the Builder.
|
||||
func (Builder) Edges() []ent.Edge {
|
||||
return nil
|
||||
}
|
||||
101
entc/integration/ent/schema/card.go
Normal file
101
entc/integration/ent/schema/card.go
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/entc/integration/ent/template"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
type CardMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (CardMixin) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
edge.Annotation{
|
||||
StructTag: `mashraki:"edges"`,
|
||||
},
|
||||
field.Annotation{
|
||||
StructTag: map[string]string{
|
||||
"id": `yaml:"-"`,
|
||||
"number": `json:"-"`,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Card holds the schema definition for the CreditCard entity.
|
||||
type Card struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (Card) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixin.Time{},
|
||||
CardMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
func (Card) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
field.Annotation{
|
||||
StructTag: map[string]string{
|
||||
"id": `json:"-"`,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the Comment.
|
||||
func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Float("balance").
|
||||
Default(0),
|
||||
field.String("number").
|
||||
Immutable().
|
||||
NotEmpty().
|
||||
Annotations(&template.Extension{
|
||||
Type: "string",
|
||||
}),
|
||||
field.String("name").
|
||||
Optional().
|
||||
Comment("Name exactly as written on card.").
|
||||
NotEmpty().
|
||||
Annotations(&template.Extension{
|
||||
Type: "string",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Card.
|
||||
func (Card) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Comment("Owner of the card. O2O inverse edge").
|
||||
Ref("card").
|
||||
Unique(),
|
||||
edge.From("spec", Spec.Type).
|
||||
Ref("card").
|
||||
Annotations(&template.Extension{
|
||||
Type: "int",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Card.
|
||||
func (Card) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
index.Fields("number").
|
||||
Unique(),
|
||||
index.Fields("id", "name", "number"),
|
||||
}
|
||||
}
|
||||
35
entc/integration/ent/schema/comment.go
Normal file
35
entc/integration/ent/schema/comment.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
schemadir "entgo.io/ent/entc/integration/ent/schema/dir"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// Comment holds the schema definition for the Comment entity.
|
||||
type Comment struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Comment.
|
||||
func (Comment) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("unique_int").
|
||||
Unique(),
|
||||
field.Float("unique_float").
|
||||
Unique(),
|
||||
field.Int("nillable_int").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("table").
|
||||
Optional(),
|
||||
field.JSON("dir", schemadir.Dir("")).
|
||||
Optional(),
|
||||
field.String("client").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
13
entc/integration/ent/schema/dir/dir.go
Normal file
13
entc/integration/ent/schema/dir/dir.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
// Package schemadir is used as an example to test cases where
|
||||
// package-name is not equal to the package-path (schemadir <> dir).
|
||||
package schemadir
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Dir http.Dir
|
||||
79
entc/integration/ent/schema/document.go
Normal file
79
entc/integration/ent/schema/document.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// DocPayload is a custom type for blob fields.
|
||||
type DocPayload struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// Document holds the schema definition for the Document entity.
|
||||
type Document struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Document.
|
||||
func (Document) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Unique(),
|
||||
field.Blob("content").
|
||||
Lazy().
|
||||
HashKey(crypto.SHA256).
|
||||
CheckRefs(),
|
||||
field.Blob("thumbnail").
|
||||
Lazy().
|
||||
CheckRefs(),
|
||||
field.Blob("attachment").
|
||||
DualWrite().
|
||||
CheckRefs(),
|
||||
// "metadata" and the fields below deliberately skip CheckRefs, covering the
|
||||
// default: cleanup removes the object without looking for other holders.
|
||||
field.Blob("metadata").
|
||||
Optional(),
|
||||
field.Blob("payload").
|
||||
DualWrite(map[string]string{
|
||||
dialect.MySQL: "longblob",
|
||||
dialect.Postgres: "jsonb",
|
||||
dialect.SQLite: "json",
|
||||
}).
|
||||
GoType(&DocPayload{}).
|
||||
ValueScanner(field.ValueScannerFunc[*DocPayload, *sql.NullString]{
|
||||
V: func(v *DocPayload) (driver.Value, error) {
|
||||
return json.Marshal(v)
|
||||
},
|
||||
S: func(s *sql.NullString) (*DocPayload, error) {
|
||||
if !s.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
var p DocPayload
|
||||
if err := json.Unmarshal([]byte(s.String), &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
},
|
||||
}).
|
||||
Optional(),
|
||||
field.Blob("description").
|
||||
GoType("").
|
||||
Optional(),
|
||||
field.Blob("archive").
|
||||
Lazy().
|
||||
DualWrite().
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
103
entc/integration/ent/schema/exvaluescan.go
Normal file
103
entc/integration/ent/schema/exvaluescan.go
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// ExValueScan holds the schema definition for the ExValueScan entity.
|
||||
type ExValueScan struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the ExValueScan.
|
||||
func (ExValueScan) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("binary").
|
||||
GoType(&url.URL{}).
|
||||
ValueScanner(field.BinaryValueScanner[*url.URL]{}),
|
||||
field.Bytes("binary_bytes").
|
||||
GoType(&url.URL{}).
|
||||
ValueScanner(field.BinaryValueScanner[*url.URL]{}),
|
||||
field.String("binary_optional").
|
||||
Optional().
|
||||
GoType(&url.URL{}).
|
||||
ValueScanner(field.BinaryValueScanner[*url.URL]{}),
|
||||
field.String("text").
|
||||
GoType(&big.Int{}).
|
||||
ValueScanner(field.TextValueScanner[*big.Int]{}),
|
||||
field.String("text_optional").
|
||||
Optional().
|
||||
GoType(&big.Int{}).
|
||||
ValueScanner(field.TextValueScanner[*big.Int]{}),
|
||||
field.String("base64").
|
||||
ValueScanner(field.ValueScannerFunc[string, *sql.NullString]{
|
||||
V: func(s string) (driver.Value, error) {
|
||||
return base64.StdEncoding.EncodeToString([]byte(s)), nil
|
||||
},
|
||||
S: func(ns *sql.NullString) (string, error) {
|
||||
if !ns.Valid {
|
||||
return "", nil
|
||||
}
|
||||
b, err := base64.StdEncoding.DecodeString(ns.String)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
},
|
||||
}),
|
||||
field.String("custom").
|
||||
ValueScanner(PrefixedHex{
|
||||
prefix: "0x",
|
||||
}),
|
||||
field.String("custom_optional").
|
||||
Optional().
|
||||
ValueScanner(PrefixedHex{
|
||||
prefix: "0X",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// PrefixedHex is a custom type that implements the ValueScanner interface.
|
||||
type PrefixedHex struct {
|
||||
prefix string
|
||||
}
|
||||
|
||||
// Value implements the TypeValueScanner.Value method.
|
||||
func (p PrefixedHex) Value(s string) (driver.Value, error) {
|
||||
return p.prefix + ":" + hex.EncodeToString([]byte(s)), nil
|
||||
}
|
||||
|
||||
// ScanValue implements the TypeValueScanner.ScanValue method.
|
||||
func (PrefixedHex) ScanValue() field.ValueScanner {
|
||||
return &sql.NullString{}
|
||||
}
|
||||
|
||||
// FromValue implements the TypeValueScanner.FromValue method.
|
||||
func (p PrefixedHex) FromValue(v driver.Value) (string, error) {
|
||||
s, ok := v.(*sql.NullString)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unexpected input for FromValue: %T", v)
|
||||
}
|
||||
if !s.Valid {
|
||||
return "", nil
|
||||
}
|
||||
d, err := hex.DecodeString(strings.TrimPrefix(s.String, p.prefix+":"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(d), nil
|
||||
}
|
||||
545
entc/integration/ent/schema/fieldtype.go
Normal file
545
entc/integration/ent/schema/fieldtype.go
Normal file
@@ -0,0 +1,545 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql/driver"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/entc/integration/ent/role"
|
||||
"entgo.io/ent/schema/field"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// FieldType holds the schema definition for the FieldType entity.
|
||||
// used for testing field types.
|
||||
type FieldType struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the File.
|
||||
func (FieldType) Fields() []ent.Field { //nolint:funlen
|
||||
return []ent.Field{
|
||||
// ----------------------------------------------------------------------------
|
||||
// Basic types
|
||||
|
||||
field.Int("int"),
|
||||
field.Int8("int8"),
|
||||
field.Int16("int16"),
|
||||
field.Int32("int32"),
|
||||
field.Int64("int64").
|
||||
UpdateDefault(func() int64 {
|
||||
return 100
|
||||
}),
|
||||
field.Int("optional_int").
|
||||
Optional(),
|
||||
field.Int8("optional_int8").
|
||||
Optional(),
|
||||
field.Int16("optional_int16").
|
||||
Optional(),
|
||||
field.Int32("optional_int32").
|
||||
Optional(),
|
||||
field.Int64("optional_int64").
|
||||
Optional(),
|
||||
field.Int("nillable_int").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int8("nillable_int8").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int16("nillable_int16").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int32("nillable_int32").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int64("nillable_int64").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Int32("validate_optional_int32").
|
||||
Optional().
|
||||
Max(100),
|
||||
field.Uint("optional_uint").
|
||||
Optional(),
|
||||
field.Uint8("optional_uint8").
|
||||
Optional(),
|
||||
field.Uint16("optional_uint16").
|
||||
Optional(),
|
||||
field.Uint32("optional_uint32").
|
||||
Optional(),
|
||||
field.Uint64("optional_uint64").
|
||||
Optional(),
|
||||
field.Enum("state").
|
||||
Values("on", "off").
|
||||
Optional(),
|
||||
field.Float("optional_float").
|
||||
Optional(),
|
||||
field.Float32("optional_float32").
|
||||
Optional(),
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Dialect-specific types
|
||||
|
||||
field.Text("text").
|
||||
Optional().
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "mediumtext",
|
||||
}),
|
||||
field.Time("datetime").
|
||||
Optional().
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "datetime",
|
||||
dialect.Postgres: "date",
|
||||
}),
|
||||
field.Float("decimal").
|
||||
Optional().
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "decimal(6,2)",
|
||||
dialect.Postgres: "numeric",
|
||||
}),
|
||||
field.Other("link_other", &Link{}).
|
||||
SchemaType(map[string]string{
|
||||
dialect.Postgres: "varchar",
|
||||
dialect.MySQL: "varchar(255)",
|
||||
dialect.SQLite: "varchar(255)",
|
||||
}).
|
||||
Optional().
|
||||
Default(DefaultLink()),
|
||||
field.Other("link_other_func", &Link{}).
|
||||
SchemaType(map[string]string{
|
||||
dialect.Postgres: "varchar",
|
||||
dialect.MySQL: "varchar(255)",
|
||||
dialect.SQLite: "varchar(255)",
|
||||
}).
|
||||
Optional().
|
||||
Default(DefaultLink),
|
||||
field.String("mac").
|
||||
Optional().
|
||||
GoType(MAC{}).
|
||||
SchemaType(map[string]string{
|
||||
dialect.Postgres: "macaddr",
|
||||
}).
|
||||
Validate(func(s string) error {
|
||||
_, err := net.ParseMAC(s)
|
||||
return err
|
||||
}),
|
||||
field.Other("string_array", Strings{}).
|
||||
Optional().
|
||||
SchemaType(map[string]string{
|
||||
dialect.Postgres: "text[]",
|
||||
dialect.SQLite: "json",
|
||||
dialect.MySQL: "blob",
|
||||
}),
|
||||
field.String("password").
|
||||
Optional().
|
||||
Sensitive().
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "char(32)",
|
||||
}),
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Custom Go types
|
||||
|
||||
field.String("string_scanner").
|
||||
GoType(StringScanner("")).
|
||||
Nillable().
|
||||
Optional(),
|
||||
field.Int64("duration").
|
||||
GoType(time.Duration(0)).
|
||||
UpdateDefault(func() time.Duration {
|
||||
return time.Duration(100)
|
||||
}).
|
||||
Optional(),
|
||||
field.String("dir").
|
||||
GoType(http.Dir("dir")).
|
||||
DefaultFunc(func() http.Dir {
|
||||
return "unknown"
|
||||
}),
|
||||
field.String("ndir").
|
||||
Optional().
|
||||
Nillable().
|
||||
NotEmpty().
|
||||
GoType(http.Dir("ndir")),
|
||||
field.String("str").
|
||||
Optional().
|
||||
GoType(sql.NullString{}).
|
||||
DefaultFunc(func() sql.NullString {
|
||||
return sql.NullString{String: "default", Valid: true}
|
||||
}),
|
||||
field.String("null_str").
|
||||
Optional().
|
||||
Nillable().
|
||||
GoType(&sql.NullString{}).
|
||||
DefaultFunc(func() *sql.NullString {
|
||||
return &sql.NullString{String: "default", Valid: true}
|
||||
}),
|
||||
field.String("link").
|
||||
Optional().
|
||||
NotEmpty().
|
||||
GoType(Link{}),
|
||||
field.String("null_link").
|
||||
Optional().
|
||||
Nillable().
|
||||
GoType(&Link{}),
|
||||
field.Bool("active").
|
||||
Optional().
|
||||
GoType(Status(false)),
|
||||
field.Bool("null_active").
|
||||
Optional().
|
||||
Nillable().
|
||||
GoType(Status(false)),
|
||||
field.Bool("deleted").
|
||||
Optional().
|
||||
Nillable().
|
||||
GoType(&sql.NullBool{}),
|
||||
field.Time("deleted_at").
|
||||
Optional().
|
||||
GoType(&sql.NullTime{}).
|
||||
Default(func() *sql.NullTime {
|
||||
return &sql.NullTime{Time: time.Now(), Valid: true}
|
||||
}).
|
||||
UpdateDefault(func() *sql.NullTime {
|
||||
return &sql.NullTime{Time: time.Now(), Valid: true}
|
||||
}),
|
||||
field.Bytes("raw_data").
|
||||
Optional().
|
||||
MaxLen(20).
|
||||
MinLen(3),
|
||||
field.Bytes("sensitive").
|
||||
Optional().
|
||||
Sensitive(),
|
||||
field.Bytes("ip").
|
||||
Optional().
|
||||
GoType(net.IP("127.0.0.1")).
|
||||
DefaultFunc(func() net.IP {
|
||||
return net.IP("127.0.0.1")
|
||||
}).
|
||||
Validate(func(i []byte) error {
|
||||
if net.ParseIP(string(i)) == nil {
|
||||
return fmt.Errorf("ent/schema: invalid ip %q", string(i))
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
field.Int("null_int64").
|
||||
Optional().
|
||||
GoType(&sql.NullInt64{}),
|
||||
field.Int("schema_int").
|
||||
Optional().
|
||||
GoType(Int(0)),
|
||||
field.Int8("schema_int8").
|
||||
Optional().
|
||||
GoType(Int8(0)),
|
||||
field.Int64("schema_int64").
|
||||
Optional().
|
||||
GoType(Int64(0)),
|
||||
field.Float("schema_float").
|
||||
Optional().
|
||||
GoType(Float64(0)),
|
||||
field.Float32("schema_float32").
|
||||
Optional().
|
||||
GoType(Float32(0)),
|
||||
field.Float("null_float").
|
||||
Optional().
|
||||
GoType(&sql.NullFloat64{}),
|
||||
field.Enum("role").
|
||||
Default(string(role.Read)).
|
||||
GoType(role.Role("role")),
|
||||
field.Enum("priority").
|
||||
Optional().
|
||||
GoType(role.Priority(0)),
|
||||
field.UUID("optional_uuid", uuid.UUID{}).
|
||||
Optional(),
|
||||
field.UUID("nillable_uuid", uuid.UUID{}).
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.Strings("strings").
|
||||
Optional(),
|
||||
field.Bytes("pair").
|
||||
GoType(Pair{}).
|
||||
DefaultFunc(func() Pair {
|
||||
return Pair{K: []byte("K"), V: []byte("V")}
|
||||
}),
|
||||
field.Bytes("nil_pair").
|
||||
GoType(&Pair{}).
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("vstring").
|
||||
GoType(VString("")).
|
||||
DefaultFunc(func() VString {
|
||||
return "value scanner string"
|
||||
}),
|
||||
field.String("triple").
|
||||
GoType(Triple{}).
|
||||
DefaultFunc(func() Triple {
|
||||
return Triple{E: [3]string{"A", "B", "C"}}
|
||||
}),
|
||||
field.Int("big_int").
|
||||
Optional().
|
||||
GoType(BigInt{}),
|
||||
field.Other("password_other", Password("")).
|
||||
Optional().
|
||||
Sensitive().
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "char(32)",
|
||||
dialect.SQLite: "char(32)",
|
||||
dialect.Postgres: "varchar",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
type Password string
|
||||
|
||||
func (p Password) Value() (driver.Value, error) {
|
||||
return string(p), nil
|
||||
}
|
||||
|
||||
func (p *Password) Scan(src any) error {
|
||||
switch src := src.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case string:
|
||||
*p = Password(src)
|
||||
return nil
|
||||
case []byte:
|
||||
*p = Password(src)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("scan: unable to scan type %T into string", src)
|
||||
}
|
||||
}
|
||||
|
||||
type Strings []string
|
||||
|
||||
func (s *Strings) Scan(v any) (err error) {
|
||||
switch v := v.(type) {
|
||||
case nil:
|
||||
case []byte:
|
||||
err = s.scan(string(v))
|
||||
case string:
|
||||
err = s.scan(v)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Strings) scan(v string) error {
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
if l := len(v); l < 2 || v[0] != '{' && v[l-1] != '}' {
|
||||
return fmt.Errorf("unexpected array format %q", v)
|
||||
}
|
||||
*s = strings.Split(v[1:len(v)-1], ",")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Strings) Value() (driver.Value, error) {
|
||||
return "{" + strings.Join(s, ",") + "}", nil
|
||||
}
|
||||
|
||||
type VString string
|
||||
|
||||
func (s *VString) Scan(v any) (err error) {
|
||||
switch v := v.(type) {
|
||||
case nil:
|
||||
case string:
|
||||
*s = VString(v)
|
||||
case []byte:
|
||||
*s = VString(v)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s VString) Value() (driver.Value, error) {
|
||||
return string(s), nil
|
||||
}
|
||||
|
||||
type Triple struct {
|
||||
E [3]string
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (t Triple) Value() (driver.Value, error) {
|
||||
return fmt.Sprintf("(%s,%s,%s)", t.E[0], t.E[1], t.E[2]), nil
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (t *Triple) Scan(value any) (err error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
case []byte:
|
||||
es := strings.Split(strings.TrimPrefix(string(v), "()"), ",")
|
||||
t.E[0], t.E[1], t.E[2] = es[0], es[1], es[2]
|
||||
case string:
|
||||
es := strings.Split(strings.TrimPrefix(v, "()"), ",")
|
||||
t.E[0], t.E[1], t.E[2] = es[0], es[1], es[2]
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Pair struct {
|
||||
K, V []byte
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (p Pair) Value() (driver.Value, error) {
|
||||
var b bytes.Buffer
|
||||
if err := gob.NewEncoder(&b).Encode(p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (p *Pair) Scan(value any) (err error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
case []byte:
|
||||
err = gob.NewDecoder(bytes.NewBuffer(v)).Decode(p)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type (
|
||||
Int int
|
||||
Int8 int8
|
||||
Int64 int64
|
||||
Status bool
|
||||
Float64 float64
|
||||
Float32 float32
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
*url.URL
|
||||
}
|
||||
|
||||
func DefaultLink() *Link {
|
||||
u, _ := url.Parse("127.0.0.1")
|
||||
return &Link{URL: u}
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (l *Link) Scan(value any) (err error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
case []byte:
|
||||
l.URL, err = url.Parse(string(v))
|
||||
case string:
|
||||
l.URL, err = url.Parse(v)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (l Link) Value() (driver.Value, error) {
|
||||
if l.URL == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return l.String(), nil
|
||||
}
|
||||
|
||||
type MAC struct {
|
||||
net.HardwareAddr
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (m *MAC) Scan(value any) (err error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
case []byte:
|
||||
m.HardwareAddr, err = net.ParseMAC(string(v))
|
||||
case string:
|
||||
m.HardwareAddr, err = net.ParseMAC(v)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (m MAC) Value() (driver.Value, error) {
|
||||
return m.HardwareAddr.String(), nil
|
||||
}
|
||||
|
||||
type StringScanner string
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (s *StringScanner) Scan(value any) (err error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
case string:
|
||||
*s = StringScanner(v)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected type %T", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (s StringScanner) Value() (driver.Value, error) {
|
||||
return string(s), nil
|
||||
}
|
||||
|
||||
type BigInt struct {
|
||||
*big.Int
|
||||
}
|
||||
|
||||
func NewBigInt(i int64) BigInt {
|
||||
return BigInt{Int: big.NewInt(i)}
|
||||
}
|
||||
|
||||
func (b *BigInt) Scan(src any) error {
|
||||
var i sql.NullString
|
||||
if err := i.Scan(src); err != nil {
|
||||
return err
|
||||
}
|
||||
if !i.Valid {
|
||||
return nil
|
||||
}
|
||||
if b.Int == nil {
|
||||
b.Int = big.NewInt(0)
|
||||
}
|
||||
// Value came in a floating point format.
|
||||
if strings.ContainsAny(i.String, ".+e") {
|
||||
f := big.NewFloat(0)
|
||||
if _, err := fmt.Sscan(i.String, f); err != nil {
|
||||
return err
|
||||
}
|
||||
b.Int, _ = f.Int(b.Int)
|
||||
} else if _, err := fmt.Sscan(i.String, b.Int); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b BigInt) Value() (driver.Value, error) {
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
func (b BigInt) Add(c BigInt) BigInt {
|
||||
b.Int = b.Int.Add(b.Int, c.Int)
|
||||
return b
|
||||
}
|
||||
90
entc/integration/ent/schema/file.go
Normal file
90
entc/integration/ent/schema/file.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
)
|
||||
|
||||
// File holds the schema definition for the File entity.
|
||||
type File struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (File) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
edge.Annotation{
|
||||
StructTag: `json:"file_edges"`,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the File.
|
||||
func (File) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("set_id").
|
||||
Max(10).
|
||||
Optional(),
|
||||
field.Int("size").
|
||||
StorageKey("fsize").
|
||||
Default(math.MaxInt32).
|
||||
Positive(),
|
||||
field.String("name"),
|
||||
field.String("user").
|
||||
Optional().
|
||||
Nillable(),
|
||||
field.String("group").
|
||||
Optional(),
|
||||
field.Bool("op").
|
||||
Optional(),
|
||||
// Skip generating the "FieldID" predicate
|
||||
// as it conflicts with the "FieldID" constant.
|
||||
field.Int("field_id").
|
||||
Optional(),
|
||||
field.Time("create_time").
|
||||
Optional().
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the File.
|
||||
func (File) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Ref("files").
|
||||
Unique(),
|
||||
edge.From("type", FileType.Type).
|
||||
Ref("files").
|
||||
Unique(),
|
||||
edge.To("field", FieldType.Type),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of a file.
|
||||
func (File) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
// non-unique index should not prevent duplicates.
|
||||
index.Fields("name", "size").
|
||||
StorageKey("file_name_size"),
|
||||
// unique index prevents duplicates records.
|
||||
index.Fields("name", "user").
|
||||
Unique(),
|
||||
// index on edges only.
|
||||
index.Edges("owner", "type"),
|
||||
// unique index under the "owner" sub-tree.
|
||||
// user/owner can't have files with duplicate names.
|
||||
index.Fields("name").
|
||||
Edges("owner", "type").
|
||||
Unique(),
|
||||
index.Fields("name").
|
||||
Edges("owner"),
|
||||
}
|
||||
}
|
||||
44
entc/integration/ent/schema/filetype.go
Normal file
44
entc/integration/ent/schema/filetype.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// FileType holds the schema definition for the FileType entity.
|
||||
type FileType struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the FileType.
|
||||
func (FileType) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Unique(),
|
||||
field.Enum("type").
|
||||
NamedValues(
|
||||
"PNG", "png",
|
||||
"SVG", "svg",
|
||||
"JPG", "jpg",
|
||||
).
|
||||
Default("png"),
|
||||
field.Enum("state").
|
||||
NamedValues(
|
||||
"On", "ON",
|
||||
"Off", "OFF",
|
||||
).
|
||||
Default("ON"),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the FileType.
|
||||
func (FileType) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("files", File.Type),
|
||||
}
|
||||
}
|
||||
22
entc/integration/ent/schema/goods.go
Normal file
22
entc/integration/ent/schema/goods.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import "entgo.io/ent"
|
||||
|
||||
// Goods holds the schema definition for the Goods entity.
|
||||
type Goods struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Goods.
|
||||
func (Goods) Fields() []ent.Field {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Edges of the Goods.
|
||||
func (Goods) Edges() []ent.Edge {
|
||||
return nil
|
||||
}
|
||||
65
entc/integration/ent/schema/group.go
Normal file
65
entc/integration/ent/schema/group.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// CheckError is returned by the validators.
|
||||
type CheckError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (c CheckError) Error() string {
|
||||
return c.msg
|
||||
}
|
||||
|
||||
// Group holds the schema for the group entity.
|
||||
type Group struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the group.
|
||||
func (Group) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Bool("active").
|
||||
Default(true),
|
||||
field.Time("expire"),
|
||||
field.String("type").
|
||||
Optional().
|
||||
Nillable().
|
||||
MinLen(3).
|
||||
MaxLen(255),
|
||||
field.Int("max_users").
|
||||
Optional().
|
||||
Positive().
|
||||
Default(10),
|
||||
field.String("name").
|
||||
Comment("Name field with multiple validators").
|
||||
Match(regexp.MustCompile("[a-zA-Z_]+$")).
|
||||
Validate(func(s string) error {
|
||||
if strings.ToLower(s) == s {
|
||||
return CheckError{msg: "last name must begin with uppercase"}
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the group.
|
||||
func (Group) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("files", File.Type),
|
||||
edge.To("blocked", User.Type),
|
||||
edge.From("users", User.Type).Ref("groups"),
|
||||
edge.To("info", GroupInfo.Type).Unique().Required(),
|
||||
}
|
||||
}
|
||||
33
entc/integration/ent/schema/groupinfo.go
Normal file
33
entc/integration/ent/schema/groupinfo.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// GroupInfo holds the schema for the group-info entity.
|
||||
type GroupInfo struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the group.
|
||||
func (GroupInfo) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("desc"),
|
||||
field.Int("max_users").
|
||||
Default(1e4),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the group.
|
||||
func (GroupInfo) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("groups", Group.Type).
|
||||
Ref("info"),
|
||||
}
|
||||
}
|
||||
30
entc/integration/ent/schema/item.go
Normal file
30
entc/integration/ent/schema/item.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Item holds the schema definition for the Item entity.
|
||||
type Item struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Item.
|
||||
func (Item) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
DefaultFunc(uuid.NewString).
|
||||
MaxLen(64),
|
||||
field.String("text").
|
||||
MaxLen(128).
|
||||
Unique().
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
35
entc/integration/ent/schema/license.go
Normal file
35
entc/integration/ent/schema/license.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
|
||||
"ariga.io/atlas/sql/postgres"
|
||||
)
|
||||
|
||||
// License holds the schema definition for the License entity.
|
||||
type License struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (License) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixin.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the License.
|
||||
func (License) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("id").
|
||||
SchemaType(map[string]string{
|
||||
dialect.Postgres: postgres.TypeBigSerial,
|
||||
}),
|
||||
}
|
||||
}
|
||||
42
entc/integration/ent/schema/node.go
Normal file
42
entc/integration/ent/schema/node.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// Node holds the schema definition for the linked-list Node entity.
|
||||
type Node struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Node.
|
||||
func (Node) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("value").
|
||||
Optional(),
|
||||
field.Time("updated_at").
|
||||
Nillable().
|
||||
Optional().
|
||||
UpdateDefault(time.Now),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Node.
|
||||
func (Node) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("next", Node.Type).
|
||||
StructTag(`gqlgen:"next"`).
|
||||
Unique().
|
||||
From("prev").
|
||||
StructTag(`gqlgen:"prev"`).
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
18
entc/integration/ent/schema/pc.go
Normal file
18
entc/integration/ent/schema/pc.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package schema
|
||||
|
||||
import "entgo.io/ent"
|
||||
|
||||
// PC holds the schema definition for the PC entity.
|
||||
type PC struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the PC.
|
||||
func (PC) Fields() []ent.Field {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Edges of the PC.
|
||||
func (PC) Edges() []ent.Edge {
|
||||
return nil
|
||||
}
|
||||
66
entc/integration/ent/schema/pet.go
Normal file
66
entc/integration/ent/schema/pet.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Pet holds the schema definition for the Pet entity.
|
||||
type Pet struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations of the Pet.
|
||||
func (Pet) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "pet"},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the Pet.
|
||||
func (Pet) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Float("age").
|
||||
Default(0),
|
||||
field.String("name"),
|
||||
field.UUID("uuid", uuid.UUID{}).
|
||||
Optional(),
|
||||
field.String("nickname").
|
||||
Optional(),
|
||||
field.Bool("trained").
|
||||
Default(false),
|
||||
field.Time("optional_time").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Dog.
|
||||
func (Pet) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("team", User.Type).
|
||||
Ref("team").
|
||||
Unique(),
|
||||
edge.From("owner", User.Type).
|
||||
Ref("pets").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
func (Pet) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("name").
|
||||
Edges("owner"),
|
||||
index.Fields("nickname").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
21
entc/integration/ent/schema/spec.go
Normal file
21
entc/integration/ent/schema/spec.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
)
|
||||
|
||||
type Spec struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Edges of the Spec.
|
||||
func (Spec) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("card", Card.Type),
|
||||
}
|
||||
}
|
||||
55
entc/integration/ent/schema/task.go
Normal file
55
entc/integration/ent/schema/task.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
|
||||
"entgo.io/ent/entc/integration/ent/schema/task"
|
||||
)
|
||||
|
||||
// Task holds the schema definition for the Task entity.
|
||||
type Task struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Fields of the Task.
|
||||
func (Task) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("priority").
|
||||
GoType(task.Priority(0)).
|
||||
Default(int(task.PriorityMid)),
|
||||
field.JSON("priorities", map[string]task.Priority{}).
|
||||
Optional(),
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
Immutable().
|
||||
Nillable(),
|
||||
field.String("name").
|
||||
Optional().
|
||||
Deprecated(),
|
||||
field.String("owner").
|
||||
Optional(),
|
||||
field.Int("order").
|
||||
Optional(),
|
||||
field.Int("order_option").
|
||||
Optional(),
|
||||
field.String("op").
|
||||
MaxLen(45).
|
||||
Default(""),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the Task.
|
||||
func (Task) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("name", "owner").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
37
entc/integration/ent/schema/task/priority.go
Normal file
37
entc/integration/ent/schema/task/priority.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package task
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Priority int
|
||||
|
||||
const (
|
||||
PriorityLow Priority = iota
|
||||
PriorityMid
|
||||
PriorityHigh
|
||||
)
|
||||
|
||||
func (p Priority) String() string {
|
||||
s := "unknown"
|
||||
switch p {
|
||||
case PriorityLow:
|
||||
s = "low"
|
||||
case PriorityMid:
|
||||
s = "mid"
|
||||
case PriorityHigh:
|
||||
s = "high"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (p Priority) Validate() error {
|
||||
switch p {
|
||||
case PriorityLow, PriorityMid, PriorityHigh:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid priority value: %v", p)
|
||||
}
|
||||
}
|
||||
97
entc/integration/ent/schema/user.go
Normal file
97
entc/integration/ent/schema/user.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// User holds the schema for the user entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (User) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
UserMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the user.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("age"),
|
||||
field.String("name").
|
||||
StructTag(`json:"first_name" graphql:"first_name"`),
|
||||
field.String("last").
|
||||
Default("unknown").
|
||||
StructTag(`graphql:"last_name"`),
|
||||
field.String("nickname").
|
||||
Optional().
|
||||
Unique(),
|
||||
field.String("address").
|
||||
Optional().
|
||||
DefaultFunc(func() string { return "static" }),
|
||||
field.String("phone").
|
||||
Optional().
|
||||
Unique(),
|
||||
field.String("password").
|
||||
Optional().
|
||||
Sensitive(),
|
||||
field.Enum("role").
|
||||
Values("user", "admin", "free-user", "test user").
|
||||
Default("user"),
|
||||
field.Enum("employment").
|
||||
Values("Full-Time", "Part-Time", "Contract").
|
||||
Default("Full-Time"),
|
||||
field.String("SSOCert").
|
||||
Optional(),
|
||||
// Some users store the associations
|
||||
// count as a separate field.
|
||||
field.Int("files_count").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the user.
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("card", Card.Type).Comment("Cards associated with this user. O2O edge").Unique(),
|
||||
edge.To("pets", Pet.Type),
|
||||
edge.To("files", File.Type),
|
||||
edge.To("groups", Group.Type),
|
||||
edge.To("friends", User.Type),
|
||||
edge.To("following", User.Type).From("followers"),
|
||||
edge.To("team", Pet.Type).Unique(),
|
||||
edge.To("spouse", User.Type).Unique(),
|
||||
edge.To("parent", User.Type).Unique().From("children"),
|
||||
}
|
||||
}
|
||||
|
||||
// Annotations of the User.
|
||||
func (User) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.IncrementStart(2 << 32),
|
||||
}
|
||||
}
|
||||
|
||||
// UserMixin composes create/update time mixin.
|
||||
type UserMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
// Fields of the time mixin.
|
||||
func (UserMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("optional_int").
|
||||
Optional().
|
||||
Positive(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user