intercept.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Code generated by ent, DO NOT EDIT.
  2. package intercept
  3. import (
  4. "context"
  5. "fmt"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/server"
  10. "wechat-api/ent/wx"
  11. "entgo.io/ent/dialect/sql"
  12. )
  13. // The Query interface represents an operation that queries a graph.
  14. // By using this interface, users can write generic code that manipulates
  15. // query builders of different types.
  16. type Query interface {
  17. // Type returns the string representation of the query type.
  18. Type() string
  19. // Limit the number of records to be returned by this query.
  20. Limit(int)
  21. // Offset to start from.
  22. Offset(int)
  23. // Unique configures the query builder to filter duplicate records.
  24. Unique(bool)
  25. // Order specifies how the records should be ordered.
  26. Order(...func(*sql.Selector))
  27. // WhereP appends storage-level predicates to the query builder. Using this method, users
  28. // can use type-assertion to append predicates that do not depend on any generated package.
  29. WhereP(...func(*sql.Selector))
  30. }
  31. // The Func type is an adapter that allows ordinary functions to be used as interceptors.
  32. // Unlike traversal functions, interceptors are skipped during graph traversals. Note that the
  33. // implementation of Func is different from the one defined in entgo.io/ent.InterceptFunc.
  34. type Func func(context.Context, Query) error
  35. // Intercept calls f(ctx, q) and then applied the next Querier.
  36. func (f Func) Intercept(next ent.Querier) ent.Querier {
  37. return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
  38. query, err := NewQuery(q)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if err := f(ctx, query); err != nil {
  43. return nil, err
  44. }
  45. return next.Query(ctx, q)
  46. })
  47. }
  48. // The TraverseFunc type is an adapter to allow the use of ordinary function as Traverser.
  49. // If f is a function with the appropriate signature, TraverseFunc(f) is a Traverser that calls f.
  50. type TraverseFunc func(context.Context, Query) error
  51. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  52. func (f TraverseFunc) Intercept(next ent.Querier) ent.Querier {
  53. return next
  54. }
  55. // Traverse calls f(ctx, q).
  56. func (f TraverseFunc) Traverse(ctx context.Context, q ent.Query) error {
  57. query, err := NewQuery(q)
  58. if err != nil {
  59. return err
  60. }
  61. return f(ctx, query)
  62. }
  63. // The ContactFunc type is an adapter to allow the use of ordinary function as a Querier.
  64. type ContactFunc func(context.Context, *ent.ContactQuery) (ent.Value, error)
  65. // Query calls f(ctx, q).
  66. func (f ContactFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  67. if q, ok := q.(*ent.ContactQuery); ok {
  68. return f(ctx, q)
  69. }
  70. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  71. }
  72. // The TraverseContact type is an adapter to allow the use of ordinary function as Traverser.
  73. type TraverseContact func(context.Context, *ent.ContactQuery) error
  74. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  75. func (f TraverseContact) Intercept(next ent.Querier) ent.Querier {
  76. return next
  77. }
  78. // Traverse calls f(ctx, q).
  79. func (f TraverseContact) Traverse(ctx context.Context, q ent.Query) error {
  80. if q, ok := q.(*ent.ContactQuery); ok {
  81. return f(ctx, q)
  82. }
  83. return fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  84. }
  85. // The ServerFunc type is an adapter to allow the use of ordinary function as a Querier.
  86. type ServerFunc func(context.Context, *ent.ServerQuery) (ent.Value, error)
  87. // Query calls f(ctx, q).
  88. func (f ServerFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  89. if q, ok := q.(*ent.ServerQuery); ok {
  90. return f(ctx, q)
  91. }
  92. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  93. }
  94. // The TraverseServer type is an adapter to allow the use of ordinary function as Traverser.
  95. type TraverseServer func(context.Context, *ent.ServerQuery) error
  96. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  97. func (f TraverseServer) Intercept(next ent.Querier) ent.Querier {
  98. return next
  99. }
  100. // Traverse calls f(ctx, q).
  101. func (f TraverseServer) Traverse(ctx context.Context, q ent.Query) error {
  102. if q, ok := q.(*ent.ServerQuery); ok {
  103. return f(ctx, q)
  104. }
  105. return fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  106. }
  107. // The WxFunc type is an adapter to allow the use of ordinary function as a Querier.
  108. type WxFunc func(context.Context, *ent.WxQuery) (ent.Value, error)
  109. // Query calls f(ctx, q).
  110. func (f WxFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  111. if q, ok := q.(*ent.WxQuery); ok {
  112. return f(ctx, q)
  113. }
  114. return nil, fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  115. }
  116. // The TraverseWx type is an adapter to allow the use of ordinary function as Traverser.
  117. type TraverseWx func(context.Context, *ent.WxQuery) error
  118. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  119. func (f TraverseWx) Intercept(next ent.Querier) ent.Querier {
  120. return next
  121. }
  122. // Traverse calls f(ctx, q).
  123. func (f TraverseWx) Traverse(ctx context.Context, q ent.Query) error {
  124. if q, ok := q.(*ent.WxQuery); ok {
  125. return f(ctx, q)
  126. }
  127. return fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  128. }
  129. // NewQuery returns the generic Query interface for the given typed query.
  130. func NewQuery(q ent.Query) (Query, error) {
  131. switch q := q.(type) {
  132. case *ent.ContactQuery:
  133. return &query[*ent.ContactQuery, predicate.Contact, contact.OrderOption]{typ: ent.TypeContact, tq: q}, nil
  134. case *ent.ServerQuery:
  135. return &query[*ent.ServerQuery, predicate.Server, server.OrderOption]{typ: ent.TypeServer, tq: q}, nil
  136. case *ent.WxQuery:
  137. return &query[*ent.WxQuery, predicate.Wx, wx.OrderOption]{typ: ent.TypeWx, tq: q}, nil
  138. default:
  139. return nil, fmt.Errorf("unknown query type %T", q)
  140. }
  141. }
  142. type query[T any, P ~func(*sql.Selector), R ~func(*sql.Selector)] struct {
  143. typ string
  144. tq interface {
  145. Limit(int) T
  146. Offset(int) T
  147. Unique(bool) T
  148. Order(...R) T
  149. Where(...P) T
  150. }
  151. }
  152. func (q query[T, P, R]) Type() string {
  153. return q.typ
  154. }
  155. func (q query[T, P, R]) Limit(limit int) {
  156. q.tq.Limit(limit)
  157. }
  158. func (q query[T, P, R]) Offset(offset int) {
  159. q.tq.Offset(offset)
  160. }
  161. func (q query[T, P, R]) Unique(unique bool) {
  162. q.tq.Unique(unique)
  163. }
  164. func (q query[T, P, R]) Order(orders ...func(*sql.Selector)) {
  165. rs := make([]R, len(orders))
  166. for i := range orders {
  167. rs[i] = orders[i]
  168. }
  169. q.tq.Order(rs...)
  170. }
  171. func (q query[T, P, R]) WhereP(ps ...func(*sql.Selector)) {
  172. p := make([]P, len(ps))
  173. for i := range ps {
  174. p[i] = ps[i]
  175. }
  176. q.tq.Where(p...)
  177. }