File: types.tmpl

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (94 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{{ 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 }}() {}

{{ 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 }}