1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package types
import (
"context"
)
type AroundNodeAllowedFuncs interface {
~func(context.Context, func(context.Context)) | ~func(context.Context) context.Context | ~func()
}
type AroundNodeFunc func(ctx context.Context, body func(ctx context.Context))
func AroundNode[F AroundNodeAllowedFuncs](f F, cl CodeLocation) AroundNodeDecorator {
if f == nil {
panic("BuildAroundNode cannot be called with a nil function.")
}
var aroundNodeFunc func(context.Context, func(context.Context))
switch x := any(f).(type) {
case func(context.Context, func(context.Context)):
aroundNodeFunc = x
case func(context.Context) context.Context:
aroundNodeFunc = func(ctx context.Context, body func(context.Context)) {
ctx = x(ctx)
body(ctx)
}
case func():
aroundNodeFunc = func(ctx context.Context, body func(context.Context)) {
x()
body(ctx)
}
}
return AroundNodeDecorator{
Body: aroundNodeFunc,
CodeLocation: cl,
}
}
type AroundNodeDecorator struct {
Body AroundNodeFunc
CodeLocation CodeLocation
}
type AroundNodes []AroundNodeDecorator
func (an AroundNodes) Clone() AroundNodes {
out := make(AroundNodes, len(an))
copy(out, an)
return out
}
func (an AroundNodes) Append(other ...AroundNodeDecorator) AroundNodes {
out := make(AroundNodes, len(an)+len(other))
copy(out, an)
copy(out[len(an):], other)
return out
}
|