Debian Codegraph @

api/v1/lib/extras/gen/handlers.go

raw symbols SCIP
1 // +build ignore
2
3 package main
4
5 import (
6 "os"
7 "text/template"
8 )
9
10 func main() {
11 Run(handlersTemplate, nil, os.Args...)
12 }
13
14 var handlersTemplate = template.Must(template.New("").Parse(`package {{.Package}}
15
16 // go generate {{.Args}}
17 // GENERATED CODE FOLLOWS; DO NOT EDIT.
18
19 import (
20 "context"
21 {{range .Imports}}
22 {{ printf "%q" . -}}
23 {{end}}
24 )
25
26 {{.RequireType "E" -}}
27 {{.RequireType "ET" -}}
28 type (
29 // Handler is invoked upon the occurrence of some scheduler event that is generated
30 // by some other component in the Mesos ecosystem (e.g. master, agent, executor, etc.)
31 Handler interface {
32 HandleEvent(context.Context, {{.Type "E"}}) error
33 }
34
35 // HandlerFunc is a functional adaptation of the Handler interface
36 HandlerFunc func(context.Context, {{.Type "E"}}) error
37
38 // Handlers executes an event Handler according to the event's type
39 Handlers map[{{.Type "ET"}}]Handler
40
41 // HandlerFuncs executes an event HandlerFunc according to the event's type
42 HandlerFuncs map[{{.Type "ET"}}]HandlerFunc
43 )
44
45 // HandleEvent implements Handler for HandlerFunc
46 func (f HandlerFunc) HandleEvent(ctx context.Context, e {{.Type "E"}}) error { return f(ctx, e) }
47
48 type noopHandler int
49
50 func (noopHandler) HandleEvent(_ context.Context, _ {{.Type "E"}}) error { return nil }
51
52 // NoopHandler is a Handler that does nothing and always returns nil
53 const NoopHandler = noopHandler(0)
54
55 // HandleEvent implements Handler for Handlers
56 func (hs Handlers) HandleEvent(ctx context.Context, e {{.Type "E"}}) (err error) {
57 if h := hs[e.GetType()]; h != nil {
58 return h.HandleEvent(ctx, e)
59 }
60 return nil
61 }
62
63 // HandleEvent implements Handler for HandlerFuncs
64 func (hs HandlerFuncs) HandleEvent(ctx context.Context, e {{.Type "E"}}) (err error) {
65 if h := hs[e.GetType()]; h != nil {
66 return h.HandleEvent(ctx, e)
67 }
68 return nil
69 }
70
71 // Otherwise returns a HandlerFunc that attempts to process an event with the Handlers map; unmatched event types are
72 // processed by the given HandlerFunc. A nil HandlerFunc parameter is effecitvely a noop.
73 func (hs Handlers) Otherwise(f HandlerFunc) HandlerFunc {
74 if f == nil {
75 return hs.HandleEvent
76 }
77 return func(ctx context.Context, e {{.Type "E"}}) error {
78 if h := hs[e.GetType()]; h != nil {
79 return h.HandleEvent(ctx, e)
80 }
81 return f(ctx, e)
82 }
83 }
84
85 // Otherwise returns a HandlerFunc that attempts to process an event with the HandlerFuncs map; unmatched event types
86 // are processed by the given HandlerFunc. A nil HandlerFunc parameter is effecitvely a noop.
87 func (hs HandlerFuncs) Otherwise(f HandlerFunc) HandlerFunc {
88 if f == nil {
89 return hs.HandleEvent
90 }
91 return func(ctx context.Context, e {{.Type "E"}}) error {
92 if h := hs[e.GetType()]; h != nil {
93 return h.HandleEvent(ctx, e)
94 }
95 return f(ctx, e)
96 }
97 }
98
99 var (
100 _ = Handler(Handlers(nil))
101 _ = Handler(HandlerFunc(nil))
102 _ = Handler(HandlerFuncs(nil))
103 )
104 `))