/ ent / ent.go
ent.go
  1  // Code generated by ent, DO NOT EDIT.
  2  
  3  package ent
  4  
  5  import (
  6  	"context"
  7  	"errors"
  8  	"fmt"
  9  	"reflect"
 10  	"sync"
 11  
 12  	"entgo.io/ent"
 13  	"entgo.io/ent/dialect/sql"
 14  	"entgo.io/ent/dialect/sql/sqlgraph"
 15  	"github.com/mrusme/journalist/ent/feed"
 16  	"github.com/mrusme/journalist/ent/item"
 17  	"github.com/mrusme/journalist/ent/read"
 18  	"github.com/mrusme/journalist/ent/subscription"
 19  	"github.com/mrusme/journalist/ent/token"
 20  	"github.com/mrusme/journalist/ent/user"
 21  )
 22  
 23  // ent aliases to avoid import conflicts in user's code.
 24  type (
 25  	Op            = ent.Op
 26  	Hook          = ent.Hook
 27  	Value         = ent.Value
 28  	Query         = ent.Query
 29  	QueryContext  = ent.QueryContext
 30  	Querier       = ent.Querier
 31  	QuerierFunc   = ent.QuerierFunc
 32  	Interceptor   = ent.Interceptor
 33  	InterceptFunc = ent.InterceptFunc
 34  	Traverser     = ent.Traverser
 35  	TraverseFunc  = ent.TraverseFunc
 36  	Policy        = ent.Policy
 37  	Mutator       = ent.Mutator
 38  	Mutation      = ent.Mutation
 39  	MutateFunc    = ent.MutateFunc
 40  )
 41  
 42  type clientCtxKey struct{}
 43  
 44  // FromContext returns a Client stored inside a context, or nil if there isn't one.
 45  func FromContext(ctx context.Context) *Client {
 46  	c, _ := ctx.Value(clientCtxKey{}).(*Client)
 47  	return c
 48  }
 49  
 50  // NewContext returns a new context with the given Client attached.
 51  func NewContext(parent context.Context, c *Client) context.Context {
 52  	return context.WithValue(parent, clientCtxKey{}, c)
 53  }
 54  
 55  type txCtxKey struct{}
 56  
 57  // TxFromContext returns a Tx stored inside a context, or nil if there isn't one.
 58  func TxFromContext(ctx context.Context) *Tx {
 59  	tx, _ := ctx.Value(txCtxKey{}).(*Tx)
 60  	return tx
 61  }
 62  
 63  // NewTxContext returns a new context with the given Tx attached.
 64  func NewTxContext(parent context.Context, tx *Tx) context.Context {
 65  	return context.WithValue(parent, txCtxKey{}, tx)
 66  }
 67  
 68  // OrderFunc applies an ordering on the sql selector.
 69  // Deprecated: Use Asc/Desc functions or the package builders instead.
 70  type OrderFunc func(*sql.Selector)
 71  
 72  var (
 73  	initCheck   sync.Once
 74  	columnCheck sql.ColumnCheck
 75  )
 76  
 77  // columnChecker checks if the column exists in the given table.
 78  func checkColumn(table, column string) error {
 79  	initCheck.Do(func() {
 80  		columnCheck = sql.NewColumnCheck(map[string]func(string) bool{
 81  			feed.Table:         feed.ValidColumn,
 82  			item.Table:         item.ValidColumn,
 83  			read.Table:         read.ValidColumn,
 84  			subscription.Table: subscription.ValidColumn,
 85  			token.Table:        token.ValidColumn,
 86  			user.Table:         user.ValidColumn,
 87  		})
 88  	})
 89  	return columnCheck(table, column)
 90  }
 91  
 92  // Asc applies the given fields in ASC order.
 93  func Asc(fields ...string) func(*sql.Selector) {
 94  	return func(s *sql.Selector) {
 95  		for _, f := range fields {
 96  			if err := checkColumn(s.TableName(), f); err != nil {
 97  				s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
 98  			}
 99  			s.OrderBy(sql.Asc(s.C(f)))
100  		}
101  	}
102  }
103  
104  // Desc applies the given fields in DESC order.
105  func Desc(fields ...string) func(*sql.Selector) {
106  	return func(s *sql.Selector) {
107  		for _, f := range fields {
108  			if err := checkColumn(s.TableName(), f); err != nil {
109  				s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
110  			}
111  			s.OrderBy(sql.Desc(s.C(f)))
112  		}
113  	}
114  }
115  
116  // AggregateFunc applies an aggregation step on the group-by traversal/selector.
117  type AggregateFunc func(*sql.Selector) string
118  
119  // As is a pseudo aggregation function for renaming another other functions with custom names. For example:
120  //
121  //	GroupBy(field1, field2).
122  //	Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
123  //	Scan(ctx, &v)
124  func As(fn AggregateFunc, end string) AggregateFunc {
125  	return func(s *sql.Selector) string {
126  		return sql.As(fn(s), end)
127  	}
128  }
129  
130  // Count applies the "count" aggregation function on each group.
131  func Count() AggregateFunc {
132  	return func(s *sql.Selector) string {
133  		return sql.Count("*")
134  	}
135  }
136  
137  // Max applies the "max" aggregation function on the given field of each group.
138  func Max(field string) AggregateFunc {
139  	return func(s *sql.Selector) string {
140  		if err := checkColumn(s.TableName(), field); err != nil {
141  			s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
142  			return ""
143  		}
144  		return sql.Max(s.C(field))
145  	}
146  }
147  
148  // Mean applies the "mean" aggregation function on the given field of each group.
149  func Mean(field string) AggregateFunc {
150  	return func(s *sql.Selector) string {
151  		if err := checkColumn(s.TableName(), field); err != nil {
152  			s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
153  			return ""
154  		}
155  		return sql.Avg(s.C(field))
156  	}
157  }
158  
159  // Min applies the "min" aggregation function on the given field of each group.
160  func Min(field string) AggregateFunc {
161  	return func(s *sql.Selector) string {
162  		if err := checkColumn(s.TableName(), field); err != nil {
163  			s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
164  			return ""
165  		}
166  		return sql.Min(s.C(field))
167  	}
168  }
169  
170  // Sum applies the "sum" aggregation function on the given field of each group.
171  func Sum(field string) AggregateFunc {
172  	return func(s *sql.Selector) string {
173  		if err := checkColumn(s.TableName(), field); err != nil {
174  			s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
175  			return ""
176  		}
177  		return sql.Sum(s.C(field))
178  	}
179  }
180  
181  // ValidationError returns when validating a field or edge fails.
182  type ValidationError struct {
183  	Name string // Field or edge name.
184  	err  error
185  }
186  
187  // Error implements the error interface.
188  func (e *ValidationError) Error() string {
189  	return e.err.Error()
190  }
191  
192  // Unwrap implements the errors.Wrapper interface.
193  func (e *ValidationError) Unwrap() error {
194  	return e.err
195  }
196  
197  // IsValidationError returns a boolean indicating whether the error is a validation error.
198  func IsValidationError(err error) bool {
199  	if err == nil {
200  		return false
201  	}
202  	var e *ValidationError
203  	return errors.As(err, &e)
204  }
205  
206  // NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
207  type NotFoundError struct {
208  	label string
209  }
210  
211  // Error implements the error interface.
212  func (e *NotFoundError) Error() string {
213  	return "ent: " + e.label + " not found"
214  }
215  
216  // IsNotFound returns a boolean indicating whether the error is a not found error.
217  func IsNotFound(err error) bool {
218  	if err == nil {
219  		return false
220  	}
221  	var e *NotFoundError
222  	return errors.As(err, &e)
223  }
224  
225  // MaskNotFound masks not found error.
226  func MaskNotFound(err error) error {
227  	if IsNotFound(err) {
228  		return nil
229  	}
230  	return err
231  }
232  
233  // NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.
234  type NotSingularError struct {
235  	label string
236  }
237  
238  // Error implements the error interface.
239  func (e *NotSingularError) Error() string {
240  	return "ent: " + e.label + " not singular"
241  }
242  
243  // IsNotSingular returns a boolean indicating whether the error is a not singular error.
244  func IsNotSingular(err error) bool {
245  	if err == nil {
246  		return false
247  	}
248  	var e *NotSingularError
249  	return errors.As(err, &e)
250  }
251  
252  // NotLoadedError returns when trying to get a node that was not loaded by the query.
253  type NotLoadedError struct {
254  	edge string
255  }
256  
257  // Error implements the error interface.
258  func (e *NotLoadedError) Error() string {
259  	return "ent: " + e.edge + " edge was not loaded"
260  }
261  
262  // IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
263  func IsNotLoaded(err error) bool {
264  	if err == nil {
265  		return false
266  	}
267  	var e *NotLoadedError
268  	return errors.As(err, &e)
269  }
270  
271  // ConstraintError returns when trying to create/update one or more entities and
272  // one or more of their constraints failed. For example, violation of edge or
273  // field uniqueness.
274  type ConstraintError struct {
275  	msg  string
276  	wrap error
277  }
278  
279  // Error implements the error interface.
280  func (e ConstraintError) Error() string {
281  	return "ent: constraint failed: " + e.msg
282  }
283  
284  // Unwrap implements the errors.Wrapper interface.
285  func (e *ConstraintError) Unwrap() error {
286  	return e.wrap
287  }
288  
289  // IsConstraintError returns a boolean indicating whether the error is a constraint failure.
290  func IsConstraintError(err error) bool {
291  	if err == nil {
292  		return false
293  	}
294  	var e *ConstraintError
295  	return errors.As(err, &e)
296  }
297  
298  // selector embedded by the different Select/GroupBy builders.
299  type selector struct {
300  	label string
301  	flds  *[]string
302  	fns   []AggregateFunc
303  	scan  func(context.Context, any) error
304  }
305  
306  // ScanX is like Scan, but panics if an error occurs.
307  func (s *selector) ScanX(ctx context.Context, v any) {
308  	if err := s.scan(ctx, v); err != nil {
309  		panic(err)
310  	}
311  }
312  
313  // Strings returns list of strings from a selector. It is only allowed when selecting one field.
314  func (s *selector) Strings(ctx context.Context) ([]string, error) {
315  	if len(*s.flds) > 1 {
316  		return nil, errors.New("ent: Strings is not achievable when selecting more than 1 field")
317  	}
318  	var v []string
319  	if err := s.scan(ctx, &v); err != nil {
320  		return nil, err
321  	}
322  	return v, nil
323  }
324  
325  // StringsX is like Strings, but panics if an error occurs.
326  func (s *selector) StringsX(ctx context.Context) []string {
327  	v, err := s.Strings(ctx)
328  	if err != nil {
329  		panic(err)
330  	}
331  	return v
332  }
333  
334  // String returns a single string from a selector. It is only allowed when selecting one field.
335  func (s *selector) String(ctx context.Context) (_ string, err error) {
336  	var v []string
337  	if v, err = s.Strings(ctx); err != nil {
338  		return
339  	}
340  	switch len(v) {
341  	case 1:
342  		return v[0], nil
343  	case 0:
344  		err = &NotFoundError{s.label}
345  	default:
346  		err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v))
347  	}
348  	return
349  }
350  
351  // StringX is like String, but panics if an error occurs.
352  func (s *selector) StringX(ctx context.Context) string {
353  	v, err := s.String(ctx)
354  	if err != nil {
355  		panic(err)
356  	}
357  	return v
358  }
359  
360  // Ints returns list of ints from a selector. It is only allowed when selecting one field.
361  func (s *selector) Ints(ctx context.Context) ([]int, error) {
362  	if len(*s.flds) > 1 {
363  		return nil, errors.New("ent: Ints is not achievable when selecting more than 1 field")
364  	}
365  	var v []int
366  	if err := s.scan(ctx, &v); err != nil {
367  		return nil, err
368  	}
369  	return v, nil
370  }
371  
372  // IntsX is like Ints, but panics if an error occurs.
373  func (s *selector) IntsX(ctx context.Context) []int {
374  	v, err := s.Ints(ctx)
375  	if err != nil {
376  		panic(err)
377  	}
378  	return v
379  }
380  
381  // Int returns a single int from a selector. It is only allowed when selecting one field.
382  func (s *selector) Int(ctx context.Context) (_ int, err error) {
383  	var v []int
384  	if v, err = s.Ints(ctx); err != nil {
385  		return
386  	}
387  	switch len(v) {
388  	case 1:
389  		return v[0], nil
390  	case 0:
391  		err = &NotFoundError{s.label}
392  	default:
393  		err = fmt.Errorf("ent: Ints returned %d results when one was expected", len(v))
394  	}
395  	return
396  }
397  
398  // IntX is like Int, but panics if an error occurs.
399  func (s *selector) IntX(ctx context.Context) int {
400  	v, err := s.Int(ctx)
401  	if err != nil {
402  		panic(err)
403  	}
404  	return v
405  }
406  
407  // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
408  func (s *selector) Float64s(ctx context.Context) ([]float64, error) {
409  	if len(*s.flds) > 1 {
410  		return nil, errors.New("ent: Float64s is not achievable when selecting more than 1 field")
411  	}
412  	var v []float64
413  	if err := s.scan(ctx, &v); err != nil {
414  		return nil, err
415  	}
416  	return v, nil
417  }
418  
419  // Float64sX is like Float64s, but panics if an error occurs.
420  func (s *selector) Float64sX(ctx context.Context) []float64 {
421  	v, err := s.Float64s(ctx)
422  	if err != nil {
423  		panic(err)
424  	}
425  	return v
426  }
427  
428  // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
429  func (s *selector) Float64(ctx context.Context) (_ float64, err error) {
430  	var v []float64
431  	if v, err = s.Float64s(ctx); err != nil {
432  		return
433  	}
434  	switch len(v) {
435  	case 1:
436  		return v[0], nil
437  	case 0:
438  		err = &NotFoundError{s.label}
439  	default:
440  		err = fmt.Errorf("ent: Float64s returned %d results when one was expected", len(v))
441  	}
442  	return
443  }
444  
445  // Float64X is like Float64, but panics if an error occurs.
446  func (s *selector) Float64X(ctx context.Context) float64 {
447  	v, err := s.Float64(ctx)
448  	if err != nil {
449  		panic(err)
450  	}
451  	return v
452  }
453  
454  // Bools returns list of bools from a selector. It is only allowed when selecting one field.
455  func (s *selector) Bools(ctx context.Context) ([]bool, error) {
456  	if len(*s.flds) > 1 {
457  		return nil, errors.New("ent: Bools is not achievable when selecting more than 1 field")
458  	}
459  	var v []bool
460  	if err := s.scan(ctx, &v); err != nil {
461  		return nil, err
462  	}
463  	return v, nil
464  }
465  
466  // BoolsX is like Bools, but panics if an error occurs.
467  func (s *selector) BoolsX(ctx context.Context) []bool {
468  	v, err := s.Bools(ctx)
469  	if err != nil {
470  		panic(err)
471  	}
472  	return v
473  }
474  
475  // Bool returns a single bool from a selector. It is only allowed when selecting one field.
476  func (s *selector) Bool(ctx context.Context) (_ bool, err error) {
477  	var v []bool
478  	if v, err = s.Bools(ctx); err != nil {
479  		return
480  	}
481  	switch len(v) {
482  	case 1:
483  		return v[0], nil
484  	case 0:
485  		err = &NotFoundError{s.label}
486  	default:
487  		err = fmt.Errorf("ent: Bools returned %d results when one was expected", len(v))
488  	}
489  	return
490  }
491  
492  // BoolX is like Bool, but panics if an error occurs.
493  func (s *selector) BoolX(ctx context.Context) bool {
494  	v, err := s.Bool(ctx)
495  	if err != nil {
496  		panic(err)
497  	}
498  	return v
499  }
500  
501  // withHooks invokes the builder operation with the given hooks, if any.
502  func withHooks[V Value, M any, PM interface {
503  	*M
504  	Mutation
505  }](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) {
506  	if len(hooks) == 0 {
507  		return exec(ctx)
508  	}
509  	var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
510  		mutationT, ok := any(m).(PM)
511  		if !ok {
512  			return nil, fmt.Errorf("unexpected mutation type %T", m)
513  		}
514  		// Set the mutation to the builder.
515  		*mutation = *mutationT
516  		return exec(ctx)
517  	})
518  	for i := len(hooks) - 1; i >= 0; i-- {
519  		if hooks[i] == nil {
520  			return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
521  		}
522  		mut = hooks[i](mut)
523  	}
524  	v, err := mut.Mutate(ctx, mutation)
525  	if err != nil {
526  		return value, err
527  	}
528  	nv, ok := v.(V)
529  	if !ok {
530  		return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation)
531  	}
532  	return nv, nil
533  }
534  
535  // setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist.
536  func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context {
537  	if ent.QueryFromContext(ctx) == nil {
538  		qc.Op = op
539  		ctx = ent.NewQueryContext(ctx, qc)
540  	}
541  	return ctx
542  }
543  
544  func querierAll[V Value, Q interface {
545  	sqlAll(context.Context, ...queryHook) (V, error)
546  }]() Querier {
547  	return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
548  		query, ok := q.(Q)
549  		if !ok {
550  			return nil, fmt.Errorf("unexpected query type %T", q)
551  		}
552  		return query.sqlAll(ctx)
553  	})
554  }
555  
556  func querierCount[Q interface {
557  	sqlCount(context.Context) (int, error)
558  }]() Querier {
559  	return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
560  		query, ok := q.(Q)
561  		if !ok {
562  			return nil, fmt.Errorf("unexpected query type %T", q)
563  		}
564  		return query.sqlCount(ctx)
565  	})
566  }
567  
568  func withInterceptors[V Value](ctx context.Context, q Query, qr Querier, inters []Interceptor) (v V, err error) {
569  	for i := len(inters) - 1; i >= 0; i-- {
570  		qr = inters[i].Intercept(qr)
571  	}
572  	rv, err := qr.Query(ctx, q)
573  	if err != nil {
574  		return v, err
575  	}
576  	vt, ok := rv.(V)
577  	if !ok {
578  		return v, fmt.Errorf("unexpected type %T returned from %T. expected type: %T", vt, q, v)
579  	}
580  	return vt, nil
581  }
582  
583  func scanWithInterceptors[Q1 ent.Query, Q2 interface {
584  	sqlScan(context.Context, Q1, any) error
585  }](ctx context.Context, rootQuery Q1, selectOrGroup Q2, inters []Interceptor, v any) error {
586  	rv := reflect.ValueOf(v)
587  	var qr Querier = QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
588  		query, ok := q.(Q1)
589  		if !ok {
590  			return nil, fmt.Errorf("unexpected query type %T", q)
591  		}
592  		if err := selectOrGroup.sqlScan(ctx, query, v); err != nil {
593  			return nil, err
594  		}
595  		if k := rv.Kind(); k == reflect.Pointer && rv.Elem().CanInterface() {
596  			return rv.Elem().Interface(), nil
597  		}
598  		return v, nil
599  	})
600  	for i := len(inters) - 1; i >= 0; i-- {
601  		qr = inters[i].Intercept(qr)
602  	}
603  	vv, err := qr.Query(ctx, rootQuery)
604  	if err != nil {
605  		return err
606  	}
607  	switch rv2 := reflect.ValueOf(vv); {
608  	case rv.IsNil(), rv2.IsNil(), rv.Kind() != reflect.Pointer:
609  	case rv.Type() == rv2.Type():
610  		rv.Elem().Set(rv2.Elem())
611  	case rv.Elem().Type() == rv2.Type():
612  		rv.Elem().Set(rv2)
613  	}
614  	return nil
615  }
616  
617  // queryHook describes an internal hook for the different sqlAll methods.
618  type queryHook func(context.Context, *sqlgraph.QuerySpec)