intercept.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/message"
  11. "wechat-api/ent/predicate"
  12. "wechat-api/ent/server"
  13. "wechat-api/ent/wx"
  14. "entgo.io/ent/dialect/sql"
  15. )
  16. // The Query interface represents an operation that queries a graph.
  17. // By using this interface, users can write generic code that manipulates
  18. // query builders of different types.
  19. type Query interface {
  20. // Type returns the string representation of the query type.
  21. Type() string
  22. // Limit the number of records to be returned by this query.
  23. Limit(int)
  24. // Offset to start from.
  25. Offset(int)
  26. // Unique configures the query builder to filter duplicate records.
  27. Unique(bool)
  28. // Order specifies how the records should be ordered.
  29. Order(...func(*sql.Selector))
  30. // WhereP appends storage-level predicates to the query builder. Using this method, users
  31. // can use type-assertion to append predicates that do not depend on any generated package.
  32. WhereP(...func(*sql.Selector))
  33. }
  34. // The Func type is an adapter that allows ordinary functions to be used as interceptors.
  35. // Unlike traversal functions, interceptors are skipped during graph traversals. Note that the
  36. // implementation of Func is different from the one defined in entgo.io/ent.InterceptFunc.
  37. type Func func(context.Context, Query) error
  38. // Intercept calls f(ctx, q) and then applied the next Querier.
  39. func (f Func) Intercept(next ent.Querier) ent.Querier {
  40. return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
  41. query, err := NewQuery(q)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if err := f(ctx, query); err != nil {
  46. return nil, err
  47. }
  48. return next.Query(ctx, q)
  49. })
  50. }
  51. // The TraverseFunc type is an adapter to allow the use of ordinary function as Traverser.
  52. // If f is a function with the appropriate signature, TraverseFunc(f) is a Traverser that calls f.
  53. type TraverseFunc func(context.Context, Query) error
  54. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  55. func (f TraverseFunc) Intercept(next ent.Querier) ent.Querier {
  56. return next
  57. }
  58. // Traverse calls f(ctx, q).
  59. func (f TraverseFunc) Traverse(ctx context.Context, q ent.Query) error {
  60. query, err := NewQuery(q)
  61. if err != nil {
  62. return err
  63. }
  64. return f(ctx, query)
  65. }
  66. // The ContactFunc type is an adapter to allow the use of ordinary function as a Querier.
  67. type ContactFunc func(context.Context, *ent.ContactQuery) (ent.Value, error)
  68. // Query calls f(ctx, q).
  69. func (f ContactFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  70. if q, ok := q.(*ent.ContactQuery); ok {
  71. return f(ctx, q)
  72. }
  73. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  74. }
  75. // The TraverseContact type is an adapter to allow the use of ordinary function as Traverser.
  76. type TraverseContact func(context.Context, *ent.ContactQuery) error
  77. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  78. func (f TraverseContact) Intercept(next ent.Querier) ent.Querier {
  79. return next
  80. }
  81. // Traverse calls f(ctx, q).
  82. func (f TraverseContact) Traverse(ctx context.Context, q ent.Query) error {
  83. if q, ok := q.(*ent.ContactQuery); ok {
  84. return f(ctx, q)
  85. }
  86. return fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  87. }
  88. // The LabelFunc type is an adapter to allow the use of ordinary function as a Querier.
  89. type LabelFunc func(context.Context, *ent.LabelQuery) (ent.Value, error)
  90. // Query calls f(ctx, q).
  91. func (f LabelFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  92. if q, ok := q.(*ent.LabelQuery); ok {
  93. return f(ctx, q)
  94. }
  95. return nil, fmt.Errorf("unexpected query type %T. expect *ent.LabelQuery", q)
  96. }
  97. // The TraverseLabel type is an adapter to allow the use of ordinary function as Traverser.
  98. type TraverseLabel func(context.Context, *ent.LabelQuery) error
  99. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  100. func (f TraverseLabel) Intercept(next ent.Querier) ent.Querier {
  101. return next
  102. }
  103. // Traverse calls f(ctx, q).
  104. func (f TraverseLabel) Traverse(ctx context.Context, q ent.Query) error {
  105. if q, ok := q.(*ent.LabelQuery); ok {
  106. return f(ctx, q)
  107. }
  108. return fmt.Errorf("unexpected query type %T. expect *ent.LabelQuery", q)
  109. }
  110. // The LabelRelationshipFunc type is an adapter to allow the use of ordinary function as a Querier.
  111. type LabelRelationshipFunc func(context.Context, *ent.LabelRelationshipQuery) (ent.Value, error)
  112. // Query calls f(ctx, q).
  113. func (f LabelRelationshipFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  114. if q, ok := q.(*ent.LabelRelationshipQuery); ok {
  115. return f(ctx, q)
  116. }
  117. return nil, fmt.Errorf("unexpected query type %T. expect *ent.LabelRelationshipQuery", q)
  118. }
  119. // The TraverseLabelRelationship type is an adapter to allow the use of ordinary function as Traverser.
  120. type TraverseLabelRelationship func(context.Context, *ent.LabelRelationshipQuery) error
  121. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  122. func (f TraverseLabelRelationship) Intercept(next ent.Querier) ent.Querier {
  123. return next
  124. }
  125. // Traverse calls f(ctx, q).
  126. func (f TraverseLabelRelationship) Traverse(ctx context.Context, q ent.Query) error {
  127. if q, ok := q.(*ent.LabelRelationshipQuery); ok {
  128. return f(ctx, q)
  129. }
  130. return fmt.Errorf("unexpected query type %T. expect *ent.LabelRelationshipQuery", q)
  131. }
  132. // The MessageFunc type is an adapter to allow the use of ordinary function as a Querier.
  133. type MessageFunc func(context.Context, *ent.MessageQuery) (ent.Value, error)
  134. // Query calls f(ctx, q).
  135. func (f MessageFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  136. if q, ok := q.(*ent.MessageQuery); ok {
  137. return f(ctx, q)
  138. }
  139. return nil, fmt.Errorf("unexpected query type %T. expect *ent.MessageQuery", q)
  140. }
  141. // The TraverseMessage type is an adapter to allow the use of ordinary function as Traverser.
  142. type TraverseMessage func(context.Context, *ent.MessageQuery) error
  143. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  144. func (f TraverseMessage) Intercept(next ent.Querier) ent.Querier {
  145. return next
  146. }
  147. // Traverse calls f(ctx, q).
  148. func (f TraverseMessage) Traverse(ctx context.Context, q ent.Query) error {
  149. if q, ok := q.(*ent.MessageQuery); ok {
  150. return f(ctx, q)
  151. }
  152. return fmt.Errorf("unexpected query type %T. expect *ent.MessageQuery", q)
  153. }
  154. // The ServerFunc type is an adapter to allow the use of ordinary function as a Querier.
  155. type ServerFunc func(context.Context, *ent.ServerQuery) (ent.Value, error)
  156. // Query calls f(ctx, q).
  157. func (f ServerFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  158. if q, ok := q.(*ent.ServerQuery); ok {
  159. return f(ctx, q)
  160. }
  161. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  162. }
  163. // The TraverseServer type is an adapter to allow the use of ordinary function as Traverser.
  164. type TraverseServer func(context.Context, *ent.ServerQuery) error
  165. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  166. func (f TraverseServer) Intercept(next ent.Querier) ent.Querier {
  167. return next
  168. }
  169. // Traverse calls f(ctx, q).
  170. func (f TraverseServer) Traverse(ctx context.Context, q ent.Query) error {
  171. if q, ok := q.(*ent.ServerQuery); ok {
  172. return f(ctx, q)
  173. }
  174. return fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  175. }
  176. // The WxFunc type is an adapter to allow the use of ordinary function as a Querier.
  177. type WxFunc func(context.Context, *ent.WxQuery) (ent.Value, error)
  178. // Query calls f(ctx, q).
  179. func (f WxFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  180. if q, ok := q.(*ent.WxQuery); ok {
  181. return f(ctx, q)
  182. }
  183. return nil, fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  184. }
  185. // The TraverseWx type is an adapter to allow the use of ordinary function as Traverser.
  186. type TraverseWx func(context.Context, *ent.WxQuery) error
  187. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  188. func (f TraverseWx) Intercept(next ent.Querier) ent.Querier {
  189. return next
  190. }
  191. // Traverse calls f(ctx, q).
  192. func (f TraverseWx) Traverse(ctx context.Context, q ent.Query) error {
  193. if q, ok := q.(*ent.WxQuery); ok {
  194. return f(ctx, q)
  195. }
  196. return fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  197. }
  198. // NewQuery returns the generic Query interface for the given typed query.
  199. func NewQuery(q ent.Query) (Query, error) {
  200. switch q := q.(type) {
  201. case *ent.ContactQuery:
  202. return &query[*ent.ContactQuery, predicate.Contact, contact.OrderOption]{typ: ent.TypeContact, tq: q}, nil
  203. case *ent.LabelQuery:
  204. return &query[*ent.LabelQuery, predicate.Label, label.OrderOption]{typ: ent.TypeLabel, tq: q}, nil
  205. case *ent.LabelRelationshipQuery:
  206. return &query[*ent.LabelRelationshipQuery, predicate.LabelRelationship, labelrelationship.OrderOption]{typ: ent.TypeLabelRelationship, tq: q}, nil
  207. case *ent.MessageQuery:
  208. return &query[*ent.MessageQuery, predicate.Message, message.OrderOption]{typ: ent.TypeMessage, tq: q}, nil
  209. case *ent.ServerQuery:
  210. return &query[*ent.ServerQuery, predicate.Server, server.OrderOption]{typ: ent.TypeServer, tq: q}, nil
  211. case *ent.WxQuery:
  212. return &query[*ent.WxQuery, predicate.Wx, wx.OrderOption]{typ: ent.TypeWx, tq: q}, nil
  213. default:
  214. return nil, fmt.Errorf("unknown query type %T", q)
  215. }
  216. }
  217. type query[T any, P ~func(*sql.Selector), R ~func(*sql.Selector)] struct {
  218. typ string
  219. tq interface {
  220. Limit(int) T
  221. Offset(int) T
  222. Unique(bool) T
  223. Order(...R) T
  224. Where(...P) T
  225. }
  226. }
  227. func (q query[T, P, R]) Type() string {
  228. return q.typ
  229. }
  230. func (q query[T, P, R]) Limit(limit int) {
  231. q.tq.Limit(limit)
  232. }
  233. func (q query[T, P, R]) Offset(offset int) {
  234. q.tq.Offset(offset)
  235. }
  236. func (q query[T, P, R]) Unique(unique bool) {
  237. q.tq.Unique(unique)
  238. }
  239. func (q query[T, P, R]) Order(orders ...func(*sql.Selector)) {
  240. rs := make([]R, len(orders))
  241. for i := range orders {
  242. rs[i] = orders[i]
  243. }
  244. q.tq.Order(rs...)
  245. }
  246. func (q query[T, P, R]) WhereP(ps ...func(*sql.Selector)) {
  247. p := make([]P, len(ps))
  248. for i := range ps {
  249. p[i] = ps[i]
  250. }
  251. q.tq.Where(p...)
  252. }