-
Notifications
You must be signed in to change notification settings - Fork 2
/
actor.go
49 lines (41 loc) · 1013 Bytes
/
actor.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
package greetings
import (
"context"
"fmt"
"github.com/verifa/horizon/pkg/hz"
)
var _ (hz.Action[Greeting]) = (*GreetingsHelloAction)(nil)
type GreetingsHelloAction struct{}
// Action implements hz.Action.
func (GreetingsHelloAction) Action() string {
return "hello"
}
// Do implements hz.Action.
func (a GreetingsHelloAction) Do(
ctx context.Context,
greeting Greeting,
) (Greeting, error) {
if err := a.validate(greeting); err != nil {
return greeting, fmt.Errorf("validating greeting: %w", err)
}
greeting.Status = &GreetingStatus{
Ready: true,
Response: "Greetings, " + greeting.Spec.Name + "!",
}
return greeting, nil
}
func (a GreetingsHelloAction) validate(greeting Greeting) error {
if greeting.Spec == nil {
return fmt.Errorf("spec is required")
}
if greeting.Spec.Name == "" {
return fmt.Errorf("name is required")
}
if !isFriend(greeting.Spec.Name) {
return fmt.Errorf(
"we don't greet strangers in Finland, we only know: %v",
friends,
)
}
return nil
}