🚧 Refactor basic services
This commit is contained in:
240
internal/models/ent/intercept/intercept.go
Normal file
240
internal/models/ent/intercept/intercept.go
Normal file
@@ -0,0 +1,240 @@
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
|
||||
package intercept
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"voidraft/internal/models/ent"
|
||||
"voidraft/internal/models/ent/document"
|
||||
"voidraft/internal/models/ent/extension"
|
||||
"voidraft/internal/models/ent/keybinding"
|
||||
"voidraft/internal/models/ent/predicate"
|
||||
"voidraft/internal/models/ent/theme"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
// The Query interface represents an operation that queries a graph.
|
||||
// By using this interface, users can write generic code that manipulates
|
||||
// query builders of different types.
|
||||
type Query interface {
|
||||
// Type returns the string representation of the query type.
|
||||
Type() string
|
||||
// Limit the number of records to be returned by this query.
|
||||
Limit(int)
|
||||
// Offset to start from.
|
||||
Offset(int)
|
||||
// Unique configures the query builder to filter duplicate records.
|
||||
Unique(bool)
|
||||
// Order specifies how the records should be ordered.
|
||||
Order(...func(*sql.Selector))
|
||||
// WhereP appends storage-level predicates to the query builder. Using this method, users
|
||||
// can use type-assertion to append predicates that do not depend on any generated package.
|
||||
WhereP(...func(*sql.Selector))
|
||||
}
|
||||
|
||||
// The Func type is an adapter that allows ordinary functions to be used as interceptors.
|
||||
// Unlike traversal functions, interceptors are skipped during graph traversals. Note that the
|
||||
// implementation of Func is different from the one defined in entgo.io/ent.InterceptFunc.
|
||||
type Func func(context.Context, Query) error
|
||||
|
||||
// Intercept calls f(ctx, q) and then applied the next Querier.
|
||||
func (f Func) Intercept(next ent.Querier) ent.Querier {
|
||||
return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
query, err := NewQuery(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := f(ctx, query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return next.Query(ctx, q)
|
||||
})
|
||||
}
|
||||
|
||||
// The TraverseFunc type is an adapter to allow the use of ordinary function as Traverser.
|
||||
// If f is a function with the appropriate signature, TraverseFunc(f) is a Traverser that calls f.
|
||||
type TraverseFunc func(context.Context, Query) error
|
||||
|
||||
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
|
||||
func (f TraverseFunc) Intercept(next ent.Querier) ent.Querier {
|
||||
return next
|
||||
}
|
||||
|
||||
// Traverse calls f(ctx, q).
|
||||
func (f TraverseFunc) Traverse(ctx context.Context, q ent.Query) error {
|
||||
query, err := NewQuery(q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f(ctx, query)
|
||||
}
|
||||
|
||||
// The DocumentFunc type is an adapter to allow the use of ordinary function as a Querier.
|
||||
type DocumentFunc func(context.Context, *ent.DocumentQuery) (ent.Value, error)
|
||||
|
||||
// Query calls f(ctx, q).
|
||||
func (f DocumentFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
if q, ok := q.(*ent.DocumentQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected query type %T. expect *ent.DocumentQuery", q)
|
||||
}
|
||||
|
||||
// The TraverseDocument type is an adapter to allow the use of ordinary function as Traverser.
|
||||
type TraverseDocument func(context.Context, *ent.DocumentQuery) error
|
||||
|
||||
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
|
||||
func (f TraverseDocument) Intercept(next ent.Querier) ent.Querier {
|
||||
return next
|
||||
}
|
||||
|
||||
// Traverse calls f(ctx, q).
|
||||
func (f TraverseDocument) Traverse(ctx context.Context, q ent.Query) error {
|
||||
if q, ok := q.(*ent.DocumentQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return fmt.Errorf("unexpected query type %T. expect *ent.DocumentQuery", q)
|
||||
}
|
||||
|
||||
// The ExtensionFunc type is an adapter to allow the use of ordinary function as a Querier.
|
||||
type ExtensionFunc func(context.Context, *ent.ExtensionQuery) (ent.Value, error)
|
||||
|
||||
// Query calls f(ctx, q).
|
||||
func (f ExtensionFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
if q, ok := q.(*ent.ExtensionQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected query type %T. expect *ent.ExtensionQuery", q)
|
||||
}
|
||||
|
||||
// The TraverseExtension type is an adapter to allow the use of ordinary function as Traverser.
|
||||
type TraverseExtension func(context.Context, *ent.ExtensionQuery) error
|
||||
|
||||
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
|
||||
func (f TraverseExtension) Intercept(next ent.Querier) ent.Querier {
|
||||
return next
|
||||
}
|
||||
|
||||
// Traverse calls f(ctx, q).
|
||||
func (f TraverseExtension) Traverse(ctx context.Context, q ent.Query) error {
|
||||
if q, ok := q.(*ent.ExtensionQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return fmt.Errorf("unexpected query type %T. expect *ent.ExtensionQuery", q)
|
||||
}
|
||||
|
||||
// The KeyBindingFunc type is an adapter to allow the use of ordinary function as a Querier.
|
||||
type KeyBindingFunc func(context.Context, *ent.KeyBindingQuery) (ent.Value, error)
|
||||
|
||||
// Query calls f(ctx, q).
|
||||
func (f KeyBindingFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
if q, ok := q.(*ent.KeyBindingQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected query type %T. expect *ent.KeyBindingQuery", q)
|
||||
}
|
||||
|
||||
// The TraverseKeyBinding type is an adapter to allow the use of ordinary function as Traverser.
|
||||
type TraverseKeyBinding func(context.Context, *ent.KeyBindingQuery) error
|
||||
|
||||
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
|
||||
func (f TraverseKeyBinding) Intercept(next ent.Querier) ent.Querier {
|
||||
return next
|
||||
}
|
||||
|
||||
// Traverse calls f(ctx, q).
|
||||
func (f TraverseKeyBinding) Traverse(ctx context.Context, q ent.Query) error {
|
||||
if q, ok := q.(*ent.KeyBindingQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return fmt.Errorf("unexpected query type %T. expect *ent.KeyBindingQuery", q)
|
||||
}
|
||||
|
||||
// The ThemeFunc type is an adapter to allow the use of ordinary function as a Querier.
|
||||
type ThemeFunc func(context.Context, *ent.ThemeQuery) (ent.Value, error)
|
||||
|
||||
// Query calls f(ctx, q).
|
||||
func (f ThemeFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
if q, ok := q.(*ent.ThemeQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected query type %T. expect *ent.ThemeQuery", q)
|
||||
}
|
||||
|
||||
// The TraverseTheme type is an adapter to allow the use of ordinary function as Traverser.
|
||||
type TraverseTheme func(context.Context, *ent.ThemeQuery) error
|
||||
|
||||
// Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
|
||||
func (f TraverseTheme) Intercept(next ent.Querier) ent.Querier {
|
||||
return next
|
||||
}
|
||||
|
||||
// Traverse calls f(ctx, q).
|
||||
func (f TraverseTheme) Traverse(ctx context.Context, q ent.Query) error {
|
||||
if q, ok := q.(*ent.ThemeQuery); ok {
|
||||
return f(ctx, q)
|
||||
}
|
||||
return fmt.Errorf("unexpected query type %T. expect *ent.ThemeQuery", q)
|
||||
}
|
||||
|
||||
// NewQuery returns the generic Query interface for the given typed query.
|
||||
func NewQuery(q ent.Query) (Query, error) {
|
||||
switch q := q.(type) {
|
||||
case *ent.DocumentQuery:
|
||||
return &query[*ent.DocumentQuery, predicate.Document, document.OrderOption]{typ: ent.TypeDocument, tq: q}, nil
|
||||
case *ent.ExtensionQuery:
|
||||
return &query[*ent.ExtensionQuery, predicate.Extension, extension.OrderOption]{typ: ent.TypeExtension, tq: q}, nil
|
||||
case *ent.KeyBindingQuery:
|
||||
return &query[*ent.KeyBindingQuery, predicate.KeyBinding, keybinding.OrderOption]{typ: ent.TypeKeyBinding, tq: q}, nil
|
||||
case *ent.ThemeQuery:
|
||||
return &query[*ent.ThemeQuery, predicate.Theme, theme.OrderOption]{typ: ent.TypeTheme, tq: q}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown query type %T", q)
|
||||
}
|
||||
}
|
||||
|
||||
type query[T any, P ~func(*sql.Selector), R ~func(*sql.Selector)] struct {
|
||||
typ string
|
||||
tq interface {
|
||||
Limit(int) T
|
||||
Offset(int) T
|
||||
Unique(bool) T
|
||||
Order(...R) T
|
||||
Where(...P) T
|
||||
}
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) Type() string {
|
||||
return q.typ
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) Limit(limit int) {
|
||||
q.tq.Limit(limit)
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) Offset(offset int) {
|
||||
q.tq.Offset(offset)
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) Unique(unique bool) {
|
||||
q.tq.Unique(unique)
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) Order(orders ...func(*sql.Selector)) {
|
||||
rs := make([]R, len(orders))
|
||||
for i := range orders {
|
||||
rs[i] = orders[i]
|
||||
}
|
||||
q.tq.Order(rs...)
|
||||
}
|
||||
|
||||
func (q query[T, P, R]) WhereP(ps ...func(*sql.Selector)) {
|
||||
p := make([]P, len(ps))
|
||||
for i := range ps {
|
||||
p[i] = ps[i]
|
||||
}
|
||||
q.tq.Where(p...)
|
||||
}
|
||||
Reference in New Issue
Block a user