| 1 |
// Code generated by ent, DO NOT EDIT. |
| 2 |
|
| 3 |
package hook |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"fmt" |
| 8 |
|
| 9 |
"github.com/in-toto/archivista/ent" |
| 10 |
) |
| 11 |
|
| 12 |
// The AttestationFunc type is an adapter to allow the use of ordinary |
| 13 |
// function as Attestation mutator. |
| 14 |
type AttestationFunc func(context.Context, *ent.AttestationMutation) (ent.Value, error) |
| 15 |
|
| 16 |
// Mutate calls f(ctx, m). |
| 17 |
func (f AttestationFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 18 |
if mv, ok := m.(*ent.AttestationMutation); ok { |
| 19 |
return f(ctx, mv) |
| 20 |
} |
| 21 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AttestationMutation", m) |
| 22 |
} |
| 23 |
|
| 24 |
// The AttestationCollectionFunc type is an adapter to allow the use of ordinary |
| 25 |
// function as AttestationCollection mutator. |
| 26 |
type AttestationCollectionFunc func(context.Context, *ent.AttestationCollectionMutation) (ent.Value, error) |
| 27 |
|
| 28 |
// Mutate calls f(ctx, m). |
| 29 |
func (f AttestationCollectionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 30 |
if mv, ok := m.(*ent.AttestationCollectionMutation); ok { |
| 31 |
return f(ctx, mv) |
| 32 |
} |
| 33 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AttestationCollectionMutation", m) |
| 34 |
} |
| 35 |
|
| 36 |
// The AttestationPolicyFunc type is an adapter to allow the use of ordinary |
| 37 |
// function as AttestationPolicy mutator. |
| 38 |
type AttestationPolicyFunc func(context.Context, *ent.AttestationPolicyMutation) (ent.Value, error) |
| 39 |
|
| 40 |
// Mutate calls f(ctx, m). |
| 41 |
func (f AttestationPolicyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 42 |
if mv, ok := m.(*ent.AttestationPolicyMutation); ok { |
| 43 |
return f(ctx, mv) |
| 44 |
} |
| 45 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AttestationPolicyMutation", m) |
| 46 |
} |
| 47 |
|
| 48 |
// The DsseFunc type is an adapter to allow the use of ordinary |
| 49 |
// function as Dsse mutator. |
| 50 |
type DsseFunc func(context.Context, *ent.DsseMutation) (ent.Value, error) |
| 51 |
|
| 52 |
// Mutate calls f(ctx, m). |
| 53 |
func (f DsseFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 54 |
if mv, ok := m.(*ent.DsseMutation); ok { |
| 55 |
return f(ctx, mv) |
| 56 |
} |
| 57 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.DsseMutation", m) |
| 58 |
} |
| 59 |
|
| 60 |
// The PayloadDigestFunc type is an adapter to allow the use of ordinary |
| 61 |
// function as PayloadDigest mutator. |
| 62 |
type PayloadDigestFunc func(context.Context, *ent.PayloadDigestMutation) (ent.Value, error) |
| 63 |
|
| 64 |
// Mutate calls f(ctx, m). |
| 65 |
func (f PayloadDigestFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 66 |
if mv, ok := m.(*ent.PayloadDigestMutation); ok { |
| 67 |
return f(ctx, mv) |
| 68 |
} |
| 69 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PayloadDigestMutation", m) |
| 70 |
} |
| 71 |
|
| 72 |
// The SignatureFunc type is an adapter to allow the use of ordinary |
| 73 |
// function as Signature mutator. |
| 74 |
type SignatureFunc func(context.Context, *ent.SignatureMutation) (ent.Value, error) |
| 75 |
|
| 76 |
// Mutate calls f(ctx, m). |
| 77 |
func (f SignatureFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 78 |
if mv, ok := m.(*ent.SignatureMutation); ok { |
| 79 |
return f(ctx, mv) |
| 80 |
} |
| 81 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SignatureMutation", m) |
| 82 |
} |
| 83 |
|
| 84 |
// The StatementFunc type is an adapter to allow the use of ordinary |
| 85 |
// function as Statement mutator. |
| 86 |
type StatementFunc func(context.Context, *ent.StatementMutation) (ent.Value, error) |
| 87 |
|
| 88 |
// Mutate calls f(ctx, m). |
| 89 |
func (f StatementFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 90 |
if mv, ok := m.(*ent.StatementMutation); ok { |
| 91 |
return f(ctx, mv) |
| 92 |
} |
| 93 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.StatementMutation", m) |
| 94 |
} |
| 95 |
|
| 96 |
// The SubjectFunc type is an adapter to allow the use of ordinary |
| 97 |
// function as Subject mutator. |
| 98 |
type SubjectFunc func(context.Context, *ent.SubjectMutation) (ent.Value, error) |
| 99 |
|
| 100 |
// Mutate calls f(ctx, m). |
| 101 |
func (f SubjectFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 102 |
if mv, ok := m.(*ent.SubjectMutation); ok { |
| 103 |
return f(ctx, mv) |
| 104 |
} |
| 105 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SubjectMutation", m) |
| 106 |
} |
| 107 |
|
| 108 |
// The SubjectDigestFunc type is an adapter to allow the use of ordinary |
| 109 |
// function as SubjectDigest mutator. |
| 110 |
type SubjectDigestFunc func(context.Context, *ent.SubjectDigestMutation) (ent.Value, error) |
| 111 |
|
| 112 |
// Mutate calls f(ctx, m). |
| 113 |
func (f SubjectDigestFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 114 |
if mv, ok := m.(*ent.SubjectDigestMutation); ok { |
| 115 |
return f(ctx, mv) |
| 116 |
} |
| 117 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SubjectDigestMutation", m) |
| 118 |
} |
| 119 |
|
| 120 |
// The TimestampFunc type is an adapter to allow the use of ordinary |
| 121 |
// function as Timestamp mutator. |
| 122 |
type TimestampFunc func(context.Context, *ent.TimestampMutation) (ent.Value, error) |
| 123 |
|
| 124 |
// Mutate calls f(ctx, m). |
| 125 |
func (f TimestampFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 126 |
if mv, ok := m.(*ent.TimestampMutation); ok { |
| 127 |
return f(ctx, mv) |
| 128 |
} |
| 129 |
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TimestampMutation", m) |
| 130 |
} |
| 131 |
|
| 132 |
// Condition is a hook condition function. |
| 133 |
type Condition func(context.Context, ent.Mutation) bool |
| 134 |
|
| 135 |
// And groups conditions with the AND operator. |
| 136 |
func And(first, second Condition, rest ...Condition) Condition { |
| 137 |
return func(ctx context.Context, m ent.Mutation) bool { |
| 138 |
if !first(ctx, m) || !second(ctx, m) { |
| 139 |
return false |
| 140 |
} |
| 141 |
for _, cond := range rest { |
| 142 |
if !cond(ctx, m) { |
| 143 |
return false |
| 144 |
} |
| 145 |
} |
| 146 |
return true |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Or groups conditions with the OR operator. |
| 151 |
func Or(first, second Condition, rest ...Condition) Condition { |
| 152 |
return func(ctx context.Context, m ent.Mutation) bool { |
| 153 |
if first(ctx, m) || second(ctx, m) { |
| 154 |
return true |
| 155 |
} |
| 156 |
for _, cond := range rest { |
| 157 |
if cond(ctx, m) { |
| 158 |
return true |
| 159 |
} |
| 160 |
} |
| 161 |
return false |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Not negates a given condition. |
| 166 |
func Not(cond Condition) Condition { |
| 167 |
return func(ctx context.Context, m ent.Mutation) bool { |
| 168 |
return !cond(ctx, m) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// HasOp is a condition testing mutation operation. |
| 173 |
func HasOp(op ent.Op) Condition { |
| 174 |
return func(_ context.Context, m ent.Mutation) bool { |
| 175 |
return m.Op().Is(op) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// HasAddedFields is a condition validating `.AddedField` on fields. |
| 180 |
func HasAddedFields(field string, fields ...string) Condition { |
| 181 |
return func(_ context.Context, m ent.Mutation) bool { |
| 182 |
if _, exists := m.AddedField(field); !exists { |
| 183 |
return false |
| 184 |
} |
| 185 |
for _, field := range fields { |
| 186 |
if _, exists := m.AddedField(field); !exists { |
| 187 |
return false |
| 188 |
} |
| 189 |
} |
| 190 |
return true |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// HasClearedFields is a condition validating `.FieldCleared` on fields. |
| 195 |
func HasClearedFields(field string, fields ...string) Condition { |
| 196 |
return func(_ context.Context, m ent.Mutation) bool { |
| 197 |
if exists := m.FieldCleared(field); !exists { |
| 198 |
return false |
| 199 |
} |
| 200 |
for _, field := range fields { |
| 201 |
if exists := m.FieldCleared(field); !exists { |
| 202 |
return false |
| 203 |
} |
| 204 |
} |
| 205 |
return true |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// HasFields is a condition validating `.Field` on fields. |
| 210 |
func HasFields(field string, fields ...string) Condition { |
| 211 |
return func(_ context.Context, m ent.Mutation) bool { |
| 212 |
if _, exists := m.Field(field); !exists { |
| 213 |
return false |
| 214 |
} |
| 215 |
for _, field := range fields { |
| 216 |
if _, exists := m.Field(field); !exists { |
| 217 |
return false |
| 218 |
} |
| 219 |
} |
| 220 |
return true |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// If executes the given hook under condition. |
| 225 |
// |
| 226 |
// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) |
| 227 |
func If(hk ent.Hook, cond Condition) ent.Hook { |
| 228 |
return func(next ent.Mutator) ent.Mutator { |
| 229 |
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| 230 |
if cond(ctx, m) { |
| 231 |
return hk(next).Mutate(ctx, m) |
| 232 |
} |
| 233 |
return next.Mutate(ctx, m) |
| 234 |
}) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// On executes the given hook only for the given operation. |
| 239 |
// |
| 240 |
// hook.On(Log, ent.Delete|ent.Create) |
| 241 |
func On(hk ent.Hook, op ent.Op) ent.Hook { |
| 242 |
return If(hk, HasOp(op)) |
| 243 |
} |
| 244 |
|
| 245 |
// Unless skips the given hook only for the given operation. |
| 246 |
// |
| 247 |
// hook.Unless(Log, ent.Update|ent.UpdateOne) |
| 248 |
func Unless(hk ent.Hook, op ent.Op) ent.Hook { |
| 249 |
return If(hk, Not(HasOp(op))) |
| 250 |
} |
| 251 |
|
| 252 |
// FixedError is a hook returning a fixed error. |
| 253 |
func FixedError(err error) ent.Hook { |
| 254 |
return func(ent.Mutator) ent.Mutator { |
| 255 |
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) { |
| 256 |
return nil, err |
| 257 |
}) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
// Reject returns a hook that rejects all operations that match op. |
| 262 |
// |
| 263 |
// func (T) Hooks() []ent.Hook { |
| 264 |
// return []ent.Hook{ |
| 265 |
// Reject(ent.Delete|ent.Update), |
| 266 |
// } |
| 267 |
// } |
| 268 |
func Reject(op ent.Op) ent.Hook { |
| 269 |
hk := FixedError(fmt.Errorf("%s operation is not allowed", op)) |
| 270 |
return On(hk, op) |
| 271 |
} |
| 272 |
|
| 273 |
// Chain acts as a list of hooks and is effectively immutable. |
| 274 |
// Once created, it will always hold the same set of hooks in the same order. |
| 275 |
type Chain struct { |
| 276 |
hooks []ent.Hook |
| 277 |
} |
| 278 |
|
| 279 |
// NewChain creates a new chain of hooks. |
| 280 |
func NewChain(hooks ...ent.Hook) Chain { |
| 281 |
return Chain{append([]ent.Hook(nil), hooks...)} |
| 282 |
} |
| 283 |
|
| 284 |
// Hook chains the list of hooks and returns the final hook. |
| 285 |
func (c Chain) Hook() ent.Hook { |
| 286 |
return func(mutator ent.Mutator) ent.Mutator { |
| 287 |
for i := len(c.hooks) - 1; i >= 0; i-- { |
| 288 |
mutator = c.hooks[i](mutator) |
| 289 |
} |
| 290 |
return mutator |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// Append extends a chain, adding the specified hook |
| 295 |
// as the last ones in the mutation flow. |
| 296 |
func (c Chain) Append(hooks ...ent.Hook) Chain { |
| 297 |
newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks)) |
| 298 |
newHooks = append(newHooks, c.hooks...) |
| 299 |
newHooks = append(newHooks, hooks...) |
| 300 |
return Chain{newHooks} |
| 301 |
} |
| 302 |
|
| 303 |
// Extend extends a chain, adding the specified chain |
| 304 |
// as the last ones in the mutation flow. |
| 305 |
func (c Chain) Extend(chain Chain) Chain { |
| 306 |
return c.Append(chain.hooks...) |
| 307 |
} |