github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/db/ent/hook/hook.go (about)

     1  // Code generated by ent, DO NOT EDIT.
     2  
     3  package hook
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
    10  )
    11  
    12  // The AppCoinFunc type is an adapter to allow the use of ordinary
    13  // function as AppCoin mutator.
    14  type AppCoinFunc func(context.Context, *ent.AppCoinMutation) (ent.Value, error)
    15  
    16  // Mutate calls f(ctx, m).
    17  func (f AppCoinFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    18  	mv, ok := m.(*ent.AppCoinMutation)
    19  	if !ok {
    20  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AppCoinMutation", m)
    21  	}
    22  	return f(ctx, mv)
    23  }
    24  
    25  // The ChainBaseFunc type is an adapter to allow the use of ordinary
    26  // function as ChainBase mutator.
    27  type ChainBaseFunc func(context.Context, *ent.ChainBaseMutation) (ent.Value, error)
    28  
    29  // Mutate calls f(ctx, m).
    30  func (f ChainBaseFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    31  	mv, ok := m.(*ent.ChainBaseMutation)
    32  	if !ok {
    33  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChainBaseMutation", m)
    34  	}
    35  	return f(ctx, mv)
    36  }
    37  
    38  // The CoinBaseFunc type is an adapter to allow the use of ordinary
    39  // function as CoinBase mutator.
    40  type CoinBaseFunc func(context.Context, *ent.CoinBaseMutation) (ent.Value, error)
    41  
    42  // Mutate calls f(ctx, m).
    43  func (f CoinBaseFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    44  	mv, ok := m.(*ent.CoinBaseMutation)
    45  	if !ok {
    46  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinBaseMutation", m)
    47  	}
    48  	return f(ctx, mv)
    49  }
    50  
    51  // The CoinDescriptionFunc type is an adapter to allow the use of ordinary
    52  // function as CoinDescription mutator.
    53  type CoinDescriptionFunc func(context.Context, *ent.CoinDescriptionMutation) (ent.Value, error)
    54  
    55  // Mutate calls f(ctx, m).
    56  func (f CoinDescriptionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    57  	mv, ok := m.(*ent.CoinDescriptionMutation)
    58  	if !ok {
    59  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinDescriptionMutation", m)
    60  	}
    61  	return f(ctx, mv)
    62  }
    63  
    64  // The CoinExtraFunc type is an adapter to allow the use of ordinary
    65  // function as CoinExtra mutator.
    66  type CoinExtraFunc func(context.Context, *ent.CoinExtraMutation) (ent.Value, error)
    67  
    68  // Mutate calls f(ctx, m).
    69  func (f CoinExtraFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    70  	mv, ok := m.(*ent.CoinExtraMutation)
    71  	if !ok {
    72  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinExtraMutation", m)
    73  	}
    74  	return f(ctx, mv)
    75  }
    76  
    77  // The CoinFiatFunc type is an adapter to allow the use of ordinary
    78  // function as CoinFiat mutator.
    79  type CoinFiatFunc func(context.Context, *ent.CoinFiatMutation) (ent.Value, error)
    80  
    81  // Mutate calls f(ctx, m).
    82  func (f CoinFiatFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    83  	mv, ok := m.(*ent.CoinFiatMutation)
    84  	if !ok {
    85  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinFiatMutation", m)
    86  	}
    87  	return f(ctx, mv)
    88  }
    89  
    90  // The CoinFiatCurrencyFunc type is an adapter to allow the use of ordinary
    91  // function as CoinFiatCurrency mutator.
    92  type CoinFiatCurrencyFunc func(context.Context, *ent.CoinFiatCurrencyMutation) (ent.Value, error)
    93  
    94  // Mutate calls f(ctx, m).
    95  func (f CoinFiatCurrencyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
    96  	mv, ok := m.(*ent.CoinFiatCurrencyMutation)
    97  	if !ok {
    98  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinFiatCurrencyMutation", m)
    99  	}
   100  	return f(ctx, mv)
   101  }
   102  
   103  // The CoinFiatCurrencyHistoryFunc type is an adapter to allow the use of ordinary
   104  // function as CoinFiatCurrencyHistory mutator.
   105  type CoinFiatCurrencyHistoryFunc func(context.Context, *ent.CoinFiatCurrencyHistoryMutation) (ent.Value, error)
   106  
   107  // Mutate calls f(ctx, m).
   108  func (f CoinFiatCurrencyHistoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   109  	mv, ok := m.(*ent.CoinFiatCurrencyHistoryMutation)
   110  	if !ok {
   111  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinFiatCurrencyHistoryMutation", m)
   112  	}
   113  	return f(ctx, mv)
   114  }
   115  
   116  // The CoinUsedForFunc type is an adapter to allow the use of ordinary
   117  // function as CoinUsedFor mutator.
   118  type CoinUsedForFunc func(context.Context, *ent.CoinUsedForMutation) (ent.Value, error)
   119  
   120  // Mutate calls f(ctx, m).
   121  func (f CoinUsedForFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   122  	mv, ok := m.(*ent.CoinUsedForMutation)
   123  	if !ok {
   124  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CoinUsedForMutation", m)
   125  	}
   126  	return f(ctx, mv)
   127  }
   128  
   129  // The CurrencyFunc type is an adapter to allow the use of ordinary
   130  // function as Currency mutator.
   131  type CurrencyFunc func(context.Context, *ent.CurrencyMutation) (ent.Value, error)
   132  
   133  // Mutate calls f(ctx, m).
   134  func (f CurrencyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   135  	mv, ok := m.(*ent.CurrencyMutation)
   136  	if !ok {
   137  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CurrencyMutation", m)
   138  	}
   139  	return f(ctx, mv)
   140  }
   141  
   142  // The CurrencyFeedFunc type is an adapter to allow the use of ordinary
   143  // function as CurrencyFeed mutator.
   144  type CurrencyFeedFunc func(context.Context, *ent.CurrencyFeedMutation) (ent.Value, error)
   145  
   146  // Mutate calls f(ctx, m).
   147  func (f CurrencyFeedFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   148  	mv, ok := m.(*ent.CurrencyFeedMutation)
   149  	if !ok {
   150  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CurrencyFeedMutation", m)
   151  	}
   152  	return f(ctx, mv)
   153  }
   154  
   155  // The CurrencyHistoryFunc type is an adapter to allow the use of ordinary
   156  // function as CurrencyHistory mutator.
   157  type CurrencyHistoryFunc func(context.Context, *ent.CurrencyHistoryMutation) (ent.Value, error)
   158  
   159  // Mutate calls f(ctx, m).
   160  func (f CurrencyHistoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   161  	mv, ok := m.(*ent.CurrencyHistoryMutation)
   162  	if !ok {
   163  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CurrencyHistoryMutation", m)
   164  	}
   165  	return f(ctx, mv)
   166  }
   167  
   168  // The ExchangeRateFunc type is an adapter to allow the use of ordinary
   169  // function as ExchangeRate mutator.
   170  type ExchangeRateFunc func(context.Context, *ent.ExchangeRateMutation) (ent.Value, error)
   171  
   172  // Mutate calls f(ctx, m).
   173  func (f ExchangeRateFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   174  	mv, ok := m.(*ent.ExchangeRateMutation)
   175  	if !ok {
   176  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ExchangeRateMutation", m)
   177  	}
   178  	return f(ctx, mv)
   179  }
   180  
   181  // The FiatFunc type is an adapter to allow the use of ordinary
   182  // function as Fiat mutator.
   183  type FiatFunc func(context.Context, *ent.FiatMutation) (ent.Value, error)
   184  
   185  // Mutate calls f(ctx, m).
   186  func (f FiatFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   187  	mv, ok := m.(*ent.FiatMutation)
   188  	if !ok {
   189  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.FiatMutation", m)
   190  	}
   191  	return f(ctx, mv)
   192  }
   193  
   194  // The FiatCurrencyFunc type is an adapter to allow the use of ordinary
   195  // function as FiatCurrency mutator.
   196  type FiatCurrencyFunc func(context.Context, *ent.FiatCurrencyMutation) (ent.Value, error)
   197  
   198  // Mutate calls f(ctx, m).
   199  func (f FiatCurrencyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   200  	mv, ok := m.(*ent.FiatCurrencyMutation)
   201  	if !ok {
   202  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.FiatCurrencyMutation", m)
   203  	}
   204  	return f(ctx, mv)
   205  }
   206  
   207  // The FiatCurrencyFeedFunc type is an adapter to allow the use of ordinary
   208  // function as FiatCurrencyFeed mutator.
   209  type FiatCurrencyFeedFunc func(context.Context, *ent.FiatCurrencyFeedMutation) (ent.Value, error)
   210  
   211  // Mutate calls f(ctx, m).
   212  func (f FiatCurrencyFeedFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   213  	mv, ok := m.(*ent.FiatCurrencyFeedMutation)
   214  	if !ok {
   215  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.FiatCurrencyFeedMutation", m)
   216  	}
   217  	return f(ctx, mv)
   218  }
   219  
   220  // The FiatCurrencyHistoryFunc type is an adapter to allow the use of ordinary
   221  // function as FiatCurrencyHistory mutator.
   222  type FiatCurrencyHistoryFunc func(context.Context, *ent.FiatCurrencyHistoryMutation) (ent.Value, error)
   223  
   224  // Mutate calls f(ctx, m).
   225  func (f FiatCurrencyHistoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   226  	mv, ok := m.(*ent.FiatCurrencyHistoryMutation)
   227  	if !ok {
   228  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.FiatCurrencyHistoryMutation", m)
   229  	}
   230  	return f(ctx, mv)
   231  }
   232  
   233  // The SettingFunc type is an adapter to allow the use of ordinary
   234  // function as Setting mutator.
   235  type SettingFunc func(context.Context, *ent.SettingMutation) (ent.Value, error)
   236  
   237  // Mutate calls f(ctx, m).
   238  func (f SettingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   239  	mv, ok := m.(*ent.SettingMutation)
   240  	if !ok {
   241  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SettingMutation", m)
   242  	}
   243  	return f(ctx, mv)
   244  }
   245  
   246  // The TranFunc type is an adapter to allow the use of ordinary
   247  // function as Tran mutator.
   248  type TranFunc func(context.Context, *ent.TranMutation) (ent.Value, error)
   249  
   250  // Mutate calls f(ctx, m).
   251  func (f TranFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   252  	mv, ok := m.(*ent.TranMutation)
   253  	if !ok {
   254  		return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TranMutation", m)
   255  	}
   256  	return f(ctx, mv)
   257  }
   258  
   259  // Condition is a hook condition function.
   260  type Condition func(context.Context, ent.Mutation) bool
   261  
   262  // And groups conditions with the AND operator.
   263  func And(first, second Condition, rest ...Condition) Condition {
   264  	return func(ctx context.Context, m ent.Mutation) bool {
   265  		if !first(ctx, m) || !second(ctx, m) {
   266  			return false
   267  		}
   268  		for _, cond := range rest {
   269  			if !cond(ctx, m) {
   270  				return false
   271  			}
   272  		}
   273  		return true
   274  	}
   275  }
   276  
   277  // Or groups conditions with the OR operator.
   278  func Or(first, second Condition, rest ...Condition) Condition {
   279  	return func(ctx context.Context, m ent.Mutation) bool {
   280  		if first(ctx, m) || second(ctx, m) {
   281  			return true
   282  		}
   283  		for _, cond := range rest {
   284  			if cond(ctx, m) {
   285  				return true
   286  			}
   287  		}
   288  		return false
   289  	}
   290  }
   291  
   292  // Not negates a given condition.
   293  func Not(cond Condition) Condition {
   294  	return func(ctx context.Context, m ent.Mutation) bool {
   295  		return !cond(ctx, m)
   296  	}
   297  }
   298  
   299  // HasOp is a condition testing mutation operation.
   300  func HasOp(op ent.Op) Condition {
   301  	return func(_ context.Context, m ent.Mutation) bool {
   302  		return m.Op().Is(op)
   303  	}
   304  }
   305  
   306  // HasAddedFields is a condition validating `.AddedField` on fields.
   307  func HasAddedFields(field string, fields ...string) Condition {
   308  	return func(_ context.Context, m ent.Mutation) bool {
   309  		if _, exists := m.AddedField(field); !exists {
   310  			return false
   311  		}
   312  		for _, field := range fields {
   313  			if _, exists := m.AddedField(field); !exists {
   314  				return false
   315  			}
   316  		}
   317  		return true
   318  	}
   319  }
   320  
   321  // HasClearedFields is a condition validating `.FieldCleared` on fields.
   322  func HasClearedFields(field string, fields ...string) Condition {
   323  	return func(_ context.Context, m ent.Mutation) bool {
   324  		if exists := m.FieldCleared(field); !exists {
   325  			return false
   326  		}
   327  		for _, field := range fields {
   328  			if exists := m.FieldCleared(field); !exists {
   329  				return false
   330  			}
   331  		}
   332  		return true
   333  	}
   334  }
   335  
   336  // HasFields is a condition validating `.Field` on fields.
   337  func HasFields(field string, fields ...string) Condition {
   338  	return func(_ context.Context, m ent.Mutation) bool {
   339  		if _, exists := m.Field(field); !exists {
   340  			return false
   341  		}
   342  		for _, field := range fields {
   343  			if _, exists := m.Field(field); !exists {
   344  				return false
   345  			}
   346  		}
   347  		return true
   348  	}
   349  }
   350  
   351  // If executes the given hook under condition.
   352  //
   353  //	hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
   354  //
   355  func If(hk ent.Hook, cond Condition) ent.Hook {
   356  	return func(next ent.Mutator) ent.Mutator {
   357  		return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
   358  			if cond(ctx, m) {
   359  				return hk(next).Mutate(ctx, m)
   360  			}
   361  			return next.Mutate(ctx, m)
   362  		})
   363  	}
   364  }
   365  
   366  // On executes the given hook only for the given operation.
   367  //
   368  //	hook.On(Log, ent.Delete|ent.Create)
   369  //
   370  func On(hk ent.Hook, op ent.Op) ent.Hook {
   371  	return If(hk, HasOp(op))
   372  }
   373  
   374  // Unless skips the given hook only for the given operation.
   375  //
   376  //	hook.Unless(Log, ent.Update|ent.UpdateOne)
   377  //
   378  func Unless(hk ent.Hook, op ent.Op) ent.Hook {
   379  	return If(hk, Not(HasOp(op)))
   380  }
   381  
   382  // FixedError is a hook returning a fixed error.
   383  func FixedError(err error) ent.Hook {
   384  	return func(ent.Mutator) ent.Mutator {
   385  		return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
   386  			return nil, err
   387  		})
   388  	}
   389  }
   390  
   391  // Reject returns a hook that rejects all operations that match op.
   392  //
   393  //	func (T) Hooks() []ent.Hook {
   394  //		return []ent.Hook{
   395  //			Reject(ent.Delete|ent.Update),
   396  //		}
   397  //	}
   398  //
   399  func Reject(op ent.Op) ent.Hook {
   400  	hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
   401  	return On(hk, op)
   402  }
   403  
   404  // Chain acts as a list of hooks and is effectively immutable.
   405  // Once created, it will always hold the same set of hooks in the same order.
   406  type Chain struct {
   407  	hooks []ent.Hook
   408  }
   409  
   410  // NewChain creates a new chain of hooks.
   411  func NewChain(hooks ...ent.Hook) Chain {
   412  	return Chain{append([]ent.Hook(nil), hooks...)}
   413  }
   414  
   415  // Hook chains the list of hooks and returns the final hook.
   416  func (c Chain) Hook() ent.Hook {
   417  	return func(mutator ent.Mutator) ent.Mutator {
   418  		for i := len(c.hooks) - 1; i >= 0; i-- {
   419  			mutator = c.hooks[i](mutator)
   420  		}
   421  		return mutator
   422  	}
   423  }
   424  
   425  // Append extends a chain, adding the specified hook
   426  // as the last ones in the mutation flow.
   427  func (c Chain) Append(hooks ...ent.Hook) Chain {
   428  	newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
   429  	newHooks = append(newHooks, c.hooks...)
   430  	newHooks = append(newHooks, hooks...)
   431  	return Chain{newHooks}
   432  }
   433  
   434  // Extend extends a chain, adding the specified chain
   435  // as the last ones in the mutation flow.
   436  func (c Chain) Extend(chain Chain) Chain {
   437  	return c.Append(chain.hooks...)
   438  }