snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
96
entql/internal/gen.go
Normal file
96
entql/internal/gen.go
Normal file
@@ -0,0 +1,96 @@
|
||||
// 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.
|
||||
|
||||
// A codegen cmd for generating builder types from template.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/format"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
func main() {
|
||||
buf, err := os.ReadFile("internal/types.tmpl")
|
||||
if err != nil {
|
||||
log.Fatal("reading template file:", err)
|
||||
}
|
||||
tmpl := template.Must(template.New("types").
|
||||
Funcs(template.FuncMap{
|
||||
"ops": ops,
|
||||
"title": strings.Title,
|
||||
"ident": ident,
|
||||
"type": typ,
|
||||
}).
|
||||
Parse(string(buf)))
|
||||
b := &bytes.Buffer{}
|
||||
if err := tmpl.Execute(b, struct {
|
||||
Types []field.Type
|
||||
}{
|
||||
Types: []field.Type{
|
||||
field.TypeBool,
|
||||
field.TypeBytes,
|
||||
field.TypeTime,
|
||||
field.TypeUint,
|
||||
field.TypeUint8,
|
||||
field.TypeUint16,
|
||||
field.TypeUint32,
|
||||
field.TypeUint64,
|
||||
field.TypeInt,
|
||||
field.TypeInt8,
|
||||
field.TypeInt16,
|
||||
field.TypeInt32,
|
||||
field.TypeInt64,
|
||||
field.TypeFloat32,
|
||||
field.TypeFloat64,
|
||||
field.TypeString,
|
||||
field.TypeUUID,
|
||||
field.TypeOther,
|
||||
},
|
||||
}); err != nil {
|
||||
log.Fatal("executing template:", err)
|
||||
}
|
||||
if buf, err = format.Source(b.Bytes()); err != nil {
|
||||
log.Fatal("formatting output:", err)
|
||||
}
|
||||
if err := os.WriteFile("types.go", buf, 0644); err != nil {
|
||||
log.Fatal("writing go file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func ops(t field.Type) []string {
|
||||
switch t {
|
||||
case field.TypeBool, field.TypeBytes, field.TypeUUID, field.TypeOther:
|
||||
return []string{"EQ", "NEQ"}
|
||||
default:
|
||||
return []string{"EQ", "NEQ", "LT", "LTE", "GT", "GTE"}
|
||||
}
|
||||
}
|
||||
|
||||
func ident(t field.Type) string {
|
||||
switch t {
|
||||
case field.TypeBytes:
|
||||
return "bytes"
|
||||
case field.TypeTime:
|
||||
return "time"
|
||||
case field.TypeUUID:
|
||||
return "value"
|
||||
case field.TypeOther:
|
||||
return "other"
|
||||
default:
|
||||
return t.String()
|
||||
}
|
||||
}
|
||||
|
||||
func typ(t field.Type) string {
|
||||
if t == field.TypeUUID || t == field.TypeOther {
|
||||
return "driver.Valuer"
|
||||
}
|
||||
return t.String()
|
||||
}
|
||||
108
entql/internal/types.tmpl
Normal file
108
entql/internal/types.tmpl
Normal file
@@ -0,0 +1,108 @@
|
||||
{{ define "types" }}
|
||||
// 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.
|
||||
|
||||
// Code generated by internal/gen.go, DO NOT EDIT.
|
||||
|
||||
package entql
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:generate go run internal/gen.go
|
||||
|
||||
// Fielder is the interface for creating a predicate (entql.P)
|
||||
// by a field name from the different builder types below.
|
||||
type Fielder interface {
|
||||
Field(string) P
|
||||
}
|
||||
|
||||
{{ range $t := $.Types }}
|
||||
|
||||
{{ $titled := ident $t | title }}
|
||||
{{ $iface := print $titled "P" }}
|
||||
{{ $builder := print (ident $t) "P" }}
|
||||
|
||||
// {{ $iface }} is the interface for predicates of type {{ $t }} (`type P[{{ $t }}]`).
|
||||
type {{ $iface }} interface {
|
||||
Fielder
|
||||
{{ ident $t }}()
|
||||
}
|
||||
|
||||
// {{ $builder }} implements the {{ $iface }} interface.
|
||||
type {{ $builder }} struct {
|
||||
P
|
||||
done func(string)
|
||||
}
|
||||
|
||||
func (p *{{ $builder }}) Field(name string) P {
|
||||
p.done(name)
|
||||
return p.P
|
||||
}
|
||||
|
||||
func (*{{ $builder }}) {{ ident $t }}() {}
|
||||
|
||||
// {{ $titled }}Nil applies the Nil operation
|
||||
func {{ $titled }}Nil() {{ $iface }} {
|
||||
field := &Field{}
|
||||
done := func(name string) { field.Name = name }
|
||||
return &{{ $builder }}{P: EQ(field, (*Value)(nil)), done: done}
|
||||
}
|
||||
|
||||
// {{ $titled }}NotNil applies the NotNil operation
|
||||
func {{ $titled }}NotNil() {{ $iface }} {
|
||||
field := &Field{}
|
||||
done := func(name string) { field.Name = name }
|
||||
return &{{ $builder }}{P: NEQ(field, (*Value)(nil)), done: done}
|
||||
}
|
||||
|
||||
{{ range $op := ops $t }}
|
||||
// {{ $titled }}{{ $op }} applies the {{ $op }} operation on the given value.
|
||||
func {{ $titled }}{{ $op }}(v {{ type $t }}) {{ $iface }} {
|
||||
field := &Field{}
|
||||
value := &Value{V: v}
|
||||
done := func(name string) { field.Name = name }
|
||||
return &{{ $builder }}{P: {{ $op }}(field, value), done: done}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
// {{ $titled }}Or returns a composed predicate that represents the logical OR predicate.
|
||||
func {{ $titled }}Or(x, y {{ $iface }}, z ...{{ $iface }}) {{ $iface }} {
|
||||
expr := &{{ $builder }}{}
|
||||
expr.done = func(name string) {
|
||||
zs := make([]P, len(z))
|
||||
for i := range z {
|
||||
zs[i] = z[i].Field(name)
|
||||
}
|
||||
expr.P = Or(x.Field(name), y.Field(name), zs...)
|
||||
}
|
||||
return expr
|
||||
}
|
||||
|
||||
// {{ $titled }}And returns a composed predicate that represents the logical AND predicate.
|
||||
func {{ $titled }}And(x, y {{ $iface }}, z ...{{ $iface }}) {{ $iface }} {
|
||||
expr := &{{ $builder }}{}
|
||||
expr.done = func(name string) {
|
||||
zs := make([]P, len(z))
|
||||
for i := range z {
|
||||
zs[i] = z[i].Field(name)
|
||||
}
|
||||
expr.P = And(x.Field(name), y.Field(name), zs...)
|
||||
}
|
||||
return expr
|
||||
}
|
||||
|
||||
// {{ $titled }}Not returns a predicate that represents the logical negation of the given predicate.
|
||||
func {{ $titled }}Not(x {{ $iface }}) {{ $iface }} {
|
||||
expr := &{{ $builder }}{}
|
||||
expr.done = func(name string) {
|
||||
expr.P = Not(x.Field(name))
|
||||
}
|
||||
return expr
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user