-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.go
59 lines (47 loc) · 1.3 KB
/
annotate.go
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
57
58
59
package state
import (
"context"
"fmt"
)
type annotationState struct {
*group
annotation string
}
// WithAnnotation returns new state with merged children and assigned annotation to it.
func WithAnnotation(message string, children ...State) State {
return withAnnotation(message, children...)
}
func withAnnotation(message string, children ...State) *annotationState {
return &annotationState{
group: merge(children...),
annotation: message,
}
}
// Err returns the first encountered error in State's children annotated
// with state's annotation.
// Returns nil if no errors found.
func (a *annotationState) Err() error {
for _, m := range a.states {
if err := m.Err(); err != nil {
return fmt.Errorf("%s: %w", a.annotation, err)
}
}
return nil
}
// Shutdown shuts down state's children and returns annotated shutdown error.
// Returns nil no errors occurred.
func (a *annotationState) Shutdown(ctx context.Context) error {
if err := a.group.Shutdown(ctx); err != nil {
return fmt.Errorf("%s: %w", a.annotation, err)
}
return nil
}
func (a *annotationState) DependsOn(children ...State) State {
return withDependency(a, children...)
}
func (a *annotationState) cause() error {
if err := a.group.cause(); err != nil {
return fmt.Errorf("%s: %w", a.annotation, err)
}
return nil
}