File: around_node.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,340 kB
  • sloc: javascript: 65; makefile: 23; sh: 14
file content (56 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download
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
}