snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-15 22:12:36 +05:00
commit 4b3ad0b611
2680 changed files with 719472 additions and 0 deletions

127
schema/mixin/mixin.go Normal file
View File

@@ -0,0 +1,127 @@
// 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 mixin
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
)
// Schema is the default implementation for the ent.Mixin interface.
// It should be embedded in end-user mixin as follows:
//
// type M struct {
// mixin.Schema
// }
type Schema struct{}
// Fields of the mixin.
func (Schema) Fields() []ent.Field { return nil }
// Edges of the mixin.
func (Schema) Edges() []ent.Edge { return nil }
// Indexes of the mixin.
func (Schema) Indexes() []ent.Index { return nil }
// Hooks of the mixin.
func (Schema) Hooks() []ent.Hook { return nil }
// Interceptors of the schema.
func (Schema) Interceptors() []ent.Interceptor { return nil }
// Policy of the mixin.
func (Schema) Policy() ent.Policy { return nil }
// Annotations of the mixin.
func (Schema) Annotations() []schema.Annotation { return nil }
// schema mixin must implement `Mixin` interface.
var _ ent.Mixin = (*Schema)(nil)
// CreateTime adds created at time field.
type CreateTime struct{ Schema }
// Fields of the create time mixin.
func (CreateTime) Fields() []ent.Field {
return []ent.Field{
field.Time("create_time").
Default(time.Now).
Immutable(),
}
}
// create time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*CreateTime)(nil)
// UpdateTime adds updated at time field.
type UpdateTime struct{ Schema }
// Fields of the update time mixin.
func (UpdateTime) Fields() []ent.Field {
return []ent.Field{
field.Time("update_time").
Default(time.Now).
UpdateDefault(time.Now),
}
}
// update time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*UpdateTime)(nil)
// Time composes create/update time mixin.
type Time struct{ Schema }
// Fields of the time mixin.
func (Time) Fields() []ent.Field {
return append(
CreateTime{}.Fields(),
UpdateTime{}.Fields()...,
)
}
// time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*Time)(nil)
// AnnotateFields adds field annotations to underlying mixin fields.
func AnnotateFields(m ent.Mixin, annotations ...schema.Annotation) ent.Mixin {
return fieldAnnotator{Mixin: m, annotations: annotations}
}
// AnnotateEdges adds edge annotations to underlying mixin edges.
func AnnotateEdges(m ent.Mixin, annotations ...schema.Annotation) ent.Mixin {
return edgeAnnotator{Mixin: m, annotations: annotations}
}
type fieldAnnotator struct {
ent.Mixin
annotations []schema.Annotation
}
func (a fieldAnnotator) Fields() []ent.Field {
fields := a.Mixin.Fields()
for i := range fields {
desc := fields[i].Descriptor()
desc.Annotations = append(desc.Annotations, a.annotations...)
}
return fields
}
type edgeAnnotator struct {
ent.Mixin
annotations []schema.Annotation
}
func (a edgeAnnotator) Edges() []ent.Edge {
edges := a.Mixin.Edges()
for i := range edges {
desc := edges[i].Descriptor()
desc.Annotations = append(desc.Annotations, a.annotations...)
}
return edges
}

100
schema/mixin/mixin_test.go Normal file
View File

@@ -0,0 +1,100 @@
// 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 mixin_test
import (
"testing"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/mixin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTimeMixin(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
fields := mixin.CreateTime{}.Fields()
require.Len(t, fields, 1)
desc := fields[0].Descriptor()
assert.Equal(t, "create_time", desc.Name)
assert.True(t, desc.Immutable)
assert.NotNil(t, desc.Default)
assert.Nil(t, desc.UpdateDefault)
})
t.Run("Update", func(t *testing.T) {
t.Parallel()
fields := mixin.UpdateTime{}.Fields()
require.Len(t, fields, 1)
desc := fields[0].Descriptor()
assert.Equal(t, "update_time", desc.Name)
assert.NotNil(t, desc.Default)
assert.NotNil(t, desc.UpdateDefault)
})
t.Run("Compose", func(t *testing.T) {
t.Parallel()
fields := mixin.Time{}.Fields()
require.Len(t, fields, 2)
assert.Equal(t, "create_time", fields[0].Descriptor().Name)
assert.Equal(t, "update_time", fields[1].Descriptor().Name)
})
}
type annotation string
func (annotation) Name() string { return "" }
func TestAnnotateFields(t *testing.T) {
annotations := []schema.Annotation{
annotation("foo"),
annotation("bar"),
annotation("baz"),
}
fields := mixin.AnnotateFields(
mixin.Time{}, annotations...,
).Fields()
require.Len(t, fields, 2)
for _, f := range fields {
desc := f.Descriptor()
require.Len(t, desc.Annotations, len(annotations))
for i := range desc.Annotations {
assert.Equal(t, annotations[i], desc.Annotations[i])
}
}
}
type TestSchema struct {
ent.Schema
}
func (TestSchema) Edges() []ent.Edge {
return []ent.Edge{
edge.To("one", TestSchema.Type),
edge.From("two", TestSchema.Type).
Ref("one"),
}
}
func TestAnnotateEdges(t *testing.T) {
annotations := []schema.Annotation{
annotation("foo"),
annotation("bar"),
annotation("baz"),
}
edges := mixin.AnnotateEdges(
TestSchema{}, annotations...,
).Edges()
require.Len(t, edges, 2)
for _, e := range edges {
desc := e.Descriptor()
require.Len(t, desc.Annotations, len(annotations))
for i := range desc.Annotations {
assert.Equal(t, annotations[i], desc.Annotations[i])
}
}
}