hook.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Code generated by entc, DO NOT EDIT.
  2. package hook
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/go__ent_demo/ent"
  7. )
  8. // The CarFunc type is an adapter to allow the use of ordinary
  9. // function as Car mutator.
  10. type CarFunc func(context.Context, *ent.CarMutation) (ent.Value, error)
  11. // Mutate calls f(ctx, m).
  12. func (f CarFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  13. mv, ok := m.(*ent.CarMutation)
  14. if !ok {
  15. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CarMutation", m)
  16. }
  17. return f(ctx, mv)
  18. }
  19. // The GroupFunc type is an adapter to allow the use of ordinary
  20. // function as Group mutator.
  21. type GroupFunc func(context.Context, *ent.GroupMutation) (ent.Value, error)
  22. // Mutate calls f(ctx, m).
  23. func (f GroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  24. mv, ok := m.(*ent.GroupMutation)
  25. if !ok {
  26. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.GroupMutation", m)
  27. }
  28. return f(ctx, mv)
  29. }
  30. // The UserFunc type is an adapter to allow the use of ordinary
  31. // function as User mutator.
  32. type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
  33. // Mutate calls f(ctx, m).
  34. func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  35. mv, ok := m.(*ent.UserMutation)
  36. if !ok {
  37. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
  38. }
  39. return f(ctx, mv)
  40. }
  41. // Condition is a hook condition function.
  42. type Condition func(context.Context, ent.Mutation) bool
  43. // And groups conditions with the AND operator.
  44. func And(first, second Condition, rest ...Condition) Condition {
  45. return func(ctx context.Context, m ent.Mutation) bool {
  46. if !first(ctx, m) || !second(ctx, m) {
  47. return false
  48. }
  49. for _, cond := range rest {
  50. if !cond(ctx, m) {
  51. return false
  52. }
  53. }
  54. return true
  55. }
  56. }
  57. // Or groups conditions with the OR operator.
  58. func Or(first, second Condition, rest ...Condition) Condition {
  59. return func(ctx context.Context, m ent.Mutation) bool {
  60. if first(ctx, m) || second(ctx, m) {
  61. return true
  62. }
  63. for _, cond := range rest {
  64. if cond(ctx, m) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. }
  71. // Not negates a given condition.
  72. func Not(cond Condition) Condition {
  73. return func(ctx context.Context, m ent.Mutation) bool {
  74. return !cond(ctx, m)
  75. }
  76. }
  77. // HasOp is a condition testing mutation operation.
  78. func HasOp(op ent.Op) Condition {
  79. return func(_ context.Context, m ent.Mutation) bool {
  80. return m.Op().Is(op)
  81. }
  82. }
  83. // HasAddedFields is a condition validating `.AddedField` on fields.
  84. func HasAddedFields(field string, fields ...string) Condition {
  85. return func(_ context.Context, m ent.Mutation) bool {
  86. if _, exists := m.AddedField(field); !exists {
  87. return false
  88. }
  89. for _, field := range fields {
  90. if _, exists := m.AddedField(field); !exists {
  91. return false
  92. }
  93. }
  94. return true
  95. }
  96. }
  97. // HasClearedFields is a condition validating `.FieldCleared` on fields.
  98. func HasClearedFields(field string, fields ...string) Condition {
  99. return func(_ context.Context, m ent.Mutation) bool {
  100. if exists := m.FieldCleared(field); !exists {
  101. return false
  102. }
  103. for _, field := range fields {
  104. if exists := m.FieldCleared(field); !exists {
  105. return false
  106. }
  107. }
  108. return true
  109. }
  110. }
  111. // HasFields is a condition validating `.Field` on fields.
  112. func HasFields(field string, fields ...string) Condition {
  113. return func(_ context.Context, m ent.Mutation) bool {
  114. if _, exists := m.Field(field); !exists {
  115. return false
  116. }
  117. for _, field := range fields {
  118. if _, exists := m.Field(field); !exists {
  119. return false
  120. }
  121. }
  122. return true
  123. }
  124. }
  125. // If executes the given hook under condition.
  126. //
  127. // hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
  128. //
  129. func If(hk ent.Hook, cond Condition) ent.Hook {
  130. return func(next ent.Mutator) ent.Mutator {
  131. return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  132. if cond(ctx, m) {
  133. return hk(next).Mutate(ctx, m)
  134. }
  135. return next.Mutate(ctx, m)
  136. })
  137. }
  138. }
  139. // On executes the given hook only for the given operation.
  140. //
  141. // hook.On(Log, ent.Delete|ent.Create)
  142. //
  143. func On(hk ent.Hook, op ent.Op) ent.Hook {
  144. return If(hk, HasOp(op))
  145. }
  146. // Unless skips the given hook only for the given operation.
  147. //
  148. // hook.Unless(Log, ent.Update|ent.UpdateOne)
  149. //
  150. func Unless(hk ent.Hook, op ent.Op) ent.Hook {
  151. return If(hk, Not(HasOp(op)))
  152. }
  153. // FixedError is a hook returning a fixed error.
  154. func FixedError(err error) ent.Hook {
  155. return func(ent.Mutator) ent.Mutator {
  156. return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
  157. return nil, err
  158. })
  159. }
  160. }
  161. // Reject returns a hook that rejects all operations that match op.
  162. //
  163. // func (T) Hooks() []ent.Hook {
  164. // return []ent.Hook{
  165. // Reject(ent.Delete|ent.Update),
  166. // }
  167. // }
  168. //
  169. func Reject(op ent.Op) ent.Hook {
  170. hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
  171. return On(hk, op)
  172. }
  173. // Chain acts as a list of hooks and is effectively immutable.
  174. // Once created, it will always hold the same set of hooks in the same order.
  175. type Chain struct {
  176. hooks []ent.Hook
  177. }
  178. // NewChain creates a new chain of hooks.
  179. func NewChain(hooks ...ent.Hook) Chain {
  180. return Chain{append([]ent.Hook(nil), hooks...)}
  181. }
  182. // Hook chains the list of hooks and returns the final hook.
  183. func (c Chain) Hook() ent.Hook {
  184. return func(mutator ent.Mutator) ent.Mutator {
  185. for i := len(c.hooks) - 1; i >= 0; i-- {
  186. mutator = c.hooks[i](mutator)
  187. }
  188. return mutator
  189. }
  190. }
  191. // Append extends a chain, adding the specified hook
  192. // as the last ones in the mutation flow.
  193. func (c Chain) Append(hooks ...ent.Hook) Chain {
  194. newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
  195. newHooks = append(newHooks, c.hooks...)
  196. newHooks = append(newHooks, hooks...)
  197. return Chain{newHooks}
  198. }
  199. // Extend extends a chain, adding the specified chain
  200. // as the last ones in the mutation flow.
  201. func (c Chain) Extend(chain Chain) Chain {
  202. return c.Append(chain.hooks...)
  203. }