github.com/prebid/prebid-server/v2@v2.18.0/hooks/hookstage/mutation.go (about) 1 package hookstage 2 3 type MutationType int 4 5 const ( 6 MutationAdd MutationType = iota 7 MutationUpdate 8 MutationDelete 9 ) 10 11 func (mt MutationType) String() string { 12 if v, ok := map[MutationType]string{ 13 MutationAdd: "add", 14 MutationUpdate: "update", 15 MutationDelete: "delete", 16 }[mt]; ok { 17 return v 18 } 19 20 return "unknown" 21 } 22 23 type MutationFunc[T any] func(T) (T, error) 24 25 type Mutation[T any] struct { 26 mutType MutationType 27 key []string // key indicates path to the modified field 28 fn MutationFunc[T] // fn actual function that makes changes to payload 29 } 30 31 func (m Mutation[T]) Type() MutationType { 32 return m.mutType 33 } 34 35 func (m Mutation[T]) Key() []string { 36 return m.key 37 } 38 39 func (m Mutation[T]) Apply(p T) (T, error) { 40 return m.fn(p) 41 } 42 43 type ChangeSet[T any] struct { 44 muts []Mutation[T] 45 } 46 47 func (c *ChangeSet[T]) Mutations() []Mutation[T] { 48 return c.muts 49 } 50 51 func (c *ChangeSet[T]) AddMutation(fn MutationFunc[T], t MutationType, k ...string) *ChangeSet[T] { 52 c.muts = append(c.muts, Mutation[T]{fn: fn, mutType: t, key: k}) 53 return c 54 }