Files
opencloud/vendor/github.com/testcontainers/testcontainers-go/wait/walk.go
dependabot[bot] 0a4741e4df build(deps): bump github.com/testcontainers/testcontainers-go/modules/opensearch
Bumps [github.com/testcontainers/testcontainers-go/modules/opensearch](https://github.com/testcontainers/testcontainers-go) from 0.42.0 to 0.43.0.
- [Release notes](https://github.com/testcontainers/testcontainers-go/releases)
- [Commits](https://github.com/testcontainers/testcontainers-go/compare/v0.42.0...v0.43.0)

---
updated-dependencies:
- dependency-name: github.com/testcontainers/testcontainers-go/modules/opensearch
  dependency-version: 0.43.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-06-25 14:44:19 +00:00

92 lines
2.2 KiB
Go

package wait
import (
"errors"
"slices"
)
var (
// ErrVisitStop is used as a return value from [VisitFunc] to stop the walk.
// It is not returned as an error by any function.
ErrVisitStop = errors.New("stop the walk")
// Deprecated: use [ErrVisitStop] instead.
VisitStop = ErrVisitStop
// ErrVisitRemove is used as a return value from [VisitFunc] to have the current node removed.
// It is not returned as an error by any function.
ErrVisitRemove = errors.New("remove this strategy")
// Deprecated: use [ErrVisitRemove] instead.
VisitRemove = ErrVisitRemove
)
// VisitFunc is a function that visits a strategy node.
// If it returns [ErrVisitStop], the walk stops.
// If it returns [ErrVisitRemove], the current node is removed.
type VisitFunc func(root Strategy) error
// Walk walks the strategies tree and calls the visit function for each node.
func Walk(root *Strategy, visit VisitFunc) error {
if root == nil {
return errors.New("root strategy is nil")
}
if err := walk(root, visit); err != nil {
if errors.Is(err, ErrVisitRemove) || errors.Is(err, ErrVisitStop) {
return nil
}
return err
}
return nil
}
// walk walks the strategies tree and calls the visit function for each node.
// It returns an error if the visit function returns an error.
func walk(root *Strategy, visit VisitFunc) error {
if *root == nil {
// No strategy.
return nil
}
// Allow the visit function to customize the behaviour of the walk before visiting the children.
if err := visit(*root); err != nil {
if errors.Is(err, ErrVisitRemove) {
*root = nil
}
return err
}
switch s := (*root).(type) {
case *MultiStrategy:
if err := walkAndMutate(&s.Strategies, visit); err != nil {
return err
}
case *AnyMultiStrategy:
if err := walkAndMutate(&s.Strategies, visit); err != nil {
return err
}
}
return nil
}
func walkAndMutate(strategies *[]Strategy, visit VisitFunc) error {
for i := 0; i < len(*strategies); {
if err := walk(&(*strategies)[i], visit); err != nil {
if errors.Is(err, ErrVisitRemove) {
*strategies = slices.Delete(*strategies, i, i+1)
if errors.Is(err, VisitStop) {
return VisitStop
}
continue
}
return err
}
i++
}
return nil
}